1 /* 2 * Copyright (C) 2017 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.content.Context; 20 import android.content.Intent; 21 import android.os.Process; 22 23 import androidx.annotation.Nullable; 24 25 import com.android.launcher3.Flags; 26 import com.android.launcher3.Utilities; 27 import com.android.launcher3.icons.BitmapInfo; 28 import com.android.launcher3.icons.BitmapInfo.DrawableCreationFlags; 29 import com.android.launcher3.icons.FastBitmapDrawable; 30 import com.android.launcher3.logging.FileLog; 31 import com.android.launcher3.pm.PackageInstallInfo; 32 import com.android.launcher3.util.ApiWrapper; 33 34 /** 35 * Represents an ItemInfo which also holds an icon. 36 */ 37 public abstract class ItemInfoWithIcon extends ItemInfo { 38 39 public static final String TAG = "ItemInfoDebug"; 40 41 /** 42 * The bitmap for the application icon 43 */ 44 public BitmapInfo bitmap = BitmapInfo.LOW_RES_INFO; 45 46 /** 47 * Indicates that the icon is disabled due to safe mode restrictions. 48 */ 49 public static final int FLAG_DISABLED_SAFEMODE = 1 << 0; 50 51 /** 52 * Indicates that the icon is disabled as the app is not available. 53 */ 54 public static final int FLAG_DISABLED_NOT_AVAILABLE = 1 << 1; 55 56 /** 57 * Indicates that the icon is disabled as the app is suspended 58 */ 59 public static final int FLAG_DISABLED_SUSPENDED = 1 << 2; 60 61 /** 62 * Indicates that the icon is disabled as the user is in quiet mode. 63 */ 64 public static final int FLAG_DISABLED_QUIET_USER = 1 << 3; 65 66 /** 67 * Indicates that the icon is disabled as the publisher has disabled the actual shortcut. 68 */ 69 public static final int FLAG_DISABLED_BY_PUBLISHER = 1 << 4; 70 71 /** 72 * Indicates that the icon is disabled as the user partition is currently locked. 73 */ 74 public static final int FLAG_DISABLED_LOCKED_USER = 1 << 5; 75 76 /** 77 * The item points to a system app. 78 */ 79 public static final int FLAG_SYSTEM_YES = 1 << 6; 80 81 /** 82 * The item points to a non system app. 83 */ 84 public static final int FLAG_SYSTEM_NO = 1 << 7; 85 86 public static final int FLAG_SYSTEM_MASK = FLAG_SYSTEM_YES | FLAG_SYSTEM_NO; 87 88 /** 89 * The icon is being installed. If {@link WorkspaceItemInfo#FLAG_RESTORED_ICON} or 90 * {@link WorkspaceItemInfo#FLAG_AUTOINSTALL_ICON} is set, then the icon is either being 91 * installed or is in a broken state. 92 */ 93 public static final int FLAG_INSTALL_SESSION_ACTIVE = 1 << 10; 94 95 /** 96 * This icon is still being downloaded. 97 */ 98 public static final int FLAG_INCREMENTAL_DOWNLOAD_ACTIVE = 1 << 11; 99 100 public static final int FLAG_SHOW_DOWNLOAD_PROGRESS_MASK = FLAG_INSTALL_SESSION_ACTIVE 101 | FLAG_INCREMENTAL_DOWNLOAD_ACTIVE; 102 103 /** 104 * Indicates that the icon is a disabled shortcut and application updates are required. 105 */ 106 public static final int FLAG_DISABLED_VERSION_LOWER = 1 << 12; 107 108 public static final int FLAG_DISABLED_MASK = FLAG_DISABLED_SAFEMODE 109 | FLAG_DISABLED_NOT_AVAILABLE | FLAG_DISABLED_SUSPENDED 110 | FLAG_DISABLED_QUIET_USER | FLAG_DISABLED_BY_PUBLISHER | FLAG_DISABLED_LOCKED_USER 111 | FLAG_DISABLED_VERSION_LOWER; 112 113 /** 114 * Flag indicating this item can't be pinned to home screen. 115 */ 116 public static final int FLAG_NOT_PINNABLE = 1 << 13; 117 118 /** 119 * Flag indicating whether the package related to the item & user corresponds to that of 120 * archived app. 121 */ 122 public static final int FLAG_ARCHIVED = 1 << 14; 123 124 /** 125 * Flag indicating whether the package related to the item & user does not support resizing. 126 */ 127 public static final int FLAG_NOT_RESIZEABLE = 1 << 15; 128 129 /** 130 * Flag indicating whether the package related to the item & user supports multiple instances. 131 */ 132 public static final int FLAG_SUPPORTS_MULTI_INSTANCE = 1 << 16; 133 134 /** 135 * Status associated with the system state of the underlying item. This is calculated every 136 * time a new info is created and not persisted on the disk. 137 */ 138 public int runtimeStatusFlags = 0; 139 140 /** 141 * The download progress of the package that this shortcut represents. For legacy apps, this 142 * will always be the installation progress. For apps that support incremental downloads, this 143 * will only match be the installation progress until the app is installed, then this will the 144 * total download progress. 145 */ 146 private int mProgressLevel = 100; 147 ItemInfoWithIcon()148 protected ItemInfoWithIcon() { 149 } 150 ItemInfoWithIcon(ItemInfoWithIcon info)151 protected ItemInfoWithIcon(ItemInfoWithIcon info) { 152 super(info); 153 bitmap = info.bitmap; 154 mProgressLevel = info.mProgressLevel; 155 runtimeStatusFlags = info.runtimeStatusFlags; 156 user = info.user; 157 } 158 159 @Override isDisabled()160 public boolean isDisabled() { 161 return (runtimeStatusFlags & FLAG_DISABLED_MASK) != 0; 162 } 163 164 /** 165 * @return {@code true} if the app is pending download (0 progress) or if the app is archived 166 * and its install session is active 167 */ isPendingDownload()168 public boolean isPendingDownload() { 169 return getProgressLevel() == 0; 170 } 171 172 /** 173 * Returns true if the app corresponding to the item is archived. 174 */ isArchived()175 public boolean isArchived() { 176 if (!Flags.enableSupportForArchiving()) { 177 return false; 178 } 179 return (runtimeStatusFlags & FLAG_ARCHIVED) != 0; 180 } 181 182 /** Returns true if the app is archived and doesn't have an active install session. */ isInactiveArchive()183 public boolean isInactiveArchive() { 184 return isArchived() && (runtimeStatusFlags & FLAG_INSTALL_SESSION_ACTIVE) == 0; 185 } 186 187 /** 188 * Indicates whether we're using a low res icon 189 */ usingLowResIcon()190 public boolean usingLowResIcon() { 191 return bitmap.isLowRes(); 192 } 193 194 /** 195 * Returns whether the app this shortcut represents is able to be started. For legacy apps, 196 * this returns whether it is fully installed. For apps that support incremental downloads, 197 * this returns whether the app is either fully downloaded or has installed and is downloading 198 * incrementally. 199 */ isAppStartable()200 public boolean isAppStartable() { 201 return ((runtimeStatusFlags & FLAG_INSTALL_SESSION_ACTIVE) == 0) 202 && (((runtimeStatusFlags & FLAG_INCREMENTAL_DOWNLOAD_ACTIVE) != 0) 203 || mProgressLevel == 100 || isArchived()); 204 } 205 206 /** 207 * Returns the download progress for the app this shortcut represents. If this app is not yet 208 * installed or does not support incremental downloads, this will return the installation 209 * progress. 210 */ getProgressLevel()211 public int getProgressLevel() { 212 if (((runtimeStatusFlags & FLAG_SHOW_DOWNLOAD_PROGRESS_MASK) != 0) 213 // This condition for archived apps is so that in case unarchival/update of 214 // archived app is cancelled, the state transitions back to 0% installed state. 215 || isArchived()) { 216 return mProgressLevel; 217 } 218 return 100; 219 } 220 221 /** 222 * Sets the download progress for the app this shortcut represents. If this app is not yet 223 * installed or does not support incremental downloads, this will set 224 * {@code FLAG_INSTALL_SESSION_ACTIVE}. If this app is downloading incrementally, this will 225 * set {@code FLAG_INCREMENTAL_DOWNLOAD_ACTIVE}. Otherwise, this will remove both flags. 226 */ setProgressLevel(PackageInstallInfo installInfo)227 public void setProgressLevel(PackageInstallInfo installInfo) { 228 setProgressLevel(installInfo.progress, installInfo.state); 229 230 if (installInfo.state == PackageInstallInfo.STATUS_FAILED) { 231 FileLog.d(TAG, 232 "Icon info: " + this + " marked broken with install info: " + installInfo, 233 new Exception()); 234 } 235 } 236 237 /** 238 * Sets the download progress for the app this shortcut represents. 239 */ setProgressLevel(int progress, int status)240 public void setProgressLevel(int progress, int status) { 241 if (status == PackageInstallInfo.STATUS_INSTALLING) { 242 mProgressLevel = progress; 243 runtimeStatusFlags = progress < 100 244 ? runtimeStatusFlags | FLAG_INSTALL_SESSION_ACTIVE 245 : runtimeStatusFlags & ~FLAG_INSTALL_SESSION_ACTIVE; 246 } else if (status == PackageInstallInfo.STATUS_INSTALLED_DOWNLOADING) { 247 mProgressLevel = progress; 248 runtimeStatusFlags = runtimeStatusFlags & ~FLAG_INSTALL_SESSION_ACTIVE; 249 runtimeStatusFlags = progress < 100 250 ? runtimeStatusFlags | FLAG_INCREMENTAL_DOWNLOAD_ACTIVE 251 : runtimeStatusFlags & ~FLAG_INCREMENTAL_DOWNLOAD_ACTIVE; 252 } else { 253 mProgressLevel = status == PackageInstallInfo.STATUS_INSTALLED ? 100 : 0; 254 runtimeStatusFlags &= ~FLAG_INSTALL_SESSION_ACTIVE; 255 runtimeStatusFlags &= ~FLAG_INCREMENTAL_DOWNLOAD_ACTIVE; 256 } 257 } 258 259 /** 260 * Sets whether this app info supports multi-instance. 261 */ 262 protected void setSupportsMultiInstance(boolean supportsMultiInstance) { 263 if (supportsMultiInstance) { 264 runtimeStatusFlags |= FLAG_SUPPORTS_MULTI_INSTANCE; 265 } else { 266 runtimeStatusFlags &= ~FLAG_SUPPORTS_MULTI_INSTANCE; 267 } 268 } 269 270 /** 271 * Returns whether this app info supports multi-instance. 272 */ 273 public boolean supportsMultiInstance() { 274 return (runtimeStatusFlags & FLAG_SUPPORTS_MULTI_INSTANCE) != 0; 275 } 276 277 /** 278 * Sets whether this app info is non-resizeable. 279 */ 280 public void setNonResizeable(boolean nonResizeable) { 281 if (nonResizeable) { 282 runtimeStatusFlags |= FLAG_NOT_RESIZEABLE; 283 } else { 284 runtimeStatusFlags &= ~FLAG_NOT_RESIZEABLE; 285 } 286 } 287 288 /** 289 * Returns whether this app info is resizeable. 290 */ 291 public boolean isNonResizeable() { 292 return (runtimeStatusFlags & FLAG_NOT_RESIZEABLE) != 0; 293 } 294 295 /** Creates an intent to that launches the app store at this app's page. */ 296 @Nullable 297 public Intent getMarketIntent(Context context) { 298 String targetPackage = getTargetPackage(); 299 300 return targetPackage != null 301 ? ApiWrapper.INSTANCE.get(context).getAppMarketActivityIntent( 302 targetPackage, Process.myUserHandle()) 303 : null; 304 } 305 306 /** 307 * @return a copy of this 308 */ 309 public abstract ItemInfoWithIcon clone(); 310 311 312 /** 313 * Returns a FastBitmapDrawable with the icon. 314 */ 315 public FastBitmapDrawable newIcon(Context context) { 316 return newIcon(context, 0); 317 } 318 319 /** 320 * Returns a FastBitmapDrawable with the icon and context theme applied 321 */ 322 public FastBitmapDrawable newIcon(Context context, @DrawableCreationFlags int creationFlags) { 323 FastBitmapDrawable drawable = bitmap.newIcon(context, creationFlags); 324 drawable.setIsDisabled(isDisabled()); 325 return drawable; 326 } 327 328 @Override 329 protected String dumpProperties() { 330 return super.dumpProperties() 331 + " supportsMultiInstance=" + supportsMultiInstance() 332 + " nonResizeable=" + isNonResizeable(); 333 } 334 } 335