Home | History | Annotate | Download | only in plugins
      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.systemui.plugins;
     16 
     17 import android.content.ComponentName;
     18 import android.content.Context;
     19 import android.content.SharedPreferences;
     20 import android.content.pm.PackageManager;
     21 
     22 import com.android.internal.annotations.VisibleForTesting;
     23 import com.android.systemui.shared.plugins.PluginEnabler;
     24 
     25 public class PluginEnablerImpl implements PluginEnabler {
     26     private static final String CRASH_DISABLED_PLUGINS_PREF_FILE = "auto_disabled_plugins_prefs";
     27 
     28     private PackageManager mPm;
     29     private final SharedPreferences mAutoDisabledPrefs;
     30 
     31     public PluginEnablerImpl(Context context) {
     32         this(context, context.getPackageManager());
     33     }
     34 
     35     @VisibleForTesting public PluginEnablerImpl(Context context, PackageManager pm) {
     36         mAutoDisabledPrefs = context.getSharedPreferences(
     37                 CRASH_DISABLED_PLUGINS_PREF_FILE, Context.MODE_PRIVATE);
     38         mPm = pm;
     39     }
     40 
     41     @Override
     42     public void setEnabled(ComponentName component) {
     43         setDisabled(component, ENABLED);
     44     }
     45 
     46     @Override
     47     public void setDisabled(ComponentName component, @DisableReason int reason) {
     48         boolean enabled = reason == ENABLED;
     49         final int desiredState = enabled ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
     50                 : PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
     51         mPm.setComponentEnabledSetting(component, desiredState, PackageManager.DONT_KILL_APP);
     52         if (enabled) {
     53             mAutoDisabledPrefs.edit().remove(component.flattenToString()).apply();
     54         } else {
     55             mAutoDisabledPrefs.edit().putInt(component.flattenToString(), reason).apply();
     56         }
     57     }
     58 
     59     @Override
     60     public boolean isEnabled(ComponentName component) {
     61         return mPm.getComponentEnabledSetting(component)
     62                 != PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
     63     }
     64 
     65     @Override
     66     public @DisableReason int getDisableReason(ComponentName componentName) {
     67         if (isEnabled(componentName)) {
     68             return ENABLED;
     69         }
     70         return mAutoDisabledPrefs.getInt(componentName.flattenToString(), DISABLED_MANUALLY);
     71     }
     72 }
     73