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