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
|
|
|
|
|
|
|
|
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;
|
2015-03-11 16:56:52 -07: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;
|
2015-03-11 16:56:52 -07:00
|
|
|
import android.os.Handler;
|
2015-05-20 21:57:06 -07:00
|
|
|
import android.os.SystemClock;
|
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;
|
2015-05-21 13:04:53 -07:00
|
|
|
import com.android.launcher3.model.PackageItemInfo;
|
2015-03-16 14:10:24 -07:00
|
|
|
import com.android.launcher3.util.ComponentKey;
|
2015-03-18 14:16:05 -07:00
|
|
|
import com.android.launcher3.util.Thunk;
|
2014-04-30 03:02:21 +01:00
|
|
|
|
2010-02-08 13:44:00 -08:00
|
|
|
import java.util.HashMap;
|
2014-02-10 12:16:54 -05:00
|
|
|
import java.util.HashSet;
|
2015-02-18 16:46:50 -08:00
|
|
|
import java.util.List;
|
2015-04-01 16:04:21 -07:00
|
|
|
import java.util.Locale;
|
2015-05-15 16:59:36 -07:00
|
|
|
import java.util.Stack;
|
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
|
|
|
|
2015-03-11 16:56:52 -07:00
|
|
|
private static final int LOW_RES_SCALE_FACTOR = 8;
|
|
|
|
|
|
2015-06-02 09:38:28 -07:00
|
|
|
@Thunk static final Object ICON_UPDATE_TOKEN = new Object();
|
2015-05-20 21:57:06 -07:00
|
|
|
|
2015-03-18 14:16:05 -07:00
|
|
|
@Thunk static class CacheEntry {
|
2010-02-08 13:44:00 -08:00
|
|
|
public Bitmap icon;
|
2015-05-28 12:53:44 -07:00
|
|
|
public CharSequence title = "";
|
|
|
|
|
public CharSequence contentDescription = "";
|
2015-03-11 16:56:52 -07:00
|
|
|
public boolean isLowResIcon;
|
2010-02-08 13:44:00 -08:00
|
|
|
}
|
|
|
|
|
|
2015-03-18 10:07:49 -07:00
|
|
|
private final HashMap<UserHandleCompat, Bitmap> mDefaultIcons = new HashMap<>();
|
2015-06-02 09:38:28 -07:00
|
|
|
@Thunk final MainThreadExecutor mMainThreadExecutor = new MainThreadExecutor();
|
2015-03-18 10:07:49 -07:00
|
|
|
|
2013-06-11 14:45:48 -04:00
|
|
|
private final Context mContext;
|
2010-03-15 14:44:42 -07:00
|
|
|
private final PackageManager mPackageManager;
|
2015-06-02 09:38:28 -07:00
|
|
|
@Thunk final UserManagerCompat mUserManager;
|
2014-04-30 03:02:21 +01:00
|
|
|
private final LauncherAppsCompat mLauncherApps;
|
2015-03-16 14:10:24 -07:00
|
|
|
private final HashMap<ComponentKey, CacheEntry> mCache =
|
|
|
|
|
new HashMap<ComponentKey, CacheEntry>(INITIAL_ICON_CACHE_CAPACITY);
|
2015-02-18 16:46:50 -08:00
|
|
|
private final int mIconDpi;
|
2015-06-02 09:38:28 -07:00
|
|
|
@Thunk final IconDB mIconDb;
|
2010-02-08 13:44:00 -08:00
|
|
|
|
2015-06-02 09:38:28 -07:00
|
|
|
@Thunk final Handler mWorkerHandler;
|
2015-03-11 16:56:52 -07:00
|
|
|
|
2015-05-06 11:42:25 -07:00
|
|
|
public IconCache(Context context, InvariantDeviceProfile inv) {
|
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);
|
2015-05-22 12:25:45 -07:00
|
|
|
mIconDpi = inv.fillResIconDpi;
|
2015-02-18 16:46:50 -08:00
|
|
|
mIconDb = new IconDB(context);
|
2015-03-11 16:56:52 -07:00
|
|
|
|
2015-04-16 15:20:51 -07:00
|
|
|
mWorkerHandler = new Handler(LauncherModel.getWorkerLooper());
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
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) {
|
2015-03-16 14:10:24 -07:00
|
|
|
mCache.remove(new ComponentKey(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) {
|
2015-03-16 14:10:24 -07:00
|
|
|
HashSet<ComponentKey> forDeletion = new HashSet<ComponentKey>();
|
|
|
|
|
for (ComponentKey key: mCache.keySet()) {
|
2014-04-30 03:02:21 +01:00
|
|
|
if (key.componentName.getPackageName().equals(packageName)
|
|
|
|
|
&& key.user.equals(user)) {
|
|
|
|
|
forDeletion.add(key);
|
2014-02-10 12:16:54 -05:00
|
|
|
}
|
|
|
|
|
}
|
2015-03-16 14:10:24 -07:00
|
|
|
for (ComponentKey condemned: forDeletion) {
|
2014-04-30 03:02:21 +01:00
|
|
|
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)) {
|
2015-04-06 12:45:40 -07:00
|
|
|
addIconToDBAndMemCache(app, info, userSerial);
|
2015-02-18 16:46:50 -08:00
|
|
|
}
|
|
|
|
|
} 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)});
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-20 21:57:06 -07:00
|
|
|
public void updateDbIcons() {
|
|
|
|
|
// Remove all active icon update tasks.
|
|
|
|
|
mWorkerHandler.removeCallbacksAndMessages(ICON_UPDATE_TOKEN);
|
|
|
|
|
|
2015-06-18 13:56:59 -07:00
|
|
|
mIconDb.updateSystemStateString();
|
2015-05-20 21:57:06 -07:00
|
|
|
for (UserHandleCompat user : mUserManager.getUserProfiles()) {
|
|
|
|
|
// Query for the set of apps
|
|
|
|
|
final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
|
|
|
|
|
// Fail if we don't have any apps
|
|
|
|
|
// TODO: Fix this. Only fail for the current user.
|
|
|
|
|
if (apps == null || apps.isEmpty()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update icon cache. This happens in segments and {@link #onPackageIconsUpdated}
|
|
|
|
|
// is called by the icon cache when the job is complete.
|
|
|
|
|
updateDBIcons(user, apps);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-18 16:46:50 -08:00
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
2015-05-20 21:57:06 -07:00
|
|
|
private void updateDBIcons(UserHandleCompat user, List<LauncherActivityInfoCompat> apps) {
|
2015-02-18 16:46:50 -08:00
|
|
|
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,
|
2015-04-01 16:04:21 -07:00
|
|
|
IconDB.COLUMN_LAST_UPDATED, IconDB.COLUMN_VERSION,
|
|
|
|
|
IconDB.COLUMN_SYSTEM_STATE},
|
2015-02-18 16:46:50 -08:00
|
|
|
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);
|
2015-04-01 16:04:21 -07:00
|
|
|
final int systemStateIndex = c.getColumnIndex(IconDB.COLUMN_SYSTEM_STATE);
|
2015-02-18 16:46:50 -08:00
|
|
|
|
|
|
|
|
HashSet<Integer> itemsToRemove = new HashSet<Integer>();
|
2015-05-20 21:57:06 -07:00
|
|
|
Stack<LauncherActivityInfoCompat> appsToUpdate = new Stack<>();
|
2015-02-18 16:46:50 -08:00
|
|
|
|
|
|
|
|
while (c.moveToNext()) {
|
|
|
|
|
String cn = c.getString(indexComponent);
|
|
|
|
|
ComponentName component = ComponentName.unflattenFromString(cn);
|
|
|
|
|
PackageInfo info = pkgInfoMap.get(component.getPackageName());
|
|
|
|
|
if (info == null) {
|
2015-06-04 15:19:31 -07:00
|
|
|
remove(component, user);
|
2015-02-18 16:46:50 -08:00
|
|
|
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);
|
2015-04-01 16:04:21 -07:00
|
|
|
if (version == info.versionCode && updateTime == info.lastUpdateTime &&
|
|
|
|
|
TextUtils.equals(mIconDb.mSystemState, c.getString(systemStateIndex))) {
|
2015-02-18 16:46:50 -08:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (app == null) {
|
2015-06-04 15:19:31 -07:00
|
|
|
remove(component, user);
|
2015-02-18 16:46:50 -08:00
|
|
|
itemsToRemove.add(c.getInt(rowIndex));
|
2015-05-20 21:57:06 -07:00
|
|
|
} else {
|
|
|
|
|
appsToUpdate.add(app);
|
2015-02-18 16:46:50 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
c.close();
|
|
|
|
|
if (!itemsToRemove.isEmpty()) {
|
|
|
|
|
mIconDb.getWritableDatabase().delete(IconDB.TABLE_NAME,
|
2015-06-10 16:00:42 -07:00
|
|
|
Utilities.createDbSelectionQuery(IconDB.COLUMN_ROWID, itemsToRemove),
|
2015-02-18 16:46:50 -08:00
|
|
|
null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Insert remaining apps.
|
2015-05-20 21:57:06 -07:00
|
|
|
if (!componentMap.isEmpty() || !appsToUpdate.isEmpty()) {
|
|
|
|
|
Stack<LauncherActivityInfoCompat> appsToAdd = new Stack<>();
|
|
|
|
|
appsToAdd.addAll(componentMap.values());
|
|
|
|
|
new SerializedIconUpdateTask(userSerial, pkgInfoMap,
|
|
|
|
|
appsToAdd, appsToUpdate).scheduleNext();
|
2015-02-18 16:46:50 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-02 09:38:28 -07:00
|
|
|
@Thunk void addIconToDBAndMemCache(LauncherActivityInfoCompat app, PackageInfo info,
|
2015-04-06 12:45:40 -07:00
|
|
|
long userSerial) {
|
2015-05-15 16:59:36 -07:00
|
|
|
// Reuse the existing entry if it already exists in the DB. This ensures that we do not
|
|
|
|
|
// create bitmap if it was already created during loader.
|
|
|
|
|
ContentValues values = updateCacheAndGetContentValues(app, false);
|
2015-04-06 12:45:40 -07:00
|
|
|
addIconToDB(values, app.getComponentName(), info, userSerial);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Updates {@param values} to contain versoning information and adds it to the DB.
|
|
|
|
|
* @param values {@link ContentValues} containing icon & title
|
|
|
|
|
*/
|
|
|
|
|
private void addIconToDB(ContentValues values, ComponentName key,
|
|
|
|
|
PackageInfo info, long userSerial) {
|
|
|
|
|
values.put(IconDB.COLUMN_COMPONENT, key.flattenToString());
|
2015-02-18 16:46:50 -08:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-02 09:38:28 -07:00
|
|
|
@Thunk ContentValues updateCacheAndGetContentValues(LauncherActivityInfoCompat app,
|
2015-05-15 16:59:36 -07:00
|
|
|
boolean replaceExisting) {
|
|
|
|
|
final ComponentKey key = new ComponentKey(app.getComponentName(), app.getUser());
|
|
|
|
|
CacheEntry entry = null;
|
|
|
|
|
if (!replaceExisting) {
|
|
|
|
|
entry = mCache.get(key);
|
|
|
|
|
// We can't reuse the entry if the high-res icon is not present.
|
|
|
|
|
if (entry == null || entry.isLowResIcon || entry.icon == null) {
|
|
|
|
|
entry = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (entry == null) {
|
|
|
|
|
entry = new CacheEntry();
|
|
|
|
|
entry.icon = Utilities.createIconBitmap(app.getBadgedIcon(mIconDpi), mContext);
|
|
|
|
|
}
|
2015-02-18 16:46:50 -08:00
|
|
|
entry.title = app.getLabel();
|
|
|
|
|
entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, app.getUser());
|
2015-03-16 14:10:24 -07:00
|
|
|
mCache.put(new ComponentKey(app.getComponentName(), app.getUser()), entry);
|
2015-02-18 16:46:50 -08:00
|
|
|
|
2015-03-11 16:56:52 -07:00
|
|
|
return mIconDb.newContentValues(entry.icon, entry.title.toString());
|
2015-02-18 16:46:50 -08:00
|
|
|
}
|
|
|
|
|
|
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
|
|
|
*/
|
2015-03-11 16:56:52 -07:00
|
|
|
public IconLoadRequest updateIconInBackground(final BubbleTextView caller, final ItemInfo info) {
|
|
|
|
|
Runnable request = new Runnable() {
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
if (info instanceof AppInfo) {
|
|
|
|
|
getTitleAndIcon((AppInfo) info, null, false);
|
|
|
|
|
} else if (info instanceof ShortcutInfo) {
|
|
|
|
|
ShortcutInfo st = (ShortcutInfo) info;
|
|
|
|
|
getTitleAndIcon(st,
|
|
|
|
|
st.promisedIntent != null ? st.promisedIntent : st.intent,
|
|
|
|
|
st.user, false);
|
2015-05-12 11:32:39 -07:00
|
|
|
} else if (info instanceof PackageItemInfo) {
|
|
|
|
|
PackageItemInfo pti = (PackageItemInfo) info;
|
|
|
|
|
getTitleAndIconForApp(pti.packageName, pti.user, false, pti);
|
2015-03-11 16:56:52 -07:00
|
|
|
}
|
2015-03-18 10:07:49 -07:00
|
|
|
mMainThreadExecutor.execute(new Runnable() {
|
2015-03-11 16:56:52 -07:00
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
caller.reapplyItemInfo(info);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
mWorkerHandler.post(request);
|
|
|
|
|
return new IconLoadRequest(request, mWorkerHandler);
|
|
|
|
|
}
|
2010-02-08 13:44:00 -08:00
|
|
|
|
2015-04-16 15:20:51 -07:00
|
|
|
private Bitmap getNonNullIcon(CacheEntry entry, UserHandleCompat user) {
|
|
|
|
|
return entry.icon == null ? getDefaultIcon(user) : entry.icon;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-11 16:56:52 -07:00
|
|
|
/**
|
|
|
|
|
* Fill in "application" with the icon and label for "info."
|
|
|
|
|
*/
|
|
|
|
|
public synchronized void getTitleAndIcon(AppInfo application,
|
|
|
|
|
LauncherActivityInfoCompat info, boolean useLowResIcon) {
|
2015-04-16 15:20:51 -07:00
|
|
|
UserHandleCompat user = info == null ? application.user : info.getUser();
|
|
|
|
|
CacheEntry entry = cacheLocked(application.componentName, info, user,
|
2015-03-11 16:56:52 -07:00
|
|
|
false, useLowResIcon);
|
2015-05-08 17:00:10 -07:00
|
|
|
application.title = Utilities.trim(entry.title);
|
2015-04-16 15:20:51 -07:00
|
|
|
application.iconBitmap = getNonNullIcon(entry, user);
|
2014-10-16 14:07:29 -07:00
|
|
|
application.contentDescription = entry.contentDescription;
|
2015-03-11 16:56:52 -07:00
|
|
|
application.usingLowResIcon = entry.isLowResIcon;
|
2014-02-10 12:16:54 -05: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) {
|
|
|
|
|
CacheEntry entry = cacheLocked(application.componentName, null, application.user,
|
|
|
|
|
false, application.usingLowResIcon);
|
|
|
|
|
if (entry.icon != null && !isDefaultIcon(entry.icon, application.user)) {
|
2015-05-08 17:00:10 -07:00
|
|
|
application.title = Utilities.trim(entry.title);
|
2015-05-06 16:53:21 -07:00
|
|
|
application.iconBitmap = entry.icon;
|
|
|
|
|
application.contentDescription = entry.contentDescription;
|
|
|
|
|
application.usingLowResIcon = entry.isLowResIcon;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-11 16:56:52 -07:00
|
|
|
/**
|
|
|
|
|
* Returns a high res icon for the given intent and user
|
|
|
|
|
*/
|
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-03-11 16:56:52 -07:00
|
|
|
CacheEntry entry = cacheLocked(component, launcherActInfo, user, true, 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-03-11 16:56:52 -07:00
|
|
|
UserHandleCompat user, boolean useLowResIcon) {
|
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;
|
2015-03-11 16:56:52 -07:00
|
|
|
shortcutInfo.usingLowResIcon = false;
|
2014-10-16 14:07:29 -07:00
|
|
|
} else {
|
2015-02-18 16:46:50 -08:00
|
|
|
LauncherActivityInfoCompat info = mLauncherApps.resolveActivity(intent, user);
|
2015-03-11 16:56:52 -07:00
|
|
|
getTitleAndIcon(shortcutInfo, component, info, user, 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}
|
|
|
|
|
*/
|
|
|
|
|
public synchronized void getTitleAndIcon(
|
|
|
|
|
ShortcutInfo shortcutInfo, ComponentName component, LauncherActivityInfoCompat info,
|
2015-03-11 16:56:52 -07:00
|
|
|
UserHandleCompat user, boolean usePkgIcon, boolean useLowResIcon) {
|
|
|
|
|
CacheEntry entry = cacheLocked(component, info, user, usePkgIcon, useLowResIcon);
|
2015-04-16 15:20:51 -07:00
|
|
|
shortcutInfo.setIcon(getNonNullIcon(entry, user));
|
2015-05-08 17:00:10 -07:00
|
|
|
shortcutInfo.title = Utilities.trim(entry.title);
|
2015-02-18 16:46:50 -08:00
|
|
|
shortcutInfo.usingFallbackIcon = isDefaultIcon(entry.icon, user);
|
2015-03-11 16:56:52 -07:00
|
|
|
shortcutInfo.usingLowResIcon = entry.isLowResIcon;
|
2015-02-18 16:46:50 -08:00
|
|
|
}
|
2014-08-29 17:20:55 -07:00
|
|
|
|
2015-04-06 12:45:40 -07:00
|
|
|
/**
|
|
|
|
|
* Fill in {@param appInfo} with the icon and label for {@param packageName}
|
|
|
|
|
*/
|
|
|
|
|
public synchronized void getTitleAndIconForApp(
|
2015-04-08 19:01:34 -07:00
|
|
|
String packageName, UserHandleCompat user, boolean useLowResIcon,
|
|
|
|
|
PackageItemInfo infoOut) {
|
2015-04-06 12:45:40 -07:00
|
|
|
CacheEntry entry = getEntryForPackageLocked(packageName, user, useLowResIcon);
|
2015-04-16 15:20:51 -07:00
|
|
|
infoOut.iconBitmap = getNonNullIcon(entry, user);
|
2015-05-08 17:00:10 -07:00
|
|
|
infoOut.title = Utilities.trim(entry.title);
|
2015-04-08 19:01:34 -07:00
|
|
|
infoOut.usingLowResIcon = entry.isLowResIcon;
|
|
|
|
|
infoOut.contentDescription = entry.contentDescription;
|
2015-04-06 12:45:40 -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-03-11 16:56:52 -07:00
|
|
|
UserHandleCompat user, boolean usePackageIcon, boolean useLowResIcon) {
|
2015-03-16 14:10:24 -07:00
|
|
|
ComponentKey cacheKey = new ComponentKey(componentName, user);
|
2014-04-30 03:02:21 +01:00
|
|
|
CacheEntry entry = mCache.get(cacheKey);
|
2015-03-11 16:56:52 -07:00
|
|
|
if (entry == null || (entry.isLowResIcon && !useLowResIcon)) {
|
2010-02-08 13:44:00 -08:00
|
|
|
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.
|
2015-03-11 16:56:52 -07:00
|
|
|
if (!getEntryFromDB(componentName, user, entry, useLowResIcon)) {
|
2015-02-18 16:46:50 -08:00
|
|
|
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) {
|
2015-04-06 12:45:40 -07:00
|
|
|
CacheEntry packageEntry = getEntryForPackageLocked(
|
|
|
|
|
componentName.getPackageName(), user, false);
|
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) {
|
2015-05-08 17:00:10 -07:00
|
|
|
entry.title = info.getLabel();
|
2015-02-18 16:46:50 -08:00
|
|
|
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
|
|
|
|
2015-04-06 12:45:40 -07:00
|
|
|
CacheEntry entry = getEntryForPackageLocked(packageName, user, false);
|
2014-08-29 17:20:55 -07:00
|
|
|
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.
|
2015-04-06 12:45:40 -07:00
|
|
|
*
|
2014-08-11 17:05:23 -07:00
|
|
|
*/
|
2015-04-06 12:45:40 -07:00
|
|
|
private CacheEntry getEntryForPackageLocked(String packageName, UserHandleCompat user,
|
|
|
|
|
boolean useLowResIcon) {
|
2015-06-04 15:19:31 -07:00
|
|
|
ComponentName cn = new ComponentName(packageName, packageName + EMPTY_CLASS_NAME);
|
2015-03-16 14:10:24 -07:00
|
|
|
ComponentKey cacheKey = new ComponentKey(cn, user);
|
2014-08-11 17:05:23 -07:00
|
|
|
CacheEntry entry = mCache.get(cacheKey);
|
2015-06-04 15:19:31 -07:00
|
|
|
|
2015-04-06 12:45:40 -07:00
|
|
|
if (entry == null || (entry.isLowResIcon && !useLowResIcon)) {
|
2014-08-11 17:05:23 -07:00
|
|
|
entry = new CacheEntry();
|
2015-05-28 12:53:44 -07:00
|
|
|
boolean entryUpdated = true;
|
2014-08-11 17:05:23 -07:00
|
|
|
|
2015-04-06 12:45:40 -07:00
|
|
|
// Check the DB first.
|
|
|
|
|
if (!getEntryFromDB(cn, user, entry, useLowResIcon)) {
|
|
|
|
|
try {
|
|
|
|
|
PackageInfo info = mPackageManager.getPackageInfo(packageName, 0);
|
|
|
|
|
ApplicationInfo appInfo = info.applicationInfo;
|
|
|
|
|
if (appInfo == null) {
|
|
|
|
|
throw new NameNotFoundException("ApplicationInfo is null");
|
|
|
|
|
}
|
|
|
|
|
Drawable drawable = mUserManager.getBadgedDrawableForUser(
|
|
|
|
|
appInfo.loadIcon(mPackageManager), user);
|
|
|
|
|
entry.icon = Utilities.createIconBitmap(drawable, mContext);
|
|
|
|
|
entry.title = appInfo.loadLabel(mPackageManager);
|
|
|
|
|
entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, user);
|
|
|
|
|
entry.isLowResIcon = false;
|
|
|
|
|
|
|
|
|
|
// Add the icon in the DB here, since these do not get written during
|
|
|
|
|
// package updates.
|
|
|
|
|
ContentValues values =
|
|
|
|
|
mIconDb.newContentValues(entry.icon, entry.title.toString());
|
|
|
|
|
addIconToDB(values, cn, info, mUserManager.getSerialNumberForUser(user));
|
|
|
|
|
|
|
|
|
|
} catch (NameNotFoundException e) {
|
|
|
|
|
if (DEBUG) Log.d(TAG, "Application not installed " + packageName);
|
2015-05-28 12:53:44 -07:00
|
|
|
entryUpdated = false;
|
2015-04-06 12:45:40 -07:00
|
|
|
}
|
2014-08-11 17:05:23 -07:00
|
|
|
}
|
2015-05-28 12:53:44 -07:00
|
|
|
|
|
|
|
|
// Only add a filled-out entry to the cache
|
|
|
|
|
if (entryUpdated) {
|
|
|
|
|
mCache.put(cacheKey, entry);
|
|
|
|
|
}
|
2014-08-11 17:05:23 -07:00
|
|
|
}
|
|
|
|
|
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-03-11 16:56:52 -07:00
|
|
|
ContentValues values = mIconDb.newContentValues(icon, label);
|
2015-02-18 16:46:50 -08:00
|
|
|
values.put(IconDB.COLUMN_COMPONENT, componentName.flattenToString());
|
|
|
|
|
values.put(IconDB.COLUMN_USER, userSerial);
|
|
|
|
|
mIconDb.getWritableDatabase().insertWithOnConflict(IconDB.TABLE_NAME, null, values,
|
|
|
|
|
SQLiteDatabase.CONFLICT_REPLACE);
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-11 16:56:52 -07:00
|
|
|
private boolean getEntryFromDB(ComponentName component, UserHandleCompat user,
|
|
|
|
|
CacheEntry entry, boolean lowRes) {
|
2015-02-18 16:46:50 -08:00
|
|
|
Cursor c = mIconDb.getReadableDatabase().query(IconDB.TABLE_NAME,
|
2015-03-11 16:56:52 -07:00
|
|
|
new String[] {lowRes ? IconDB.COLUMN_ICON_LOW_RES : IconDB.COLUMN_ICON,
|
|
|
|
|
IconDB.COLUMN_LABEL},
|
2015-02-18 16:46:50 -08:00
|
|
|
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()) {
|
2015-03-11 16:56:52 -07:00
|
|
|
entry.icon = loadIconNoResize(c, 0);
|
|
|
|
|
entry.isLowResIcon = lowRes;
|
2015-02-18 16:46:50 -08:00
|
|
|
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-03-11 16:56:52 -07:00
|
|
|
public static class IconLoadRequest {
|
|
|
|
|
private final Runnable mRunnable;
|
|
|
|
|
private final Handler mHandler;
|
|
|
|
|
|
|
|
|
|
IconLoadRequest(Runnable runnable, Handler handler) {
|
|
|
|
|
mRunnable = runnable;
|
|
|
|
|
mHandler = handler;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void cancel() {
|
|
|
|
|
mHandler.removeCallbacks(mRunnable);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-15 16:59:36 -07:00
|
|
|
/**
|
2015-05-20 21:57:06 -07:00
|
|
|
* A runnable that updates invalid icons and adds missing icons in the DB for the provided
|
|
|
|
|
* LauncherActivityInfoCompat list. Items are updated/added one at a time, so that the
|
|
|
|
|
* worker thread doesn't get blocked.
|
2015-05-15 16:59:36 -07:00
|
|
|
*/
|
2015-06-02 09:38:28 -07:00
|
|
|
@Thunk class SerializedIconUpdateTask implements Runnable {
|
2015-05-15 16:59:36 -07:00
|
|
|
private final long mUserSerial;
|
|
|
|
|
private final HashMap<String, PackageInfo> mPkgInfoMap;
|
|
|
|
|
private final Stack<LauncherActivityInfoCompat> mAppsToAdd;
|
2015-05-20 21:57:06 -07:00
|
|
|
private final Stack<LauncherActivityInfoCompat> mAppsToUpdate;
|
|
|
|
|
private final HashSet<String> mUpdatedPackages = new HashSet<String>();
|
2015-05-15 16:59:36 -07:00
|
|
|
|
2015-06-02 09:38:28 -07:00
|
|
|
@Thunk SerializedIconUpdateTask(long userSerial, HashMap<String, PackageInfo> pkgInfoMap,
|
2015-05-20 21:57:06 -07:00
|
|
|
Stack<LauncherActivityInfoCompat> appsToAdd,
|
|
|
|
|
Stack<LauncherActivityInfoCompat> appsToUpdate) {
|
2015-05-15 16:59:36 -07:00
|
|
|
mUserSerial = userSerial;
|
|
|
|
|
mPkgInfoMap = pkgInfoMap;
|
2015-05-20 21:57:06 -07:00
|
|
|
mAppsToAdd = appsToAdd;
|
|
|
|
|
mAppsToUpdate = appsToUpdate;
|
2015-05-15 16:59:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
2015-05-20 21:57:06 -07:00
|
|
|
if (!mAppsToUpdate.isEmpty()) {
|
|
|
|
|
LauncherActivityInfoCompat app = mAppsToUpdate.pop();
|
|
|
|
|
String cn = app.getComponentName().flattenToString();
|
|
|
|
|
ContentValues values = updateCacheAndGetContentValues(app, true);
|
|
|
|
|
mIconDb.getWritableDatabase().update(IconDB.TABLE_NAME, values,
|
|
|
|
|
IconDB.COLUMN_COMPONENT + " = ? AND " + IconDB.COLUMN_USER + " = ?",
|
|
|
|
|
new String[] {cn, Long.toString(mUserSerial)});
|
|
|
|
|
mUpdatedPackages.add(app.getComponentName().getPackageName());
|
|
|
|
|
|
|
|
|
|
if (mAppsToUpdate.isEmpty() && !mUpdatedPackages.isEmpty()) {
|
|
|
|
|
// No more app to update. Notify model.
|
|
|
|
|
LauncherAppState.getInstance().getModel().onPackageIconsUpdated(
|
|
|
|
|
mUpdatedPackages, mUserManager.getUserForSerialNumber(mUserSerial));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Let it run one more time.
|
|
|
|
|
scheduleNext();
|
|
|
|
|
} else if (!mAppsToAdd.isEmpty()) {
|
2015-05-15 16:59:36 -07:00
|
|
|
LauncherActivityInfoCompat app = mAppsToAdd.pop();
|
|
|
|
|
PackageInfo info = mPkgInfoMap.get(app.getComponentName().getPackageName());
|
|
|
|
|
if (info != null) {
|
|
|
|
|
synchronized (IconCache.this) {
|
|
|
|
|
addIconToDBAndMemCache(app, info, mUserSerial);
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-05-20 21:57:06 -07:00
|
|
|
|
|
|
|
|
if (!mAppsToAdd.isEmpty()) {
|
|
|
|
|
scheduleNext();
|
|
|
|
|
}
|
2015-05-15 16:59:36 -07:00
|
|
|
}
|
2015-05-20 21:57:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void scheduleNext() {
|
|
|
|
|
mWorkerHandler.postAtTime(this, ICON_UPDATE_TOKEN, SystemClock.uptimeMillis() + 1);
|
2015-05-15 16:59:36 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-18 16:46:50 -08:00
|
|
|
private static final class IconDB extends SQLiteOpenHelper {
|
2015-05-22 12:25:45 -07:00
|
|
|
private final static int DB_VERSION = 5;
|
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";
|
2015-03-11 16:56:52 -07:00
|
|
|
private final static String COLUMN_ICON_LOW_RES = "icon_low_res";
|
2015-02-18 16:46:50 -08:00
|
|
|
private final static String COLUMN_LABEL = "label";
|
2015-04-01 16:04:21 -07:00
|
|
|
private final static String COLUMN_SYSTEM_STATE = "system_state";
|
|
|
|
|
|
|
|
|
|
public String mSystemState;
|
2015-02-18 16:46:50 -08:00
|
|
|
|
|
|
|
|
public IconDB(Context context) {
|
|
|
|
|
super(context, LauncherFiles.APP_ICONS_DB, null, DB_VERSION);
|
2015-06-18 13:56:59 -07:00
|
|
|
updateSystemStateString();
|
2015-04-01 16:04:21 -07:00
|
|
|
}
|
|
|
|
|
|
2015-06-18 13:56:59 -07:00
|
|
|
public void updateSystemStateString() {
|
|
|
|
|
mSystemState = Locale.getDefault().toString();
|
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, " +
|
2015-03-11 16:56:52 -07:00
|
|
|
COLUMN_ICON_LOW_RES + " BLOB, " +
|
2015-02-18 16:46:50 -08:00
|
|
|
COLUMN_LABEL + " TEXT, " +
|
2015-04-01 16:04:21 -07:00
|
|
|
COLUMN_SYSTEM_STATE + " TEXT, " +
|
2015-02-18 16:46:50 -08:00
|
|
|
"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);
|
|
|
|
|
}
|
2015-03-11 16:56:52 -07:00
|
|
|
|
|
|
|
|
public ContentValues newContentValues(Bitmap icon, String label) {
|
|
|
|
|
ContentValues values = new ContentValues();
|
2015-04-01 16:04:21 -07:00
|
|
|
values.put(COLUMN_ICON, Utilities.flattenBitmap(icon));
|
|
|
|
|
values.put(COLUMN_ICON_LOW_RES, Utilities.flattenBitmap(
|
2015-03-11 16:56:52 -07:00
|
|
|
Bitmap.createScaledBitmap(icon,
|
|
|
|
|
icon.getWidth() / LOW_RES_SCALE_FACTOR,
|
|
|
|
|
icon.getHeight() / LOW_RES_SCALE_FACTOR, true)));
|
2015-04-01 16:04:21 -07:00
|
|
|
values.put(COLUMN_LABEL, label);
|
|
|
|
|
values.put(COLUMN_SYSTEM_STATE, mSystemState);
|
2015-03-11 16:56:52 -07:00
|
|
|
return values;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static Bitmap loadIconNoResize(Cursor c, int iconIndex) {
|
|
|
|
|
byte[] data = c.getBlob(iconIndex);
|
|
|
|
|
try {
|
|
|
|
|
return BitmapFactory.decodeByteArray(data, 0, data.length);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2014-02-10 12:16:54 -05:00
|
|
|
}
|
2010-02-08 13:44:00 -08:00
|
|
|
}
|