• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.launcher3.uioverrides.plugins.PluginManagerWrapper;
26 import com.android.systemui.plugins.PluginListener;
27 import com.android.systemui.plugins.ResourceProvider;
28 
29 /**
30  * Utility class to support customizing resource values using plugins
31  *
32  * To load resources, call
33  *    DynamicResource.provider(context).getInt(resId) or any other supported methods
34  *
35  * To allow customization for a particular resource, add them to dynamic_resources.xml
36  */
37 public class DynamicResource implements ResourceProvider, PluginListener<ResourceProvider> {
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
getInt(@ntegerRes int resId)52     public int getInt(@IntegerRes int resId) {
53         return mContext.getResources().getInteger(resId);
54     }
55 
56     @Override
getFraction(@ractionRes int resId)57     public float getFraction(@FractionRes int resId) {
58         return mContext.getResources().getFraction(resId, 1, 1);
59     }
60 
61     @Override
getDimension(@imenRes int resId)62     public float getDimension(@DimenRes int resId) {
63         return mContext.getResources().getDimension(resId);
64     }
65 
66     @Override
getColor(@olorRes int resId)67     public int getColor(@ColorRes int resId) {
68         return mContext.getResources().getColor(resId, null);
69     }
70 
71     @Override
getFloat(@imenRes int resId)72     public float getFloat(@DimenRes int resId) {
73         return mContext.getResources().getFloat(resId);
74     }
75 
76     @Override
onPluginConnected(ResourceProvider plugin, Context context)77     public void onPluginConnected(ResourceProvider plugin, Context context) {
78         mPlugin = plugin;
79     }
80 
81     @Override
onPluginDisconnected(ResourceProvider plugin)82     public void onPluginDisconnected(ResourceProvider plugin) {
83         mPlugin = null;
84     }
85 
86     /**
87      * Returns the currently active or default provider
88      */
provider(Context context)89     public static ResourceProvider provider(Context context) {
90         DynamicResource dr = DynamicResource.INSTANCE.get(context);
91         ResourceProvider plugin = dr.mPlugin;
92         return plugin == null ? dr : plugin;
93     }
94 }
95