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 22 import androidx.annotation.Nullable; 23 24 import com.android.launcher3.icons.BitmapInfo; 25 import com.android.launcher3.icons.BitmapInfo.DrawableCreationFlags; 26 import com.android.launcher3.icons.FastBitmapDrawable; 27 import com.android.launcher3.logging.FileLog; 28 import com.android.launcher3.pm.PackageInstallInfo; 29 import com.android.launcher3.util.PackageManagerHelper; 30 31 /** 32 * Represents an ItemInfo which also holds an icon. 33 */ 34 public abstract class ItemInfoWithIcon extends ItemInfo { 35 36 public static final String TAG = "ItemInfoDebug"; 37 38 /** 39 * The bitmap for the application icon 40 */ 41 public BitmapInfo bitmap = BitmapInfo.LOW_RES_INFO; 42 43 /** 44 * Indicates that the icon is disabled due to safe mode restrictions. 45 */ 46 public static final int FLAG_DISABLED_SAFEMODE = 1 << 0; 47 48 /** 49 * Indicates that the icon is disabled as the app is not available. 50 */ 51 public static final int FLAG_DISABLED_NOT_AVAILABLE = 1 << 1; 52 53 /** 54 * Indicates that the icon is disabled as the app is suspended 55 */ 56 public static final int FLAG_DISABLED_SUSPENDED = 1 << 2; 57 58 /** 59 * Indicates that the icon is disabled as the user is in quiet mode. 60 */ 61 public static final int FLAG_DISABLED_QUIET_USER = 1 << 3; 62 63 /** 64 * Indicates that the icon is disabled as the publisher has disabled the actual shortcut. 65 */ 66 public static final int FLAG_DISABLED_BY_PUBLISHER = 1 << 4; 67 68 /** 69 * Indicates that the icon is disabled as the user partition is currently locked. 70 */ 71 public static final int FLAG_DISABLED_LOCKED_USER = 1 << 5; 72 73 /** 74 * The item points to a system app. 75 */ 76 public static final int FLAG_SYSTEM_YES = 1 << 6; 77 78 /** 79 * The item points to a non system app. 80 */ 81 public static final int FLAG_SYSTEM_NO = 1 << 7; 82 83 public static final int FLAG_SYSTEM_MASK = FLAG_SYSTEM_YES | FLAG_SYSTEM_NO; 84 85 /** 86 * Flag indicating that the icon is an {@link android.graphics.drawable.AdaptiveIconDrawable} 87 * that can be optimized in various way. 88 */ 89 public static final int FLAG_ADAPTIVE_ICON = 1 << 8; 90 91 /** 92 * Flag indicating that the icon is badged. 93 */ 94 public static final int FLAG_ICON_BADGED = 1 << 9; 95 96 /** 97 * The icon is being installed. If {@link WorkspaceItemInfo#FLAG_RESTORED_ICON} or 98 * {@link WorkspaceItemInfo#FLAG_AUTOINSTALL_ICON} is set, then the icon is either being 99 * installed or is in a broken state. 100 */ 101 public static final int FLAG_INSTALL_SESSION_ACTIVE = 1 << 10; 102 103 /** 104 * This icon is still being downloaded. 105 */ 106 public static final int FLAG_INCREMENTAL_DOWNLOAD_ACTIVE = 1 << 11; 107 108 public static final int FLAG_SHOW_DOWNLOAD_PROGRESS_MASK = FLAG_INSTALL_SESSION_ACTIVE 109 | FLAG_INCREMENTAL_DOWNLOAD_ACTIVE; 110 111 /** 112 * Indicates that the icon is a disabled shortcut and application updates are required. 113 */ 114 public static final int FLAG_DISABLED_VERSION_LOWER = 1 << 12; 115 116 public static final int FLAG_DISABLED_MASK = FLAG_DISABLED_SAFEMODE 117 | FLAG_DISABLED_NOT_AVAILABLE | FLAG_DISABLED_SUSPENDED 118 | FLAG_DISABLED_QUIET_USER | FLAG_DISABLED_BY_PUBLISHER | FLAG_DISABLED_LOCKED_USER 119 | FLAG_DISABLED_VERSION_LOWER; 120 121 /** 122 * Status associated with the system state of the underlying item. This is calculated every 123 * time a new info is created and not persisted on the disk. 124 */ 125 public int runtimeStatusFlags = 0; 126 127 /** 128 * The download progress of the package that this shortcut represents. For legacy apps, this 129 * will always be the installation progress. For apps that support incremental downloads, this 130 * will only match be the installation progress until the app is installed, then this will the 131 * total download progress. 132 */ 133 private int mProgressLevel = 100; 134 ItemInfoWithIcon()135 protected ItemInfoWithIcon() { } 136 ItemInfoWithIcon(ItemInfoWithIcon info)137 protected ItemInfoWithIcon(ItemInfoWithIcon info) { 138 super(info); 139 bitmap = info.bitmap; 140 mProgressLevel = info.mProgressLevel; 141 runtimeStatusFlags = info.runtimeStatusFlags; 142 user = info.user; 143 } 144 145 @Override isDisabled()146 public boolean isDisabled() { 147 return (runtimeStatusFlags & FLAG_DISABLED_MASK) != 0; 148 } 149 150 /** 151 * Indicates whether we're using a low res icon 152 */ usingLowResIcon()153 public boolean usingLowResIcon() { 154 return bitmap.isLowRes(); 155 } 156 157 /** 158 * Returns whether the app this shortcut represents is able to be started. For legacy apps, 159 * this returns whether it is fully installed. For apps that support incremental downloads, 160 * this returns whether the app is either fully downloaded or has installed and is downloading 161 * incrementally. 162 */ isAppStartable()163 public boolean isAppStartable() { 164 return ((runtimeStatusFlags & FLAG_INSTALL_SESSION_ACTIVE) == 0) 165 && (((runtimeStatusFlags & FLAG_INCREMENTAL_DOWNLOAD_ACTIVE) != 0) 166 || mProgressLevel == 100); 167 } 168 169 /** 170 * Returns the download progress for the app this shortcut represents. If this app is not yet 171 * installed or does not support incremental downloads, this will return the installation 172 * progress. 173 */ getProgressLevel()174 public int getProgressLevel() { 175 if ((runtimeStatusFlags & FLAG_SHOW_DOWNLOAD_PROGRESS_MASK) != 0) { 176 return mProgressLevel; 177 } 178 return 100; 179 } 180 181 /** 182 * Sets the download progress for the app this shortcut represents. If this app is not yet 183 * installed or does not support incremental downloads, this will set 184 * {@code FLAG_INSTALL_SESSION_ACTIVE}. If this app is downloading incrementally, this will 185 * set {@code FLAG_INCREMENTAL_DOWNLOAD_ACTIVE}. Otherwise, this will remove both flags. 186 */ setProgressLevel(PackageInstallInfo installInfo)187 public void setProgressLevel(PackageInstallInfo installInfo) { 188 setProgressLevel(installInfo.progress, installInfo.state); 189 190 if (installInfo.state == PackageInstallInfo.STATUS_FAILED) { 191 FileLog.d(TAG, 192 "Icon info: " + this + " marked broken with install info: " + installInfo, 193 new Exception()); 194 } 195 } 196 197 /** 198 * Sets the download progress for the app this shortcut represents. 199 */ setProgressLevel(int progress, int status)200 public void setProgressLevel(int progress, int status) { 201 if (status == PackageInstallInfo.STATUS_INSTALLING) { 202 mProgressLevel = progress; 203 runtimeStatusFlags = progress < 100 204 ? runtimeStatusFlags | FLAG_INSTALL_SESSION_ACTIVE 205 : runtimeStatusFlags & ~FLAG_INSTALL_SESSION_ACTIVE; 206 } else if (status == PackageInstallInfo.STATUS_INSTALLED_DOWNLOADING) { 207 mProgressLevel = progress; 208 runtimeStatusFlags = runtimeStatusFlags & ~FLAG_INSTALL_SESSION_ACTIVE; 209 runtimeStatusFlags = progress < 100 210 ? runtimeStatusFlags | FLAG_INCREMENTAL_DOWNLOAD_ACTIVE 211 : runtimeStatusFlags & ~FLAG_INCREMENTAL_DOWNLOAD_ACTIVE; 212 } else { 213 mProgressLevel = status == PackageInstallInfo.STATUS_INSTALLED ? 100 : 0; 214 runtimeStatusFlags &= ~FLAG_INSTALL_SESSION_ACTIVE; 215 runtimeStatusFlags &= ~FLAG_INCREMENTAL_DOWNLOAD_ACTIVE; 216 } 217 } 218 219 /** Creates an intent to that launches the app store at this app's page. */ 220 @Nullable 221 public Intent getMarketIntent(Context context) { 222 String targetPackage = getTargetPackage(); 223 224 return targetPackage != null 225 ? new PackageManagerHelper(context).getMarketIntent(targetPackage) 226 : null; 227 } 228 229 /** 230 * @return a copy of this 231 */ 232 public abstract ItemInfoWithIcon clone(); 233 234 235 /** 236 * Returns a FastBitmapDrawable with the icon. 237 */ 238 public FastBitmapDrawable newIcon(Context context) { 239 return newIcon(context, 0); 240 } 241 242 /** 243 * Returns a FastBitmapDrawable with the icon and context theme applied 244 */ 245 public FastBitmapDrawable newIcon(Context context, @DrawableCreationFlags int creationFlags) { 246 FastBitmapDrawable drawable = bitmap.newIcon(context, creationFlags); 247 drawable.setIsDisabled(isDisabled()); 248 return drawable; 249 } 250 } 251