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;
|
2010-02-08 13:44:00 -08:00
|
|
|
import android.content.pm.PackageManager;
|
|
|
|
|
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;
|
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;
|
|
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Cache of application icons. Icons can be made from any thread.
|
|
|
|
|
*/
|
|
|
|
|
public class IconCache {
|
2012-04-13 14:44:29 -07:00
|
|
|
@SuppressWarnings("unused")
|
2010-02-08 13:44:00 -08:00
|
|
|
private static final String TAG = "Launcher.IconCache";
|
|
|
|
|
|
|
|
|
|
private static final int INITIAL_ICON_CACHE_CAPACITY = 50;
|
|
|
|
|
|
|
|
|
|
private static class CacheEntry {
|
|
|
|
|
public Bitmap icon;
|
|
|
|
|
public String title;
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-15 14:44:42 -07:00
|
|
|
private final Bitmap mDefaultIcon;
|
|
|
|
|
private final LauncherApplication mContext;
|
|
|
|
|
private final PackageManager mPackageManager;
|
2010-02-08 13:44:00 -08:00
|
|
|
private final HashMap<ComponentName, CacheEntry> mCache =
|
|
|
|
|
new HashMap<ComponentName, CacheEntry>(INITIAL_ICON_CACHE_CAPACITY);
|
2010-11-01 11:52:08 -07:00
|
|
|
private int mIconDpi;
|
2010-02-08 13:44:00 -08:00
|
|
|
|
|
|
|
|
public IconCache(LauncherApplication 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();
|
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
|
2010-03-15 14:44:42 -07:00
|
|
|
mDefaultIcon = makeDefaultIcon();
|
|
|
|
|
}
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return getFullResDefaultActivityIcon();
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-15 14:44:42 -07:00
|
|
|
private Bitmap makeDefaultIcon() {
|
2010-11-01 11:52:08 -07:00
|
|
|
Drawable d = getFullResDefaultActivityIcon();
|
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.
|
|
|
|
|
*/
|
|
|
|
|
public void remove(ComponentName componentName) {
|
|
|
|
|
synchronized (mCache) {
|
|
|
|
|
mCache.remove(componentName);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Empty out the cache.
|
|
|
|
|
*/
|
|
|
|
|
public void flush() {
|
|
|
|
|
synchronized (mCache) {
|
|
|
|
|
mCache.clear();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Fill in "application" with the icon and label for "info."
|
|
|
|
|
*/
|
2011-07-11 17:44:15 -07:00
|
|
|
public void getTitleAndIcon(ApplicationInfo application, ResolveInfo info,
|
|
|
|
|
HashMap<Object, CharSequence> labelCache) {
|
2010-02-08 13:44:00 -08:00
|
|
|
synchronized (mCache) {
|
2011-07-11 17:44:15 -07:00
|
|
|
CacheEntry entry = cacheLocked(application.componentName, info, labelCache);
|
2010-02-08 13:44:00 -08:00
|
|
|
|
|
|
|
|
application.title = entry.title;
|
|
|
|
|
application.iconBitmap = entry.icon;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Bitmap getIcon(Intent intent) {
|
2010-05-04 12:12:41 -07:00
|
|
|
synchronized (mCache) {
|
|
|
|
|
final ResolveInfo resolveInfo = mPackageManager.resolveActivity(intent, 0);
|
|
|
|
|
ComponentName component = intent.getComponent();
|
2010-02-08 13:44:00 -08:00
|
|
|
|
2010-05-04 12:12:41 -07:00
|
|
|
if (resolveInfo == null || component == null) {
|
|
|
|
|
return mDefaultIcon;
|
|
|
|
|
}
|
2010-02-08 13:44:00 -08:00
|
|
|
|
2011-07-11 17:44:15 -07:00
|
|
|
CacheEntry entry = cacheLocked(component, resolveInfo, null);
|
2010-05-04 12:12:41 -07:00
|
|
|
return entry.icon;
|
|
|
|
|
}
|
2010-02-08 13:44:00 -08:00
|
|
|
}
|
|
|
|
|
|
2011-08-17 10:37:13 -07:00
|
|
|
public Bitmap getIcon(ComponentName component, ResolveInfo resolveInfo,
|
|
|
|
|
HashMap<Object, CharSequence> labelCache) {
|
2010-05-04 12:12:41 -07:00
|
|
|
synchronized (mCache) {
|
|
|
|
|
if (resolveInfo == null || component == null) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2010-02-08 13:44:00 -08:00
|
|
|
|
2011-08-17 10:37:13 -07:00
|
|
|
CacheEntry entry = cacheLocked(component, resolveInfo, labelCache);
|
2010-05-04 12:12:41 -07:00
|
|
|
return entry.icon;
|
|
|
|
|
}
|
2010-02-08 13:44:00 -08:00
|
|
|
}
|
|
|
|
|
|
2010-08-30 18:30:15 -07:00
|
|
|
public boolean isDefaultIcon(Bitmap icon) {
|
|
|
|
|
return mDefaultIcon == icon;
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-11 17:44:15 -07:00
|
|
|
private CacheEntry cacheLocked(ComponentName componentName, ResolveInfo info,
|
|
|
|
|
HashMap<Object, CharSequence> labelCache) {
|
2010-02-08 13:44:00 -08:00
|
|
|
CacheEntry entry = mCache.get(componentName);
|
|
|
|
|
if (entry == null) {
|
|
|
|
|
entry = new CacheEntry();
|
|
|
|
|
|
2010-02-12 17:53:35 -05:00
|
|
|
mCache.put(componentName, entry);
|
|
|
|
|
|
2011-08-18 12:12:41 -07:00
|
|
|
ComponentName key = LauncherModel.getComponentNameFromResolveInfo(info);
|
|
|
|
|
if (labelCache != null && labelCache.containsKey(key)) {
|
|
|
|
|
entry.title = labelCache.get(key).toString();
|
2011-07-11 17:44:15 -07:00
|
|
|
} else {
|
|
|
|
|
entry.title = info.loadLabel(mPackageManager).toString();
|
|
|
|
|
if (labelCache != null) {
|
2011-08-18 12:12:41 -07:00
|
|
|
labelCache.put(key, entry.title);
|
2011-07-11 17:44:15 -07:00
|
|
|
}
|
|
|
|
|
}
|
2010-02-08 13:44:00 -08:00
|
|
|
if (entry.title == null) {
|
|
|
|
|
entry.title = info.activityInfo.name;
|
|
|
|
|
}
|
2011-03-10 10:53:06 -08:00
|
|
|
|
2011-07-07 15:33:20 -07:00
|
|
|
entry.icon = Utilities.createIconBitmap(
|
2011-10-31 13:05:15 -07:00
|
|
|
getFullResIcon(info), mContext);
|
2010-02-08 13:44:00 -08:00
|
|
|
}
|
|
|
|
|
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>();
|
|
|
|
|
for (ComponentName cn : mCache.keySet()) {
|
|
|
|
|
final CacheEntry e = mCache.get(cn);
|
|
|
|
|
set.put(cn, e.icon);
|
|
|
|
|
}
|
|
|
|
|
return set;
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-02-08 13:44:00 -08:00
|
|
|
}
|