Android P 上Launcher3 中实现两种方式替换桌面图标

1.第一种方案,是通过桌面应用的包名,跟图片的包名进行匹配,获取一个图片的资源ID

    麻烦点就是需要手动修改图片的名称,跟包名一致。

代码:src/com/android/launcher3/IconCache.java

 

    * Adds an entry into the DB and the in-memory cache.
      * @param replaceExisting if true, it will recreate the bitmap even if it already exists in
@@ -401,7 +453,12 @@ public class IconCache {
         entry.title = app.getLabel();
         entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, app.getUser());
         mCache.put(key, entry);
+        int resId = getThemeIconResByClassName(app);
+        if(resId!=0){
+           entry.icon = createCustomBitMap(resId);
+        }else{
+           entry.icon = createCompoundBitmap(entry.icon);
+        }
         Bitmap lowResIcon = generateLowResIcon(entry.icon);
         ContentValues values = newContentValues(entry.icon, lowResIcon, entry.color,
                 entry.title.toString(), app.getApplicationInfo().packageName);
@@ -561,10 +618,16 @@ public class IconCache {
                 providerFetchedOnce = true;
 
                 if (info != null) {
+                    int resId = getThemeIconResByClassName(info);
+                    if(resId!=0){
+                       entry.icon = createCustomBitMap(resId);
+                    }else{
                        LauncherIcons li = LauncherIcons.obtain(mContext);
                        li.createBadgedIconBitmap(getFullResIcon(info), info.getUser(),info.getApplicationInfo().targetSdkVersion).applyTo(entry);
                        li.recycle();
+                       entry.icon = createCompoundBitmap(entry.icon);
+                 }
                 } else {
                     if (usePackageIcon) {
                         CacheEntry packageEn

你可能感兴趣的:(Android,P)