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