1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5  * except in compliance with the License. You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the
10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11  * KIND, either express or implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */
14 
15 package com.android.launcher3.uioverrides.plugins;
16 
17 import android.content.ComponentName;
18 import android.content.Context;
19 import android.content.SharedPreferences;
20 
21 import com.android.launcher3.Utilities;
22 import com.android.systemui.shared.plugins.PluginEnabler;
23 
24 import androidx.preference.PreferenceDataStore;
25 
26 public class PluginEnablerImpl extends PreferenceDataStore implements PluginEnabler {
27 
28     private static final String PREFIX_PLUGIN_ENABLED = "PLUGIN_ENABLED_";
29 
30     final private SharedPreferences mSharedPrefs;
31 
PluginEnablerImpl(Context context)32     public PluginEnablerImpl(Context context) {
33         mSharedPrefs = Utilities.getDevicePrefs(context);
34     }
35 
36     @Override
setEnabled(ComponentName component)37     public void setEnabled(ComponentName component) {
38         setState(component, true);
39     }
40 
41     @Override
setDisabled(ComponentName component, int reason)42     public void setDisabled(ComponentName component, int reason) {
43         setState(component, reason == ENABLED);
44     }
45 
setState(ComponentName component, boolean enabled)46     private void setState(ComponentName component, boolean enabled) {
47         putBoolean(pluginEnabledKey(component), enabled);
48     }
49 
50     @Override
isEnabled(ComponentName component)51     public boolean isEnabled(ComponentName component) {
52         return getBoolean(pluginEnabledKey(component), true);
53     }
54 
55     @Override
getDisableReason(ComponentName componentName)56     public int getDisableReason(ComponentName componentName) {
57         return isEnabled(componentName) ? ENABLED : DISABLED_MANUALLY;
58     }
59 
60     @Override
putBoolean(String key, boolean value)61     public void putBoolean(String key, boolean value) {
62         mSharedPrefs.edit().putBoolean(key, value).apply();
63     }
64 
65     @Override
getBoolean(String key, boolean defValue)66     public boolean getBoolean(String key, boolean defValue) {
67         return mSharedPrefs.getBoolean(key, defValue);
68     }
69 
pluginEnabledKey(ComponentName cn)70     static String pluginEnabledKey(ComponentName cn) {
71         return PREFIX_PLUGIN_ENABLED + cn.flattenToString();
72     }
73 }
74