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;
|
2015-02-18 16:46:50 -08:00
|
|
|
import android.content.ContentValues;
|
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;
|
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;
|
2010-11-01 11:52:08 -07:00
|
|
|
import android.content.res.Resources;
|
2015-02-18 16:46:50 -08:00
|
|
|
import android.database.Cursor;
|
|
|
|
|
import android.database.sqlite.SQLiteDatabase;
|
|
|
|
|
import android.database.sqlite.SQLiteOpenHelper;
|
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;
|
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;
|
|
|
|
|
|
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;
|
2015-02-18 16:46:50 -08:00
|
|
|
import java.util.List;
|
2013-10-15 10:18:02 -07:00
|
|
|
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
|
|
|
|
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);
|
2015-02-18 16:46:50 -08:00
|
|
|
private final int mIconDpi;
|
|
|
|
|
private final IconDB mIconDb;
|
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();
|
2015-02-18 16:46:50 -08:00
|
|
|
mIconDb = new IconDB(context);
|
2010-03-15 14:44:42 -07:00
|
|
|
}
|
|
|
|
|
|
2015-02-18 16:46:50 -08:00
|
|
|
private Drawable getFullResDefaultActivityIcon() {
|
2014-10-16 14:07:29 -07:00
|
|
|
return getFullResIcon(Resources.getSystem(), android.R.mipmap.sym_def_app_icon);
|
2010-11-01 11:52:08 -07:00
|
|
|
}
|
|
|
|
|
|
2014-10-06 16:23:56 -07:00
|
|
|
private 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;
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-18 15:04:49 -07:00
|
|
|
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-10-16 14:07:29 -07:00
|
|
|
public synchronized void remove(ComponentName componentName, UserHandleCompat user) {
|
|
|
|
|
mCache.remove(new CacheKey(componentName, user));
|
2010-02-08 13:44:00 -08:00
|
|
|
}
|
|
|
|
|
|
2014-02-10 12:16:54 -05:00
|
|
|
/**
|
2015-02-18 16:46:50 -08:00
|
|
|
* Remove any records for the supplied package name from memory.
|
2014-02-10 12:16:54 -05:00
|
|
|
*/
|
2015-02-18 16:46:50 -08:00
|
|
|
private void removeFromMemCacheLocked(String packageName, UserHandleCompat user) {
|
2014-04-30 03:02:21 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-18 16:46:50 -08:00
|
|
|
/**
|
|
|
|
|
* Updates the entries related to the given package in memory and persistent DB.
|
|
|
|
|
*/
|
|
|
|
|
public synchronized void updateIconsForPkg(String packageName, UserHandleCompat user) {
|
|
|
|
|
removeIconsForPkg(packageName, user);
|
|
|
|
|
try {
|
|
|
|
|
PackageInfo info = mPackageManager.getPackageInfo(packageName,
|
|
|
|
|
PackageManager.GET_UNINSTALLED_PACKAGES);
|
|
|
|
|
long userSerial = mUserManager.getSerialNumberForUser(user);
|
|
|
|
|
for (LauncherActivityInfoCompat app : mLauncherApps.getActivityList(packageName, user)) {
|
|
|
|
|
addIconToDB(app, info, userSerial);
|
|
|
|
|
}
|
|
|
|
|
} catch (NameNotFoundException e) {
|
|
|
|
|
Log.d(TAG, "Package not found", e);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Removes the entries related to the given package in memory and persistent DB.
|
|
|
|
|
*/
|
|
|
|
|
public synchronized void removeIconsForPkg(String packageName, UserHandleCompat user) {
|
|
|
|
|
removeFromMemCacheLocked(packageName, user);
|
|
|
|
|
long userSerial = mUserManager.getSerialNumberForUser(user);
|
|
|
|
|
mIconDb.getWritableDatabase().delete(IconDB.TABLE_NAME,
|
|
|
|
|
IconDB.COLUMN_COMPONENT + " LIKE ? AND " + IconDB.COLUMN_USER + " = ?",
|
|
|
|
|
new String[] {packageName + "/%", Long.toString(userSerial)});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Updates the persistent DB, such that only entries corresponding to {@param apps} remain in
|
|
|
|
|
* the DB and are updated.
|
|
|
|
|
* @return The set of packages for which icons have updated.
|
|
|
|
|
*/
|
|
|
|
|
public HashSet<String> updateDBIcons(UserHandleCompat user, List<LauncherActivityInfoCompat> apps) {
|
|
|
|
|
long userSerial = mUserManager.getSerialNumberForUser(user);
|
|
|
|
|
PackageManager pm = mContext.getPackageManager();
|
|
|
|
|
HashMap<String, PackageInfo> pkgInfoMap = new HashMap<String, PackageInfo>();
|
|
|
|
|
for (PackageInfo info : pm.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES)) {
|
|
|
|
|
pkgInfoMap.put(info.packageName, info);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HashMap<ComponentName, LauncherActivityInfoCompat> componentMap = new HashMap<>();
|
|
|
|
|
for (LauncherActivityInfoCompat app : apps) {
|
|
|
|
|
componentMap.put(app.getComponentName(), app);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Cursor c = mIconDb.getReadableDatabase().query(IconDB.TABLE_NAME,
|
|
|
|
|
new String[] {IconDB.COLUMN_ROWID, IconDB.COLUMN_COMPONENT,
|
|
|
|
|
IconDB.COLUMN_LAST_UPDATED, IconDB.COLUMN_VERSION},
|
|
|
|
|
IconDB.COLUMN_USER + " = ? ",
|
|
|
|
|
new String[] {Long.toString(userSerial)},
|
|
|
|
|
null, null, null);
|
|
|
|
|
|
|
|
|
|
final int indexComponent = c.getColumnIndex(IconDB.COLUMN_COMPONENT);
|
|
|
|
|
final int indexLastUpdate = c.getColumnIndex(IconDB.COLUMN_LAST_UPDATED);
|
|
|
|
|
final int indexVersion = c.getColumnIndex(IconDB.COLUMN_VERSION);
|
|
|
|
|
final int rowIndex = c.getColumnIndex(IconDB.COLUMN_ROWID);
|
|
|
|
|
|
|
|
|
|
HashSet<Integer> itemsToRemove = new HashSet<Integer>();
|
|
|
|
|
HashSet<String> updatedPackages = new HashSet<String>();
|
|
|
|
|
|
|
|
|
|
while (c.moveToNext()) {
|
|
|
|
|
String cn = c.getString(indexComponent);
|
|
|
|
|
ComponentName component = ComponentName.unflattenFromString(cn);
|
|
|
|
|
PackageInfo info = pkgInfoMap.get(component.getPackageName());
|
|
|
|
|
if (info == null) {
|
|
|
|
|
itemsToRemove.add(c.getInt(rowIndex));
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if ((info.applicationInfo.flags & ApplicationInfo.FLAG_IS_DATA_ONLY) != 0) {
|
|
|
|
|
// Application is not present
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
long updateTime = c.getLong(indexLastUpdate);
|
|
|
|
|
int version = c.getInt(indexVersion);
|
|
|
|
|
LauncherActivityInfoCompat app = componentMap.remove(component);
|
|
|
|
|
if (version == info.versionCode && updateTime == info.lastUpdateTime) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (app == null) {
|
|
|
|
|
itemsToRemove.add(c.getInt(rowIndex));
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
ContentValues values = updateCacheAndGetContentValues(app);
|
|
|
|
|
mIconDb.getWritableDatabase().update(IconDB.TABLE_NAME, values,
|
|
|
|
|
IconDB.COLUMN_COMPONENT + " = ?",
|
|
|
|
|
new String[] { cn });
|
|
|
|
|
|
|
|
|
|
updatedPackages.add(component.getPackageName());
|
|
|
|
|
}
|
|
|
|
|
c.close();
|
|
|
|
|
if (!itemsToRemove.isEmpty()) {
|
|
|
|
|
mIconDb.getWritableDatabase().delete(IconDB.TABLE_NAME,
|
|
|
|
|
IconDB.COLUMN_ROWID + " IN ( " + TextUtils.join(", ", itemsToRemove) +" )",
|
|
|
|
|
null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Insert remaining apps.
|
|
|
|
|
for (LauncherActivityInfoCompat app : componentMap.values()) {
|
|
|
|
|
PackageInfo info = pkgInfoMap.get(app.getComponentName().getPackageName());
|
|
|
|
|
if (info == null) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
addIconToDB(app, info, userSerial);
|
|
|
|
|
}
|
|
|
|
|
return updatedPackages;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void addIconToDB(LauncherActivityInfoCompat app, PackageInfo info, long userSerial) {
|
|
|
|
|
ContentValues values = updateCacheAndGetContentValues(app);
|
|
|
|
|
values.put(IconDB.COLUMN_COMPONENT, app.getComponentName().flattenToString());
|
|
|
|
|
values.put(IconDB.COLUMN_USER, userSerial);
|
|
|
|
|
values.put(IconDB.COLUMN_LAST_UPDATED, info.lastUpdateTime);
|
|
|
|
|
values.put(IconDB.COLUMN_VERSION, info.versionCode);
|
|
|
|
|
mIconDb.getWritableDatabase().insertWithOnConflict(IconDB.TABLE_NAME, null, values,
|
|
|
|
|
SQLiteDatabase.CONFLICT_REPLACE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ContentValues updateCacheAndGetContentValues(LauncherActivityInfoCompat app) {
|
|
|
|
|
CacheEntry entry = new CacheEntry();
|
|
|
|
|
entry.icon = Utilities.createIconBitmap(app.getBadgedIcon(mIconDpi), mContext);
|
|
|
|
|
entry.title = app.getLabel();
|
|
|
|
|
entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, app.getUser());
|
|
|
|
|
mCache.put(new CacheKey(app.getComponentName(), app.getUser()), entry);
|
|
|
|
|
|
|
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
|
values.put(IconDB.COLUMN_ICON, ItemInfo.flattenBitmap(entry.icon));
|
|
|
|
|
values.put(IconDB.COLUMN_LABEL, entry.title.toString());
|
|
|
|
|
return values;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2010-02-08 13:44:00 -08:00
|
|
|
/**
|
|
|
|
|
* Empty out the cache.
|
|
|
|
|
*/
|
2014-10-16 14:07:29 -07:00
|
|
|
public synchronized void flush() {
|
|
|
|
|
mCache.clear();
|
2010-02-08 13:44:00 -08:00
|
|
|
}
|
|
|
|
|
|
2013-10-14 17:03:04 -07:00
|
|
|
/**
|
|
|
|
|
* Empty out the cache that aren't of the correct grid size
|
|
|
|
|
*/
|
2014-10-16 14:07:29 -07:00
|
|
|
public synchronized void flushInvalidIcons(DeviceProfile grid) {
|
|
|
|
|
Iterator<Entry<CacheKey, CacheEntry>> it = mCache.entrySet().iterator();
|
|
|
|
|
while (it.hasNext()) {
|
|
|
|
|
final CacheEntry e = it.next().getValue();
|
|
|
|
|
if ((e.icon != null) && (e.icon.getWidth() < grid.iconSizePx
|
|
|
|
|
|| e.icon.getHeight() < grid.iconSizePx)) {
|
|
|
|
|
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."
|
|
|
|
|
*/
|
2015-02-18 16:46:50 -08:00
|
|
|
public synchronized void getTitleAndIcon(AppInfo application, LauncherActivityInfoCompat info) {
|
|
|
|
|
CacheEntry entry = cacheLocked(application.componentName, info, info.getUser(), false);
|
2010-02-08 13:44:00 -08:00
|
|
|
|
2014-10-16 14:07:29 -07:00
|
|
|
application.title = entry.title;
|
|
|
|
|
application.iconBitmap = entry.icon;
|
|
|
|
|
application.contentDescription = entry.contentDescription;
|
2014-02-10 12:16:54 -05:00
|
|
|
}
|
|
|
|
|
|
2014-10-16 14:07:29 -07:00
|
|
|
public synchronized Bitmap getIcon(Intent intent, UserHandleCompat user) {
|
|
|
|
|
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) {
|
|
|
|
|
return getDefaultIcon(user);
|
2010-05-04 12:12:41 -07:00
|
|
|
}
|
2014-10-16 14:07:29 -07:00
|
|
|
|
|
|
|
|
LauncherActivityInfoCompat launcherActInfo = mLauncherApps.resolveActivity(intent, user);
|
2015-02-18 16:46:50 -08:00
|
|
|
CacheEntry entry = cacheLocked(component, launcherActInfo, user, true);
|
2014-10-16 14:07:29 -07:00
|
|
|
return entry.icon;
|
2010-02-08 13:44:00 -08:00
|
|
|
}
|
|
|
|
|
|
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 intent}. If the
|
|
|
|
|
* corresponding activity is not found, it reverts to the package icon.
|
2014-08-29 17:20:55 -07:00
|
|
|
*/
|
2014-10-16 14:07:29 -07:00
|
|
|
public synchronized void getTitleAndIcon(ShortcutInfo shortcutInfo, Intent intent,
|
2015-02-18 16:46:50 -08:00
|
|
|
UserHandleCompat user) {
|
2014-10-16 14:07:29 -07:00
|
|
|
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 {
|
2015-02-18 16:46:50 -08:00
|
|
|
LauncherActivityInfoCompat info = mLauncherApps.resolveActivity(intent, user);
|
|
|
|
|
getTitleAndIcon(shortcutInfo, component, info, user, true);
|
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}
|
|
|
|
|
*/
|
|
|
|
|
public synchronized void getTitleAndIcon(
|
|
|
|
|
ShortcutInfo shortcutInfo, ComponentName component, LauncherActivityInfoCompat info,
|
|
|
|
|
UserHandleCompat user, boolean usePkgIcon) {
|
|
|
|
|
CacheEntry entry = cacheLocked(component, info, user, usePkgIcon);
|
|
|
|
|
shortcutInfo.setIcon(entry.icon);
|
|
|
|
|
shortcutInfo.title = entry.title;
|
|
|
|
|
shortcutInfo.usingFallbackIcon = isDefaultIcon(entry.icon, user);
|
|
|
|
|
}
|
2014-08-29 17:20:55 -07:00
|
|
|
|
2014-10-16 14:07:29 -07:00
|
|
|
public synchronized Bitmap getDefaultIcon(UserHandleCompat user) {
|
2014-04-30 03:02:21 +01:00
|
|
|
if (!mDefaultIcons.containsKey(user)) {
|
|
|
|
|
mDefaultIcons.put(user, makeDefaultIcon(user));
|
|
|
|
|
}
|
|
|
|
|
return mDefaultIcons.get(user);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean isDefaultIcon(Bitmap icon, UserHandleCompat user) {
|
|
|
|
|
return mDefaultIcons.get(user) == icon;
|
2010-08-30 18:30:15 -07:00
|
|
|
}
|
|
|
|
|
|
2014-10-16 14:07:29 -07:00
|
|
|
/**
|
|
|
|
|
* Retrieves the entry from the cache. If the entry is not present, it creates a new entry.
|
|
|
|
|
* This method is not thread safe, it must be called from a synchronized method.
|
|
|
|
|
*/
|
2014-04-30 03:02:21 +01:00
|
|
|
private CacheEntry cacheLocked(ComponentName componentName, LauncherActivityInfoCompat info,
|
2015-02-18 16:46:50 -08:00
|
|
|
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
|
|
|
|
2015-02-18 16:46:50 -08:00
|
|
|
// Check the DB first.
|
|
|
|
|
if (!getEntryFromDB(componentName, user, entry)) {
|
|
|
|
|
if (info != null) {
|
|
|
|
|
entry.icon = Utilities.createIconBitmap(info.getBadgedIcon(mIconDpi), mContext);
|
2014-02-10 12:16:54 -05:00
|
|
|
} 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;
|
2015-02-18 16:46:50 -08:00
|
|
|
entry.contentDescription = packageEntry.contentDescription;
|
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
|
|
|
}
|
|
|
|
|
}
|
2015-02-18 16:46:50 -08:00
|
|
|
|
|
|
|
|
if (TextUtils.isEmpty(entry.title) && info != null) {
|
|
|
|
|
entry.title = info.getLabel().toString();
|
|
|
|
|
entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, user);
|
|
|
|
|
}
|
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.
|
|
|
|
|
*/
|
2014-10-16 14:07:29 -07:00
|
|
|
public synchronized void cachePackageInstallInfo(String packageName, UserHandleCompat user,
|
2014-08-29 17:20:55 -07:00
|
|
|
Bitmap icon, CharSequence title) {
|
2015-02-18 16:46:50 -08:00
|
|
|
removeFromMemCacheLocked(packageName, user);
|
2014-09-18 13:25:15 -07:00
|
|
|
|
2014-08-29 17:20:55 -07:00
|
|
|
CacheEntry entry = getEntryForPackage(packageName, user);
|
|
|
|
|
if (!TextUtils.isEmpty(title)) {
|
|
|
|
|
entry.title = title;
|
|
|
|
|
}
|
|
|
|
|
if (icon != null) {
|
2014-10-07 12:01:58 -07:00
|
|
|
entry.icon = Utilities.createIconBitmap(icon, mContext);
|
2014-08-29 17:20:55 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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.
|
2014-10-16 14:07:29 -07:00
|
|
|
* This method is not thread safe, it must be called from a synchronized method.
|
2014-08-11 17:05:23 -07:00
|
|
|
*/
|
|
|
|
|
private CacheEntry getEntryForPackage(String packageName, UserHandleCompat user) {
|
2014-10-16 14:07:29 -07:00
|
|
|
ComponentName cn = new ComponentName(packageName, EMPTY_CLASS_NAME);;
|
2014-08-11 17:05:23 -07:00
|
|
|
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 = "";
|
2015-02-18 16:46:50 -08:00
|
|
|
entry.contentDescription = "";
|
2014-08-11 17:05:23 -07:00
|
|
|
mCache.put(cacheKey, entry);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
ApplicationInfo info = mPackageManager.getApplicationInfo(packageName, 0);
|
|
|
|
|
entry.icon = Utilities.createIconBitmap(info.loadIcon(mPackageManager), mContext);
|
2015-02-18 16:46:50 -08:00
|
|
|
entry.title = info.loadLabel(mPackageManager);
|
|
|
|
|
entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, user);
|
2014-08-11 17:05:23 -07:00
|
|
|
} catch (NameNotFoundException e) {
|
|
|
|
|
if (DEBUG) Log.d(TAG, "Application not installed " + packageName);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return entry;
|
|
|
|
|
}
|
|
|
|
|
|
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 componentName the icon should be returned for this component
|
|
|
|
|
* @param icon the icon to be persisted
|
|
|
|
|
* @param dpi the native density of the icon
|
|
|
|
|
*/
|
2015-02-18 16:46:50 -08:00
|
|
|
public void preloadIcon(ComponentName componentName, Bitmap icon, int dpi, String label,
|
|
|
|
|
long userSerial) {
|
2014-02-10 12:16:54 -05:00
|
|
|
// TODO rescale to the correct native DPI
|
|
|
|
|
try {
|
2015-02-18 16:46:50 -08:00
|
|
|
PackageManager packageManager = mContext.getPackageManager();
|
2014-02-10 12:16:54 -05:00
|
|
|
packageManager.getActivityIcon(componentName);
|
|
|
|
|
// component is present on the system already, do nothing
|
|
|
|
|
return;
|
|
|
|
|
} catch (PackageManager.NameNotFoundException e) {
|
|
|
|
|
// pass
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-18 16:46:50 -08:00
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
|
values.put(IconDB.COLUMN_COMPONENT, componentName.flattenToString());
|
|
|
|
|
values.put(IconDB.COLUMN_USER, userSerial);
|
|
|
|
|
values.put(IconDB.COLUMN_ICON, ItemInfo.flattenBitmap(icon));
|
|
|
|
|
values.put(IconDB.COLUMN_LABEL, label);
|
|
|
|
|
mIconDb.getWritableDatabase().insertWithOnConflict(IconDB.TABLE_NAME, null, values,
|
|
|
|
|
SQLiteDatabase.CONFLICT_REPLACE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private boolean getEntryFromDB(ComponentName component, UserHandleCompat user, CacheEntry entry) {
|
|
|
|
|
Cursor c = mIconDb.getReadableDatabase().query(IconDB.TABLE_NAME,
|
|
|
|
|
new String[] {IconDB.COLUMN_ICON, IconDB.COLUMN_LABEL},
|
|
|
|
|
IconDB.COLUMN_COMPONENT + " = ? AND " + IconDB.COLUMN_USER + " = ?",
|
|
|
|
|
new String[] {component.flattenToString(),
|
|
|
|
|
Long.toString(mUserManager.getSerialNumberForUser(user))},
|
|
|
|
|
null, null, null);
|
2014-02-10 12:16:54 -05:00
|
|
|
try {
|
2015-02-18 16:46:50 -08:00
|
|
|
if (c.moveToNext()) {
|
|
|
|
|
entry.icon = Utilities.createIconBitmap(c, 0, mContext);
|
|
|
|
|
entry.title = c.getString(1);
|
|
|
|
|
if (entry.title == null) {
|
|
|
|
|
entry.title = "";
|
|
|
|
|
entry.contentDescription = "";
|
|
|
|
|
} else {
|
|
|
|
|
entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, user);
|
2014-02-10 12:16:54 -05:00
|
|
|
}
|
2015-02-18 16:46:50 -08:00
|
|
|
return true;
|
2014-02-10 12:16:54 -05:00
|
|
|
}
|
2015-02-18 16:46:50 -08:00
|
|
|
} finally {
|
|
|
|
|
c.close();
|
2014-02-10 12:16:54 -05:00
|
|
|
}
|
2015-02-18 16:46:50 -08:00
|
|
|
return false;
|
2014-02-10 12:16:54 -05:00
|
|
|
}
|
|
|
|
|
|
2015-02-18 16:46:50 -08:00
|
|
|
private static final class IconDB extends SQLiteOpenHelper {
|
|
|
|
|
private final static int DB_VERSION = 1;
|
2014-02-10 12:16:54 -05:00
|
|
|
|
2015-02-18 16:46:50 -08:00
|
|
|
private final static String TABLE_NAME = "icons";
|
|
|
|
|
private final static String COLUMN_ROWID = "rowid";
|
|
|
|
|
private final static String COLUMN_COMPONENT = "componentName";
|
|
|
|
|
private final static String COLUMN_USER = "profileId";
|
|
|
|
|
private final static String COLUMN_LAST_UPDATED = "lastUpdated";
|
|
|
|
|
private final static String COLUMN_VERSION = "version";
|
|
|
|
|
private final static String COLUMN_ICON = "icon";
|
|
|
|
|
private final static String COLUMN_LABEL = "label";
|
|
|
|
|
|
|
|
|
|
public IconDB(Context context) {
|
|
|
|
|
super(context, LauncherFiles.APP_ICONS_DB, null, DB_VERSION);
|
2014-04-30 03:02:21 +01:00
|
|
|
}
|
|
|
|
|
|
2015-02-18 16:46:50 -08:00
|
|
|
@Override
|
|
|
|
|
public void onCreate(SQLiteDatabase db) {
|
|
|
|
|
db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (" +
|
|
|
|
|
COLUMN_COMPONENT + " TEXT NOT NULL, " +
|
|
|
|
|
COLUMN_USER + " INTEGER NOT NULL, " +
|
|
|
|
|
COLUMN_LAST_UPDATED + " INTEGER NOT NULL DEFAULT 0, " +
|
|
|
|
|
COLUMN_VERSION + " INTEGER NOT NULL DEFAULT 0, " +
|
|
|
|
|
COLUMN_ICON + " BLOB, " +
|
|
|
|
|
COLUMN_LABEL + " TEXT, " +
|
|
|
|
|
"PRIMARY KEY (" + COLUMN_COMPONENT + ", " + COLUMN_USER + ") " +
|
|
|
|
|
");");
|
2014-02-10 12:16:54 -05:00
|
|
|
}
|
|
|
|
|
|
2015-02-18 16:46:50 -08:00
|
|
|
@Override
|
|
|
|
|
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
|
|
|
|
if (oldVersion != newVersion) {
|
|
|
|
|
clearDB(db);
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-02-10 12:16:54 -05:00
|
|
|
|
2015-02-18 16:46:50 -08:00
|
|
|
@Override
|
|
|
|
|
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
|
|
|
|
if (oldVersion != newVersion) {
|
|
|
|
|
clearDB(db);
|
|
|
|
|
}
|
2014-02-10 12:16:54 -05:00
|
|
|
}
|
|
|
|
|
|
2015-02-18 16:46:50 -08:00
|
|
|
private void clearDB(SQLiteDatabase db) {
|
|
|
|
|
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
|
|
|
|
|
onCreate(db);
|
|
|
|
|
}
|
2014-02-10 12:16:54 -05:00
|
|
|
}
|
2010-02-08 13:44:00 -08:00
|
|
|
}
|