Android 客製化桌面圖標

使用的是SearchLauncher。java

一、在SearchLauncher下面添加assets/icon目錄,放入要替換的圖標,而且命名爲包名,例如:com.android.settings.pngandroid

二、添加Launcher3/res/values/arrays.xmlsql

<?xml version="1.0" encoding="utf-8"?>`
<resources>
    <string-array translatable="false" name="custom_app_icon_support">
        <item>com.android.dialer</item>
        <item>com.android.settings</item>
    </string-array>
</resources>
複製代碼

三、Launcher3/src/com/android/launcher3/IconCache.java :bash

import android.content.pm.PackageInfo;
 import android.content.pm.PackageManager;
 import android.content.pm.PackageManager.NameNotFoundException;
 +import android.content.res.AssetManager;
 import android.content.res.Resources;
 import android.database.Cursor;
 import android.database.sqlite.SQLiteDatabase;
 import android.database.sqlite.SQLiteException;
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
+import android.graphics.drawable.BitmapDrawable;
 import android.graphics.drawable.Drawable;
 import android.os.Build.VERSION;
 import android.os.Handler;
 import com.android.launcher3.util.SQLiteCacheHelper;
 import com.android.launcher3.util.Thunk;
 
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Arrays;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashSet;
@@ -380,8 +385,16 @@ public class IconCache {
         if (entry == null) {
             entry = new CacheEntry();
             LauncherIcons li = LauncherIcons.obtain(mContext);
+            Drawable icon = getThemeIconForAppComp(app.getComponentName(),app.getUser());
+            if (icon != null) {
+
+                 li.createBadgedIconBitmap(icon, app.getUser(),28).applyTo(entry);
+            } else{
+   
             li.createBadgedIconBitmap(getFullResIcon(app), app.getUser(),
                     app.getApplicationInfo().targetSdkVersion).applyTo(entry);
+            }
             li.recycle();
         }
         entry.title = app.getLabel();
@@ -547,9 +560,17 @@ public class IconCache {
                 providerFetchedOnce = true;
 
                 if (info != null) {
                     LauncherIcons li = LauncherIcons.obtain(mContext);
+                    Drawable icon = getThemeIconForAppComp(componentName,info.getUser());
+                    if (icon != null) {
+            
+                        li.createBadgedIconBitmap(icon, info.getUser(),28).applyTo(entry);
+                    }else {
+                  
                     li.createBadgedIconBitmap(getFullResIcon(info), info.getUser(),
                             info.getApplicationInfo().targetSdkVersion).applyTo(entry);
+                    }
                     li.recycle();
                 } else {
                     if (usePackageIcon) {
@@ -647,13 +668,21 @@ public class IconCache {
                     if (appInfo == null) {
                         throw new NameNotFoundException("ApplicationInfo is null");
                     }
-
+                    Drawable customIcon = getThemeIconForAppComp(cacheKey.componentName,user);
+                    BitmapInfo iconInfo;
                     LauncherIcons li = LauncherIcons.obtain(mContext);
+                    if (customIcon != null) {
+                        iconInfo = li.createBadgedIconBitmap(
+                                customIcon, user, appInfo.targetSdkVersion);
+                    } else {
+                   
                     // Load the full res icon for the application, but if useLowResIcon is set, then
                     // only keep the low resolution icon instead of the larger full-sized icon
-                    BitmapInfo iconInfo = li.createBadgedIconBitmap(
+                    iconInfo = li.createBadgedIconBitmap(
                             appInfo.loadIcon(mPackageManager), user, appInfo.targetSdkVersion,
                             mInstantAppResolver.isInstantApp(appInfo));
+                    }
                     li.recycle();
 
                     Bitmap lowResIcon =  generateLowResIcon(iconInfo.icon);
@@ -892,4 +921,92 @@ public class IconCache {
 
         void reapplyItemInfo(ItemInfoWithIcon info);
     }
+
+
+    public boolean isSupportCustomAppIcon(Context context) {
+        boolean isSupport = false;
+        String[] names = context.getResources().
+                getStringArray(R.array.custom_app_icon_support);
+        if (names != null && names.length > 0) {
+            isSupport = true;
+        } else {
+            isSupport = false;
+        }
+
+        return  isSupport;
+    }
+
+    public boolean isSupportCustomAppIcon(Context context, String packages) {
+        boolean isSupport = false;
+        String[] names = context.getResources().
+                getStringArray(R.array.custom_app_icon_support);
+        if (names != null && names.length > 0) {
+            for (int i = 0; i < names.length; i++) {
+                if (packages.equals(names[i])) {
+                    isSupport = true;
+                    break;
+                }
+            }
+        } else {
+            isSupport = false;
+        }
+
+        return  isSupport;
+    }
+
+    private Drawable getThemeIconForAppComp(ComponentName componentName,
+                                            UserHandle user) {
+        Drawable icon;
+
+        icon = readCustomizedIconForAppComp(componentName);
+
+        return icon;
+    }
+
+    public Drawable readCustomizedIconForAppComp(ComponentName cn) {
+        Drawable iconDrawable = null;
+
+        if (iconDrawable == null) {
+            iconDrawable = readDrawableFromAssetForComp(cn, mContext.getAssets(), mContext.getResources());
+        }
+        return iconDrawable;
+    }
+
+    private Drawable readDrawableFromAssetForComp(ComponentName cn, AssetManager assetManager, Resources res) {
+        Bitmap bitmap = readBitmapFromAsset(cn.getPackageName(), assetManager);
+        if (bitmap == null) {
+            bitmap = readBitmapFromAsset(cn.getClassName(), assetManager);
+        }
+
+        if (bitmap == null) {
+            return null;
+        }
+        return new BitmapDrawable(res, bitmap);
+    }
+
+    private Bitmap readBitmapFromAsset(String assetName, AssetManager assetManager) {
+        Bitmap source = null;
+        InputStream is = null;
+        if (!isSupportCustomAppIcon(mContext, assetName)) {
+            return null;
+        }
+        try {
+            is = assetManager.open("icon/" + assetName + ".png");
+            source = BitmapFactory.decodeStream(is);
+            return source;
+        } catch (IOException e) {
+            //if (true) Log.d(TAG, "readAppIconFromAsset fail : " + assetName);
+            source = null;
+        } finally {
+            if (is != null) {
+                try {
+                    is.close();
+                } catch (IOException e) {
+                }
+            }
+        }
+        return source;
+    }
+    //*/

複製代碼
相關文章
相關標籤/搜索