blob: e6ee1864dce2c7737ed420d9a2f8ac5106a8fa82 [file] [log] [blame]
Sunny Goyal7b97eeb2019-11-08 13:43:58 -08001/*
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 */
16package com.android.launcher3.util;
17
18import android.content.Context;
19
20import androidx.annotation.ColorRes;
21import androidx.annotation.DimenRes;
22import androidx.annotation.FractionRes;
23import androidx.annotation.IntegerRes;
24
25import com.android.launcher3.uioverrides.plugins.PluginManagerWrapper;
26import com.android.systemui.plugins.PluginListener;
27import 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 */
37public 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
45 private DynamicResource(Context context) {
46 mContext = context;
47 PluginManagerWrapper.INSTANCE.get(context).addPluginListener(this,
48 ResourceProvider.class, false /* allowedMultiple */);
49 }
50
51 @Override
52 public int getInt(@IntegerRes int resId) {
53 return mContext.getResources().getInteger(resId);
54 }
55
56 @Override
57 public float getFraction(@FractionRes int resId) {
58 return mContext.getResources().getFraction(resId, 1, 1);
59 }
60
61 @Override
62 public float getDimension(@DimenRes int resId) {
63 return mContext.getResources().getDimension(resId);
64 }
65
66 @Override
67 public int getColor(@ColorRes int resId) {
68 return mContext.getResources().getColor(resId, null);
69 }
70
71 @Override
Jon Miranda7edcd782020-03-02 13:18:55 -080072 public float getFloat(@DimenRes int resId) {
73 return mContext.getResources().getFloat(resId);
74 }
75
76 @Override
Sunny Goyal7b97eeb2019-11-08 13:43:58 -080077 public void onPluginConnected(ResourceProvider plugin, Context context) {
78 mPlugin = plugin;
79 }
80
81 @Override
82 public void onPluginDisconnected(ResourceProvider plugin) {
83 mPlugin = null;
84 }
85
86 /**
87 * Returns the currently active or default provider
88 */
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}