Search in sources :

Example 1 with AppTarget

use of android.app.prediction.AppTarget in project android_packages_apps_Launcher3 by crdroidandroid.

the class HotseatPredictionModel method convertDataModelToAppTargetBundle.

/**
 * Creates and returns bundle using workspace items
 */
public static Bundle convertDataModelToAppTargetBundle(Context context, BgDataModel dataModel) {
    Bundle bundle = new Bundle();
    ArrayList<AppTargetEvent> events = new ArrayList<>();
    ArrayList<ItemInfo> workspaceItems = dataModel.getAllWorkspaceItems();
    for (ItemInfo item : workspaceItems) {
        AppTarget target = getAppTargetFromInfo(context, item);
        if (target != null && !isTrackedForPrediction(item))
            continue;
        events.add(wrapAppTargetWithLocation(target, AppTargetEvent.ACTION_PIN, item));
    }
    ArrayList<AppTarget> currentTargets = new ArrayList<>();
    FixedContainerItems hotseatItems = dataModel.extraItems.get(CONTAINER_HOTSEAT_PREDICTION);
    if (hotseatItems != null) {
        for (ItemInfo itemInfo : hotseatItems.items) {
            AppTarget target = getAppTargetFromInfo(context, itemInfo);
            if (target != null)
                currentTargets.add(target);
        }
    }
    bundle.putParcelableArrayList(BUNDLE_KEY_PIN_EVENTS, events);
    bundle.putParcelableArrayList(BUNDLE_KEY_CURRENT_ITEMS, currentTargets);
    return bundle;
}
Also used : AppTarget(android.app.prediction.AppTarget) ItemInfo(com.android.launcher3.model.data.ItemInfo) WorkspaceItemInfo(com.android.launcher3.model.data.WorkspaceItemInfo) Bundle(android.os.Bundle) ArrayList(java.util.ArrayList) FixedContainerItems(com.android.launcher3.model.BgDataModel.FixedContainerItems) AppTargetEvent(android.app.prediction.AppTargetEvent)

Example 2 with AppTarget

use of android.app.prediction.AppTarget 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;
}
Also used : ShortcutInfo(android.content.pm.ShortcutInfo) Optional(java.util.Optional) AppTargetId(android.app.prediction.AppTargetId) ShortcutRequest(com.android.launcher3.shortcuts.ShortcutRequest) Predicate(java.util.function.Predicate) AppTarget(android.app.prediction.AppTarget) UserHandle(android.os.UserHandle) ComponentName(android.content.ComponentName) Nullable(androidx.annotation.Nullable)

Example 3 with AppTarget

use of android.app.prediction.AppTarget 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.
}
Also used : CONTAINER_WIDGETS_PREDICTION(com.android.launcher3.LauncherSettings.Favorites.CONTAINER_WIDGETS_PREDICTION) ComponentName(android.content.ComponentName) LauncherAppState(com.android.launcher3.LauncherAppState) PendingAddWidgetInfo(com.android.launcher3.widget.PendingAddWidgetInfo) PackageUserKey(com.android.launcher3.util.PackageUserKey) Set(java.util.Set) TextUtils(android.text.TextUtils) FeatureFlags(com.android.launcher3.config.FeatureFlags) Collectors(java.util.stream.Collectors) AppTarget(android.app.prediction.AppTarget) List(java.util.List) ComponentKey(com.android.launcher3.util.ComponentKey) Map(java.util.Map) FixedContainerItems(com.android.launcher3.model.BgDataModel.FixedContainerItems) PredictorState(com.android.launcher3.model.QuickstepModelDelegate.PredictorState) ComponentKey(com.android.launcher3.util.ComponentKey) PackageUserKey(com.android.launcher3.util.PackageUserKey) FixedContainerItems(com.android.launcher3.model.BgDataModel.FixedContainerItems) AppTarget(android.app.prediction.AppTarget) PendingAddWidgetInfo(com.android.launcher3.widget.PendingAddWidgetInfo) List(java.util.List) ComponentName(android.content.ComponentName)

Example 4 with AppTarget

use of android.app.prediction.AppTarget 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);
}
Also used : CONTAINER_WIDGETS_PREDICTION(com.android.launcher3.LauncherSettings.Favorites.CONTAINER_WIDGETS_PREDICTION) ViewOnDrawExecutor(com.android.launcher3.util.ViewOnDrawExecutor) PendingAddWidgetInfo(com.android.launcher3.widget.PendingAddWidgetInfo) AppWidgetProviderInfo(android.appwidget.AppWidgetProviderInfo) IconCache(com.android.launcher3.icons.IconCache) AppTarget(android.app.prediction.AppTarget) Process(android.os.Process) MockitoAnnotations(org.mockito.MockitoAnnotations) ReflectionHelpers(org.robolectric.util.ReflectionHelpers) Mockito.doAnswer(org.mockito.Mockito.doAnswer) MAIN_EXECUTOR(com.android.launcher3.util.Executors.MAIN_EXECUTOR) ShadowDeviceFlag(com.android.launcher3.shadows.ShadowDeviceFlag) LauncherModelHelper(com.android.launcher3.util.LauncherModelHelper) Shadow(org.robolectric.shadow.api.Shadow) RuntimeEnvironment(org.robolectric.RuntimeEnvironment) Collectors(java.util.stream.Collectors) RobolectricTestRunner(org.robolectric.RobolectricTestRunner) List(java.util.List) LauncherAppWidgetProviderInfo(com.android.launcher3.widget.LauncherAppWidgetProviderInfo) FixedContainerItems(com.android.launcher3.model.BgDataModel.FixedContainerItems) ComponentWithLabel(com.android.launcher3.icons.ComponentWithLabel) ItemInfoMatcher(com.android.launcher3.util.ItemInfoMatcher) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Context(android.content.Context) MODEL_EXECUTOR(com.android.launcher3.util.Executors.MODEL_EXECUTOR) AppTargetId(android.app.prediction.AppTargetId) AppInfo(com.android.launcher3.model.data.AppInfo) Mock(org.mockito.Mock) ItemInfo(com.android.launcher3.model.data.ItemInfo) RunWith(org.junit.runner.RunWith) HashMap(java.util.HashMap) Shadows.shadowOf(org.robolectric.Shadows.shadowOf) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ShadowAppWidgetManager(org.robolectric.shadows.ShadowAppWidgetManager) WorkspaceItemInfo(com.android.launcher3.model.data.WorkspaceItemInfo) UserHandle(android.os.UserHandle) Before(org.junit.Before) IntArray(com.android.launcher3.util.IntArray) ComponentName(android.content.ComponentName) LauncherAppState(com.android.launcher3.LauncherAppState) LauncherAppWidgetInfo(com.android.launcher3.model.data.LauncherAppWidgetInfo) FeatureFlags(com.android.launcher3.config.FeatureFlags) Test(org.junit.Test) Truth.assertThat(com.google.common.truth.Truth.assertThat) AppWidgetManager(android.appwidget.AppWidgetManager) InvariantDeviceProfile(com.android.launcher3.InvariantDeviceProfile) ShadowPackageManager(org.robolectric.shadows.ShadowPackageManager) WidgetsListBaseEntry(com.android.launcher3.widget.model.WidgetsListBaseEntry) ComponentKey(com.android.launcher3.util.ComponentKey) PredictorState(com.android.launcher3.model.QuickstepModelDelegate.PredictorState) AppTarget(android.app.prediction.AppTarget) PendingAddWidgetInfo(com.android.launcher3.widget.PendingAddWidgetInfo) AppTargetId(android.app.prediction.AppTargetId) Test(org.junit.Test)

Example 5 with AppTarget

use of android.app.prediction.AppTarget 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);
}
Also used : CONTAINER_WIDGETS_PREDICTION(com.android.launcher3.LauncherSettings.Favorites.CONTAINER_WIDGETS_PREDICTION) ViewOnDrawExecutor(com.android.launcher3.util.ViewOnDrawExecutor) PendingAddWidgetInfo(com.android.launcher3.widget.PendingAddWidgetInfo) AppWidgetProviderInfo(android.appwidget.AppWidgetProviderInfo) IconCache(com.android.launcher3.icons.IconCache) AppTarget(android.app.prediction.AppTarget) Process(android.os.Process) MockitoAnnotations(org.mockito.MockitoAnnotations) ReflectionHelpers(org.robolectric.util.ReflectionHelpers) Mockito.doAnswer(org.mockito.Mockito.doAnswer) MAIN_EXECUTOR(com.android.launcher3.util.Executors.MAIN_EXECUTOR) ShadowDeviceFlag(com.android.launcher3.shadows.ShadowDeviceFlag) LauncherModelHelper(com.android.launcher3.util.LauncherModelHelper) Shadow(org.robolectric.shadow.api.Shadow) RuntimeEnvironment(org.robolectric.RuntimeEnvironment) Collectors(java.util.stream.Collectors) RobolectricTestRunner(org.robolectric.RobolectricTestRunner) List(java.util.List) LauncherAppWidgetProviderInfo(com.android.launcher3.widget.LauncherAppWidgetProviderInfo) FixedContainerItems(com.android.launcher3.model.BgDataModel.FixedContainerItems) ComponentWithLabel(com.android.launcher3.icons.ComponentWithLabel) ItemInfoMatcher(com.android.launcher3.util.ItemInfoMatcher) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Context(android.content.Context) MODEL_EXECUTOR(com.android.launcher3.util.Executors.MODEL_EXECUTOR) AppTargetId(android.app.prediction.AppTargetId) AppInfo(com.android.launcher3.model.data.AppInfo) Mock(org.mockito.Mock) ItemInfo(com.android.launcher3.model.data.ItemInfo) RunWith(org.junit.runner.RunWith) HashMap(java.util.HashMap) Shadows.shadowOf(org.robolectric.Shadows.shadowOf) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ShadowAppWidgetManager(org.robolectric.shadows.ShadowAppWidgetManager) WorkspaceItemInfo(com.android.launcher3.model.data.WorkspaceItemInfo) UserHandle(android.os.UserHandle) Before(org.junit.Before) IntArray(com.android.launcher3.util.IntArray) ComponentName(android.content.ComponentName) LauncherAppState(com.android.launcher3.LauncherAppState) LauncherAppWidgetInfo(com.android.launcher3.model.data.LauncherAppWidgetInfo) FeatureFlags(com.android.launcher3.config.FeatureFlags) Test(org.junit.Test) Truth.assertThat(com.google.common.truth.Truth.assertThat) AppWidgetManager(android.appwidget.AppWidgetManager) InvariantDeviceProfile(com.android.launcher3.InvariantDeviceProfile) ShadowPackageManager(org.robolectric.shadows.ShadowPackageManager) WidgetsListBaseEntry(com.android.launcher3.widget.model.WidgetsListBaseEntry) ComponentKey(com.android.launcher3.util.ComponentKey) PredictorState(com.android.launcher3.model.QuickstepModelDelegate.PredictorState) AppTarget(android.app.prediction.AppTarget) ShadowDeviceFlag(com.android.launcher3.shadows.ShadowDeviceFlag) PendingAddWidgetInfo(com.android.launcher3.widget.PendingAddWidgetInfo) AppTargetId(android.app.prediction.AppTargetId) Test(org.junit.Test)

Aggregations

AppTarget (android.app.prediction.AppTarget)6 ComponentName (android.content.ComponentName)5 FixedContainerItems (com.android.launcher3.model.BgDataModel.FixedContainerItems)5 UserHandle (android.os.UserHandle)4 LauncherAppState (com.android.launcher3.LauncherAppState)4 PredictorState (com.android.launcher3.model.QuickstepModelDelegate.PredictorState)4 WorkspaceItemInfo (com.android.launcher3.model.data.WorkspaceItemInfo)4 List (java.util.List)4 Collectors (java.util.stream.Collectors)4 AppTargetId (android.app.prediction.AppTargetId)3 Context (android.content.Context)3 CONTAINER_WIDGETS_PREDICTION (com.android.launcher3.LauncherSettings.Favorites.CONTAINER_WIDGETS_PREDICTION)3 FeatureFlags (com.android.launcher3.config.FeatureFlags)3 AppInfo (com.android.launcher3.model.data.AppInfo)3 ItemInfo (com.android.launcher3.model.data.ItemInfo)3 ComponentKey (com.android.launcher3.util.ComponentKey)3 PendingAddWidgetInfo (com.android.launcher3.widget.PendingAddWidgetInfo)3 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 AppWidgetManager (android.appwidget.AppWidgetManager)2