2010-02-08 13:44:00 -08: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.
|
|
|
|
|
*/
|
|
|
|
|
|
2018-09-21 14:41:05 -07:00
|
|
|
package com.android.launcher3.icons;
|
2010-02-08 13:44:00 -08:00
|
|
|
|
2012-02-13 14:27:42 -08:00
|
|
|
import android.content.Context;
|
2010-02-08 13:44:00 -08:00
|
|
|
import android.content.Intent;
|
2018-11-06 10:28:37 -08:00
|
|
|
import android.content.pm.ApplicationInfo;
|
2017-01-05 15:22:41 -08:00
|
|
|
import android.content.pm.LauncherActivityInfo;
|
2015-02-18 16:46:50 -08:00
|
|
|
import android.content.pm.PackageInfo;
|
2010-02-08 13:44:00 -08:00
|
|
|
import android.content.pm.PackageManager;
|
2014-08-11 17:05:23 -07:00
|
|
|
import android.content.pm.PackageManager.NameNotFoundException;
|
2018-11-05 17:05:35 -08:00
|
|
|
import android.graphics.drawable.Drawable;
|
2015-03-11 16:56:52 -07:00
|
|
|
import android.os.Handler;
|
2016-12-15 15:53:17 -08:00
|
|
|
import android.os.Process;
|
|
|
|
|
import android.os.UserHandle;
|
2014-02-10 12:16:54 -05:00
|
|
|
import android.util.Log;
|
2017-11-08 16:52:34 -08:00
|
|
|
|
2018-09-21 14:41:05 -07:00
|
|
|
import com.android.launcher3.AppInfo;
|
|
|
|
|
import com.android.launcher3.InvariantDeviceProfile;
|
|
|
|
|
import com.android.launcher3.ItemInfoWithIcon;
|
2018-11-06 10:28:37 -08:00
|
|
|
import com.android.launcher3.LauncherFiles;
|
2018-09-21 14:41:05 -07:00
|
|
|
import com.android.launcher3.LauncherModel;
|
2018-11-05 17:05:35 -08:00
|
|
|
import com.android.launcher3.MainThreadExecutor;
|
2018-09-21 14:41:05 -07:00
|
|
|
import com.android.launcher3.ShortcutInfo;
|
|
|
|
|
import com.android.launcher3.Utilities;
|
2018-11-05 17:05:35 -08:00
|
|
|
import com.android.launcher3.compat.LauncherAppsCompat;
|
2018-11-06 10:28:37 -08:00
|
|
|
import com.android.launcher3.compat.UserManagerCompat;
|
2018-11-05 17:05:35 -08:00
|
|
|
import com.android.launcher3.icons.ComponentWithLabel.ComponentCachingLogic;
|
2015-05-21 13:04:53 -07:00
|
|
|
import com.android.launcher3.model.PackageItemInfo;
|
2018-11-06 10:28:37 -08:00
|
|
|
import com.android.launcher3.util.InstantAppResolver;
|
2017-01-06 16:32:57 -08:00
|
|
|
import com.android.launcher3.util.Preconditions;
|
2016-12-15 17:40:07 -08:00
|
|
|
import com.android.launcher3.util.Provider;
|
2010-02-08 13:44:00 -08:00
|
|
|
|
2018-08-14 15:21:45 -07:00
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
|
|
2010-02-08 13:44:00 -08:00
|
|
|
/**
|
|
|
|
|
* Cache of application icons. Icons can be made from any thread.
|
|
|
|
|
*/
|
2018-09-24 10:51:52 -07:00
|
|
|
public class IconCache extends BaseIconCache {
|
2014-08-11 17:05:23 -07:00
|
|
|
|
2010-02-08 13:44:00 -08:00
|
|
|
private static final String TAG = "Launcher.IconCache";
|
|
|
|
|
|
2018-11-05 17:05:35 -08:00
|
|
|
private final MainThreadExecutor mMainThreadExecutor = new MainThreadExecutor();
|
|
|
|
|
|
|
|
|
|
private final CachingLogic<ComponentWithLabel> mComponentWithLabelCachingLogic;
|
|
|
|
|
private final CachingLogic<LauncherActivityInfo> mLauncherActivityInfoCachingLogic;
|
|
|
|
|
|
|
|
|
|
private final LauncherAppsCompat mLauncherApps;
|
2018-11-06 10:28:37 -08:00
|
|
|
private final UserManagerCompat mUserManager;
|
|
|
|
|
private final InstantAppResolver mInstantAppResolver;
|
2018-11-05 17:05:35 -08:00
|
|
|
|
2018-05-11 09:18:50 -07:00
|
|
|
private int mPendingIconRequestCount = 0;
|
|
|
|
|
|
2015-05-06 11:42:25 -07:00
|
|
|
public IconCache(Context context, InvariantDeviceProfile inv) {
|
2018-11-06 10:28:37 -08:00
|
|
|
super(context, LauncherFiles.APP_ICONS_DB, inv.fillResIconDpi, inv.iconBitmapSize);
|
2018-11-05 17:05:35 -08:00
|
|
|
mComponentWithLabelCachingLogic = new ComponentCachingLogic(context);
|
|
|
|
|
mLauncherActivityInfoCachingLogic = new LauncherActivtiyCachingLogic(this);
|
|
|
|
|
mLauncherApps = LauncherAppsCompat.getInstance(mContext);
|
2018-11-06 10:28:37 -08:00
|
|
|
mUserManager = UserManagerCompat.getInstance(mContext);
|
|
|
|
|
mInstantAppResolver = InstantAppResolver.newInstance(mContext);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected long getSerialNumberForUser(UserHandle user) {
|
|
|
|
|
return mUserManager.getSerialNumberForUser(user);
|
|
|
|
|
}
|
2018-11-05 17:05:35 -08:00
|
|
|
|
2018-11-06 10:28:37 -08:00
|
|
|
@Override
|
|
|
|
|
protected boolean isInstantApp(ApplicationInfo info) {
|
|
|
|
|
return mInstantAppResolver.isInstantApp(info);
|
2014-02-10 12:16:54 -05:00
|
|
|
}
|
|
|
|
|
|
2015-02-18 16:46:50 -08:00
|
|
|
/**
|
|
|
|
|
* Updates the entries related to the given package in memory and persistent DB.
|
|
|
|
|
*/
|
2016-12-15 15:53:17 -08:00
|
|
|
public synchronized void updateIconsForPkg(String packageName, UserHandle user) {
|
2015-02-18 16:46:50 -08:00
|
|
|
removeIconsForPkg(packageName, user);
|
|
|
|
|
try {
|
|
|
|
|
PackageInfo info = mPackageManager.getPackageInfo(packageName,
|
|
|
|
|
PackageManager.GET_UNINSTALLED_PACKAGES);
|
|
|
|
|
long userSerial = mUserManager.getSerialNumberForUser(user);
|
2017-01-05 15:22:41 -08:00
|
|
|
for (LauncherActivityInfo app : mLauncherApps.getActivityList(packageName, user)) {
|
2018-11-05 17:05:35 -08:00
|
|
|
addIconToDBAndMemCache(app, mLauncherActivityInfoCachingLogic, info, userSerial,
|
2018-09-24 10:51:52 -07:00
|
|
|
false /*replace existing*/);
|
2015-02-18 16:46:50 -08:00
|
|
|
}
|
|
|
|
|
} catch (NameNotFoundException e) {
|
|
|
|
|
Log.d(TAG, "Package not found", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-02-08 13:44:00 -08:00
|
|
|
/**
|
2015-03-11 16:56:52 -07:00
|
|
|
* Fetches high-res icon for the provided ItemInfo and updates the caller when done.
|
|
|
|
|
* @return a request ID that can be used to cancel the request.
|
2010-02-08 13:44:00 -08:00
|
|
|
*/
|
2017-01-03 16:52:43 -08:00
|
|
|
public IconLoadRequest updateIconInBackground(final ItemInfoUpdateReceiver caller,
|
|
|
|
|
final ItemInfoWithIcon info) {
|
2018-05-11 09:18:50 -07:00
|
|
|
Preconditions.assertUIThread();
|
|
|
|
|
if (mPendingIconRequestCount <= 0) {
|
|
|
|
|
LauncherModel.setWorkerPriority(Process.THREAD_PRIORITY_FOREGROUND);
|
|
|
|
|
}
|
|
|
|
|
mPendingIconRequestCount ++;
|
2015-03-11 16:56:52 -07:00
|
|
|
|
2018-05-11 09:18:50 -07:00
|
|
|
IconLoadRequest request = new IconLoadRequest(mWorkerHandler, this::onIconRequestEnd) {
|
2015-03-11 16:56:52 -07:00
|
|
|
@Override
|
|
|
|
|
public void run() {
|
2016-12-15 17:40:07 -08:00
|
|
|
if (info instanceof AppInfo || info instanceof ShortcutInfo) {
|
2017-01-03 16:52:43 -08:00
|
|
|
getTitleAndIcon(info, false);
|
2015-05-12 11:32:39 -07:00
|
|
|
} else if (info instanceof PackageItemInfo) {
|
2016-12-15 17:40:07 -08:00
|
|
|
getTitleAndIconForApp((PackageItemInfo) info, false);
|
2015-03-11 16:56:52 -07:00
|
|
|
}
|
2018-05-11 09:18:50 -07:00
|
|
|
mMainThreadExecutor.execute(() -> {
|
|
|
|
|
caller.reapplyItemInfo(info);
|
|
|
|
|
onEnd();
|
2015-03-11 16:56:52 -07:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
2018-05-11 09:18:50 -07:00
|
|
|
Utilities.postAsyncCallback(mWorkerHandler, request);
|
|
|
|
|
return request;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void onIconRequestEnd() {
|
|
|
|
|
mPendingIconRequestCount --;
|
|
|
|
|
if (mPendingIconRequestCount <= 0) {
|
|
|
|
|
LauncherModel.setWorkerPriority(Process.THREAD_PRIORITY_BACKGROUND);
|
|
|
|
|
}
|
2015-03-11 16:56:52 -07:00
|
|
|
}
|
2010-02-08 13:44:00 -08:00
|
|
|
|
2015-05-06 16:53:21 -07:00
|
|
|
/**
|
|
|
|
|
* Updates {@param application} only if a valid entry is found.
|
|
|
|
|
*/
|
|
|
|
|
public synchronized void updateTitleAndIcon(AppInfo application) {
|
2016-12-15 17:40:07 -08:00
|
|
|
CacheEntry entry = cacheLocked(application.componentName,
|
2018-11-05 17:05:35 -08:00
|
|
|
application.user, Provider.of(null), mLauncherActivityInfoCachingLogic,
|
2018-09-26 12:00:30 -07:00
|
|
|
false, application.usingLowResIcon());
|
2015-05-06 16:53:21 -07:00
|
|
|
if (entry.icon != null && !isDefaultIcon(entry.icon, application.user)) {
|
2016-12-15 17:40:07 -08:00
|
|
|
applyCacheEntry(entry, application);
|
2015-05-06 16:53:21 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-11 16:56:52 -07:00
|
|
|
/**
|
2016-12-15 17:40:07 -08:00
|
|
|
* Fill in {@param info} with the icon and label for {@param activityInfo}
|
2015-03-11 16:56:52 -07:00
|
|
|
*/
|
2016-12-15 17:40:07 -08:00
|
|
|
public synchronized void getTitleAndIcon(ItemInfoWithIcon info,
|
2017-01-05 15:22:41 -08:00
|
|
|
LauncherActivityInfo activityInfo, boolean useLowResIcon) {
|
2016-12-15 17:40:07 -08:00
|
|
|
// If we already have activity info, no need to use package icon
|
|
|
|
|
getTitleAndIcon(info, Provider.of(activityInfo), false, useLowResIcon);
|
2010-02-08 13:44:00 -08:00
|
|
|
}
|
|
|
|
|
|
2014-08-29 17:20:55 -07:00
|
|
|
/**
|
2016-12-15 17:40:07 -08:00
|
|
|
* Fill in {@param info} with the icon and label. If the
|
2015-02-18 16:46:50 -08:00
|
|
|
* corresponding activity is not found, it reverts to the package icon.
|
2014-08-29 17:20:55 -07:00
|
|
|
*/
|
2016-12-15 17:40:07 -08:00
|
|
|
public synchronized void getTitleAndIcon(ItemInfoWithIcon info, boolean useLowResIcon) {
|
2014-10-16 14:07:29 -07:00
|
|
|
// null info means not installed, but if we have a component from the intent then
|
|
|
|
|
// we should still look in the cache for restored app icons.
|
2016-12-15 17:40:07 -08:00
|
|
|
if (info.getTargetComponent() == null) {
|
2018-10-18 15:05:45 -07:00
|
|
|
info.applyFrom(getDefaultIcon(info.user));
|
2016-12-15 17:40:07 -08:00
|
|
|
info.title = "";
|
|
|
|
|
info.contentDescription = "";
|
2014-10-16 14:07:29 -07:00
|
|
|
} else {
|
2018-07-09 16:47:01 -07:00
|
|
|
Intent intent = info.getIntent();
|
|
|
|
|
getTitleAndIcon(info, () -> mLauncherApps.resolveActivity(intent, info.user),
|
2016-12-15 17:40:07 -08:00
|
|
|
true, useLowResIcon);
|
2014-08-29 17:20:55 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-26 12:00:30 -07:00
|
|
|
public synchronized String getTitleNoCache(ComponentWithLabel info) {
|
|
|
|
|
CacheEntry entry = cacheLocked(info.getComponent(), info.getUser(), Provider.of(info),
|
2018-11-05 17:05:35 -08:00
|
|
|
mComponentWithLabelCachingLogic, false /* usePackageIcon */,
|
|
|
|
|
true /* useLowResIcon */, false /* addToMemCache */);
|
2018-09-26 12:00:30 -07:00
|
|
|
return Utilities.trim(entry.title);
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-18 16:46:50 -08:00
|
|
|
/**
|
|
|
|
|
* Fill in {@param shortcutInfo} with the icon and label for {@param info}
|
|
|
|
|
*/
|
2016-12-15 17:40:07 -08:00
|
|
|
private synchronized void getTitleAndIcon(
|
|
|
|
|
@NonNull ItemInfoWithIcon infoInOut,
|
2017-01-05 15:22:41 -08:00
|
|
|
@NonNull Provider<LauncherActivityInfo> activityInfoProvider,
|
2016-12-15 17:40:07 -08:00
|
|
|
boolean usePkgIcon, boolean useLowResIcon) {
|
2018-09-26 12:00:30 -07:00
|
|
|
CacheEntry entry = cacheLocked(infoInOut.getTargetComponent(), infoInOut.user,
|
2018-11-05 17:05:35 -08:00
|
|
|
activityInfoProvider, mLauncherActivityInfoCachingLogic, usePkgIcon, useLowResIcon);
|
2016-12-15 17:40:07 -08:00
|
|
|
applyCacheEntry(entry, infoInOut);
|
2015-02-18 16:46:50 -08:00
|
|
|
}
|
2014-08-29 17:20:55 -07:00
|
|
|
|
2018-11-05 17:05:35 -08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Fill in {@param infoInOut} with the corresponding icon and label.
|
|
|
|
|
*/
|
|
|
|
|
public synchronized void getTitleAndIconForApp(
|
|
|
|
|
PackageItemInfo infoInOut, boolean useLowResIcon) {
|
|
|
|
|
CacheEntry entry = getEntryForPackageLocked(
|
|
|
|
|
infoInOut.packageName, infoInOut.user, useLowResIcon);
|
|
|
|
|
applyCacheEntry(entry, infoInOut);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void applyCacheEntry(CacheEntry entry, ItemInfoWithIcon info) {
|
|
|
|
|
info.title = Utilities.trim(entry.title);
|
|
|
|
|
info.contentDescription = entry.contentDescription;
|
|
|
|
|
info.applyFrom((entry.icon == null) ? getDefaultIcon(info.user) : entry);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Drawable getFullResIcon(LauncherActivityInfo info) {
|
|
|
|
|
return getFullResIcon(info, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Drawable getFullResIcon(LauncherActivityInfo info, boolean flattenDrawable) {
|
|
|
|
|
return mIconProvider.getIcon(info, mIconDpi, flattenDrawable);
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-16 15:59:24 -07:00
|
|
|
public static abstract class IconLoadRequest extends HandlerRunnable {
|
2018-05-11 09:18:50 -07:00
|
|
|
IconLoadRequest(Handler handler, Runnable endRunnable) {
|
2018-10-16 15:59:24 -07:00
|
|
|
super(handler, endRunnable);
|
2015-03-11 16:56:52 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-03 16:52:43 -08:00
|
|
|
/**
|
|
|
|
|
* Interface for receiving itemInfo with high-res icon.
|
|
|
|
|
*/
|
|
|
|
|
public interface ItemInfoUpdateReceiver {
|
|
|
|
|
|
|
|
|
|
void reapplyItemInfo(ItemInfoWithIcon info);
|
|
|
|
|
}
|
2010-02-08 13:44:00 -08:00
|
|
|
}
|