diff --git a/quickstep/src/com/android/launcher3/hybridhotseat/HotseatPredictionModel.java b/quickstep/src/com/android/launcher3/hybridhotseat/HotseatPredictionModel.java deleted file mode 100644 index c0d1fe4465..0000000000 --- a/quickstep/src/com/android/launcher3/hybridhotseat/HotseatPredictionModel.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright (C) 2020 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.android.launcher3.hybridhotseat; - -import static com.android.launcher3.LauncherSettings.Favorites.CONTAINER_HOTSEAT_PREDICTION; -import static com.android.launcher3.model.PredictionHelper.getAppTargetFromItemInfo; -import static com.android.launcher3.model.PredictionHelper.wrapAppTargetWithItemLocation; - -import android.app.prediction.AppTarget; -import android.app.prediction.AppTargetEvent; -import android.content.Context; -import android.os.Bundle; - -import com.android.launcher3.model.BgDataModel; -import com.android.launcher3.model.PredictionHelper; -import com.android.launcher3.model.data.ItemInfo; - -import java.util.ArrayList; -import java.util.Objects; -import java.util.stream.Collectors; - -/** - * Model helper for app predictions in workspace - */ -public class HotseatPredictionModel { - private static final String BUNDLE_KEY_PIN_EVENTS = "pin_events"; - private static final String BUNDLE_KEY_CURRENT_ITEMS = "current_items"; - - /** - * Creates and returns bundle using workspace items - */ - public static Bundle convertDataModelToAppTargetBundle(Context context, BgDataModel dataModel) { - Bundle bundle = new Bundle(); - ArrayList events = dataModel.itemsIdMap - .stream() - .filter(PredictionHelper::isTrackedForHotseatPrediction) - .map(item -> { - AppTarget target = getAppTargetFromItemInfo(context, item); - return target != null - ? wrapAppTargetWithItemLocation(target, AppTargetEvent.ACTION_PIN, item) - : null; - }) - .filter(Objects::nonNull) - .collect(Collectors.toCollection(ArrayList::new)); - - ArrayList currentTargets = new ArrayList<>(); - for (ItemInfo itemInfo : dataModel.getPredictedContents(CONTAINER_HOTSEAT_PREDICTION)) { - AppTarget target = getAppTargetFromItemInfo(context, itemInfo); - if (target != null) currentTargets.add(target); - } - bundle.putParcelableArrayList(BUNDLE_KEY_PIN_EVENTS, events); - bundle.putParcelableArrayList(BUNDLE_KEY_CURRENT_ITEMS, currentTargets); - return bundle; - } -} diff --git a/quickstep/src/com/android/launcher3/model/AppEventProducer.java b/quickstep/src/com/android/launcher3/model/AppEventProducer.java index 42e1733cdd..96f402c74c 100644 --- a/quickstep/src/com/android/launcher3/model/AppEventProducer.java +++ b/quickstep/src/com/android/launcher3/model/AppEventProducer.java @@ -24,7 +24,6 @@ import static android.app.prediction.AppTargetEvent.ACTION_UNPIN; import static com.android.launcher3.LauncherSettings.Favorites.CONTAINER_HOTSEAT_PREDICTION; import static com.android.launcher3.LauncherSettings.Favorites.CONTAINER_ALL_APPS_PREDICTION; import static com.android.launcher3.LauncherSettings.Favorites.CONTAINER_WIDGETS_PREDICTION; -import static com.android.launcher3.logger.LauncherAtomExtensions.ExtendedContainers.ContainerCase.DEVICE_SEARCH_RESULT_CONTAINER; import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_APP_LAUNCH_DRAGDROP; import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_APP_LAUNCH_TAP; import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_DISMISS_PREDICTION_UNDO; @@ -42,6 +41,7 @@ import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCH import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_TASK_LAUNCH_SWIPE_DOWN; import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_TASK_LAUNCH_TAP; import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_WIDGET_ADD_BUTTON_TAP; +import static com.android.launcher3.model.PredictionHelper.getLocationString; import static com.android.launcher3.model.PredictionHelper.isTrackedForHotseatPrediction; import static com.android.launcher3.model.PredictionHelper.isTrackedForWidgetPrediction; import static com.android.launcher3.util.Executors.MODEL_EXECUTOR; @@ -65,10 +65,6 @@ import androidx.annotation.WorkerThread; import com.android.launcher3.Utilities; import com.android.launcher3.logger.LauncherAtom; -import com.android.launcher3.logger.LauncherAtom.ContainerInfo; -import com.android.launcher3.logger.LauncherAtom.FolderContainer; -import com.android.launcher3.logger.LauncherAtom.HotseatContainer; -import com.android.launcher3.logger.LauncherAtom.WorkspaceContainer; import com.android.launcher3.logging.StatsLogManager.EventEnum; import com.android.launcher3.pm.UserCache; import com.android.launcher3.shortcuts.ShortcutRequest; @@ -76,7 +72,6 @@ import com.android.launcher3.util.UserIconInfo; import com.android.quickstep.logging.StatsLogCompatManager.StatsLogConsumer; import com.android.systemui.shared.system.SysUiStatsLog; -import java.util.Locale; import java.util.Optional; import java.util.function.ObjIntConsumer; @@ -121,7 +116,10 @@ public class AppEventProducer implements StatsLogConsumer { // TODO: remove the running test check when b/231648228 is fixed. if (target != null && !Utilities.isRunningInTestHarness()) { AppTargetEvent event = new AppTargetEvent.Builder(target, eventId) - .setLaunchLocation(getContainer(locationInfo)) + .setLaunchLocation(getLocationString( + locationInfo.getContainerInfo(), + locationInfo.getWidget().getSpanX(), + locationInfo.getWidget().getSpanY())) .build(); mMessageHandler.obtainMessage(MSG_LAUNCH, targetPredictor, 0, event).sendToTarget(); } @@ -266,68 +264,6 @@ public class AppEventProducer implements StatsLogConsumer { .build(); } - private String getContainer(LauncherAtom.ItemInfo info) { - ContainerInfo ci = info.getContainerInfo(); - switch (ci.getContainerCase()) { - case WORKSPACE: { - // In case the item type is not widgets, the spaceX and spanY default to 1. - int spanX = info.getWidget().getSpanX(); - int spanY = info.getWidget().getSpanY(); - return getWorkspaceContainerString(ci.getWorkspace(), spanX, spanY); - } - case HOTSEAT: { - return getHotseatContainerString(ci.getHotseat()); - } - case TASK_SWITCHER_CONTAINER: { - return "task-switcher"; - } - case ALL_APPS_CONTAINER: { - return "all-apps"; - } - case PREDICTED_HOTSEAT_CONTAINER: { - return "predictions/hotseat"; - } - case PREDICTION_CONTAINER: { - return "predictions"; - } - case SHORTCUTS_CONTAINER: { - return "deep-shortcuts"; - } - case TASK_BAR_CONTAINER: { - return "taskbar"; - } - case FOLDER: { - FolderContainer fc = ci.getFolder(); - switch (fc.getParentContainerCase()) { - case WORKSPACE: - return "folder/" + getWorkspaceContainerString(fc.getWorkspace(), 1, 1); - case HOTSEAT: - return "folder/" + getHotseatContainerString(fc.getHotseat()); - } - return "folder"; - } - case SEARCH_RESULT_CONTAINER: - return "search-results"; - case EXTENDED_CONTAINERS: { - if (ci.getExtendedContainers().getContainerCase() - == DEVICE_SEARCH_RESULT_CONTAINER) { - return "search-results"; - } - } - default: // fall out - } - return ""; - } - - private static String getWorkspaceContainerString(WorkspaceContainer wc, int spanX, int spanY) { - return String.format(Locale.ENGLISH, "workspace/%d/[%d,%d]/[%d,%d]", - wc.getPageIndex(), wc.getGridX(), wc.getGridY(), spanX, spanY); - } - - private static String getHotseatContainerString(HotseatContainer hc) { - return String.format(Locale.ENGLISH, "hotseat/%1$d/[%1$d,0]/[1,1]", hc.getIndex()); - } - private static ComponentName parseNullable(String componentNameString) { return TextUtils.isEmpty(componentNameString) ? null : ComponentName.unflattenFromString(componentNameString); diff --git a/quickstep/src/com/android/launcher3/model/PredictionHelper.java b/quickstep/src/com/android/launcher3/model/PredictionHelper.java deleted file mode 100644 index dbd99e1eab..0000000000 --- a/quickstep/src/com/android/launcher3/model/PredictionHelper.java +++ /dev/null @@ -1,133 +0,0 @@ -/* - * Copyright (C) 2021 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.android.launcher3.model; - -import static com.android.launcher3.logger.LauncherAtom.ContainerInfo.ContainerCase.WORKSPACE; - -import android.app.prediction.AppTarget; -import android.app.prediction.AppTargetEvent; -import android.app.prediction.AppTargetId; -import android.content.ComponentName; -import android.content.Context; - -import androidx.annotation.Nullable; - -import com.android.launcher3.LauncherSettings; -import com.android.launcher3.Workspace; -import com.android.launcher3.logger.LauncherAtom; -import com.android.launcher3.model.data.ItemInfo; -import com.android.launcher3.model.data.LauncherAppWidgetInfo; -import com.android.launcher3.model.data.WorkspaceItemInfo; -import com.android.launcher3.shortcuts.ShortcutKey; - -import java.util.Locale; - -/** Helper class with methods for converting launcher items to form usable by predictors */ -public final class PredictionHelper { - private static final String APP_LOCATION_HOTSEAT = "hotseat"; - private static final String APP_LOCATION_WORKSPACE = "workspace"; - - /** - * Creates and returns an {@link AppTarget} object for an {@link ItemInfo}. Returns null - * if item type is not supported in predictions - */ - @Nullable - public static AppTarget getAppTargetFromItemInfo(Context context, ItemInfo info) { - if (info == null) return null; - if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET - && info instanceof LauncherAppWidgetInfo - && ((LauncherAppWidgetInfo) info).providerName != null) { - ComponentName cn = ((LauncherAppWidgetInfo) info).providerName; - return new AppTarget.Builder(new AppTargetId("widget:" + cn.getPackageName()), - cn.getPackageName(), info.user).setClassName(cn.getClassName()).build(); - } else if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION - && info.getTargetComponent() != null) { - ComponentName cn = info.getTargetComponent(); - return new AppTarget.Builder(new AppTargetId("app:" + cn.getPackageName()), - cn.getPackageName(), info.user).setClassName(cn.getClassName()).build(); - } else if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT - && info instanceof WorkspaceItemInfo) { - ShortcutKey shortcutKey = ShortcutKey.fromItemInfo(info); - //TODO: switch to using full shortcut info - return new AppTarget.Builder(new AppTargetId("shortcut:" + shortcutKey.getId()), - shortcutKey.componentName.getPackageName(), shortcutKey.user).build(); - } else if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_FOLDER) { - return new AppTarget.Builder(new AppTargetId("folder:" + info.id), - context.getPackageName(), info.user).build(); - } else if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APP_PAIR) { - return new AppTarget.Builder(new AppTargetId("app_pair:" + info.id), - context.getPackageName(), info.user).build(); - } - return null; - } - - /** - * Creates and returns {@link AppTargetEvent} from an {@link AppTarget}, action, and item - * location using {@link ItemInfo} - */ - public static AppTargetEvent wrapAppTargetWithItemLocation( - AppTarget target, int action, ItemInfo info) { - String location = String.format(Locale.ENGLISH, "%s/%d/[%d,%d]/[%d,%d]", - info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT - ? APP_LOCATION_HOTSEAT : APP_LOCATION_WORKSPACE, - info.screenId, info.cellX, info.cellY, info.spanX, info.spanY); - return new AppTargetEvent.Builder(target, action).setLaunchLocation(location).build(); - } - - /** - * Helper method to determine if {@link ItemInfo} should be tracked and reported to hotseat - * predictors - */ - public static boolean isTrackedForHotseatPrediction(ItemInfo info) { - return info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT || ( - info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP - && info.screenId == Workspace.FIRST_SCREEN_ID); - } - - /** - * Helper method to determine if {@link LauncherAtom.ItemInfo} should be tracked and reported to - * hotseat predictors - */ - public static boolean isTrackedForHotseatPrediction(LauncherAtom.ItemInfo info) { - LauncherAtom.ContainerInfo ci = info.getContainerInfo(); - switch (ci.getContainerCase()) { - case HOTSEAT: - return true; - case WORKSPACE: - return ci.getWorkspace().getPageIndex() == 0; - default: - return false; - } - } - - /** - * Helper method to determine if {@link ItemInfo} should be tracked and reported to widget - * predictors - */ - public static boolean isTrackedForWidgetPrediction(ItemInfo info) { - return info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET - && info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP; - } - - /** - * Helper method to determine if {@link LauncherAtom.ItemInfo} should be tracked and reported - * to widget predictors - */ - public static boolean isTrackedForWidgetPrediction(LauncherAtom.ItemInfo info) { - return info.getItemCase() == LauncherAtom.ItemInfo.ItemCase.WIDGET - && info.getContainerInfo().getContainerCase() == WORKSPACE; - } -} diff --git a/quickstep/src/com/android/launcher3/model/PredictionHelper.kt b/quickstep/src/com/android/launcher3/model/PredictionHelper.kt new file mode 100644 index 0000000000..91aea874c0 --- /dev/null +++ b/quickstep/src/com/android/launcher3/model/PredictionHelper.kt @@ -0,0 +1,223 @@ +/* + * Copyright (C) 2021 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.android.launcher3.model + +import android.app.prediction.AppTarget +import android.app.prediction.AppTargetEvent +import android.app.prediction.AppTargetId +import android.content.Context +import android.os.Bundle +import com.android.launcher3.LauncherSettings.Favorites +import com.android.launcher3.LauncherSettings.Favorites.CONTAINER_DESKTOP +import com.android.launcher3.LauncherSettings.Favorites.CONTAINER_HOTSEAT +import com.android.launcher3.LauncherSettings.Favorites.CONTAINER_HOTSEAT_PREDICTION +import com.android.launcher3.LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET +import com.android.launcher3.WorkspaceLayoutManager.FIRST_SCREEN_ID +import com.android.launcher3.logger.LauncherAtom +import com.android.launcher3.logger.LauncherAtom.ContainerInfo +import com.android.launcher3.logger.LauncherAtom.ContainerInfo.ContainerCase.ALL_APPS_CONTAINER +import com.android.launcher3.logger.LauncherAtom.ContainerInfo.ContainerCase.EXTENDED_CONTAINERS +import com.android.launcher3.logger.LauncherAtom.ContainerInfo.ContainerCase.FOLDER +import com.android.launcher3.logger.LauncherAtom.ContainerInfo.ContainerCase.HOTSEAT +import com.android.launcher3.logger.LauncherAtom.ContainerInfo.ContainerCase.PREDICTED_HOTSEAT_CONTAINER +import com.android.launcher3.logger.LauncherAtom.ContainerInfo.ContainerCase.PREDICTION_CONTAINER +import com.android.launcher3.logger.LauncherAtom.ContainerInfo.ContainerCase.SEARCH_RESULT_CONTAINER +import com.android.launcher3.logger.LauncherAtom.ContainerInfo.ContainerCase.SHORTCUTS_CONTAINER +import com.android.launcher3.logger.LauncherAtom.ContainerInfo.ContainerCase.TASK_BAR_CONTAINER +import com.android.launcher3.logger.LauncherAtom.ContainerInfo.ContainerCase.TASK_SWITCHER_CONTAINER +import com.android.launcher3.logger.LauncherAtom.ContainerInfo.ContainerCase.WORKSPACE +import com.android.launcher3.logger.LauncherAtom.FolderContainer.ParentContainerCase +import com.android.launcher3.logger.LauncherAtom.HotseatContainer +import com.android.launcher3.logger.LauncherAtom.ItemInfo.ItemCase.WIDGET +import com.android.launcher3.logger.LauncherAtom.WorkspaceContainer +import com.android.launcher3.logger.LauncherAtomExtensions.ExtendedContainers.ContainerCase.DEVICE_SEARCH_RESULT_CONTAINER +import com.android.launcher3.model.data.ItemInfo +import com.android.launcher3.model.data.LauncherAppWidgetInfo +import com.android.launcher3.model.data.WorkspaceItemInfo +import com.android.launcher3.shortcuts.ShortcutKey + +/** Helper class with methods for converting launcher items to form usable by predictors */ +object PredictionHelper { + const val BUNDLE_KEY_PIN_EVENTS = "pin_events" + const val BUNDLE_KEY_CURRENT_ITEMS = "current_items" + const val BUNDLE_KEY_ADDED_APP_WIDGETS = "added_app_widgets" + + /** + * Helper method to determine if [ItemInfo] should be tracked and reported to hotseat predictors + */ + @JvmStatic + fun ItemInfo.isTrackedForHotseatPrediction(): Boolean = + container == CONTAINER_HOTSEAT || + (container == CONTAINER_DESKTOP && screenId == FIRST_SCREEN_ID) + + /** + * Helper method to determine if [LauncherAtom.ItemInfo] should be tracked and reported to + * hotseat predictors + */ + @JvmStatic + fun LauncherAtom.ItemInfo.isTrackedForHotseatPrediction(): Boolean = + containerInfo.run { + when (containerCase) { + HOTSEAT -> true + WORKSPACE -> workspace.pageIndex == 0 + else -> false + } + } + + /** + * Helper method to determine if [ItemInfo] should be tracked and reported to widget predictors + */ + @JvmStatic + fun ItemInfo.isTrackedForWidgetPrediction(): Boolean = + itemType == ITEM_TYPE_APPWIDGET && container == CONTAINER_DESKTOP + + /** + * Helper method to determine if [LauncherAtom.ItemInfo] should be tracked and reported to + * widget predictors + */ + @JvmStatic + fun LauncherAtom.ItemInfo.isTrackedForWidgetPrediction(): Boolean = + itemCase == WIDGET && containerInfo.containerCase == WORKSPACE + + /** Creates and returns bundle using workspace items for hotseat predictions */ + @JvmStatic + fun getBundleForHotseatPredictions(context: Context, dataModel: BgDataModel): Bundle { + return Bundle().apply { + putParcelableArrayList( + BUNDLE_KEY_PIN_EVENTS, + dataModel.getPinItemEvents(context) { it.isTrackedForHotseatPrediction() }, + ) + putParcelableArrayList( + BUNDLE_KEY_CURRENT_ITEMS, + dataModel.getPredictedContents(CONTAINER_HOTSEAT_PREDICTION).mapNotNullTo( + ArrayList() + ) { + getAppTargetFromItemInfo(context, it) + }, + ) + } + } + + /** Creates and returns bundle using workspace items for widget predictions */ + @JvmStatic + fun getBundleForWidgetPredictions(context: Context, dataModel: BgDataModel) = + Bundle().apply { + putParcelableArrayList( + BUNDLE_KEY_ADDED_APP_WIDGETS, + dataModel.getPinItemEvents(context) { it.isTrackedForWidgetPrediction() }, + ) + } + + /** Returns a location string to be used with [AppTargetEvent] for the [ContainerInfo] */ + @JvmStatic + fun ContainerInfo.getLocationString(spanX: Int, spanY: Int): String = + when (containerCase) { + // In case the item type is not widgets, the spaceX and spanY default to 1. + WORKSPACE -> workspace.getLocationString(spanX, spanY) + HOTSEAT -> hotseat.getLocationString() + FOLDER -> + folder.let { + when (it.parentContainerCase) { + ParentContainerCase.WORKSPACE -> + "folder/" + it.workspace.getLocationString(1, 1) + ParentContainerCase.HOTSEAT -> "folder/" + it.hotseat.getLocationString() + else -> "folder" + } + } + TASK_SWITCHER_CONTAINER -> "task-switcher" + ALL_APPS_CONTAINER -> "all-apps" + PREDICTED_HOTSEAT_CONTAINER -> "predictions/hotseat" + PREDICTION_CONTAINER -> "predictions" + SHORTCUTS_CONTAINER -> "deep-shortcuts" + TASK_BAR_CONTAINER -> "taskbar" + SEARCH_RESULT_CONTAINER -> "search-results" + EXTENDED_CONTAINERS -> + if (extendedContainers.containerCase == DEVICE_SEARCH_RESULT_CONTAINER) + "search-results" + else "" + else -> "" + } + + private fun WorkspaceContainer.getLocationString(spanX: Int, spanY: Int) = + "workspace/$pageIndex/[$gridX,$gridY]/[$spanX,$spanY]" + + private fun HotseatContainer.getLocationString() = "hotseat/$index/[$index,0]/[1,1]" + + /** + * Creates [AppTargetEvent] for every item in [BgDataModel] with [AppTargetEvent.ACTION_PIN] and + * item location using [ItemInfo] + */ + private fun BgDataModel.getPinItemEvents(context: Context, filter: (ItemInfo) -> Boolean) = + itemsIdMap.filter(filter).mapNotNullTo(ArrayList()) { info -> + getAppTargetFromItemInfo(context, info)?.let { target -> + AppTargetEvent.Builder(target, AppTargetEvent.ACTION_PIN) + .setLaunchLocation(info.containerInfo.getLocationString(info.spanX, info.spanY)) + .build() + } + } + + /** + * Creates and returns an [AppTarget] object for an [ItemInfo]. Returns null if item type is not + * supported in predictions + */ + private fun getAppTargetFromItemInfo(context: Context, info: ItemInfo?): AppTarget? { + if (info == null) return null + return when { + info.itemType == ITEM_TYPE_APPWIDGET && info is LauncherAppWidgetInfo -> + info.providerName?.let { cn -> + AppTarget.Builder( + AppTargetId("widget:" + cn.packageName), + cn.packageName, + info.user, + ) + .setClassName(cn.className) + .build() + } + info.itemType == Favorites.ITEM_TYPE_APPLICATION -> + info.targetComponent?.let { cn -> + AppTarget.Builder( + AppTargetId("app:" + cn.packageName), + cn.packageName, + info.user, + ) + .setClassName(cn.className) + .build() + } + + info.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT && info is WorkspaceItemInfo -> + // TODO: switch to using full shortcut info + ShortcutKey.fromItemInfo(info).let { shortcutKey -> + AppTarget.Builder( + AppTargetId("shortcut:" + shortcutKey.id), + shortcutKey.componentName.packageName, + shortcutKey.user, + ) + .build() + } + info.itemType == Favorites.ITEM_TYPE_FOLDER -> + AppTarget.Builder(AppTargetId("folder:" + info.id), context.packageName, info.user) + .build() + info.itemType == Favorites.ITEM_TYPE_APP_PAIR -> + AppTarget.Builder( + AppTargetId("app_pair:" + info.id), + context.packageName, + info.user, + ) + .build() + else -> null + } + } +} diff --git a/quickstep/src/com/android/launcher3/model/QuickstepModelDelegate.java b/quickstep/src/com/android/launcher3/model/QuickstepModelDelegate.java index c59c6ce29c..84d4e50b95 100644 --- a/quickstep/src/com/android/launcher3/model/QuickstepModelDelegate.java +++ b/quickstep/src/com/android/launcher3/model/QuickstepModelDelegate.java @@ -24,20 +24,15 @@ import static com.android.launcher3.LauncherSettings.Favorites.CONTAINER_ALL_APP import static com.android.launcher3.LauncherSettings.Favorites.CONTAINER_HOTSEAT_PREDICTION; import static com.android.launcher3.LauncherSettings.Favorites.CONTAINER_WIDGETS_PREDICTION; import static com.android.launcher3.LauncherSettings.Favorites.DESKTOP_ICON_FLAG; -import static com.android.launcher3.hybridhotseat.HotseatPredictionModel.convertDataModelToAppTargetBundle; import static com.android.launcher3.icons.cache.CacheLookupFlag.DEFAULT_LOOKUP_FLAG; -import static com.android.launcher3.model.PredictionHelper.getAppTargetFromItemInfo; -import static com.android.launcher3.model.PredictionHelper.wrapAppTargetWithItemLocation; +import static com.android.launcher3.model.PredictionHelper.getBundleForHotseatPredictions; +import static com.android.launcher3.model.PredictionHelper.getBundleForWidgetPredictions; import static com.android.launcher3.util.Executors.MODEL_EXECUTOR; -import static java.util.stream.Collectors.toCollection; - import android.app.StatsManager; import android.app.prediction.AppPredictionContext; -import android.app.prediction.AppTarget; import android.app.prediction.AppTargetEvent; import android.content.Context; -import android.os.Bundle; import android.text.TextUtils; import android.util.Log; import android.util.StatsEvent; @@ -66,7 +61,6 @@ import com.android.quickstep.util.ContextualSearchStateManager; import com.android.systemui.shared.system.SysUiStatsLog; import java.util.ArrayList; -import java.util.Objects; import javax.inject.Inject; import javax.inject.Named; @@ -76,7 +70,6 @@ import javax.inject.Named; */ public class QuickstepModelDelegate extends ModelDelegate { - private static final String BUNDLE_KEY_ADDED_APP_WIDGETS = "added_app_widgets"; private static final int NUM_OF_RECOMMENDED_WIDGETS_PREDICATION = 20; private static final boolean IS_DEBUG = false; @@ -318,7 +311,7 @@ public class QuickstepModelDelegate extends ModelDelegate { mWidgetsRecommendationState.registerPredictor(mContext, new AppPredictionContext.Builder(mContext) .setUiSurface("widgets") - .setExtras(getBundleForWidgetsOnWorkspace(mContext, mDataModel)) + .setExtras(getBundleForWidgetPredictions(mContext, mDataModel)) .setPredictedTargetCount(NUM_OF_RECOMMENDED_WIDGETS_PREDICATION) .build(), mModel, @@ -338,7 +331,7 @@ public class QuickstepModelDelegate extends ModelDelegate { new AppPredictionContext.Builder(context) .setUiSurface("hotseat") .setPredictedTargetCount(mIDP.numDatabaseHotseatIcons) - .setExtras(convertDataModelToAppTargetBundle(context, mDataModel)) + .setExtras(getBundleForHotseatPredictions(context, mDataModel)) .build(), mModel, PredictionUpdateTask::new); } @@ -370,21 +363,4 @@ public class QuickstepModelDelegate extends ModelDelegate { recreateHotseatPredictor(); } } - - private Bundle getBundleForWidgetsOnWorkspace(Context context, BgDataModel dataModel) { - Bundle bundle = new Bundle(); - ArrayList widgetEvents = - dataModel.itemsIdMap.stream() - .filter(PredictionHelper::isTrackedForWidgetPrediction) - .map(item -> { - AppTarget target = getAppTargetFromItemInfo(context, item); - if (target == null) return null; - return wrapAppTargetWithItemLocation( - target, AppTargetEvent.ACTION_PIN, item); - }) - .filter(Objects::nonNull) - .collect(toCollection(ArrayList::new)); - bundle.putParcelableArrayList(BUNDLE_KEY_ADDED_APP_WIDGETS, widgetEvents); - return bundle; - } } diff --git a/quickstep/tests/multivalentTests/src/com/android/launcher3/model/PredictionHelperTest.kt b/quickstep/tests/multivalentTests/src/com/android/launcher3/model/PredictionHelperTest.kt new file mode 100644 index 0000000000..09a9925c57 --- /dev/null +++ b/quickstep/tests/multivalentTests/src/com/android/launcher3/model/PredictionHelperTest.kt @@ -0,0 +1,163 @@ +/* + * Copyright (C) 2025 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.launcher3.model + +import android.app.prediction.AppTargetEvent +import android.content.ComponentName +import android.content.Intent +import androidx.test.ext.junit.runners.AndroidJUnit4 +import androidx.test.filters.SmallTest +import com.android.launcher3.LauncherSettings.Favorites.CONTAINER_ALL_APPS +import com.android.launcher3.LauncherSettings.Favorites.CONTAINER_DESKTOP +import com.android.launcher3.LauncherSettings.Favorites.CONTAINER_HOTSEAT +import com.android.launcher3.LauncherSettings.Favorites.CONTAINER_PIN_WIDGETS +import com.android.launcher3.WorkspaceLayoutManager.FIRST_SCREEN_ID +import com.android.launcher3.model.PredictionHelper.BUNDLE_KEY_ADDED_APP_WIDGETS +import com.android.launcher3.model.PredictionHelper.BUNDLE_KEY_PIN_EVENTS +import com.android.launcher3.model.PredictionHelper.getBundleForHotseatPredictions +import com.android.launcher3.model.PredictionHelper.getBundleForWidgetPredictions +import com.android.launcher3.model.PredictionHelper.isTrackedForHotseatPrediction +import com.android.launcher3.model.PredictionHelper.isTrackedForWidgetPrediction +import com.android.launcher3.model.data.AppInfo +import com.android.launcher3.model.data.LauncherAppWidgetInfo +import com.android.launcher3.util.SandboxApplication +import com.google.common.truth.Truth.assertThat +import org.junit.Assert.assertFalse +import org.junit.Assert.assertTrue +import org.junit.Rule +import org.junit.Test +import org.junit.runner.RunWith +import org.mockito.kotlin.mock + +@SmallTest +@RunWith(AndroidJUnit4::class) +class PredictionHelperTest { + + @get:Rule val context = SandboxApplication() + + @Test + fun isTrackedForHotseatPrediction_true_for_valid_items() { + createAppInfo("test").let { + assertTrue(it.isTrackedForHotseatPrediction()) + assertTrue(it.buildProto(context).isTrackedForHotseatPrediction()) + } + + createAppInfo("test", CONTAINER_HOTSEAT).let { + assertTrue(it.isTrackedForHotseatPrediction()) + assertTrue(it.buildProto(context).isTrackedForHotseatPrediction()) + } + } + + @Test + fun isTrackedForHotseatPrediction_false_for_invalid_items() { + createAppInfo("test", CONTAINER_DESKTOP, 3).let { + assertFalse(it.isTrackedForHotseatPrediction()) + assertFalse(it.buildProto(context).isTrackedForHotseatPrediction()) + } + + createAppInfo("test", 2 /* folder */).let { + assertFalse(it.isTrackedForHotseatPrediction()) + assertFalse(it.buildProto(context).isTrackedForHotseatPrediction()) + } + + createAppInfo("test", CONTAINER_ALL_APPS).let { + assertFalse(it.isTrackedForHotseatPrediction()) + assertFalse(it.buildProto(context).isTrackedForHotseatPrediction()) + } + } + + @Test + fun isTrackedForWidgetPrediction_true_for_valid_items() { + createWidgetInfoInfo("test").let { + assertTrue(it.isTrackedForWidgetPrediction()) + assertTrue(it.buildProto(context).isTrackedForWidgetPrediction()) + } + } + + @Test + fun isTrackedForWidgetPrediction_false_for_invalid_items() { + createAppInfo("test").let { + assertFalse(it.isTrackedForWidgetPrediction()) + assertFalse(it.buildProto(context).isTrackedForWidgetPrediction()) + } + + createWidgetInfoInfo("test", CONTAINER_PIN_WIDGETS).let { + assertFalse(it.isTrackedForWidgetPrediction()) + assertFalse(it.buildProto(context).isTrackedForWidgetPrediction()) + } + } + + @Test + fun getBundleForHotseatPredictions_contains_pin_events() { + val dataModel = BgDataModel(mock(), mock(), mock(), mock()) + dataModel.itemsIdMap[0] = createAppInfo("test1") + dataModel.itemsIdMap[1] = createAppInfo("test2", CONTAINER_HOTSEAT, 2) + dataModel.itemsIdMap[2] = createAppInfo("test3", 2 /* folder */) + + val items = + getBundleForHotseatPredictions(context, dataModel) + .getParcelableArrayList(BUNDLE_KEY_PIN_EVENTS)!! + assertThat(items).hasSize(2) + assertThat(items[0].launchLocation).isEqualTo("workspace/0/[0,0]/[1,1]") + assertThat(items[0].target!!.packageName).isEqualTo("test1") + + assertThat(items[1].launchLocation).isEqualTo("hotseat/2/[2,0]/[1,1]") + assertThat(items[1].target!!.packageName).isEqualTo("test2") + } + + @Test + fun getBundleForWidgetPredictions_contains_pin_events() { + val dataModel = BgDataModel(mock(), mock(), mock(), mock()) + dataModel.itemsIdMap[0] = createAppInfo("test1") + dataModel.itemsIdMap[1] = createAppInfo("test2", CONTAINER_HOTSEAT, 2) + dataModel.itemsIdMap[2] = createWidgetInfoInfo("test3") + + val items = + getBundleForWidgetPredictions(context, dataModel) + .getParcelableArrayList(BUNDLE_KEY_ADDED_APP_WIDGETS)!! + assertThat(items).hasSize(1) + assertThat(items[0].launchLocation).isEqualTo("workspace/0/[0,0]/[1,1]") + assertThat(items[0].target!!.packageName).isEqualTo("test3") + } + + private fun createAppInfo( + pkg: String, + container: Int = CONTAINER_DESKTOP, + screenId: Int = FIRST_SCREEN_ID, + ) = + AppInfo().apply { + this.container = container + this.screenId = screenId + this.intent = Intent().setComponent(ComponentName(pkg, pkg)) + this.componentName = ComponentName(pkg, pkg) + this.cellX = 0 + this.cellY = 0 + } + + private fun createWidgetInfoInfo( + pkg: String, + container: Int = CONTAINER_DESKTOP, + screenId: Int = FIRST_SCREEN_ID, + ) = + LauncherAppWidgetInfo().apply { + this.container = container + this.screenId = screenId + this.providerName = ComponentName(pkg, pkg) + this.cellX = 0 + this.cellY = 0 + } +}