android Launcher3中定制第三方apk图标,实现类似主题功能

最近遇到在Launcher中对于指定的app需使用使用指定的图标,实现类似于主题的功能。要实现这样的功能就要弄清楚Launcher是如何加载app图标的。

首先定位到com.android.launcher3.IconCache.java这个类的cacheLocked()方法

private CacheEntry cacheLocked(ComponentName componentName, LauncherActivityInfoCompat info,
            HashMap labelCache, UserHandleCompat user, boolean usePackageIcon) {
        CacheKey cacheKey = new CacheKey(componentName, user);
        CacheEntry entry = mCache.get(cacheKey);
        if (entry == null) {
            entry = new CacheEntry();
        .......
                entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, user);
                //用于初始化应用图标
                entry.icon = Utilities.createIconBitmap(
                        info.getBadgedIcon(mIconDpi), mContext);
            } else {
        ......
        return entry;
    }

首先通过通过info.getBadgedIcon(mIconDpi)来获取应用原始launcher图标的Drawable,mIconDpi为图图片资源id,然后通过Utilities.createIconBitmap(Drawable icon, Context context)方法来对图标进行简单的处理(要修改所有Launcher图标的背景等,可以在此方法中修改)。

其中info.getBadgedIcon(mIconDpi)就是决定应用图标的方法。该方法最终会进入com.Android.launcher3.compat.LauncherActivityInfoCompatV16.Java类的getBadgedIcon()–>getIcon();通过Resources来获取图标的。 我采用的方法,首先自定义一个类,LauncherIconTheme.java,该类的内容如下

package com.android.launcher3;

import com.android.launcher3.R;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.Log;

public final  class LauncherIconTheme {
    //google邮箱
    private static int GMAIL = R.drawable.ic_launcher_gmail;
    //google地图
    private static int GOOGLE_MAPS = R.drawable.ic_launcher_googlemaps;
    //自定义的应用资源id
    ...................

    private static String TAG = "LauncherIconTheme";

        //根据包名、类名获取Bitmap
    public static Bitmap  getIconBitmap(Context context , String packageName , String className) {
        Resources resources = context.getResources();
        int iconId = getIconId(packageName, className);
        if (iconId != -1){
            return BitmapFactory.decodeResource(resources, iconId);
        }
        return null;
    }
        //根据包名、类名获取Drawable   要用到的就是这个方法
    public static Drawable getIconDrawable(Context context , String packageName , String className) {
        Resources resources = context.getResources();
        int iconId = getIconId(packageName, className);
        if ( iconId != -1) {
            return resources.getDrawable(iconId);
        }
        return null;
    }
        //根据包名、类名获取资源定义的图标资源id
    private static int getIconId(String packageName , String className){
        if ( "com.google.android.gm".equals(packageName)
                && "com.google.android.gm.ConversationListActivityGmail".equals(className)) {
            return GMAIL;

        }else if ("com.google.android.apps.maps".equals(packageName)
                && "com.google.android.maps.MapsActivity".equals(className)) {
            return GOOGLE_MAPS;

        }else if ....................
            .................................
        }else{
            return -1;          
        }
    }   
}

然后在IconCache的cacheLocked方法中

 /**
     * 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.
     */
    private CacheEntry cacheLocked(ComponentName componentName, LauncherActivityInfoCompat info,
            HashMap labelCache, UserHandleCompat user, boolean usePackageIcon) {
        CacheKey cacheKey = new CacheKey(componentName, user);
        CacheEntry entry = mCache.get(cacheKey);
        if (entry == null) {
           .................................

                entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, user);
                //实现修改图标的逻辑
                Drawable themeDrawable = LauncherIconTheme
                        .getIconDrawable(mContext,
                                componentName.getPackageName(), componentName.getClassName());
                if ( themeDrawable != null ) {
                    entry.icon = Utilities.createIconBitmap(
                            themeDrawable, mContext);
                }else{
                    entry.icon = Utilities.createIconBitmap(
                            info.getBadgedIcon(mIconDpi), mContext);                    
                }
            } else {
             ...........................................
        return entry;
    }

你可能感兴趣的:(android Launcher3中定制第三方apk图标,实现类似主题功能)