2009-08-17 11:03:03 -04:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2008 The Android Open Source Project
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
2019-07-17 20:35:56 -07:00
|
|
|
package com.android.launcher3.model;
|
|
|
|
|
|
2020-04-06 15:11:17 -07:00
|
|
|
import static com.android.launcher3.model.data.AppInfo.COMPONENT_KEY_COMPARATOR;
|
|
|
|
|
import static com.android.launcher3.model.data.AppInfo.EMPTY_ARRAY;
|
2009-08-17 11:03:03 -04:00
|
|
|
|
|
|
|
|
import android.content.ComponentName;
|
|
|
|
|
import android.content.Context;
|
2017-03-15 11:56:47 -07:00
|
|
|
import android.content.pm.ApplicationInfo;
|
2017-01-05 15:22:41 -08:00
|
|
|
import android.content.pm.LauncherActivityInfo;
|
2019-10-02 16:13:34 -07:00
|
|
|
import android.content.pm.LauncherApps;
|
2019-07-31 12:51:30 -07:00
|
|
|
import android.os.LocaleList;
|
2017-03-15 11:56:47 -07:00
|
|
|
import android.os.Process;
|
2016-12-15 15:53:17 -08:00
|
|
|
import android.os.UserHandle;
|
2017-05-31 11:21:13 -07:00
|
|
|
import android.util.Log;
|
2009-08-17 11:03:03 -04:00
|
|
|
|
2019-09-20 12:51:37 -07:00
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
|
import androidx.annotation.Nullable;
|
|
|
|
|
|
2019-07-17 20:35:56 -07:00
|
|
|
import com.android.launcher3.AppFilter;
|
2019-07-31 12:51:30 -07:00
|
|
|
import com.android.launcher3.compat.AlphabeticIndexCompat;
|
2018-09-21 14:41:05 -07:00
|
|
|
import com.android.launcher3.icons.IconCache;
|
2020-04-06 15:11:17 -07:00
|
|
|
import com.android.launcher3.model.data.AppInfo;
|
|
|
|
|
import com.android.launcher3.model.data.PromiseAppInfo;
|
2019-09-20 12:51:37 -07:00
|
|
|
import com.android.launcher3.pm.PackageInstallInfo;
|
2016-03-15 09:16:30 -07:00
|
|
|
import com.android.launcher3.util.FlagOp;
|
2016-09-01 15:17:46 -07:00
|
|
|
import com.android.launcher3.util.ItemInfoMatcher;
|
2019-10-02 16:13:34 -07:00
|
|
|
import com.android.launcher3.util.PackageManagerHelper;
|
2019-07-17 20:35:56 -07:00
|
|
|
import com.android.launcher3.util.SafeCloseable;
|
2014-04-30 03:02:21 +01:00
|
|
|
|
2013-12-13 16:07:45 +01:00
|
|
|
import java.util.ArrayList;
|
2019-07-17 20:35:56 -07:00
|
|
|
import java.util.Arrays;
|
2015-05-06 16:53:21 -07:00
|
|
|
import java.util.HashSet;
|
2013-12-13 16:07:45 +01:00
|
|
|
import java.util.List;
|
2019-07-17 20:35:56 -07:00
|
|
|
import java.util.function.Consumer;
|
2013-12-13 16:07:45 +01:00
|
|
|
|
2009-08-17 11:03:03 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Stores the list of all applications for the all apps view.
|
|
|
|
|
*/
|
2016-10-10 10:41:41 -07:00
|
|
|
public class AllAppsList {
|
2019-07-17 20:35:56 -07:00
|
|
|
|
2017-05-31 11:21:13 -07:00
|
|
|
private static final String TAG = "AllAppsList";
|
2019-07-17 20:35:56 -07:00
|
|
|
private static final Consumer<AppInfo> NO_OP_CONSUMER = a -> { };
|
|
|
|
|
|
2017-05-31 11:21:13 -07:00
|
|
|
|
2009-08-17 11:03:03 -04:00
|
|
|
public static final int DEFAULT_APPLICATIONS_NUMBER = 42;
|
2014-02-10 12:16:54 -05:00
|
|
|
|
2009-08-17 11:03:03 -04:00
|
|
|
/** The list off all apps. */
|
2017-05-19 12:21:50 -07:00
|
|
|
public final ArrayList<AppInfo> data = new ArrayList<>(DEFAULT_APPLICATIONS_NUMBER);
|
2009-08-17 11:03:03 -04:00
|
|
|
|
2010-02-08 13:44:00 -08:00
|
|
|
private IconCache mIconCache;
|
2013-10-03 22:31:03 +01:00
|
|
|
private AppFilter mAppFilter;
|
|
|
|
|
|
2019-07-17 20:35:56 -07:00
|
|
|
private boolean mDataChanged = false;
|
|
|
|
|
private Consumer<AppInfo> mRemoveListener = NO_OP_CONSUMER;
|
|
|
|
|
|
2019-07-31 12:51:30 -07:00
|
|
|
private AlphabeticIndexCompat mIndex;
|
|
|
|
|
|
2009-08-17 11:03:03 -04:00
|
|
|
/**
|
|
|
|
|
* Boring constructor.
|
|
|
|
|
*/
|
2013-10-03 22:31:03 +01:00
|
|
|
public AllAppsList(IconCache iconCache, AppFilter appFilter) {
|
2010-02-08 13:44:00 -08:00
|
|
|
mIconCache = iconCache;
|
2013-10-03 22:31:03 +01:00
|
|
|
mAppFilter = appFilter;
|
2019-07-31 12:51:30 -07:00
|
|
|
mIndex = new AlphabeticIndexCompat(LocaleList.getDefault());
|
2009-08-17 11:03:03 -04:00
|
|
|
}
|
|
|
|
|
|
2019-07-17 20:35:56 -07:00
|
|
|
/**
|
|
|
|
|
* Returns true if there have been any changes since last call.
|
|
|
|
|
*/
|
|
|
|
|
public boolean getAndResetChangeFlag() {
|
|
|
|
|
boolean result = mDataChanged;
|
|
|
|
|
mDataChanged = false;
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2009-08-17 11:03:03 -04:00
|
|
|
/**
|
|
|
|
|
* Add the supplied ApplicationInfo objects to the list, and enqueue it into the
|
|
|
|
|
* list to broadcast when notify() is called.
|
2010-04-20 15:43:37 -04:00
|
|
|
*
|
|
|
|
|
* If the app is already in the list, doesn't add it.
|
2009-08-17 11:03:03 -04:00
|
|
|
*/
|
2017-01-05 15:22:41 -08:00
|
|
|
public void add(AppInfo info, LauncherActivityInfo activityInfo) {
|
2016-12-02 19:29:43 +05:30
|
|
|
if (!mAppFilter.shouldShowApp(info.componentName)) {
|
2013-10-03 22:31:03 +01:00
|
|
|
return;
|
|
|
|
|
}
|
2017-03-15 11:56:47 -07:00
|
|
|
if (findAppInfo(info.componentName, info.user) != null) {
|
2010-04-20 15:43:37 -04:00
|
|
|
return;
|
|
|
|
|
}
|
2017-01-06 16:32:57 -08:00
|
|
|
mIconCache.getTitleAndIcon(info, activityInfo, true /* useLowResIcon */);
|
2019-07-31 12:51:30 -07:00
|
|
|
info.sectionName = mIndex.computeSectionName(info.title);
|
2017-01-06 16:32:57 -08:00
|
|
|
|
2010-04-15 16:28:40 -04:00
|
|
|
data.add(info);
|
2019-07-17 20:35:56 -07:00
|
|
|
mDataChanged = true;
|
2009-08-17 11:03:03 -04:00
|
|
|
}
|
2013-10-03 22:31:03 +01:00
|
|
|
|
2019-09-20 12:51:37 -07:00
|
|
|
public void addPromiseApp(Context context, PackageInstallInfo installInfo) {
|
2019-10-02 16:13:34 -07:00
|
|
|
ApplicationInfo applicationInfo = new PackageManagerHelper(context)
|
|
|
|
|
.getApplicationInfo(installInfo.packageName, installInfo.user, 0);
|
2017-03-15 11:56:47 -07:00
|
|
|
// only if not yet installed
|
|
|
|
|
if (applicationInfo == null) {
|
|
|
|
|
PromiseAppInfo info = new PromiseAppInfo(installInfo);
|
2018-08-20 15:01:03 -07:00
|
|
|
mIconCache.getTitleAndIcon(info, info.usingLowResIcon());
|
2019-07-31 12:51:30 -07:00
|
|
|
info.sectionName = mIndex.computeSectionName(info.title);
|
|
|
|
|
|
2017-03-15 11:56:47 -07:00
|
|
|
data.add(info);
|
2019-07-17 20:35:56 -07:00
|
|
|
mDataChanged = true;
|
2017-03-15 11:56:47 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-17 20:35:56 -07:00
|
|
|
public PromiseAppInfo updatePromiseInstallInfo(PackageInstallInfo installInfo) {
|
|
|
|
|
UserHandle user = Process.myUserHandle();
|
|
|
|
|
for (int i=0; i < data.size(); i++) {
|
|
|
|
|
final AppInfo appInfo = data.get(i);
|
|
|
|
|
final ComponentName tgtComp = appInfo.getTargetComponent();
|
|
|
|
|
if (tgtComp != null && tgtComp.getPackageName().equals(installInfo.packageName)
|
|
|
|
|
&& appInfo.user.equals(user)
|
|
|
|
|
&& appInfo instanceof PromiseAppInfo) {
|
|
|
|
|
final PromiseAppInfo promiseAppInfo = (PromiseAppInfo) appInfo;
|
2019-09-20 12:51:37 -07:00
|
|
|
if (installInfo.state == PackageInstallInfo.STATUS_INSTALLING) {
|
2019-07-17 20:35:56 -07:00
|
|
|
promiseAppInfo.level = installInfo.progress;
|
|
|
|
|
return promiseAppInfo;
|
2019-09-20 12:51:37 -07:00
|
|
|
} else if (installInfo.state == PackageInstallInfo.STATUS_FAILED) {
|
2019-07-17 20:35:56 -07:00
|
|
|
removeApp(i);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
2009-08-17 11:03:03 -04:00
|
|
|
}
|
|
|
|
|
|
2019-07-17 20:35:56 -07:00
|
|
|
private void removeApp(int index) {
|
|
|
|
|
AppInfo removed = data.remove(index);
|
|
|
|
|
if (removed != null) {
|
|
|
|
|
mDataChanged = true;
|
|
|
|
|
mRemoveListener.accept(removed);
|
|
|
|
|
}
|
2009-08-17 11:03:03 -04:00
|
|
|
}
|
|
|
|
|
|
2019-07-17 20:35:56 -07:00
|
|
|
public void clear() {
|
|
|
|
|
data.clear();
|
|
|
|
|
mDataChanged = false;
|
2019-07-31 12:51:30 -07:00
|
|
|
// Reset the index as locales might have changed
|
|
|
|
|
mIndex = new AlphabeticIndexCompat(LocaleList.getDefault());
|
2009-08-17 11:03:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Add the icons for the supplied apk called packageName.
|
|
|
|
|
*/
|
2016-12-15 15:53:17 -08:00
|
|
|
public void addPackage(Context context, String packageName, UserHandle user) {
|
2019-10-02 16:13:34 -07:00
|
|
|
for (LauncherActivityInfo info : context.getSystemService(LauncherApps.class)
|
|
|
|
|
.getActivityList(packageName, user)) {
|
2017-01-06 16:32:57 -08:00
|
|
|
add(new AppInfo(context, info, user), info);
|
2009-08-17 11:03:03 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Remove the apps for the given apk identified by packageName.
|
|
|
|
|
*/
|
2016-12-15 15:53:17 -08:00
|
|
|
public void removePackage(String packageName, UserHandle user) {
|
2013-09-04 00:45:37 +02:00
|
|
|
final List<AppInfo> data = this.data;
|
2010-01-12 17:24:58 -08:00
|
|
|
for (int i = data.size() - 1; i >= 0; i--) {
|
2013-09-04 00:45:37 +02:00
|
|
|
AppInfo info = data.get(i);
|
2016-10-10 10:41:41 -07:00
|
|
|
if (info.user.equals(user) && packageName.equals(info.componentName.getPackageName())) {
|
2019-07-17 20:35:56 -07:00
|
|
|
removeApp(i);
|
2009-08-17 11:03:03 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-02-08 13:44:00 -08:00
|
|
|
|
2016-01-21 19:50:02 +00:00
|
|
|
/**
|
2016-09-01 15:17:46 -07:00
|
|
|
* Updates the disabled flags of apps matching {@param matcher} based on {@param op}.
|
2016-01-21 19:50:02 +00:00
|
|
|
*/
|
2016-09-01 15:17:46 -07:00
|
|
|
public void updateDisabledFlags(ItemInfoMatcher matcher, FlagOp op) {
|
2016-01-21 19:50:02 +00:00
|
|
|
final List<AppInfo> data = this.data;
|
|
|
|
|
for (int i = data.size() - 1; i >= 0; i--) {
|
|
|
|
|
AppInfo info = data.get(i);
|
2016-10-10 10:41:41 -07:00
|
|
|
if (matcher.matches(info, info.componentName)) {
|
2017-10-30 13:52:20 -07:00
|
|
|
info.runtimeStatusFlags = op.apply(info.runtimeStatusFlags);
|
2019-07-17 20:35:56 -07:00
|
|
|
mDataChanged = true;
|
2016-01-21 19:50:02 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-17 20:35:56 -07:00
|
|
|
public void updateIconsAndLabels(HashSet<String> packages, UserHandle user) {
|
2015-05-06 16:53:21 -07:00
|
|
|
for (AppInfo info : data) {
|
|
|
|
|
if (info.user.equals(user) && packages.contains(info.componentName.getPackageName())) {
|
|
|
|
|
mIconCache.updateTitleAndIcon(info);
|
2019-07-31 12:51:30 -07:00
|
|
|
info.sectionName = mIndex.computeSectionName(info.title);
|
2019-07-17 20:35:56 -07:00
|
|
|
mDataChanged = true;
|
2015-05-06 16:53:21 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-08-17 11:03:03 -04:00
|
|
|
/**
|
|
|
|
|
* Add and remove icons for this package which has been updated.
|
|
|
|
|
*/
|
2016-12-15 15:53:17 -08:00
|
|
|
public void updatePackage(Context context, String packageName, UserHandle user) {
|
2019-10-02 16:13:34 -07:00
|
|
|
final List<LauncherActivityInfo> matches = context.getSystemService(LauncherApps.class)
|
|
|
|
|
.getActivityList(packageName, user);
|
2009-08-17 11:03:03 -04:00
|
|
|
if (matches.size() > 0) {
|
|
|
|
|
// Find disabled/removed activities and remove them from data and add them
|
|
|
|
|
// to the removed list.
|
2010-01-12 17:24:58 -08:00
|
|
|
for (int i = data.size() - 1; i >= 0; i--) {
|
2013-09-04 00:45:37 +02:00
|
|
|
final AppInfo applicationInfo = data.get(i);
|
2014-04-30 03:02:21 +01:00
|
|
|
if (user.equals(applicationInfo.user)
|
2016-09-09 15:47:55 -07:00
|
|
|
&& packageName.equals(applicationInfo.componentName.getPackageName())) {
|
|
|
|
|
if (!findActivity(matches, applicationInfo.componentName)) {
|
2018-09-05 12:48:13 -07:00
|
|
|
Log.w(TAG, "Changing shortcut target due to app component name change.");
|
2019-07-17 20:35:56 -07:00
|
|
|
removeApp(i);
|
2009-08-17 11:03:03 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Find enabled activities and add them to the adapter
|
|
|
|
|
// Also updates existing activities with new labels/icons
|
2017-01-05 15:22:41 -08:00
|
|
|
for (final LauncherActivityInfo info : matches) {
|
2017-03-15 11:56:47 -07:00
|
|
|
AppInfo applicationInfo = findAppInfo(info.getComponentName(), user);
|
2009-08-17 11:03:03 -04:00
|
|
|
if (applicationInfo == null) {
|
2017-01-06 16:32:57 -08:00
|
|
|
add(new AppInfo(context, info, user), info);
|
2009-08-17 11:03:03 -04:00
|
|
|
} else {
|
2015-03-11 16:56:52 -07:00
|
|
|
mIconCache.getTitleAndIcon(applicationInfo, info, true /* useLowResIcon */);
|
2019-07-31 12:51:30 -07:00
|
|
|
applicationInfo.sectionName = mIndex.computeSectionName(applicationInfo.title);
|
|
|
|
|
|
2019-07-17 20:35:56 -07:00
|
|
|
mDataChanged = true;
|
2009-08-17 11:03:03 -04:00
|
|
|
}
|
|
|
|
|
}
|
2010-07-24 16:48:01 -07:00
|
|
|
} else {
|
|
|
|
|
// Remove all data for this package.
|
|
|
|
|
for (int i = data.size() - 1; i >= 0; i--) {
|
2013-09-04 00:45:37 +02:00
|
|
|
final AppInfo applicationInfo = data.get(i);
|
2014-04-30 03:02:21 +01:00
|
|
|
if (user.equals(applicationInfo.user)
|
2016-09-09 15:47:55 -07:00
|
|
|
&& packageName.equals(applicationInfo.componentName.getPackageName())) {
|
|
|
|
|
mIconCache.remove(applicationInfo.componentName, user);
|
2019-07-17 20:35:56 -07:00
|
|
|
removeApp(i);
|
2010-07-24 16:48:01 -07:00
|
|
|
}
|
|
|
|
|
}
|
2009-08-17 11:03:03 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns whether <em>apps</em> contains <em>component</em>.
|
|
|
|
|
*/
|
2017-01-05 15:22:41 -08:00
|
|
|
private static boolean findActivity(List<LauncherActivityInfo> apps,
|
2014-04-30 03:02:21 +01:00
|
|
|
ComponentName component) {
|
2017-01-05 15:22:41 -08:00
|
|
|
for (LauncherActivityInfo info : apps) {
|
2014-04-30 03:02:21 +01:00
|
|
|
if (info.getComponentName().equals(component)) {
|
2009-08-17 11:03:03 -04:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2010-04-20 15:43:37 -04:00
|
|
|
/**
|
2017-03-15 11:56:47 -07:00
|
|
|
* Find an AppInfo object for the given componentName
|
|
|
|
|
*
|
|
|
|
|
* @return the corresponding AppInfo or null
|
2009-08-17 11:03:03 -04:00
|
|
|
*/
|
2017-03-15 11:56:47 -07:00
|
|
|
private @Nullable AppInfo findAppInfo(@NonNull ComponentName componentName,
|
|
|
|
|
@NonNull UserHandle user) {
|
2013-09-04 00:45:37 +02:00
|
|
|
for (AppInfo info: data) {
|
2017-03-15 11:56:47 -07:00
|
|
|
if (componentName.equals(info.componentName) && user.equals(info.user)) {
|
2009-08-17 11:03:03 -04:00
|
|
|
return info;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2019-07-17 20:35:56 -07:00
|
|
|
|
|
|
|
|
public AppInfo[] copyData() {
|
|
|
|
|
AppInfo[] result = data.toArray(EMPTY_ARRAY);
|
|
|
|
|
Arrays.sort(result, COMPONENT_KEY_COMPARATOR);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public SafeCloseable trackRemoves(Consumer<AppInfo> removeListener) {
|
|
|
|
|
mRemoveListener = removeListener;
|
|
|
|
|
|
|
|
|
|
return () -> mRemoveListener = NO_OP_CONSUMER;
|
|
|
|
|
}
|
2009-08-17 11:03:03 -04:00
|
|
|
}
|