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.
|
|
|
|
|
*/
|
|
|
|
|
|
2013-06-05 22:57:57 -04:00
|
|
|
package com.android.launcher3;
|
2010-02-08 13:44:00 -08:00
|
|
|
|
2012-02-13 14:27:42 -08:00
|
|
|
import android.app.ActivityManager;
|
2010-02-08 13:44:00 -08:00
|
|
|
import android.content.ComponentName;
|
2012-02-13 14:27:42 -08:00
|
|
|
import android.content.Context;
|
2010-02-08 13:44:00 -08:00
|
|
|
import android.content.Intent;
|
2012-05-18 15:04:49 -07:00
|
|
|
import android.content.pm.ActivityInfo;
|
2014-08-11 17:05:23 -07:00
|
|
|
import android.content.pm.ApplicationInfo;
|
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;
|
2010-02-08 13:44:00 -08:00
|
|
|
import android.content.pm.ResolveInfo;
|
2010-11-01 11:52:08 -07:00
|
|
|
import android.content.res.Resources;
|
2010-02-08 13:44:00 -08:00
|
|
|
import android.graphics.Bitmap;
|
2014-02-10 12:16:54 -05:00
|
|
|
import android.graphics.BitmapFactory;
|
2010-03-15 14:44:42 -07:00
|
|
|
import android.graphics.Canvas;
|
2010-02-08 13:44:00 -08:00
|
|
|
import android.graphics.drawable.Drawable;
|
2014-08-29 17:20:55 -07:00
|
|
|
import android.text.TextUtils;
|
2014-02-10 12:16:54 -05:00
|
|
|
import android.util.Log;
|
|
|
|
|
|
2014-04-30 03:02:21 +01:00
|
|
|
import com.android.launcher3.compat.LauncherActivityInfoCompat;
|
|
|
|
|
import com.android.launcher3.compat.LauncherAppsCompat;
|
|
|
|
|
import com.android.launcher3.compat.UserHandleCompat;
|
|
|
|
|
import com.android.launcher3.compat.UserManagerCompat;
|
|
|
|
|
|
2014-02-10 12:16:54 -05:00
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
|
import java.io.IOException;
|
2010-02-08 13:44:00 -08:00
|
|
|
import java.util.HashMap;
|
2014-02-10 12:16:54 -05:00
|
|
|
import java.util.HashSet;
|
2013-10-15 10:18:02 -07:00
|
|
|
import java.util.Iterator;
|
|
|
|
|
import java.util.Map.Entry;
|
2010-02-08 13:44:00 -08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Cache of application icons. Icons can be made from any thread.
|
|
|
|
|
*/
|
|
|
|
|
public class IconCache {
|
2014-08-11 17:05:23 -07:00
|
|
|
|
2010-02-08 13:44:00 -08:00
|
|
|
private static final String TAG = "Launcher.IconCache";
|
|
|
|
|
|
|
|
|
|
private static final int INITIAL_ICON_CACHE_CAPACITY = 50;
|
2014-02-10 12:16:54 -05:00
|
|
|
private static final String RESOURCE_FILE_PREFIX = "icon_";
|
|
|
|
|
|
2014-08-11 17:05:23 -07:00
|
|
|
// Empty class name is used for storing package default entry.
|
|
|
|
|
private static final String EMPTY_CLASS_NAME = ".";
|
|
|
|
|
|
2014-09-09 16:27:55 -07:00
|
|
|
private static final boolean DEBUG = false;
|
2010-02-08 13:44:00 -08:00
|
|
|
|
|
|
|
|
private static class CacheEntry {
|
|
|
|
|
public Bitmap icon;
|
2014-07-21 17:11:41 +01:00
|
|
|
public CharSequence title;
|
|
|
|
|
public CharSequence contentDescription;
|
2010-02-08 13:44:00 -08:00
|
|
|
}
|
|
|
|
|
|
2014-04-30 03:02:21 +01:00
|
|
|
private static class CacheKey {
|
|
|
|
|
public ComponentName componentName;
|
|
|
|
|
public UserHandleCompat user;
|
|
|
|
|
|
|
|
|
|
CacheKey(ComponentName componentName, UserHandleCompat user) {
|
|
|
|
|
this.componentName = componentName;
|
|
|
|
|
this.user = user;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public int hashCode() {
|
|
|
|
|
return componentName.hashCode() + user.hashCode();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean equals(Object o) {
|
|
|
|
|
CacheKey other = (CacheKey) o;
|
|
|
|
|
return other.componentName.equals(componentName) && other.user.equals(user);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private final HashMap<UserHandleCompat, Bitmap> mDefaultIcons =
|
|
|
|
|
new HashMap<UserHandleCompat, Bitmap>();
|
2013-06-11 14:45:48 -04:00
|
|
|
private final Context mContext;
|
2010-03-15 14:44:42 -07:00
|
|
|
private final PackageManager mPackageManager;
|
2014-04-30 03:02:21 +01:00
|
|
|
private final UserManagerCompat mUserManager;
|
|
|
|
|
private final LauncherAppsCompat mLauncherApps;
|
|
|
|
|
private final HashMap<CacheKey, CacheEntry> mCache =
|
|
|
|
|
new HashMap<CacheKey, CacheEntry>(INITIAL_ICON_CACHE_CAPACITY);
|
2010-11-01 11:52:08 -07:00
|
|
|
private int mIconDpi;
|
2010-02-08 13:44:00 -08:00
|
|
|
|
2013-06-11 14:45:48 -04:00
|
|
|
public IconCache(Context context) {
|
2012-02-13 14:27:42 -08:00
|
|
|
ActivityManager activityManager =
|
|
|
|
|
(ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
|
|
|
|
|
|
2010-02-08 13:44:00 -08:00
|
|
|
mContext = context;
|
|
|
|
|
mPackageManager = context.getPackageManager();
|
2014-04-30 03:02:21 +01:00
|
|
|
mUserManager = UserManagerCompat.getInstance(mContext);
|
|
|
|
|
mLauncherApps = LauncherAppsCompat.getInstance(mContext);
|
2012-02-13 14:27:42 -08:00
|
|
|
mIconDpi = activityManager.getLauncherLargeIconDensity();
|
|
|
|
|
|
2010-11-01 11:52:08 -07:00
|
|
|
// need to set mIconDpi before getting default icon
|
2014-04-30 03:02:21 +01:00
|
|
|
UserHandleCompat myUser = UserHandleCompat.myUserHandle();
|
|
|
|
|
mDefaultIcons.put(myUser, makeDefaultIcon(myUser));
|
2010-03-15 14:44:42 -07:00
|
|
|
}
|
|
|
|
|
|
2010-11-01 11:52:08 -07:00
|
|
|
public Drawable getFullResDefaultActivityIcon() {
|
|
|
|
|
return getFullResIcon(Resources.getSystem(),
|
2012-04-18 14:23:14 -07:00
|
|
|
android.R.mipmap.sym_def_app_icon);
|
2010-11-01 11:52:08 -07:00
|
|
|
}
|
|
|
|
|
|
2011-07-07 15:33:20 -07:00
|
|
|
public Drawable getFullResIcon(Resources resources, int iconId) {
|
2011-08-03 11:49:59 -07:00
|
|
|
Drawable d;
|
2011-07-07 15:33:20 -07:00
|
|
|
try {
|
2011-08-03 11:49:59 -07:00
|
|
|
d = resources.getDrawableForDensity(iconId, mIconDpi);
|
2011-07-07 15:33:20 -07:00
|
|
|
} catch (Resources.NotFoundException e) {
|
2011-08-03 11:49:59 -07:00
|
|
|
d = null;
|
2011-07-07 15:33:20 -07:00
|
|
|
}
|
2011-08-03 11:49:59 -07:00
|
|
|
|
|
|
|
|
return (d != null) ? d : getFullResDefaultActivityIcon();
|
2010-11-01 11:52:08 -07:00
|
|
|
}
|
|
|
|
|
|
2011-10-31 13:05:15 -07:00
|
|
|
public Drawable getFullResIcon(String packageName, int iconId) {
|
2010-11-01 11:52:08 -07:00
|
|
|
Resources resources;
|
|
|
|
|
try {
|
2011-10-31 13:05:15 -07:00
|
|
|
resources = mPackageManager.getResourcesForApplication(packageName);
|
|
|
|
|
} catch (PackageManager.NameNotFoundException e) {
|
|
|
|
|
resources = null;
|
|
|
|
|
}
|
|
|
|
|
if (resources != null) {
|
|
|
|
|
if (iconId != 0) {
|
|
|
|
|
return getFullResIcon(resources, iconId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return getFullResDefaultActivityIcon();
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-14 17:39:34 -07:00
|
|
|
public int getFullResIconDpi() {
|
|
|
|
|
return mIconDpi;
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-31 13:05:15 -07:00
|
|
|
public Drawable getFullResIcon(ResolveInfo info) {
|
2012-05-18 15:04:49 -07:00
|
|
|
return getFullResIcon(info.activityInfo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Drawable getFullResIcon(ActivityInfo info) {
|
|
|
|
|
|
2011-10-31 13:05:15 -07:00
|
|
|
Resources resources;
|
|
|
|
|
try {
|
|
|
|
|
resources = mPackageManager.getResourcesForApplication(
|
2012-05-18 15:04:49 -07:00
|
|
|
info.applicationInfo);
|
2010-11-01 11:52:08 -07:00
|
|
|
} catch (PackageManager.NameNotFoundException e) {
|
|
|
|
|
resources = null;
|
|
|
|
|
}
|
|
|
|
|
if (resources != null) {
|
2012-05-18 15:04:49 -07:00
|
|
|
int iconId = info.getIconResource();
|
2010-11-01 11:52:08 -07:00
|
|
|
if (iconId != 0) {
|
|
|
|
|
return getFullResIcon(resources, iconId);
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-02-10 12:16:54 -05:00
|
|
|
|
2010-11-01 11:52:08 -07:00
|
|
|
return getFullResDefaultActivityIcon();
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-30 03:02:21 +01:00
|
|
|
private Bitmap makeDefaultIcon(UserHandleCompat user) {
|
|
|
|
|
Drawable unbadged = getFullResDefaultActivityIcon();
|
|
|
|
|
Drawable d = mUserManager.getBadgedDrawableForUser(unbadged, user);
|
2010-03-15 14:44:42 -07:00
|
|
|
Bitmap b = Bitmap.createBitmap(Math.max(d.getIntrinsicWidth(), 1),
|
|
|
|
|
Math.max(d.getIntrinsicHeight(), 1),
|
|
|
|
|
Bitmap.Config.ARGB_8888);
|
|
|
|
|
Canvas c = new Canvas(b);
|
|
|
|
|
d.setBounds(0, 0, b.getWidth(), b.getHeight());
|
|
|
|
|
d.draw(c);
|
2011-08-03 12:02:47 -07:00
|
|
|
c.setBitmap(null);
|
2010-03-15 14:44:42 -07:00
|
|
|
return b;
|
2010-02-08 13:44:00 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Remove any records for the supplied ComponentName.
|
|
|
|
|
*/
|
2014-04-30 03:02:21 +01:00
|
|
|
public void remove(ComponentName componentName, UserHandleCompat user) {
|
2010-02-08 13:44:00 -08:00
|
|
|
synchronized (mCache) {
|
2014-04-30 03:02:21 +01:00
|
|
|
mCache.remove(new CacheKey(componentName, user));
|
2010-02-08 13:44:00 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-10 12:16:54 -05:00
|
|
|
/**
|
|
|
|
|
* Remove any records for the supplied package name.
|
|
|
|
|
*/
|
2014-04-30 03:02:21 +01:00
|
|
|
public void remove(String packageName, UserHandleCompat user) {
|
|
|
|
|
HashSet<CacheKey> forDeletion = new HashSet<CacheKey>();
|
|
|
|
|
for (CacheKey key: mCache.keySet()) {
|
|
|
|
|
if (key.componentName.getPackageName().equals(packageName)
|
|
|
|
|
&& key.user.equals(user)) {
|
|
|
|
|
forDeletion.add(key);
|
2014-02-10 12:16:54 -05:00
|
|
|
}
|
|
|
|
|
}
|
2014-04-30 03:02:21 +01:00
|
|
|
for (CacheKey condemned: forDeletion) {
|
|
|
|
|
mCache.remove(condemned);
|
2014-02-10 12:16:54 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-02-08 13:44:00 -08:00
|
|
|
/**
|
|
|
|
|
* Empty out the cache.
|
|
|
|
|
*/
|
|
|
|
|
public void flush() {
|
|
|
|
|
synchronized (mCache) {
|
|
|
|
|
mCache.clear();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-14 17:03:04 -07:00
|
|
|
/**
|
|
|
|
|
* Empty out the cache that aren't of the correct grid size
|
|
|
|
|
*/
|
|
|
|
|
public void flushInvalidIcons(DeviceProfile grid) {
|
|
|
|
|
synchronized (mCache) {
|
2014-04-30 03:02:21 +01:00
|
|
|
Iterator<Entry<CacheKey, CacheEntry>> it = mCache.entrySet().iterator();
|
2013-10-15 10:18:02 -07:00
|
|
|
while (it.hasNext()) {
|
|
|
|
|
final CacheEntry e = it.next().getValue();
|
2013-10-25 15:24:24 -07:00
|
|
|
if (e.icon.getWidth() < grid.iconSizePx || e.icon.getHeight() < grid.iconSizePx) {
|
2013-10-15 10:18:02 -07:00
|
|
|
it.remove();
|
2013-10-14 17:03:04 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-02-08 13:44:00 -08:00
|
|
|
/**
|
|
|
|
|
* Fill in "application" with the icon and label for "info."
|
|
|
|
|
*/
|
2014-04-30 03:02:21 +01:00
|
|
|
public void getTitleAndIcon(AppInfo application, LauncherActivityInfoCompat info,
|
2011-07-11 17:44:15 -07:00
|
|
|
HashMap<Object, CharSequence> labelCache) {
|
2010-02-08 13:44:00 -08:00
|
|
|
synchronized (mCache) {
|
2014-04-30 03:02:21 +01:00
|
|
|
CacheEntry entry = cacheLocked(application.componentName, info, labelCache,
|
2014-08-11 17:05:23 -07:00
|
|
|
info.getUser(), false);
|
2010-02-08 13:44:00 -08:00
|
|
|
|
|
|
|
|
application.title = entry.title;
|
|
|
|
|
application.iconBitmap = entry.icon;
|
2014-06-30 12:30:31 +01:00
|
|
|
application.contentDescription = entry.contentDescription;
|
2010-02-08 13:44:00 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-30 03:02:21 +01:00
|
|
|
public Bitmap getIcon(Intent intent, UserHandleCompat user) {
|
2014-08-11 17:05:23 -07:00
|
|
|
return getIcon(intent, null, user, true);
|
2014-02-10 12:16:54 -05:00
|
|
|
}
|
|
|
|
|
|
2014-08-29 17:20:55 -07:00
|
|
|
private Bitmap getIcon(Intent intent, String title, UserHandleCompat user, boolean usePkgIcon) {
|
2010-05-04 12:12:41 -07:00
|
|
|
synchronized (mCache) {
|
|
|
|
|
ComponentName component = intent.getComponent();
|
2014-05-13 16:18:21 -04: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.
|
2014-08-11 17:05:23 -07:00
|
|
|
if (component == null) {
|
2014-04-30 03:02:21 +01:00
|
|
|
return getDefaultIcon(user);
|
2010-05-04 12:12:41 -07:00
|
|
|
}
|
2010-02-08 13:44:00 -08:00
|
|
|
|
2014-08-29 17:20:55 -07:00
|
|
|
LauncherActivityInfoCompat launcherActInfo = mLauncherApps.resolveActivity(intent, user);
|
2014-08-11 17:05:23 -07:00
|
|
|
CacheEntry entry = cacheLocked(component, launcherActInfo, null, user, usePkgIcon);
|
2014-02-10 12:16:54 -05:00
|
|
|
if (title != null) {
|
|
|
|
|
entry.title = title;
|
2014-06-30 12:30:31 +01:00
|
|
|
entry.contentDescription = mUserManager.getBadgedLabelForUser(title, user);
|
2014-02-10 12:16:54 -05:00
|
|
|
}
|
2010-05-04 12:12:41 -07:00
|
|
|
return entry.icon;
|
|
|
|
|
}
|
2010-02-08 13:44:00 -08:00
|
|
|
}
|
|
|
|
|
|
2014-08-29 17:20:55 -07:00
|
|
|
/**
|
|
|
|
|
* Fill in "shortcutInfo" with the icon and label for "info."
|
|
|
|
|
*/
|
|
|
|
|
public void getTitleAndIcon(ShortcutInfo shortcutInfo, Intent intent, UserHandleCompat user,
|
|
|
|
|
boolean usePkgIcon) {
|
|
|
|
|
synchronized (mCache) {
|
|
|
|
|
ComponentName component = intent.getComponent();
|
|
|
|
|
// 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.
|
|
|
|
|
if (component == null) {
|
|
|
|
|
shortcutInfo.setIcon(getDefaultIcon(user));
|
|
|
|
|
shortcutInfo.title = "";
|
|
|
|
|
shortcutInfo.usingFallbackIcon = true;
|
|
|
|
|
} else {
|
|
|
|
|
LauncherActivityInfoCompat launcherActInfo =
|
|
|
|
|
mLauncherApps.resolveActivity(intent, user);
|
|
|
|
|
CacheEntry entry = cacheLocked(component, launcherActInfo, null, user, usePkgIcon);
|
|
|
|
|
|
|
|
|
|
shortcutInfo.setIcon(entry.icon);
|
|
|
|
|
shortcutInfo.title = entry.title;
|
|
|
|
|
shortcutInfo.usingFallbackIcon = isDefaultIcon(entry.icon, user);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2014-04-30 03:02:21 +01:00
|
|
|
public Bitmap getDefaultIcon(UserHandleCompat user) {
|
|
|
|
|
if (!mDefaultIcons.containsKey(user)) {
|
|
|
|
|
mDefaultIcons.put(user, makeDefaultIcon(user));
|
|
|
|
|
}
|
|
|
|
|
return mDefaultIcons.get(user);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Bitmap getIcon(ComponentName component, LauncherActivityInfoCompat info,
|
2011-08-17 10:37:13 -07:00
|
|
|
HashMap<Object, CharSequence> labelCache) {
|
2010-05-04 12:12:41 -07:00
|
|
|
synchronized (mCache) {
|
2014-04-30 03:02:21 +01:00
|
|
|
if (info == null || component == null) {
|
2010-05-04 12:12:41 -07:00
|
|
|
return null;
|
|
|
|
|
}
|
2010-02-08 13:44:00 -08:00
|
|
|
|
2014-08-11 17:05:23 -07:00
|
|
|
CacheEntry entry = cacheLocked(component, info, labelCache, info.getUser(), false);
|
2010-05-04 12:12:41 -07:00
|
|
|
return entry.icon;
|
|
|
|
|
}
|
2010-02-08 13:44:00 -08:00
|
|
|
}
|
|
|
|
|
|
2014-04-30 03:02:21 +01:00
|
|
|
public boolean isDefaultIcon(Bitmap icon, UserHandleCompat user) {
|
|
|
|
|
return mDefaultIcons.get(user) == icon;
|
2010-08-30 18:30:15 -07:00
|
|
|
}
|
|
|
|
|
|
2014-04-30 03:02:21 +01:00
|
|
|
private CacheEntry cacheLocked(ComponentName componentName, LauncherActivityInfoCompat info,
|
2014-08-11 17:05:23 -07:00
|
|
|
HashMap<Object, CharSequence> labelCache, UserHandleCompat user, boolean usePackageIcon) {
|
2014-04-30 03:02:21 +01:00
|
|
|
CacheKey cacheKey = new CacheKey(componentName, user);
|
|
|
|
|
CacheEntry entry = mCache.get(cacheKey);
|
2010-02-08 13:44:00 -08:00
|
|
|
if (entry == null) {
|
|
|
|
|
entry = new CacheEntry();
|
|
|
|
|
|
2014-04-30 03:02:21 +01:00
|
|
|
mCache.put(cacheKey, entry);
|
2010-02-12 17:53:35 -05:00
|
|
|
|
2014-02-10 12:16:54 -05:00
|
|
|
if (info != null) {
|
2014-04-30 03:02:21 +01:00
|
|
|
ComponentName labelKey = info.getComponentName();
|
|
|
|
|
if (labelCache != null && labelCache.containsKey(labelKey)) {
|
|
|
|
|
entry.title = labelCache.get(labelKey).toString();
|
2014-02-10 12:16:54 -05:00
|
|
|
} else {
|
2014-04-30 03:02:21 +01:00
|
|
|
entry.title = info.getLabel().toString();
|
2014-02-10 12:16:54 -05:00
|
|
|
if (labelCache != null) {
|
2014-04-30 03:02:21 +01:00
|
|
|
labelCache.put(labelKey, entry.title);
|
2014-02-10 12:16:54 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-30 12:30:31 +01:00
|
|
|
entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, user);
|
2014-02-10 12:16:54 -05:00
|
|
|
entry.icon = Utilities.createIconBitmap(
|
2014-04-30 03:02:21 +01:00
|
|
|
info.getBadgedIcon(mIconDpi), mContext);
|
2011-07-11 17:44:15 -07:00
|
|
|
} else {
|
2014-02-10 12:16:54 -05:00
|
|
|
entry.title = "";
|
2014-04-30 03:02:21 +01:00
|
|
|
Bitmap preloaded = getPreloadedIcon(componentName, user);
|
2014-02-10 12:16:54 -05:00
|
|
|
if (preloaded != null) {
|
|
|
|
|
if (DEBUG) Log.d(TAG, "using preloaded icon for " +
|
|
|
|
|
componentName.toShortString());
|
|
|
|
|
entry.icon = preloaded;
|
|
|
|
|
} else {
|
2014-08-11 17:05:23 -07:00
|
|
|
if (usePackageIcon) {
|
|
|
|
|
CacheEntry packageEntry = getEntryForPackage(
|
|
|
|
|
componentName.getPackageName(), user);
|
2014-08-29 17:20:55 -07:00
|
|
|
if (packageEntry != null) {
|
2014-08-11 17:05:23 -07:00
|
|
|
if (DEBUG) Log.d(TAG, "using package default icon for " +
|
|
|
|
|
componentName.toShortString());
|
|
|
|
|
entry.icon = packageEntry.icon;
|
2014-08-29 17:20:55 -07:00
|
|
|
entry.title = packageEntry.title;
|
2014-08-11 17:05:23 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (entry.icon == null) {
|
|
|
|
|
if (DEBUG) Log.d(TAG, "using default icon for " +
|
|
|
|
|
componentName.toShortString());
|
|
|
|
|
entry.icon = getDefaultIcon(user);
|
|
|
|
|
}
|
2011-07-11 17:44:15 -07:00
|
|
|
}
|
|
|
|
|
}
|
2010-02-08 13:44:00 -08:00
|
|
|
}
|
|
|
|
|
return entry;
|
|
|
|
|
}
|
2011-05-12 00:06:32 -04:00
|
|
|
|
2014-08-29 17:20:55 -07:00
|
|
|
/**
|
|
|
|
|
* Adds a default package entry in the cache. This entry is not persisted and will be removed
|
|
|
|
|
* when the cache is flushed.
|
|
|
|
|
*/
|
|
|
|
|
public void cachePackageInstallInfo(String packageName, UserHandleCompat user,
|
|
|
|
|
Bitmap icon, CharSequence title) {
|
|
|
|
|
CacheEntry entry = getEntryForPackage(packageName, user);
|
|
|
|
|
if (!TextUtils.isEmpty(title)) {
|
|
|
|
|
entry.title = title;
|
|
|
|
|
}
|
|
|
|
|
if (icon != null) {
|
|
|
|
|
entry.icon = Utilities.createIconBitmap(icon, mContext);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-11 17:05:23 -07:00
|
|
|
/**
|
|
|
|
|
* Gets an entry for the package, which can be used as a fallback entry for various components.
|
|
|
|
|
*/
|
|
|
|
|
private CacheEntry getEntryForPackage(String packageName, UserHandleCompat user) {
|
|
|
|
|
ComponentName cn = getPackageComponent(packageName);
|
|
|
|
|
CacheKey cacheKey = new CacheKey(cn, user);
|
|
|
|
|
CacheEntry entry = mCache.get(cacheKey);
|
|
|
|
|
if (entry == null) {
|
|
|
|
|
entry = new CacheEntry();
|
2014-08-29 17:20:55 -07:00
|
|
|
entry.title = "";
|
2014-08-11 17:05:23 -07:00
|
|
|
mCache.put(cacheKey, entry);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
ApplicationInfo info = mPackageManager.getApplicationInfo(packageName, 0);
|
|
|
|
|
entry.title = info.loadLabel(mPackageManager);
|
|
|
|
|
entry.icon = Utilities.createIconBitmap(info.loadIcon(mPackageManager), mContext);
|
|
|
|
|
} catch (NameNotFoundException e) {
|
|
|
|
|
if (DEBUG) Log.d(TAG, "Application not installed " + packageName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (entry.icon == null) {
|
|
|
|
|
entry.icon = getPreloadedIcon(cn, user);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return entry;
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-12 00:06:32 -04:00
|
|
|
public HashMap<ComponentName,Bitmap> getAllIcons() {
|
|
|
|
|
synchronized (mCache) {
|
|
|
|
|
HashMap<ComponentName,Bitmap> set = new HashMap<ComponentName,Bitmap>();
|
2014-04-30 03:02:21 +01:00
|
|
|
for (CacheKey ck : mCache.keySet()) {
|
|
|
|
|
final CacheEntry e = mCache.get(ck);
|
|
|
|
|
set.put(ck.componentName, e.icon);
|
2011-05-12 00:06:32 -04:00
|
|
|
}
|
|
|
|
|
return set;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-02-10 12:16:54 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Pre-load an icon into the persistent cache.
|
|
|
|
|
*
|
|
|
|
|
* <P>Queries for a component that does not exist in the package manager
|
|
|
|
|
* will be answered by the persistent cache.
|
|
|
|
|
*
|
|
|
|
|
* @param context application context
|
|
|
|
|
* @param componentName the icon should be returned for this component
|
|
|
|
|
* @param icon the icon to be persisted
|
|
|
|
|
* @param dpi the native density of the icon
|
|
|
|
|
*/
|
|
|
|
|
public static void preloadIcon(Context context, ComponentName componentName, Bitmap icon,
|
|
|
|
|
int dpi) {
|
|
|
|
|
// TODO rescale to the correct native DPI
|
|
|
|
|
try {
|
|
|
|
|
PackageManager packageManager = context.getPackageManager();
|
|
|
|
|
packageManager.getActivityIcon(componentName);
|
|
|
|
|
// component is present on the system already, do nothing
|
|
|
|
|
return;
|
|
|
|
|
} catch (PackageManager.NameNotFoundException e) {
|
|
|
|
|
// pass
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final String key = componentName.flattenToString();
|
|
|
|
|
FileOutputStream resourceFile = null;
|
|
|
|
|
try {
|
|
|
|
|
resourceFile = context.openFileOutput(getResourceFilename(componentName),
|
|
|
|
|
Context.MODE_PRIVATE);
|
|
|
|
|
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
|
|
|
|
if (icon.compress(android.graphics.Bitmap.CompressFormat.PNG, 75, os)) {
|
|
|
|
|
byte[] buffer = os.toByteArray();
|
|
|
|
|
resourceFile.write(buffer, 0, buffer.length);
|
|
|
|
|
} else {
|
|
|
|
|
Log.w(TAG, "failed to encode cache for " + key);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
|
Log.w(TAG, "failed to pre-load cache for " + key, e);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
Log.w(TAG, "failed to pre-load cache for " + key, e);
|
|
|
|
|
} finally {
|
|
|
|
|
if (resourceFile != null) {
|
|
|
|
|
try {
|
|
|
|
|
resourceFile.close();
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
Log.d(TAG, "failed to save restored icon for: " + key, e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Read a pre-loaded icon from the persistent icon cache.
|
|
|
|
|
*
|
|
|
|
|
* @param componentName the component that should own the icon
|
|
|
|
|
* @returns a bitmap if one is cached, or null.
|
|
|
|
|
*/
|
2014-04-30 03:02:21 +01:00
|
|
|
private Bitmap getPreloadedIcon(ComponentName componentName, UserHandleCompat user) {
|
2014-02-10 12:16:54 -05:00
|
|
|
final String key = componentName.flattenToShortString();
|
|
|
|
|
|
2014-04-30 03:02:21 +01:00
|
|
|
// We don't keep icons for other profiles in persistent cache.
|
|
|
|
|
if (!user.equals(UserHandleCompat.myUserHandle())) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-10 12:16:54 -05:00
|
|
|
if (DEBUG) Log.v(TAG, "looking for pre-load icon for " + key);
|
|
|
|
|
Bitmap icon = null;
|
|
|
|
|
FileInputStream resourceFile = null;
|
|
|
|
|
try {
|
|
|
|
|
resourceFile = mContext.openFileInput(getResourceFilename(componentName));
|
|
|
|
|
byte[] buffer = new byte[1024];
|
|
|
|
|
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
|
|
|
|
|
int bytesRead = 0;
|
|
|
|
|
while(bytesRead >= 0) {
|
|
|
|
|
bytes.write(buffer, 0, bytesRead);
|
|
|
|
|
bytesRead = resourceFile.read(buffer, 0, buffer.length);
|
|
|
|
|
}
|
|
|
|
|
if (DEBUG) Log.d(TAG, "read " + bytes.size());
|
|
|
|
|
icon = BitmapFactory.decodeByteArray(bytes.toByteArray(), 0, bytes.size());
|
|
|
|
|
if (icon == null) {
|
|
|
|
|
Log.w(TAG, "failed to decode pre-load icon for " + key);
|
|
|
|
|
}
|
|
|
|
|
} catch (FileNotFoundException e) {
|
2014-09-09 16:27:55 -07:00
|
|
|
if (DEBUG) Log.d(TAG, "there is no restored icon for: " + key);
|
2014-02-10 12:16:54 -05:00
|
|
|
} catch (IOException e) {
|
|
|
|
|
Log.w(TAG, "failed to read pre-load icon for: " + key, e);
|
|
|
|
|
} finally {
|
|
|
|
|
if(resourceFile != null) {
|
|
|
|
|
try {
|
|
|
|
|
resourceFile.close();
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
Log.d(TAG, "failed to manage pre-load icon file: " + key, e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return icon;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Remove a pre-loaded icon from the persistent icon cache.
|
|
|
|
|
*
|
|
|
|
|
* @param componentName the component that should own the icon
|
|
|
|
|
* @returns true on success
|
|
|
|
|
*/
|
2014-04-30 03:02:21 +01:00
|
|
|
public boolean deletePreloadedIcon(ComponentName componentName, UserHandleCompat user) {
|
|
|
|
|
// We don't keep icons for other profiles in persistent cache.
|
|
|
|
|
if (!user.equals(UserHandleCompat.myUserHandle())) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2014-02-10 12:16:54 -05:00
|
|
|
if (componentName == null) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (mCache.remove(componentName) != null) {
|
|
|
|
|
if (DEBUG) Log.d(TAG, "removed pre-loaded icon from the in-memory cache");
|
|
|
|
|
}
|
|
|
|
|
boolean success = mContext.deleteFile(getResourceFilename(componentName));
|
|
|
|
|
if (DEBUG && success) Log.d(TAG, "removed pre-loaded icon from persistent cache");
|
|
|
|
|
|
|
|
|
|
return success;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static String getResourceFilename(ComponentName component) {
|
|
|
|
|
String resourceName = component.flattenToShortString();
|
|
|
|
|
String filename = resourceName.replace(File.separatorChar, '_');
|
|
|
|
|
return RESOURCE_FILE_PREFIX + filename;
|
|
|
|
|
}
|
2014-08-11 17:05:23 -07:00
|
|
|
|
|
|
|
|
static ComponentName getPackageComponent(String packageName) {
|
|
|
|
|
return new ComponentName(packageName, EMPTY_CLASS_NAME);
|
|
|
|
|
}
|
2010-02-08 13:44:00 -08:00
|
|
|
}
|