1 /* 2 * Copyright (C) 2008 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 17 package com.android.launcher3.model.data; 18 19 import android.app.Person; 20 import android.content.ComponentName; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.content.pm.ShortcutInfo; 24 import android.text.TextUtils; 25 26 import androidx.annotation.NonNull; 27 28 import com.android.launcher3.LauncherSettings; 29 import com.android.launcher3.LauncherSettings.Favorites; 30 import com.android.launcher3.Utilities; 31 import com.android.launcher3.icons.IconCache; 32 import com.android.launcher3.shortcuts.ShortcutKey; 33 import com.android.launcher3.uioverrides.ApiWrapper; 34 import com.android.launcher3.util.ContentWriter; 35 36 import java.util.Arrays; 37 38 /** 39 * Represents a launchable icon on the workspaces and in folders. 40 */ 41 public class WorkspaceItemInfo extends ItemInfoWithIcon { 42 43 public static final int DEFAULT = 0; 44 45 /** 46 * The shortcut was restored from a backup and it not ready to be used. This is automatically 47 * set during backup/restore 48 */ 49 public static final int FLAG_RESTORED_ICON = 1; 50 51 /** 52 * The icon was added as an auto-install app, and is not ready to be used. This flag can't 53 * be present along with {@link #FLAG_RESTORED_ICON}, and is set during default layout 54 * parsing. 55 * 56 * OR this icon was added due to it being an active install session created by the user. 57 */ 58 public static final int FLAG_AUTOINSTALL_ICON = 1 << 1; 59 60 /** 61 * Indicates that the widget restore has started. 62 */ 63 public static final int FLAG_RESTORE_STARTED = 1 << 2; 64 65 /** 66 * Web UI supported. 67 */ 68 public static final int FLAG_SUPPORTS_WEB_UI = 1 << 3; 69 70 /** 71 * 72 */ 73 public static final int FLAG_START_FOR_RESULT = 1 << 4; 74 75 /** 76 * The intent used to start the application. 77 */ 78 public Intent intent; 79 80 /** 81 * If isShortcut=true and customIcon=false, this contains a reference to the 82 * shortcut icon as an application's resource. 83 */ 84 public Intent.ShortcutIconResource iconResource; 85 86 /** 87 * A message to display when the user tries to start a disabled shortcut. 88 * This is currently only used for deep shortcuts. 89 */ 90 public CharSequence disabledMessage; 91 92 public int status; 93 94 /** 95 * A set of person's Id associated with the WorkspaceItemInfo, this is only used if the item 96 * represents a deep shortcut. 97 */ 98 @NonNull private String[] personKeys = Utilities.EMPTY_STRING_ARRAY; 99 100 public int options; 101 102 WorkspaceItemInfo()103 public WorkspaceItemInfo() { 104 itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT; 105 } 106 WorkspaceItemInfo(WorkspaceItemInfo info)107 public WorkspaceItemInfo(WorkspaceItemInfo info) { 108 super(info); 109 title = info.title; 110 intent = new Intent(info.intent); 111 iconResource = info.iconResource; 112 status = info.status; 113 personKeys = info.personKeys.clone(); 114 } 115 116 /** TODO: Remove this. It's only called by ApplicationInfo.makeWorkspaceItem. */ WorkspaceItemInfo(AppInfo info)117 public WorkspaceItemInfo(AppInfo info) { 118 super(info); 119 title = Utilities.trim(info.title); 120 intent = new Intent(info.getIntent()); 121 } 122 123 /** 124 * Creates a {@link WorkspaceItemInfo} from a {@link ShortcutInfo}. 125 */ WorkspaceItemInfo(ShortcutInfo shortcutInfo, Context context)126 public WorkspaceItemInfo(ShortcutInfo shortcutInfo, Context context) { 127 user = shortcutInfo.getUserHandle(); 128 itemType = Favorites.ITEM_TYPE_DEEP_SHORTCUT; 129 updateFromDeepShortcutInfo(shortcutInfo, context); 130 } 131 132 @Override onAddToDatabase(ContentWriter writer)133 public void onAddToDatabase(ContentWriter writer) { 134 super.onAddToDatabase(writer); 135 writer.put(Favorites.TITLE, title) 136 .put(Favorites.INTENT, getIntent()) 137 .put(Favorites.OPTIONS, options) 138 .put(Favorites.RESTORED, status); 139 140 if (!usingLowResIcon()) { 141 writer.putIcon(bitmap, user); 142 } 143 if (iconResource != null) { 144 writer.put(Favorites.ICON_PACKAGE, iconResource.packageName) 145 .put(Favorites.ICON_RESOURCE, iconResource.resourceName); 146 } 147 } 148 149 @Override getIntent()150 public Intent getIntent() { 151 return intent; 152 } 153 hasStatusFlag(int flag)154 public boolean hasStatusFlag(int flag) { 155 return (status & flag) != 0; 156 } 157 158 isPromise()159 public final boolean isPromise() { 160 return hasStatusFlag(FLAG_RESTORED_ICON | FLAG_AUTOINSTALL_ICON); 161 } 162 hasPromiseIconUi()163 public boolean hasPromiseIconUi() { 164 return isPromise() && !hasStatusFlag(FLAG_SUPPORTS_WEB_UI); 165 } 166 updateFromDeepShortcutInfo(ShortcutInfo shortcutInfo, Context context)167 public void updateFromDeepShortcutInfo(ShortcutInfo shortcutInfo, Context context) { 168 // {@link ShortcutInfo#getActivity} can change during an update. Recreate the intent 169 intent = ShortcutKey.makeIntent(shortcutInfo); 170 title = shortcutInfo.getShortLabel(); 171 172 CharSequence label = shortcutInfo.getLongLabel(); 173 if (TextUtils.isEmpty(label)) { 174 label = shortcutInfo.getShortLabel(); 175 } 176 contentDescription = context.getPackageManager().getUserBadgedLabel(label, user); 177 if (shortcutInfo.isEnabled()) { 178 runtimeStatusFlags &= ~FLAG_DISABLED_BY_PUBLISHER; 179 } else { 180 runtimeStatusFlags |= FLAG_DISABLED_BY_PUBLISHER; 181 } 182 disabledMessage = shortcutInfo.getDisabledMessage(); 183 184 Person[] persons = ApiWrapper.getPersons(shortcutInfo); 185 personKeys = persons.length == 0 ? Utilities.EMPTY_STRING_ARRAY 186 : Arrays.stream(persons).map(Person::getKey).sorted().toArray(String[]::new); 187 } 188 189 /** Returns the WorkspaceItemInfo id associated with the deep shortcut. */ getDeepShortcutId()190 public String getDeepShortcutId() { 191 return itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT 192 ? getIntent().getStringExtra(ShortcutKey.EXTRA_SHORTCUT_ID) : null; 193 } 194 195 @NonNull getPersonKeys()196 public String[] getPersonKeys() { 197 return personKeys; 198 } 199 200 @Override getTargetComponent()201 public ComponentName getTargetComponent() { 202 ComponentName cn = super.getTargetComponent(); 203 if (cn == null && (itemType == Favorites.ITEM_TYPE_SHORTCUT || hasStatusFlag( 204 FLAG_SUPPORTS_WEB_UI | FLAG_AUTOINSTALL_ICON | FLAG_RESTORED_ICON))) { 205 // Legacy shortcuts and promise icons with web UI may not have a componentName but just 206 // a packageName. In that case create a empty componentName instead of adding additional 207 // check everywhere. 208 String pkg = intent.getPackage(); 209 return pkg == null ? null : new ComponentName(pkg, IconCache.EMPTY_CLASS_NAME); 210 } 211 return cn; 212 } 213 214 @Override clone()215 public WorkspaceItemInfo clone() { 216 return new WorkspaceItemInfo(this); 217 } 218 } 219