1 /* 2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.launcher3.icons; 17 18 import android.content.Context; 19 import android.content.res.Resources; 20 import android.content.res.XmlResourceParser; 21 import android.text.TextUtils; 22 import android.util.ArrayMap; 23 import android.util.Log; 24 25 import com.android.launcher3.R; 26 import com.android.launcher3.config.FeatureFlags; 27 import com.android.launcher3.util.Themes; 28 29 import org.xmlpull.v1.XmlPullParser; 30 31 import java.util.Collections; 32 import java.util.Map; 33 34 /** 35 * Extension of {@link IconProvider} with support for overriding theme icons 36 */ 37 public class LauncherIconProvider extends IconProvider { 38 39 private static final String TAG_ICON = "icon"; 40 private static final String ATTR_PACKAGE = "package"; 41 private static final String ATTR_DRAWABLE = "drawable"; 42 43 private static final String TAG = "LIconProvider"; 44 private static final Map<String, ThemeData> DISABLED_MAP = Collections.emptyMap(); 45 46 private Map<String, ThemeData> mThemedIconMap; 47 private boolean mSupportsIconTheme; 48 LauncherIconProvider(Context context)49 public LauncherIconProvider(Context context) { 50 super(context); 51 setIconThemeSupported(Themes.isThemedIconEnabled(context)); 52 } 53 54 /** 55 * Enables or disables icon theme support 56 */ setIconThemeSupported(boolean isSupported)57 public void setIconThemeSupported(boolean isSupported) { 58 mSupportsIconTheme = isSupported; 59 mThemedIconMap = isSupported && FeatureFlags.USE_LOCAL_ICON_OVERRIDES.get() 60 ? null : DISABLED_MAP; 61 } 62 63 @Override getThemeDataForPackage(String packageName)64 protected ThemeData getThemeDataForPackage(String packageName) { 65 return getThemedIconMap().get(packageName); 66 } 67 68 @Override getSystemIconState()69 public String getSystemIconState() { 70 return super.getSystemIconState() + (mSupportsIconTheme ? ",with-theme" : ",no-theme"); 71 } 72 getThemedIconMap()73 private Map<String, ThemeData> getThemedIconMap() { 74 if (mThemedIconMap != null) { 75 return mThemedIconMap; 76 } 77 ArrayMap<String, ThemeData> map = new ArrayMap<>(); 78 Resources res = mContext.getResources(); 79 try (XmlResourceParser parser = res.getXml(R.xml.grayscale_icon_map)) { 80 final int depth = parser.getDepth(); 81 int type; 82 while ((type = parser.next()) != XmlPullParser.START_TAG 83 && type != XmlPullParser.END_DOCUMENT); 84 85 while (((type = parser.next()) != XmlPullParser.END_TAG 86 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) { 87 if (type != XmlPullParser.START_TAG) { 88 continue; 89 } 90 if (TAG_ICON.equals(parser.getName())) { 91 String pkg = parser.getAttributeValue(null, ATTR_PACKAGE); 92 int iconId = parser.getAttributeResourceValue(null, ATTR_DRAWABLE, 0); 93 if (iconId != 0 && !TextUtils.isEmpty(pkg)) { 94 map.put(pkg, new ThemeData(res, iconId)); 95 } 96 } 97 } 98 } catch (Exception e) { 99 Log.e(TAG, "Unable to parse icon map", e); 100 } 101 mThemedIconMap = map; 102 return mThemedIconMap; 103 } 104 } 105