use of com.android.launcher3.tapl.Widget in project android_packages_apps_Launcher3 by crdroidandroid.
the class AppEventProducer method toAppTarget.
@Nullable
private AppTarget toAppTarget(LauncherAtom.ItemInfo info) {
UserHandle userHandle = Process.myUserHandle();
if (info.getIsWork()) {
userHandle = UserCache.INSTANCE.get(mContext).getUserProfiles().stream().filter(((Predicate<UserHandle>) userHandle::equals).negate()).findAny().orElse(null);
}
if (userHandle == null) {
return null;
}
ComponentName cn = null;
ShortcutInfo shortcutInfo = null;
String id = null;
switch(info.getItemCase()) {
case APPLICATION:
{
LauncherAtom.Application app = info.getApplication();
if ((cn = parseNullable(app.getComponentName())) != null) {
id = "app:" + cn.getPackageName();
}
break;
}
case SHORTCUT:
{
LauncherAtom.Shortcut si = info.getShortcut();
if (!TextUtils.isEmpty(si.getShortcutId()) && (cn = parseNullable(si.getShortcutName())) != null) {
Optional<ShortcutInfo> opt = new ShortcutRequest(mContext, userHandle).forPackage(cn.getPackageName(), si.getShortcutId()).query(ShortcutRequest.ALL).stream().findFirst();
if (opt.isPresent()) {
shortcutInfo = opt.get();
} else {
return null;
}
id = "shortcut:" + si.getShortcutId();
}
break;
}
case WIDGET:
{
LauncherAtom.Widget widget = info.getWidget();
if ((cn = parseNullable(widget.getComponentName())) != null) {
id = "widget:" + cn.getPackageName();
}
break;
}
case TASK:
{
LauncherAtom.Task task = info.getTask();
if ((cn = parseNullable(task.getComponentName())) != null) {
id = "app:" + cn.getPackageName();
}
break;
}
case FOLDER_ICON:
return createTempFolderTarget();
}
if (id != null && cn != null) {
if (shortcutInfo != null) {
return new AppTarget.Builder(new AppTargetId(id), shortcutInfo).build();
}
return new AppTarget.Builder(new AppTargetId(id), cn.getPackageName(), userHandle).setClassName(cn.getClassName()).build();
}
return null;
}
use of com.android.launcher3.tapl.Widget in project android_packages_apps_Launcher3 by crdroidandroid.
the class WidgetsPredictionUpdateTask method execute.
/**
* Uses the app predication result to infer widgets that the user may want to use.
*
* <p>The algorithm uses the app prediction ranking to create a widgets ranking which only
* includes one widget per app and excludes widgets that have already been added to the
* workspace.
*/
@Override
public void execute(LauncherAppState appState, BgDataModel dataModel, AllAppsList apps) {
Set<ComponentKey> widgetsInWorkspace = dataModel.appWidgets.stream().map(widget -> new ComponentKey(widget.providerName, widget.user)).collect(Collectors.toSet());
Map<PackageUserKey, List<WidgetItem>> allWidgets = dataModel.widgetsModel.getAllWidgetsWithoutShortcuts();
FixedContainerItems fixedContainerItems = mPredictorState.items;
fixedContainerItems.items.clear();
if (FeatureFlags.ENABLE_LOCAL_RECOMMENDED_WIDGETS_FILTER.get()) {
for (AppTarget app : mTargets) {
PackageUserKey packageUserKey = new PackageUserKey(app.getPackageName(), app.getUser());
if (allWidgets.containsKey(packageUserKey)) {
List<WidgetItem> notAddedWidgets = allWidgets.get(packageUserKey).stream().filter(item -> !widgetsInWorkspace.contains(new ComponentKey(item.componentName, item.user))).collect(Collectors.toList());
if (notAddedWidgets.size() > 0) {
// Even an apps have more than one widgets, we only include one widget.
fixedContainerItems.items.add(new PendingAddWidgetInfo(notAddedWidgets.get(0).widgetInfo, CONTAINER_WIDGETS_PREDICTION));
}
}
}
} else {
Map<ComponentKey, WidgetItem> widgetItems = allWidgets.values().stream().flatMap(List::stream).collect(Collectors.toMap(widget -> (ComponentKey) widget, widget -> widget));
for (AppTarget app : mTargets) {
if (TextUtils.isEmpty(app.getClassName())) {
continue;
}
ComponentKey targetWidget = new ComponentKey(new ComponentName(app.getPackageName(), app.getClassName()), app.getUser());
if (widgetItems.containsKey(targetWidget)) {
fixedContainerItems.items.add(new PendingAddWidgetInfo(widgetItems.get(targetWidget).widgetInfo, CONTAINER_WIDGETS_PREDICTION));
}
}
}
bindExtraContainerItems(fixedContainerItems);
// Don't store widgets prediction to disk because it is not used frequently.
}
use of com.android.launcher3.tapl.Widget in project android_packages_apps_Launcher3 by crdroidandroid.
the class WidgetsPredicationUpdateTaskTest method widgetsRecommendationRan_shouldOnlyReturnNotAddedWidgetsInAppPredictionOrder.
@Test
public void widgetsRecommendationRan_shouldOnlyReturnNotAddedWidgetsInAppPredictionOrder() throws Exception {
// WHEN newPredicationTask is executed with app predication of 5 apps.
AppTarget app1 = new AppTarget(new AppTargetId("app1"), "app1", "className", mUserHandle);
AppTarget app2 = new AppTarget(new AppTargetId("app2"), "app2", "className", mUserHandle);
AppTarget app3 = new AppTarget(new AppTargetId("app3"), "app3", "className", mUserHandle);
AppTarget app4 = new AppTarget(new AppTargetId("app4"), "app4", "className", mUserHandle);
AppTarget app5 = new AppTarget(new AppTargetId("app5"), "app5", "className", mUserHandle);
mModelHelper.executeTaskForTest(newWidgetsPredicationTask(List.of(app5, app3, app2, app4, app1))).forEach(Runnable::run);
// THEN only 3 widgets are returned because
// 1. app5/provider1 & app4/provider1 have already been added to workspace. They are
// excluded from the result.
// 2. app3 doesn't have a widget.
// 3. only 1 widget is picked from app1 because we only want to promote one widget per app.
List<PendingAddWidgetInfo> recommendedWidgets = mCallback.mRecommendedWidgets.items.stream().map(itemInfo -> (PendingAddWidgetInfo) itemInfo).collect(Collectors.toList());
assertThat(recommendedWidgets).hasSize(3);
assertWidgetInfo(recommendedWidgets.get(0).info, mApp2Provider1);
assertWidgetInfo(recommendedWidgets.get(1).info, mApp4Provider2);
assertWidgetInfo(recommendedWidgets.get(2).info, mApp1Provider1);
}
use of com.android.launcher3.tapl.Widget in project android_packages_apps_Launcher3 by crdroidandroid.
the class WidgetsPredicationUpdateTaskTest method widgetsRecommendationRan_localFilterDisabled_shouldReturnWidgetsInPredicationOrder.
@Test
public void widgetsRecommendationRan_localFilterDisabled_shouldReturnWidgetsInPredicationOrder() throws Exception {
ShadowDeviceFlag shadowDeviceFlag = Shadow.extract(FeatureFlags.ENABLE_LOCAL_RECOMMENDED_WIDGETS_FILTER);
shadowDeviceFlag.setValue(false);
// WHEN newPredicationTask is executed with 5 predicated widgets.
AppTarget widget1 = new AppTarget(new AppTargetId("app1"), "app1", "provider1", mUserHandle);
AppTarget widget2 = new AppTarget(new AppTargetId("app1"), "app1", "provider2", mUserHandle);
// Not installed app
AppTarget widget3 = new AppTarget(new AppTargetId("app2"), "app3", "provider1", mUserHandle);
// Not installed widget
AppTarget widget4 = new AppTarget(new AppTargetId("app4"), "app4", "provider3", mUserHandle);
AppTarget widget5 = new AppTarget(new AppTargetId("app5"), "app5", "provider1", mUserHandle);
mModelHelper.executeTaskForTest(newWidgetsPredicationTask(List.of(widget5, widget3, widget2, widget4, widget1))).forEach(Runnable::run);
// THEN only 3 widgets are returned because the launcher only filters out non-exist widgets.
List<PendingAddWidgetInfo> recommendedWidgets = mCallback.mRecommendedWidgets.items.stream().map(itemInfo -> (PendingAddWidgetInfo) itemInfo).collect(Collectors.toList());
assertThat(recommendedWidgets).hasSize(3);
assertWidgetInfo(recommendedWidgets.get(0).info, mApp5Provider1);
assertWidgetInfo(recommendedWidgets.get(1).info, mApp1Provider2);
assertWidgetInfo(recommendedWidgets.get(2).info, mApp1Provider1);
}
use of com.android.launcher3.tapl.Widget in project android_packages_apps_Launcher3 by crdroidandroid.
the class GridSizeMigrationTask method loadWorkspaceEntries.
/**
* Loads entries for a particular screen id.
*/
protected ArrayList<DbEntry> loadWorkspaceEntries(int screen) {
Cursor c = queryWorkspace(new String[] { // 0
Favorites._ID, // 1
Favorites.ITEM_TYPE, // 2
Favorites.CELLX, // 3
Favorites.CELLY, // 4
Favorites.SPANX, // 5
Favorites.SPANY, // 6
Favorites.INTENT, // 7
Favorites.APPWIDGET_PROVIDER, // 8
Favorites.APPWIDGET_ID }, Favorites.CONTAINER + " = " + Favorites.CONTAINER_DESKTOP + " AND " + Favorites.SCREEN + " = " + screen);
final int indexId = c.getColumnIndexOrThrow(Favorites._ID);
final int indexItemType = c.getColumnIndexOrThrow(Favorites.ITEM_TYPE);
final int indexCellX = c.getColumnIndexOrThrow(Favorites.CELLX);
final int indexCellY = c.getColumnIndexOrThrow(Favorites.CELLY);
final int indexSpanX = c.getColumnIndexOrThrow(Favorites.SPANX);
final int indexSpanY = c.getColumnIndexOrThrow(Favorites.SPANY);
final int indexIntent = c.getColumnIndexOrThrow(Favorites.INTENT);
final int indexAppWidgetProvider = c.getColumnIndexOrThrow(Favorites.APPWIDGET_PROVIDER);
final int indexAppWidgetId = c.getColumnIndexOrThrow(Favorites.APPWIDGET_ID);
ArrayList<DbEntry> entries = new ArrayList<>();
WidgetManagerHelper widgetManagerHelper = new WidgetManagerHelper(mContext);
while (c.moveToNext()) {
DbEntry entry = new DbEntry();
entry.id = c.getInt(indexId);
entry.itemType = c.getInt(indexItemType);
entry.cellX = c.getInt(indexCellX);
entry.cellY = c.getInt(indexCellY);
entry.spanX = c.getInt(indexSpanX);
entry.spanY = c.getInt(indexSpanY);
entry.screenId = screen;
try {
// calculate weight
switch(entry.itemType) {
case Favorites.ITEM_TYPE_SHORTCUT:
case Favorites.ITEM_TYPE_DEEP_SHORTCUT:
case Favorites.ITEM_TYPE_APPLICATION:
{
verifyIntent(c.getString(indexIntent));
entry.weight = entry.itemType == Favorites.ITEM_TYPE_APPLICATION ? WT_APPLICATION : WT_SHORTCUT;
break;
}
case Favorites.ITEM_TYPE_APPWIDGET:
{
String provider = c.getString(indexAppWidgetProvider);
ComponentName cn = ComponentName.unflattenFromString(provider);
verifyPackage(cn.getPackageName());
entry.weight = Math.max(WT_WIDGET_MIN, WT_WIDGET_FACTOR * entry.spanX * entry.spanY);
int widgetId = c.getInt(indexAppWidgetId);
LauncherAppWidgetProviderInfo pInfo = widgetManagerHelper.getLauncherAppWidgetInfo(widgetId);
Point spans = null;
if (pInfo != null) {
spans = pInfo.getMinSpans();
}
if (spans != null) {
entry.minSpanX = spans.x > 0 ? spans.x : entry.spanX;
entry.minSpanY = spans.y > 0 ? spans.y : entry.spanY;
} else {
// Assume that the widget be resized down to 2x2
entry.minSpanX = entry.minSpanY = 2;
}
if (entry.minSpanX > mTrgX || entry.minSpanY > mTrgY) {
throw new Exception("Widget can't be resized down to fit the grid");
}
break;
}
case Favorites.ITEM_TYPE_FOLDER:
{
int total = getFolderItemsCount(entry.id);
if (total == 0) {
throw new Exception("Folder is empty");
}
entry.weight = WT_FOLDER_FACTOR * total;
break;
}
default:
throw new Exception("Invalid item type");
}
} catch (Exception e) {
if (DEBUG) {
Log.d(TAG, "Removing item " + entry.id, e);
}
mEntryToRemove.add(entry.id);
continue;
}
entries.add(entry);
}
c.close();
return entries;
}
Aggregations