1 /* 2 * Copyright (C) 2019 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.util; 17 18 import android.content.Context; 19 20 import androidx.annotation.ColorRes; 21 import androidx.annotation.DimenRes; 22 import androidx.annotation.FractionRes; 23 import androidx.annotation.IntegerRes; 24 25 import com.android.systemui.plugins.PluginListener; 26 import com.android.systemui.plugins.ResourceProvider; 27 28 /** 29 * Utility class to support customizing resource values using plugins 30 * 31 * To load resources, call 32 * DynamicResource.provider(context).getInt(resId) or any other supported methods 33 * 34 * To allow customization for a particular resource, add them to dynamic_resources.xml 35 */ 36 public class DynamicResource implements 37 ResourceProvider, PluginListener<ResourceProvider>, SafeCloseable { 38 39 private static final MainThreadInitializedObject<DynamicResource> INSTANCE = 40 new MainThreadInitializedObject<>(DynamicResource::new); 41 42 private final Context mContext; 43 private ResourceProvider mPlugin; 44 DynamicResource(Context context)45 private DynamicResource(Context context) { 46 mContext = context; 47 PluginManagerWrapper.INSTANCE.get(context).addPluginListener(this, 48 ResourceProvider.class, false /* allowedMultiple */); 49 } 50 51 @Override close()52 public void close() { 53 PluginManagerWrapper.INSTANCE.get(mContext).removePluginListener(this); 54 } 55 56 @Override getInt(@ntegerRes int resId)57 public int getInt(@IntegerRes int resId) { 58 return mContext.getResources().getInteger(resId); 59 } 60 61 @Override getFraction(@ractionRes int resId)62 public float getFraction(@FractionRes int resId) { 63 return mContext.getResources().getFraction(resId, 1, 1); 64 } 65 66 @Override getDimension(@imenRes int resId)67 public float getDimension(@DimenRes int resId) { 68 return mContext.getResources().getDimension(resId); 69 } 70 71 @Override getColor(@olorRes int resId)72 public int getColor(@ColorRes int resId) { 73 return mContext.getResources().getColor(resId, null); 74 } 75 76 @Override getFloat(@imenRes int resId)77 public float getFloat(@DimenRes int resId) { 78 return mContext.getResources().getFloat(resId); 79 } 80 81 @Override onPluginConnected(ResourceProvider plugin, Context context)82 public void onPluginConnected(ResourceProvider plugin, Context context) { 83 mPlugin = plugin; 84 } 85 86 @Override onPluginDisconnected(ResourceProvider plugin)87 public void onPluginDisconnected(ResourceProvider plugin) { 88 mPlugin = null; 89 } 90 91 /** 92 * Returns the currently active or default provider 93 */ provider(Context context)94 public static ResourceProvider provider(Context context) { 95 DynamicResource dr = DynamicResource.INSTANCE.get(context); 96 ResourceProvider plugin = dr.mPlugin; 97 return plugin == null ? dr : plugin; 98 } 99 } 100