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.ComponentName;
20 import android.content.Context;
21 import android.content.Intent;
22 
23 import androidx.annotation.Nullable;
24 
25 import com.android.launcher3.icons.BitmapInfo;
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     public static final int FLAG_DISABLED_MASK = FLAG_DISABLED_SAFEMODE
74             | FLAG_DISABLED_NOT_AVAILABLE | FLAG_DISABLED_SUSPENDED
75             | FLAG_DISABLED_QUIET_USER | FLAG_DISABLED_BY_PUBLISHER | FLAG_DISABLED_LOCKED_USER;
76 
77     /**
78      * The item points to a system app.
79      */
80     public static final int FLAG_SYSTEM_YES = 1 << 6;
81 
82     /**
83      * The item points to a non system app.
84      */
85     public static final int FLAG_SYSTEM_NO = 1 << 7;
86 
87     public static final int FLAG_SYSTEM_MASK = FLAG_SYSTEM_YES | FLAG_SYSTEM_NO;
88 
89     /**
90      * Flag indicating that the icon is an {@link android.graphics.drawable.AdaptiveIconDrawable}
91      * that can be optimized in various way.
92      */
93     public static final int FLAG_ADAPTIVE_ICON = 1 << 8;
94 
95     /**
96      * Flag indicating that the icon is badged.
97      */
98     public static final int FLAG_ICON_BADGED = 1 << 9;
99 
100     /**
101      * The icon is being installed. If {@link WorkspaceItemInfo#FLAG_RESTORED_ICON} or
102      * {@link WorkspaceItemInfo#FLAG_AUTOINSTALL_ICON} is set, then the icon is either being
103      * installed or is in a broken state.
104      */
105     public static final int FLAG_INSTALL_SESSION_ACTIVE = 1 << 10;
106 
107     /**
108      * This icon is still being downloaded.
109      */
110     public static final int FLAG_INCREMENTAL_DOWNLOAD_ACTIVE = 1 << 11;
111 
112     public static final int FLAG_SHOW_DOWNLOAD_PROGRESS_MASK = FLAG_INSTALL_SESSION_ACTIVE
113             | FLAG_INCREMENTAL_DOWNLOAD_ACTIVE;
114 
115     /**
116      * Status associated with the system state of the underlying item. This is calculated every
117      * time a new info is created and not persisted on the disk.
118      */
119     public int runtimeStatusFlags = 0;
120 
121     /**
122      * The download progress of the package that this shortcut represents. For legacy apps, this
123      * will always be the installation progress. For apps that support incremental downloads, this
124      * will only match be the installation progress until the app is installed, then this will the
125      * total download progress.
126      */
127     private int mProgressLevel = 100;
128 
ItemInfoWithIcon()129     protected ItemInfoWithIcon() { }
130 
ItemInfoWithIcon(ItemInfoWithIcon info)131     protected ItemInfoWithIcon(ItemInfoWithIcon info) {
132         super(info);
133         bitmap = info.bitmap;
134         mProgressLevel = info.mProgressLevel;
135         runtimeStatusFlags = info.runtimeStatusFlags;
136         user = info.user;
137     }
138 
139     @Override
isDisabled()140     public boolean isDisabled() {
141         return (runtimeStatusFlags & FLAG_DISABLED_MASK) != 0;
142     }
143 
144     /**
145      * Indicates whether we're using a low res icon
146      */
usingLowResIcon()147     public boolean usingLowResIcon() {
148         return bitmap.isLowRes();
149     }
150 
151     /**
152      * Returns whether the app this shortcut represents is able to be started. For legacy apps,
153      * this returns whether it is fully installed. For apps that support incremental downloads,
154      * this returns whether the app is either fully downloaded or has installed and is downloading
155      * incrementally.
156      */
isAppStartable()157     public boolean isAppStartable() {
158         return ((runtimeStatusFlags & FLAG_INSTALL_SESSION_ACTIVE) == 0)
159                 && (((runtimeStatusFlags & FLAG_INCREMENTAL_DOWNLOAD_ACTIVE) != 0)
160                     || mProgressLevel == 100);
161     }
162 
163     /**
164      * Returns the download progress for the app this shortcut represents. If this app is not yet
165      * installed or does not support incremental downloads, this will return the installation
166      * progress.
167      */
getProgressLevel()168     public int getProgressLevel() {
169         if ((runtimeStatusFlags & FLAG_SHOW_DOWNLOAD_PROGRESS_MASK) != 0) {
170             return mProgressLevel;
171         }
172         return 100;
173     }
174 
175     /**
176      * Sets the download progress for the app this shortcut represents. If this app is not yet
177      * installed or does not support incremental downloads, this will set
178      * {@code FLAG_INSTALL_SESSION_ACTIVE}. If this app is downloading incrementally, this will
179      * set {@code FLAG_INCREMENTAL_DOWNLOAD_ACTIVE}. Otherwise, this will remove both flags.
180      */
setProgressLevel(PackageInstallInfo installInfo)181     public void setProgressLevel(PackageInstallInfo installInfo) {
182         setProgressLevel(installInfo.progress, installInfo.state);
183 
184         if (installInfo.state == PackageInstallInfo.STATUS_FAILED) {
185             FileLog.d(TAG,
186                     "Icon info: " + this + " marked broken with install info: " + installInfo,
187                     new Exception());
188         }
189     }
190 
191     /**
192      * Sets the download progress for the app this shortcut represents.
193      */
setProgressLevel(int progress, int status)194     public void setProgressLevel(int progress, int status) {
195         if (status == PackageInstallInfo.STATUS_INSTALLING) {
196             mProgressLevel = progress;
197             runtimeStatusFlags = progress < 100
198                     ? runtimeStatusFlags | FLAG_INSTALL_SESSION_ACTIVE
199                     : runtimeStatusFlags & ~FLAG_INSTALL_SESSION_ACTIVE;
200         } else if (status == PackageInstallInfo.STATUS_INSTALLED_DOWNLOADING) {
201             mProgressLevel = progress;
202             runtimeStatusFlags = runtimeStatusFlags & ~FLAG_INSTALL_SESSION_ACTIVE;
203             runtimeStatusFlags = progress < 100
204                     ? runtimeStatusFlags | FLAG_INCREMENTAL_DOWNLOAD_ACTIVE
205                     : runtimeStatusFlags & ~FLAG_INCREMENTAL_DOWNLOAD_ACTIVE;
206         } else {
207             mProgressLevel = status == PackageInstallInfo.STATUS_INSTALLED ? 100 : 0;
208             runtimeStatusFlags &= ~FLAG_INSTALL_SESSION_ACTIVE;
209             runtimeStatusFlags &= ~FLAG_INCREMENTAL_DOWNLOAD_ACTIVE;
210         }
211     }
212 
213     /** Creates an intent to that launches the app store at this app's page. */
214     @Nullable
215     public Intent getMarketIntent(Context context) {
216         ComponentName componentName = getTargetComponent();
217 
218         return componentName != null
219                 ? new PackageManagerHelper(context).getMarketIntent(componentName.getPackageName())
220                 : null;
221     }
222 
223     /**
224      * @return a copy of this
225      */
226     public abstract ItemInfoWithIcon clone();
227 
228 
229     /**
230      * Returns a FastBitmapDrawable with the icon.
231      */
232     public FastBitmapDrawable newIcon(Context context) {
233         return newIcon(context, false);
234     }
235 
236     /**
237      * Returns a FastBitmapDrawable with the icon and context theme applied
238      */
239     public FastBitmapDrawable newIcon(Context context, boolean applyTheme) {
240         FastBitmapDrawable drawable = applyTheme
241                 ? bitmap.newThemedIcon(context) : bitmap.newIcon(context);
242         drawable.setIsDisabled(isDisabled());
243         return drawable;
244     }
245 }
246