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.LauncherPrefs; 22 import com.android.systemui.shared.plugins.PluginEnabler; 23 24 public class PluginEnablerImpl implements PluginEnabler { 25 26 private static final String PREFIX_PLUGIN_ENABLED = "PLUGIN_ENABLED_"; 27 28 final private SharedPreferences mSharedPrefs; 29 PluginEnablerImpl(Context context)30 public PluginEnablerImpl(Context context) { 31 mSharedPrefs = LauncherPrefs.getDevicePrefs(context); 32 } 33 34 @Override setEnabled(ComponentName component)35 public void setEnabled(ComponentName component) { 36 setState(component, true); 37 } 38 39 @Override setDisabled(ComponentName component, int reason)40 public void setDisabled(ComponentName component, int reason) { 41 setState(component, reason == ENABLED); 42 } 43 setState(ComponentName component, boolean enabled)44 private void setState(ComponentName component, boolean enabled) { 45 mSharedPrefs.edit().putBoolean(pluginEnabledKey(component), enabled).apply(); 46 } 47 48 @Override isEnabled(ComponentName component)49 public boolean isEnabled(ComponentName component) { 50 return mSharedPrefs.getBoolean(pluginEnabledKey(component), true); 51 } 52 53 @Override getDisableReason(ComponentName componentName)54 public int getDisableReason(ComponentName componentName) { 55 return isEnabled(componentName) ? ENABLED : DISABLED_MANUALLY; 56 } 57 pluginEnabledKey(ComponentName cn)58 private static String pluginEnabledKey(ComponentName cn) { 59 return PREFIX_PLUGIN_ENABLED + cn.flattenToString(); 60 } 61 } 62