• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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.tuner;
16 
17 import android.app.ActivityManager;
18 import android.content.BroadcastReceiver;
19 import android.content.ComponentName;
20 import android.content.Context;
21 import android.content.DialogInterface;
22 import android.content.DialogInterface.OnClickListener;
23 import android.content.Intent;
24 import android.content.pm.PackageManager;
25 import android.content.pm.PackageManager.NameNotFoundException;
26 import android.os.UserHandle;
27 import android.provider.Settings;
28 
29 import static android.provider.Settings.System.SHOW_BATTERY_PERCENT;
30 import com.android.systemui.DemoMode;
31 import com.android.systemui.Dependency;
32 import com.android.systemui.R;
33 import com.android.systemui.statusbar.phone.SystemUIDialog;
34 
35 public abstract class TunerService {
36 
37     public static final String ACTION_CLEAR = "com.android.systemui.action.CLEAR_TUNER";
38 
clearAll()39     public abstract void clearAll();
destroy()40     public abstract void destroy();
41 
getValue(String setting)42     public abstract String getValue(String setting);
getValue(String setting, int def)43     public abstract int getValue(String setting, int def);
getValue(String setting, String def)44     public abstract String getValue(String setting, String def);
45 
setValue(String setting, String value)46     public abstract void setValue(String setting, String value);
setValue(String setting, int value)47     public abstract void setValue(String setting, int value);
48 
addTunable(Tunable tunable, String... keys)49     public abstract void addTunable(Tunable tunable, String... keys);
removeTunable(Tunable tunable)50     public abstract void removeTunable(Tunable tunable);
51 
52     public interface Tunable {
onTuningChanged(String key, String newValue)53         void onTuningChanged(String key, String newValue);
54     }
55 
userContext(Context context)56     private static Context userContext(Context context) {
57         try {
58             return context.createPackageContextAsUser(context.getPackageName(), 0,
59                     new UserHandle(ActivityManager.getCurrentUser()));
60         } catch (NameNotFoundException e) {
61             return context;
62         }
63     }
64 
setTunerEnabled(Context context, boolean enabled)65     public static final void setTunerEnabled(Context context, boolean enabled) {
66         userContext(context).getPackageManager().setComponentEnabledSetting(
67                 new ComponentName(context, TunerActivity.class),
68                 enabled ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
69                         : PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
70                 PackageManager.DONT_KILL_APP);
71 
72         userContext(context).getPackageManager().setComponentEnabledSetting(
73                 new ComponentName(context, TunerActivity.ACTIVITY_ALIAS_NAME),
74                 enabled ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
75                         : PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
76                 PackageManager.DONT_KILL_APP);
77     }
78 
isTunerEnabled(Context context)79     public static final boolean isTunerEnabled(Context context) {
80         return userContext(context).getPackageManager().getComponentEnabledSetting(
81                 new ComponentName(context, TunerActivity.class))
82                 == PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
83     }
84 
85     public static class ClearReceiver extends BroadcastReceiver {
86         @Override
onReceive(Context context, Intent intent)87         public void onReceive(Context context, Intent intent) {
88             if (ACTION_CLEAR.equals(intent.getAction())) {
89                 Dependency.get(TunerService.class).clearAll();
90             }
91         }
92     }
93 
showResetRequest(final Context context, final Runnable onDisabled)94     public static final void showResetRequest(final Context context, final Runnable onDisabled) {
95         SystemUIDialog dialog = new SystemUIDialog(context);
96         dialog.setShowForAllUsers(true);
97         dialog.setMessage(R.string.remove_from_settings_prompt);
98         dialog.setButton(DialogInterface.BUTTON_NEGATIVE, context.getString(R.string.cancel),
99                 (OnClickListener) null);
100         dialog.setButton(DialogInterface.BUTTON_POSITIVE,
101                 context.getString(R.string.guest_exit_guest_dialog_remove), new OnClickListener() {
102             @Override
103             public void onClick(DialogInterface dialog, int which) {
104                 // Tell the tuner (in main SysUI process) to clear all its settings.
105                 context.sendBroadcast(new Intent(TunerService.ACTION_CLEAR));
106                 // Disable access to tuner.
107                 TunerService.setTunerEnabled(context, false);
108                 // Make them sit through the warning dialog again.
109                 Settings.Secure.putInt(context.getContentResolver(),
110                         TunerFragment.SETTING_SEEN_TUNER_WARNING, 0);
111                 if (onDisabled != null) {
112                     onDisabled.run();
113                 }
114             }
115         });
116         dialog.show();
117     }
118 }
119