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 isTunerEnabled(Context context)73 public static final boolean isTunerEnabled(Context context) { 74 return userContext(context).getPackageManager().getComponentEnabledSetting( 75 new ComponentName(context, TunerActivity.class)) 76 == PackageManager.COMPONENT_ENABLED_STATE_ENABLED; 77 } 78 79 public static class ClearReceiver extends BroadcastReceiver { 80 @Override onReceive(Context context, Intent intent)81 public void onReceive(Context context, Intent intent) { 82 if (ACTION_CLEAR.equals(intent.getAction())) { 83 Dependency.get(TunerService.class).clearAll(); 84 } 85 } 86 } 87 showResetRequest(final Context context, final Runnable onDisabled)88 public static final void showResetRequest(final Context context, final Runnable onDisabled) { 89 SystemUIDialog dialog = new SystemUIDialog(context); 90 dialog.setShowForAllUsers(true); 91 dialog.setMessage(R.string.remove_from_settings_prompt); 92 dialog.setButton(DialogInterface.BUTTON_NEGATIVE, context.getString(R.string.cancel), 93 (OnClickListener) null); 94 dialog.setButton(DialogInterface.BUTTON_POSITIVE, 95 context.getString(R.string.guest_exit_guest_dialog_remove), new OnClickListener() { 96 @Override 97 public void onClick(DialogInterface dialog, int which) { 98 // Tell the tuner (in main SysUI process) to clear all its settings. 99 context.sendBroadcast(new Intent(TunerService.ACTION_CLEAR)); 100 // Disable access to tuner. 101 TunerService.setTunerEnabled(context, false); 102 // Make them sit through the warning dialog again. 103 Settings.Secure.putInt(context.getContentResolver(), 104 TunerFragment.SETTING_SEEN_TUNER_WARNING, 0); 105 if (onDisabled != null) { 106 onDisabled.run(); 107 } 108 } 109 }); 110 dialog.show(); 111 } 112 } 113