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; 18 19 import static com.android.launcher3.model.data.AppInfo.COMPONENT_KEY_COMPARATOR; 20 import static com.android.launcher3.model.data.AppInfo.EMPTY_ARRAY; 21 22 import android.content.ComponentName; 23 import android.content.Context; 24 import android.content.Intent; 25 import android.content.pm.LauncherActivityInfo; 26 import android.content.pm.LauncherApps; 27 import android.os.LocaleList; 28 import android.os.UserHandle; 29 import android.util.Log; 30 31 import androidx.annotation.NonNull; 32 import androidx.annotation.Nullable; 33 34 import com.android.launcher3.AppFilter; 35 import com.android.launcher3.compat.AlphabeticIndexCompat; 36 import com.android.launcher3.icons.IconCache; 37 import com.android.launcher3.model.BgDataModel.Callbacks; 38 import com.android.launcher3.model.data.AppInfo; 39 import com.android.launcher3.pm.PackageInstallInfo; 40 import com.android.launcher3.util.FlagOp; 41 import com.android.launcher3.util.ItemInfoMatcher; 42 import com.android.launcher3.util.PackageManagerHelper; 43 import com.android.launcher3.util.SafeCloseable; 44 45 import java.util.ArrayList; 46 import java.util.Arrays; 47 import java.util.HashSet; 48 import java.util.List; 49 import java.util.function.Consumer; 50 51 52 /** 53 * Stores the list of all applications for the all apps view. 54 */ 55 public class AllAppsList { 56 57 private static final String TAG = "AllAppsList"; 58 private static final Consumer<AppInfo> NO_OP_CONSUMER = a -> { }; 59 60 61 public static final int DEFAULT_APPLICATIONS_NUMBER = 42; 62 63 /** The list off all apps. */ 64 public final ArrayList<AppInfo> data = new ArrayList<>(DEFAULT_APPLICATIONS_NUMBER); 65 66 private IconCache mIconCache; 67 private AppFilter mAppFilter; 68 69 private boolean mDataChanged = false; 70 private Consumer<AppInfo> mRemoveListener = NO_OP_CONSUMER; 71 72 private AlphabeticIndexCompat mIndex; 73 74 /** 75 * @see Callbacks#FLAG_HAS_SHORTCUT_PERMISSION 76 * @see Callbacks#FLAG_QUIET_MODE_ENABLED 77 * @see Callbacks#FLAG_QUIET_MODE_CHANGE_PERMISSION 78 */ 79 private int mFlags; 80 81 /** 82 * Boring constructor. 83 */ AllAppsList(IconCache iconCache, AppFilter appFilter)84 public AllAppsList(IconCache iconCache, AppFilter appFilter) { 85 mIconCache = iconCache; 86 mAppFilter = appFilter; 87 mIndex = new AlphabeticIndexCompat(LocaleList.getDefault()); 88 } 89 90 /** 91 * Returns true if there have been any changes since last call. 92 */ getAndResetChangeFlag()93 public boolean getAndResetChangeFlag() { 94 boolean result = mDataChanged; 95 mDataChanged = false; 96 return result; 97 } 98 99 /** 100 * Helper to checking {@link Callbacks#FLAG_HAS_SHORTCUT_PERMISSION} 101 */ hasShortcutHostPermission()102 public boolean hasShortcutHostPermission() { 103 return (mFlags & Callbacks.FLAG_HAS_SHORTCUT_PERMISSION) != 0; 104 } 105 106 /** 107 * Sets or clears the provided flag 108 */ setFlags(int flagMask, boolean enabled)109 public void setFlags(int flagMask, boolean enabled) { 110 if (enabled) { 111 mFlags |= flagMask; 112 } else { 113 mFlags &= ~flagMask; 114 } 115 mDataChanged = true; 116 } 117 118 /** 119 * Returns the model flags 120 */ getFlags()121 public int getFlags() { 122 return mFlags; 123 } 124 125 126 /** 127 * Add the supplied ApplicationInfo objects to the list, and enqueue it into the 128 * list to broadcast when notify() is called. 129 * 130 * If the app is already in the list, doesn't add it. 131 */ add(AppInfo info, LauncherActivityInfo activityInfo)132 public void add(AppInfo info, LauncherActivityInfo activityInfo) { 133 if (!mAppFilter.shouldShowApp(info.componentName)) { 134 return; 135 } 136 if (findAppInfo(info.componentName, info.user) != null) { 137 return; 138 } 139 mIconCache.getTitleAndIcon(info, activityInfo, false /* useLowResIcon */); 140 info.sectionName = mIndex.computeSectionName(info.title); 141 142 data.add(info); 143 mDataChanged = true; 144 } 145 addPromiseApp(Context context, PackageInstallInfo installInfo)146 public void addPromiseApp(Context context, PackageInstallInfo installInfo) { 147 // only if not yet installed 148 if (!new PackageManagerHelper(context) 149 .isAppInstalled(installInfo.packageName, installInfo.user)) { 150 AppInfo info = new AppInfo(installInfo); 151 mIconCache.getTitleAndIcon(info, info.usingLowResIcon()); 152 info.sectionName = mIndex.computeSectionName(info.title); 153 154 data.add(info); 155 mDataChanged = true; 156 } 157 } 158 159 /** Updates the given PackageInstallInfo's associated AppInfo's installation info. */ updatePromiseInstallInfo(PackageInstallInfo installInfo)160 public List<AppInfo> updatePromiseInstallInfo(PackageInstallInfo installInfo) { 161 List<AppInfo> updatedAppInfos = new ArrayList<>(); 162 UserHandle user = installInfo.user; 163 for (int i = data.size() - 1; i >= 0; i--) { 164 final AppInfo appInfo = data.get(i); 165 final ComponentName tgtComp = appInfo.getTargetComponent(); 166 if (tgtComp != null && tgtComp.getPackageName().equals(installInfo.packageName) 167 && appInfo.user.equals(user)) { 168 if (installInfo.state == PackageInstallInfo.STATUS_INSTALLED_DOWNLOADING 169 || installInfo.state == PackageInstallInfo.STATUS_INSTALLING) { 170 if (appInfo.isAppStartable() 171 && installInfo.state == PackageInstallInfo.STATUS_INSTALLING) { 172 continue; 173 } 174 appInfo.setProgressLevel(installInfo); 175 176 updatedAppInfos.add(appInfo); 177 } else if (installInfo.state == PackageInstallInfo.STATUS_FAILED 178 && !appInfo.isAppStartable()) { 179 removeApp(i); 180 } 181 } 182 } 183 return updatedAppInfos; 184 } 185 removeApp(int index)186 private void removeApp(int index) { 187 AppInfo removed = data.remove(index); 188 if (removed != null) { 189 mDataChanged = true; 190 mRemoveListener.accept(removed); 191 } 192 } 193 clear()194 public void clear() { 195 data.clear(); 196 mDataChanged = false; 197 // Reset the index as locales might have changed 198 mIndex = new AlphabeticIndexCompat(LocaleList.getDefault()); 199 } 200 201 /** 202 * Add the icons for the supplied apk called packageName. 203 */ addPackage( Context context, String packageName, UserHandle user)204 public List<LauncherActivityInfo> addPackage( 205 Context context, String packageName, UserHandle user) { 206 List<LauncherActivityInfo> activities = context.getSystemService(LauncherApps.class) 207 .getActivityList(packageName, user); 208 209 for (LauncherActivityInfo info : activities) { 210 add(new AppInfo(context, info, user), info); 211 } 212 213 return activities; 214 } 215 216 /** 217 * Remove the apps for the given apk identified by packageName. 218 */ removePackage(String packageName, UserHandle user)219 public void removePackage(String packageName, UserHandle user) { 220 final List<AppInfo> data = this.data; 221 for (int i = data.size() - 1; i >= 0; i--) { 222 AppInfo info = data.get(i); 223 if (info.user.equals(user) && packageName.equals(info.componentName.getPackageName())) { 224 removeApp(i); 225 } 226 } 227 } 228 229 /** 230 * Updates the disabled flags of apps matching {@param matcher} based on {@param op}. 231 */ updateDisabledFlags(ItemInfoMatcher matcher, FlagOp op)232 public void updateDisabledFlags(ItemInfoMatcher matcher, FlagOp op) { 233 final List<AppInfo> data = this.data; 234 for (int i = data.size() - 1; i >= 0; i--) { 235 AppInfo info = data.get(i); 236 if (matcher.matches(info, info.componentName)) { 237 info.runtimeStatusFlags = op.apply(info.runtimeStatusFlags); 238 mDataChanged = true; 239 } 240 } 241 } 242 updateIconsAndLabels(HashSet<String> packages, UserHandle user)243 public void updateIconsAndLabels(HashSet<String> packages, UserHandle user) { 244 for (AppInfo info : data) { 245 if (info.user.equals(user) && packages.contains(info.componentName.getPackageName())) { 246 mIconCache.updateTitleAndIcon(info); 247 info.sectionName = mIndex.computeSectionName(info.title); 248 mDataChanged = true; 249 } 250 } 251 } 252 253 /** 254 * Add and remove icons for this package which has been updated. 255 */ updatePackage( Context context, String packageName, UserHandle user)256 public List<LauncherActivityInfo> updatePackage( 257 Context context, String packageName, UserHandle user) { 258 final List<LauncherActivityInfo> matches = context.getSystemService(LauncherApps.class) 259 .getActivityList(packageName, user); 260 if (matches.size() > 0) { 261 // Find disabled/removed activities and remove them from data and add them 262 // to the removed list. 263 for (int i = data.size() - 1; i >= 0; i--) { 264 final AppInfo applicationInfo = data.get(i); 265 if (user.equals(applicationInfo.user) 266 && packageName.equals(applicationInfo.componentName.getPackageName())) { 267 if (!findActivity(matches, applicationInfo.componentName)) { 268 Log.w(TAG, "Changing shortcut target due to app component name change."); 269 removeApp(i); 270 } 271 } 272 } 273 274 // Find enabled activities and add them to the adapter 275 // Also updates existing activities with new labels/icons 276 for (final LauncherActivityInfo info : matches) { 277 AppInfo applicationInfo = findAppInfo(info.getComponentName(), user); 278 if (applicationInfo == null) { 279 add(new AppInfo(context, info, user), info); 280 } else { 281 Intent launchIntent = AppInfo.makeLaunchIntent(info); 282 283 mIconCache.getTitleAndIcon(applicationInfo, info, false /* useLowResIcon */); 284 applicationInfo.sectionName = mIndex.computeSectionName(applicationInfo.title); 285 applicationInfo.setProgressLevel( 286 PackageManagerHelper.getLoadingProgress(info), 287 PackageInstallInfo.STATUS_INSTALLED_DOWNLOADING); 288 applicationInfo.intent = launchIntent; 289 290 mDataChanged = true; 291 } 292 } 293 } else { 294 // Remove all data for this package. 295 for (int i = data.size() - 1; i >= 0; i--) { 296 final AppInfo applicationInfo = data.get(i); 297 if (user.equals(applicationInfo.user) 298 && packageName.equals(applicationInfo.componentName.getPackageName())) { 299 mIconCache.remove(applicationInfo.componentName, user); 300 removeApp(i); 301 } 302 } 303 } 304 305 return matches; 306 } 307 308 /** 309 * Returns whether <em>apps</em> contains <em>component</em>. 310 */ findActivity(List<LauncherActivityInfo> apps, ComponentName component)311 private static boolean findActivity(List<LauncherActivityInfo> apps, 312 ComponentName component) { 313 for (LauncherActivityInfo info : apps) { 314 if (info.getComponentName().equals(component)) { 315 return true; 316 } 317 } 318 return false; 319 } 320 321 /** 322 * Find an AppInfo object for the given componentName 323 * 324 * @return the corresponding AppInfo or null 325 */ findAppInfo(@onNull ComponentName componentName, @NonNull UserHandle user)326 public @Nullable AppInfo findAppInfo(@NonNull ComponentName componentName, 327 @NonNull UserHandle user) { 328 for (AppInfo info: data) { 329 if (componentName.equals(info.componentName) && user.equals(info.user)) { 330 return info; 331 } 332 } 333 return null; 334 } 335 copyData()336 public AppInfo[] copyData() { 337 AppInfo[] result = data.toArray(EMPTY_ARRAY); 338 Arrays.sort(result, COMPONENT_KEY_COMPARATOR); 339 return result; 340 } 341 trackRemoves(Consumer<AppInfo> removeListener)342 public SafeCloseable trackRemoves(Consumer<AppInfo> removeListener) { 343 mRemoveListener = removeListener; 344 345 return () -> mRemoveListener = NO_OP_CONSUMER; 346 } 347 } 348