The order of initialization for IconShape is closely tied to ThemeManager which makes it difficult for change listeners to work. Merging these keeps all the theming information at one place Bug: 381897614 Test: Updated tests Flag: EXEMPT refactor Change-Id: Ie29efa20c3308a24be3e284c1c93ed7444b68d58
140 lines
5.1 KiB
Java
140 lines
5.1 KiB
Java
/*
|
|
* Copyright (C) 2022 The Android Open Source Project
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
package com.android.launcher3.icons;
|
|
|
|
import android.content.Context;
|
|
import android.content.pm.ApplicationInfo;
|
|
import android.content.res.Resources;
|
|
import android.content.res.XmlResourceParser;
|
|
import android.graphics.drawable.AdaptiveIconDrawable;
|
|
import android.graphics.drawable.Drawable;
|
|
import android.text.TextUtils;
|
|
import android.util.ArrayMap;
|
|
import android.util.Log;
|
|
|
|
import androidx.annotation.NonNull;
|
|
import androidx.annotation.Nullable;
|
|
|
|
import com.android.launcher3.R;
|
|
import com.android.launcher3.config.FeatureFlags;
|
|
import com.android.launcher3.graphics.ShapeDelegate;
|
|
import com.android.launcher3.graphics.ThemeManager;
|
|
import com.android.launcher3.util.ApiWrapper;
|
|
|
|
import org.xmlpull.v1.XmlPullParser;
|
|
|
|
import java.util.Collections;
|
|
import java.util.Map;
|
|
|
|
/**
|
|
* Extension of {@link IconProvider} with support for overriding theme icons
|
|
*/
|
|
public class LauncherIconProvider extends IconProvider {
|
|
|
|
private static final String TAG_ICON = "icon";
|
|
private static final String ATTR_PACKAGE = "package";
|
|
private static final String ATTR_DRAWABLE = "drawable";
|
|
|
|
private static final String TAG = "LIconProvider";
|
|
private static final Map<String, ThemeData> DISABLED_MAP = Collections.emptyMap();
|
|
|
|
private Map<String, ThemeData> mThemedIconMap;
|
|
|
|
private final ApiWrapper mApiWrapper;
|
|
private final ThemeManager mThemeManager;
|
|
|
|
public LauncherIconProvider(Context context) {
|
|
super(context);
|
|
mThemeManager = ThemeManager.INSTANCE.get(context);
|
|
mApiWrapper = ApiWrapper.INSTANCE.get(context);
|
|
setIconThemeSupported(mThemeManager.isMonoThemeEnabled());
|
|
}
|
|
|
|
/**
|
|
* Enables or disables icon theme support
|
|
*/
|
|
public void setIconThemeSupported(boolean isSupported) {
|
|
mThemedIconMap = isSupported && FeatureFlags.USE_LOCAL_ICON_OVERRIDES.get()
|
|
? null : DISABLED_MAP;
|
|
}
|
|
|
|
@Override
|
|
protected ThemeData getThemeDataForPackage(String packageName) {
|
|
return getThemedIconMap().get(packageName);
|
|
}
|
|
|
|
@Override
|
|
public void updateSystemState() {
|
|
super.updateSystemState();
|
|
mSystemState += "," + ThemeManager.INSTANCE.get(mContext).getIconState().toUniqueId();
|
|
}
|
|
|
|
@Override
|
|
protected String getApplicationInfoHash(@NonNull ApplicationInfo appInfo) {
|
|
return mApiWrapper.getApplicationInfoHash(appInfo);
|
|
}
|
|
|
|
@Nullable
|
|
@Override
|
|
protected Drawable loadAppInfoIcon(ApplicationInfo info, Resources resources, int density) {
|
|
// Tries to load the round icon res, if the app defines it as an adaptive icon
|
|
if (mThemeManager.getIconShape() instanceof ShapeDelegate.Circle) {
|
|
int roundIconRes = mApiWrapper.getRoundIconRes(info);
|
|
if (roundIconRes != 0 && roundIconRes != info.icon) {
|
|
try {
|
|
Drawable d = resources.getDrawableForDensity(roundIconRes, density);
|
|
if (d instanceof AdaptiveIconDrawable) {
|
|
return d;
|
|
}
|
|
} catch (Resources.NotFoundException exc) { }
|
|
}
|
|
}
|
|
return super.loadAppInfoIcon(info, resources, density);
|
|
}
|
|
|
|
private Map<String, ThemeData> getThemedIconMap() {
|
|
if (mThemedIconMap != null) {
|
|
return mThemedIconMap;
|
|
}
|
|
ArrayMap<String, ThemeData> map = new ArrayMap<>();
|
|
Resources res = mContext.getResources();
|
|
try (XmlResourceParser parser = res.getXml(R.xml.grayscale_icon_map)) {
|
|
final int depth = parser.getDepth();
|
|
int type;
|
|
while ((type = parser.next()) != XmlPullParser.START_TAG
|
|
&& type != XmlPullParser.END_DOCUMENT);
|
|
|
|
while (((type = parser.next()) != XmlPullParser.END_TAG
|
|
|| parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
|
|
if (type != XmlPullParser.START_TAG) {
|
|
continue;
|
|
}
|
|
if (TAG_ICON.equals(parser.getName())) {
|
|
String pkg = parser.getAttributeValue(null, ATTR_PACKAGE);
|
|
int iconId = parser.getAttributeResourceValue(null, ATTR_DRAWABLE, 0);
|
|
if (iconId != 0 && !TextUtils.isEmpty(pkg)) {
|
|
map.put(pkg, new ThemeData(res, iconId));
|
|
}
|
|
}
|
|
}
|
|
} catch (Exception e) {
|
|
Log.e(TAG, "Unable to parse icon map", e);
|
|
}
|
|
mThemedIconMap = map;
|
|
return mThemedIconMap;
|
|
}
|
|
}
|