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
|
|
|
|
2018-09-24 10:51:52 -07:00
|
|
|
import static com.android.launcher3.icons.CachingLogic.LAUNCHER_ACTIVITY_INFO;
|
2018-08-20 15:01:03 -07: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;
|
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;
|
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;
|
|
|
|
|
import com.android.launcher3.LauncherModel;
|
|
|
|
|
import com.android.launcher3.ShortcutInfo;
|
|
|
|
|
import com.android.launcher3.Utilities;
|
2015-05-21 13:04:53 -07:00
|
|
|
import com.android.launcher3.model.PackageItemInfo;
|
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-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-09-24 10:51:52 -07:00
|
|
|
super(context, inv.fillResIconDpi, inv.iconBitmapSize);
|
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-09-24 10:51:52 -07:00
|
|
|
addIconToDBAndMemCache(app, LAUNCHER_ACTIVITY_INFO, info, userSerial,
|
|
|
|
|
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-09-24 10:51:52 -07:00
|
|
|
Provider.of(null), LAUNCHER_ACTIVITY_INFO,
|
2018-08-20 15:01:03 -07:00
|
|
|
application.user, 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) {
|
2017-12-19 16:49:24 -08:00
|
|
|
getDefaultIcon(info.user).applyTo(info);
|
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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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) {
|
|
|
|
|
CacheEntry entry = cacheLocked(infoInOut.getTargetComponent(), activityInfoProvider,
|
2018-09-24 10:51:52 -07:00
|
|
|
LAUNCHER_ACTIVITY_INFO, infoInOut.user, 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-05-11 09:18:50 -07:00
|
|
|
public static abstract class IconLoadRequest implements Runnable {
|
2015-03-11 16:56:52 -07:00
|
|
|
private final Handler mHandler;
|
2018-05-11 09:18:50 -07:00
|
|
|
private final Runnable mEndRunnable;
|
|
|
|
|
|
|
|
|
|
private boolean mEnded = false;
|
2015-03-11 16:56:52 -07:00
|
|
|
|
2018-05-11 09:18:50 -07:00
|
|
|
IconLoadRequest(Handler handler, Runnable endRunnable) {
|
2015-03-11 16:56:52 -07:00
|
|
|
mHandler = handler;
|
2018-05-11 09:18:50 -07:00
|
|
|
mEndRunnable = endRunnable;
|
2015-03-11 16:56:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void cancel() {
|
2018-05-11 09:18:50 -07:00
|
|
|
mHandler.removeCallbacks(this);
|
|
|
|
|
onEnd();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void onEnd() {
|
|
|
|
|
if (!mEnded) {
|
|
|
|
|
mEnded = true;
|
|
|
|
|
mEndRunnable.run();
|
|
|
|
|
}
|
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
|
|
|
}
|