1 /* 2 * Copyright (C) 2021 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.launcher3.model; 17 18 import static com.android.launcher3.logger.LauncherAtom.ContainerInfo.ContainerCase.WORKSPACE; 19 20 import android.app.prediction.AppTarget; 21 import android.app.prediction.AppTargetEvent; 22 import android.app.prediction.AppTargetId; 23 import android.content.ComponentName; 24 import android.content.Context; 25 26 import androidx.annotation.Nullable; 27 28 import com.android.launcher3.LauncherSettings; 29 import com.android.launcher3.Workspace; 30 import com.android.launcher3.logger.LauncherAtom; 31 import com.android.launcher3.model.data.ItemInfo; 32 import com.android.launcher3.model.data.LauncherAppWidgetInfo; 33 import com.android.launcher3.model.data.WorkspaceItemInfo; 34 import com.android.launcher3.shortcuts.ShortcutKey; 35 36 import java.util.Locale; 37 38 /** Helper class with methods for converting launcher items to form usable by predictors */ 39 public final class PredictionHelper { 40 private static final String APP_LOCATION_HOTSEAT = "hotseat"; 41 private static final String APP_LOCATION_WORKSPACE = "workspace"; 42 43 /** 44 * Creates and returns an {@link AppTarget} object for an {@link ItemInfo}. Returns null 45 * if item type is not supported in predictions 46 */ 47 @Nullable getAppTargetFromItemInfo(Context context, ItemInfo info)48 public static AppTarget getAppTargetFromItemInfo(Context context, ItemInfo info) { 49 if (info == null) return null; 50 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET 51 && info instanceof LauncherAppWidgetInfo 52 && ((LauncherAppWidgetInfo) info).providerName != null) { 53 ComponentName cn = ((LauncherAppWidgetInfo) info).providerName; 54 return new AppTarget.Builder(new AppTargetId("widget:" + cn.getPackageName()), 55 cn.getPackageName(), info.user).setClassName(cn.getClassName()).build(); 56 } else if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION 57 && info.getTargetComponent() != null) { 58 ComponentName cn = info.getTargetComponent(); 59 return new AppTarget.Builder(new AppTargetId("app:" + cn.getPackageName()), 60 cn.getPackageName(), info.user).setClassName(cn.getClassName()).build(); 61 } else if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT 62 && info instanceof WorkspaceItemInfo) { 63 ShortcutKey shortcutKey = ShortcutKey.fromItemInfo(info); 64 //TODO: switch to using full shortcut info 65 return new AppTarget.Builder(new AppTargetId("shortcut:" + shortcutKey.getId()), 66 shortcutKey.componentName.getPackageName(), shortcutKey.user).build(); 67 } else if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_FOLDER) { 68 return new AppTarget.Builder(new AppTargetId("folder:" + info.id), 69 context.getPackageName(), info.user).build(); 70 } 71 return null; 72 } 73 74 /** 75 * Creates and returns {@link AppTargetEvent} from an {@link AppTarget}, action, and item 76 * location using {@link ItemInfo} 77 */ wrapAppTargetWithItemLocation( AppTarget target, int action, ItemInfo info)78 public static AppTargetEvent wrapAppTargetWithItemLocation( 79 AppTarget target, int action, ItemInfo info) { 80 String location = String.format(Locale.ENGLISH, "%s/%d/[%d,%d]/[%d,%d]", 81 info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT 82 ? APP_LOCATION_HOTSEAT : APP_LOCATION_WORKSPACE, 83 info.screenId, info.cellX, info.cellY, info.spanX, info.spanY); 84 return new AppTargetEvent.Builder(target, action).setLaunchLocation(location).build(); 85 } 86 87 /** 88 * Helper method to determine if {@link ItemInfo} should be tracked and reported to hotseat 89 * predictors 90 */ isTrackedForHotseatPrediction(ItemInfo info)91 public static boolean isTrackedForHotseatPrediction(ItemInfo info) { 92 return info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT || ( 93 info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP 94 && info.screenId == Workspace.FIRST_SCREEN_ID); 95 } 96 97 /** 98 * Helper method to determine if {@link LauncherAtom.ItemInfo} should be tracked and reported to 99 * hotseat predictors 100 */ isTrackedForHotseatPrediction(LauncherAtom.ItemInfo info)101 public static boolean isTrackedForHotseatPrediction(LauncherAtom.ItemInfo info) { 102 LauncherAtom.ContainerInfo ci = info.getContainerInfo(); 103 switch (ci.getContainerCase()) { 104 case HOTSEAT: 105 return true; 106 case WORKSPACE: 107 return ci.getWorkspace().getPageIndex() == 0; 108 default: 109 return false; 110 } 111 } 112 113 /** 114 * Helper method to determine if {@link ItemInfo} should be tracked and reported to widget 115 * predictors 116 */ isTrackedForWidgetPrediction(ItemInfo info)117 public static boolean isTrackedForWidgetPrediction(ItemInfo info) { 118 return info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET 119 && info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP; 120 } 121 122 /** 123 * Helper method to determine if {@link LauncherAtom.ItemInfo} should be tracked and reported 124 * to widget predictors 125 */ isTrackedForWidgetPrediction(LauncherAtom.ItemInfo info)126 public static boolean isTrackedForWidgetPrediction(LauncherAtom.ItemInfo info) { 127 return info.getItemCase() == LauncherAtom.ItemInfo.ItemCase.WIDGET 128 && info.getContainerInfo().getContainerCase() == WORKSPACE; 129 } 130 } 131