Launcher3中IconCache很明显是用于应用Icon缓存。
在Launcher3的Java文件IconCacher.java:
/**
* Fill in "application" with the icon and label for "info."
*/
public void getTitleAndIcon(AppInfo application, ResolveInfo info,
HashMap<Object, CharSequence> labelCache) {
synchronized (mCache) {
CacheEntry entry = cacheLocked(application.componentName, info,
labelCache);
application.title = entry.title;
application.iconBitmap = entry.icon;
}
}
该函数就是获取应用的title和icon。
CacheEntry也就是:
private static class CacheEntry {
public Bitmap icon;
public String title;
}
该类对象中只保存应用的Icon和title。
cacheLocked()函数就是:
private CacheEntry cacheLocked(ComponentName componentName,
ResolveInfo info, HashMap<Object, CharSequence> labelCache) {
CacheEntry entry = mCache.get(componentName); // 从缓存中获取对象
if (entry == null) {
entry = new CacheEntry();
mCache.put(componentName, entry);
if (info != null) {
ComponentName key = LauncherModel
.getComponentNameFromResolveInfo(info);
if (labelCache != null && labelCache.containsKey(key)) {
entry.title = labelCache.get(key).toString();
} else {
entry.title = info.loadLabel(mPackageManager).toString();
if (labelCache != null) {
labelCache.put(key, entry.title);
}
}
if (entry.title == null) {
entry.title = info.activityInfo.name;
}
switch (model) {
case 0:
entry.icon = Utilities.createIconBitmap(
getFullResIcon(info), mContext);
break;
case 1:
entry.icon = Utilities.createFramedPhoto2(themeBg1_new, //圆形图标
getFullResIcon(info));
break;
case 2:
entry.icon = Utilities.createFramedPhoto2(themeBg2_new,
getFullResIcon(info));
break;
case 3:
entry.icon = Utilities.createFramedPhoto3(themeBg3_new, //方形图标
getFullResIcon(info));
break;
case 4:
entry.icon = Utilities.createFramedPhoto2(themeBg4_new, getFullResIcon(info));
break;
case 5:
entry.icon = Utilities.createFramedPhoto3(themeBg5_new, getFullResIcon(info));
break;
case 6:
entry.icon = Utilities.createFramedPhoto2(themeBg6_new, getFullResIcon(info));
break;
case 7:
entry.icon = Utilities.createFramedPhoto2(themeBg7_new, getFullResIcon(info));
break;
case 8:
entry.icon = Utilities.createFramedPhoto3(themeBg8_new, getFullResIcon(info));
break;
case 9:
entry.icon = Utilities.createFramedPhoto2(themeBg9_new, getFullResIcon(info));
break;
case 10:
entry.icon = Utilities.createFramedPhoto2(themeBg10_new, getFullResIcon(info));
break;
case 11:
entry.icon = Utilities.createFramedPhoto2(themeBg11_new, getFullResIcon(info));
break;
// case 1:
// Bitmap iconOld = Utilities.createIconBitmap(
// getFullResIcon(info), mContext);
// int width = iconOld.getWidth();
// int height = iconOld.getHeight();
// Bitmap icon = iconOld.createScaledBitmap(iconOld, width,
// height, true);
// int bgcolor = Utilities.getBackgroundColor(icon); // 获取背景颜色
// int outerRadiusRat = 12;
// entry.icon = Utilities.createFramedPhoto(icon,
// outerRadiusRat, bgcolor);
// break;
// case 2: // 圆圈图片
// entry.icon = Utilities.createFramedPhoto2(themeBg2,
// getFullResIcon(info));
// break;
// case 3: // 方框图片
// entry.icon = Utilities.createFramedPhoto3(themeBg3,
// getFullResIcon(info));
// break;
default:
entry.icon = Utilities.createIconBitmap(
getFullResIcon(info), mContext);
break;
}
} else {
entry.title = "";
Bitmap preloaded = getPreloadedIcon(componentName);
if (preloaded != null) {
if (DEBUG)
Log.d(TAG,
"using preloaded icon for "
+ componentName.toShortString());
entry.icon = preloaded;
} else {
if (DEBUG)
Log.d(TAG,
"using default icon for "
+ componentName.toShortString());
entry.icon = mDefaultIcon;
}
}
}
return entry;
}
首先从我们的缓存中获取,如果有就直接返回,如果没有就去获取。获取title也一样,先从缓存中获取,如果有就使用,如果没有就从应用的信息中获取。
entry.icon = Utilities.createIconBitmap(getFullResIcon(info), mContext);
Launcher原来的自带的获取Icon的方法,
entry.icon = Utilities.createFramedPhoto2(themeBg1_new,getFullResIcon(info));//圆形图标
我们自己修改的获取出来的Icon的方法,这样Launcher中的Icon图标就是我们希望修改的图标。该方法定义在Utilities.java的文件中。
最后,在我们点击修改样式的时候,调用重新启动Launcher的方法,Launcher将会重新加载,应用图标也就会重新加载。
ActivityManager am = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
am.restartPackage("com.android.nfc");
在Launcher3的Java文件IconCacher.java:
/**
* Fill in "application" with the icon and label for "info."
*/
public void getTitleAndIcon(AppInfo application, ResolveInfo info,
HashMap<Object, CharSequence> labelCache) {
synchronized (mCache) {
CacheEntry entry = cacheLocked(application.componentName, info,
labelCache);
application.title = entry.title;
application.iconBitmap = entry.icon;
}
}
该函数就是获取应用的title和icon。
CacheEntry也就是:
private static class CacheEntry {
public Bitmap icon;
public String title;
}
该类对象中只保存应用的Icon和title。
cacheLocked()函数就是:
private CacheEntry cacheLocked(ComponentName componentName,
ResolveInfo info, HashMap<Object, CharSequence> labelCache) {
CacheEntry entry = mCache.get(componentName); // 从缓存中获取对象
if (entry == null) {
entry = new CacheEntry();
mCache.put(componentName, entry);
if (info != null) {
ComponentName key = LauncherModel
.getComponentNameFromResolveInfo(info);
if (labelCache != null && labelCache.containsKey(key)) {
entry.title = labelCache.get(key).toString();
} else {
entry.title = info.loadLabel(mPackageManager).toString();
if (labelCache != null) {
labelCache.put(key, entry.title);
}
}
if (entry.title == null) {
entry.title = info.activityInfo.name;
}
switch (model) {
case 0:
entry.icon = Utilities.createIconBitmap(
getFullResIcon(info), mContext);
break;
case 1:
entry.icon = Utilities.createFramedPhoto2(themeBg1_new, //圆形图标
getFullResIcon(info));
break;
case 2:
entry.icon = Utilities.createFramedPhoto2(themeBg2_new,
getFullResIcon(info));
break;
case 3:
entry.icon = Utilities.createFramedPhoto3(themeBg3_new, //方形图标
getFullResIcon(info));
break;
case 4:
entry.icon = Utilities.createFramedPhoto2(themeBg4_new, getFullResIcon(info));
break;
case 5:
entry.icon = Utilities.createFramedPhoto3(themeBg5_new, getFullResIcon(info));
break;
case 6:
entry.icon = Utilities.createFramedPhoto2(themeBg6_new, getFullResIcon(info));
break;
case 7:
entry.icon = Utilities.createFramedPhoto2(themeBg7_new, getFullResIcon(info));
break;
case 8:
entry.icon = Utilities.createFramedPhoto3(themeBg8_new, getFullResIcon(info));
break;
case 9:
entry.icon = Utilities.createFramedPhoto2(themeBg9_new, getFullResIcon(info));
break;
case 10:
entry.icon = Utilities.createFramedPhoto2(themeBg10_new, getFullResIcon(info));
break;
case 11:
entry.icon = Utilities.createFramedPhoto2(themeBg11_new, getFullResIcon(info));
break;
// case 1:
// Bitmap iconOld = Utilities.createIconBitmap(
// getFullResIcon(info), mContext);
// int width = iconOld.getWidth();
// int height = iconOld.getHeight();
// Bitmap icon = iconOld.createScaledBitmap(iconOld, width,
// height, true);
// int bgcolor = Utilities.getBackgroundColor(icon); // 获取背景颜色
// int outerRadiusRat = 12;
// entry.icon = Utilities.createFramedPhoto(icon,
// outerRadiusRat, bgcolor);
// break;
// case 2: // 圆圈图片
// entry.icon = Utilities.createFramedPhoto2(themeBg2,
// getFullResIcon(info));
// break;
// case 3: // 方框图片
// entry.icon = Utilities.createFramedPhoto3(themeBg3,
// getFullResIcon(info));
// break;
default:
entry.icon = Utilities.createIconBitmap(
getFullResIcon(info), mContext);
break;
}
} else {
entry.title = "";
Bitmap preloaded = getPreloadedIcon(componentName);
if (preloaded != null) {
if (DEBUG)
Log.d(TAG,
"using preloaded icon for "
+ componentName.toShortString());
entry.icon = preloaded;
} else {
if (DEBUG)
Log.d(TAG,
"using default icon for "
+ componentName.toShortString());
entry.icon = mDefaultIcon;
}
}
}
return entry;
}
首先从我们的缓存中获取,如果有就直接返回,如果没有就去获取。获取title也一样,先从缓存中获取,如果有就使用,如果没有就从应用的信息中获取。
entry.icon = Utilities.createIconBitmap(getFullResIcon(info), mContext);
Launcher原来的自带的获取Icon的方法,
entry.icon = Utilities.createFramedPhoto2(themeBg1_new,getFullResIcon(info));//圆形图标
我们自己修改的获取出来的Icon的方法,这样Launcher中的Icon图标就是我们希望修改的图标。该方法定义在Utilities.java的文件中。
最后,在我们点击修改样式的时候,调用重新启动Launcher的方法,Launcher将会重新加载,应用图标也就会重新加载。
ActivityManager am = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
am.restartPackage("com.android.nfc");