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 15package com.android.systemui.tuner; 16 17import android.app.ActivityManager; 18import android.content.BroadcastReceiver; 19import android.content.ComponentName; 20import android.content.Context; 21import android.content.DialogInterface; 22import android.content.DialogInterface.OnClickListener; 23import android.content.Intent; 24import android.content.pm.PackageManager; 25import android.content.pm.PackageManager.NameNotFoundException; 26import android.os.UserHandle; 27import android.provider.Settings; 28 29import static android.provider.Settings.System.SHOW_BATTERY_PERCENT; 30import com.android.systemui.DemoMode; 31import com.android.systemui.Dependency; 32import com.android.systemui.R; 33import com.android.systemui.statusbar.phone.SystemUIDialog; 34 35public abstract class TunerService { 36 37 public static final String ACTION_CLEAR = "com.android.systemui.action.CLEAR_TUNER"; 38 39 public abstract void clearAll(); 40 public abstract void destroy(); 41 42 public abstract String getValue(String setting); 43 public abstract int getValue(String setting, int def); 44 public abstract String getValue(String setting, String def); 45 46 public abstract void setValue(String setting, String value); 47 public abstract void setValue(String setting, int value); 48 49 public abstract void addTunable(Tunable tunable, String... keys); 50 public abstract void removeTunable(Tunable tunable); 51 52 public interface Tunable { 53 void onTuningChanged(String key, String newValue); 54 } 55 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 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 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 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 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