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 * Flag indicating this item can't be pinned to home screen. 123 */ 124 public static final int FLAG_NOT_PINNABLE = 1 << 13; 125 126 /** 127 * Status associated with the system state of the underlying item. This is calculated every 128 * time a new info is created and not persisted on the disk. 129 */ 130 public int runtimeStatusFlags = 0; 131 132 /** 133 * The download progress of the package that this shortcut represents. For legacy apps, this 134 * will always be the installation progress. For apps that support incremental downloads, this 135 * will only match be the installation progress until the app is installed, then this will the 136 * total download progress. 137 */ 138 private int mProgressLevel = 100; 139 ItemInfoWithIcon()140 protected ItemInfoWithIcon() { } 141 ItemInfoWithIcon(ItemInfoWithIcon info)142 protected ItemInfoWithIcon(ItemInfoWithIcon info) { 143 super(info); 144 bitmap = info.bitmap; 145 mProgressLevel = info.mProgressLevel; 146 runtimeStatusFlags = info.runtimeStatusFlags; 147 user = info.user; 148 } 149 150 @Override isDisabled()151 public boolean isDisabled() { 152 return (runtimeStatusFlags & FLAG_DISABLED_MASK) != 0; 153 } 154 155 /** 156 * Indicates whether we're using a low res icon 157 */ usingLowResIcon()158 public boolean usingLowResIcon() { 159 return bitmap.isLowRes(); 160 } 161 162 /** 163 * Returns whether the app this shortcut represents is able to be started. For legacy apps, 164 * this returns whether it is fully installed. For apps that support incremental downloads, 165 * this returns whether the app is either fully downloaded or has installed and is downloading 166 * incrementally. 167 */ isAppStartable()168 public boolean isAppStartable() { 169 return ((runtimeStatusFlags & FLAG_INSTALL_SESSION_ACTIVE) == 0) 170 && (((runtimeStatusFlags & FLAG_INCREMENTAL_DOWNLOAD_ACTIVE) != 0) 171 || mProgressLevel == 100); 172 } 173 174 /** 175 * Returns the download progress for the app this shortcut represents. If this app is not yet 176 * installed or does not support incremental downloads, this will return the installation 177 * progress. 178 */ getProgressLevel()179 public int getProgressLevel() { 180 if ((runtimeStatusFlags & FLAG_SHOW_DOWNLOAD_PROGRESS_MASK) != 0) { 181 return mProgressLevel; 182 } 183 return 100; 184 } 185 186 /** 187 * Sets the download progress for the app this shortcut represents. If this app is not yet 188 * installed or does not support incremental downloads, this will set 189 * {@code FLAG_INSTALL_SESSION_ACTIVE}. If this app is downloading incrementally, this will 190 * set {@code FLAG_INCREMENTAL_DOWNLOAD_ACTIVE}. Otherwise, this will remove both flags. 191 */ setProgressLevel(PackageInstallInfo installInfo)192 public void setProgressLevel(PackageInstallInfo installInfo) { 193 setProgressLevel(installInfo.progress, installInfo.state); 194 195 if (installInfo.state == PackageInstallInfo.STATUS_FAILED) { 196 FileLog.d(TAG, 197 "Icon info: " + this + " marked broken with install info: " + installInfo, 198 new Exception()); 199 } 200 } 201 202 /** 203 * Sets the download progress for the app this shortcut represents. 204 */ setProgressLevel(int progress, int status)205 public void setProgressLevel(int progress, int status) { 206 if (status == PackageInstallInfo.STATUS_INSTALLING) { 207 mProgressLevel = progress; 208 runtimeStatusFlags = progress < 100 209 ? runtimeStatusFlags | FLAG_INSTALL_SESSION_ACTIVE 210 : runtimeStatusFlags & ~FLAG_INSTALL_SESSION_ACTIVE; 211 } else if (status == PackageInstallInfo.STATUS_INSTALLED_DOWNLOADING) { 212 mProgressLevel = progress; 213 runtimeStatusFlags = runtimeStatusFlags & ~FLAG_INSTALL_SESSION_ACTIVE; 214 runtimeStatusFlags = progress < 100 215 ? runtimeStatusFlags | FLAG_INCREMENTAL_DOWNLOAD_ACTIVE 216 : runtimeStatusFlags & ~FLAG_INCREMENTAL_DOWNLOAD_ACTIVE; 217 } else { 218 mProgressLevel = status == PackageInstallInfo.STATUS_INSTALLED ? 100 : 0; 219 runtimeStatusFlags &= ~FLAG_INSTALL_SESSION_ACTIVE; 220 runtimeStatusFlags &= ~FLAG_INCREMENTAL_DOWNLOAD_ACTIVE; 221 } 222 } 223 224 /** Creates an intent to that launches the app store at this app's page. */ 225 @Nullable 226 public Intent getMarketIntent(Context context) { 227 String targetPackage = getTargetPackage(); 228 229 return targetPackage != null 230 ? new PackageManagerHelper(context).getMarketIntent(targetPackage) 231 : null; 232 } 233 234 /** 235 * @return a copy of this 236 */ 237 public abstract ItemInfoWithIcon clone(); 238 239 240 /** 241 * Returns a FastBitmapDrawable with the icon. 242 */ 243 public FastBitmapDrawable newIcon(Context context) { 244 return newIcon(context, 0); 245 } 246 247 /** 248 * Returns a FastBitmapDrawable with the icon and context theme applied 249 */ 250 public FastBitmapDrawable newIcon(Context context, @DrawableCreationFlags int creationFlags) { 251 FastBitmapDrawable drawable = bitmap.newIcon(context, creationFlags); 252 drawable.setIsDisabled(isDisabled()); 253 return drawable; 254 } 255 } 256