TunerService.java revision 72def0de65d964836c16a68933b34543f742b3e2
1/* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License 15 */ 16package com.android.systemui.tuner; 17 18import android.app.ActivityManager; 19import android.content.BroadcastReceiver; 20import android.content.ComponentName; 21import android.content.ContentResolver; 22import android.content.Context; 23import android.content.DialogInterface; 24import android.content.DialogInterface.OnClickListener; 25import android.content.Intent; 26import android.content.pm.PackageManager; 27import android.content.pm.PackageManager.NameNotFoundException; 28import android.database.ContentObserver; 29import android.net.Uri; 30import android.os.Handler; 31import android.os.Looper; 32import android.os.UserHandle; 33import android.provider.Settings; 34import android.util.ArrayMap; 35 36import com.android.systemui.BatteryMeterDrawable; 37import com.android.systemui.DemoMode; 38import com.android.systemui.R; 39import com.android.systemui.SystemUI; 40import com.android.systemui.SystemUIApplication; 41import com.android.systemui.settings.CurrentUserTracker; 42import com.android.systemui.statusbar.phone.SystemUIDialog; 43 44import java.util.ArrayList; 45import java.util.HashMap; 46import java.util.List; 47 48 49public class TunerService extends SystemUI { 50 51 public static final String ACTION_CLEAR = "com.android.systemui.action.CLEAR_TUNER"; 52 53 private final Observer mObserver = new Observer(); 54 // Map of Uris we listen on to their settings keys. 55 private final ArrayMap<Uri, String> mListeningUris = new ArrayMap<>(); 56 // Map of settings keys to the listener. 57 private final HashMap<String, List<Tunable>> mTunableLookup = new HashMap<>(); 58 59 private ContentResolver mContentResolver; 60 private int mCurrentUser; 61 private CurrentUserTracker mUserTracker; 62 63 @Override 64 public void start() { 65 mContentResolver = mContext.getContentResolver(); 66 putComponent(TunerService.class, this); 67 68 mCurrentUser = ActivityManager.getCurrentUser(); 69 mUserTracker = new CurrentUserTracker(mContext) { 70 @Override 71 public void onUserSwitched(int newUserId) { 72 mCurrentUser = newUserId; 73 reloadAll(); 74 reregisterAll(); 75 } 76 }; 77 mUserTracker.startTracking(); 78 } 79 80 public void addTunable(Tunable tunable, String... keys) { 81 for (String key : keys) { 82 addTunable(tunable, key); 83 } 84 } 85 86 private void addTunable(Tunable tunable, String key) { 87 if (!mTunableLookup.containsKey(key)) { 88 mTunableLookup.put(key, new ArrayList<Tunable>()); 89 } 90 mTunableLookup.get(key).add(tunable); 91 Uri uri = Settings.Secure.getUriFor(key); 92 if (!mListeningUris.containsKey(uri)) { 93 mListeningUris.put(uri, key); 94 mContentResolver.registerContentObserver(uri, false, mObserver, mCurrentUser); 95 } 96 // Send the first state. 97 String value = Settings.Secure.getStringForUser(mContentResolver, key, mCurrentUser); 98 tunable.onTuningChanged(key, value); 99 } 100 101 public void removeTunable(Tunable tunable) { 102 for (List<Tunable> list : mTunableLookup.values()) { 103 list.remove(tunable); 104 } 105 } 106 107 protected void reregisterAll() { 108 if (mListeningUris.size() == 0) { 109 return; 110 } 111 mContentResolver.unregisterContentObserver(mObserver); 112 for (Uri uri : mListeningUris.keySet()) { 113 mContentResolver.registerContentObserver(uri, false, mObserver, mCurrentUser); 114 } 115 } 116 117 public void reloadSetting(Uri uri) { 118 String key = mListeningUris.get(uri); 119 List<Tunable> tunables = mTunableLookup.get(key); 120 if (tunables == null) { 121 return; 122 } 123 String value = Settings.Secure.getStringForUser(mContentResolver, key, mCurrentUser); 124 for (Tunable tunable : tunables) { 125 tunable.onTuningChanged(key, value); 126 } 127 } 128 129 private void reloadAll() { 130 for (String key : mTunableLookup.keySet()) { 131 String value = Settings.Secure.getStringForUser(mContentResolver, key, 132 mCurrentUser); 133 for (Tunable tunable : mTunableLookup.get(key)) { 134 tunable.onTuningChanged(key, value); 135 } 136 } 137 } 138 139 public void clearAll() { 140 // A couple special cases. 141 Settings.Global.putString(mContentResolver, DemoMode.DEMO_MODE_ALLOWED, null); 142 Settings.System.putString(mContentResolver, BatteryMeterDrawable.SHOW_PERCENT_SETTING, null); 143 Intent intent = new Intent(DemoMode.ACTION_DEMO); 144 intent.putExtra(DemoMode.EXTRA_COMMAND, DemoMode.COMMAND_EXIT); 145 mContext.sendBroadcast(intent); 146 147 for (String key : mTunableLookup.keySet()) { 148 Settings.Secure.putString(mContentResolver, key, null); 149 } 150 } 151 152 // Only used in other processes, such as the tuner. 153 private static TunerService sInstance; 154 155 public static TunerService get(Context context) { 156 SystemUIApplication sysUi = (SystemUIApplication) context.getApplicationContext(); 157 TunerService service = sysUi.getComponent(TunerService.class); 158 if (service == null) { 159 // Can't get it as a component, must in the tuner, lets just create one for now. 160 return getStaticService(context); 161 } 162 return service; 163 } 164 165 private static TunerService getStaticService(Context context) { 166 if (sInstance == null) { 167 sInstance = new TunerService(); 168 sInstance.mContext = context.getApplicationContext(); 169 sInstance.mComponents = new HashMap<>(); 170 sInstance.start(); 171 } 172 return sInstance; 173 } 174 175 public static final void showResetRequest(final Context context, final Runnable onDisabled) { 176 SystemUIDialog dialog = new SystemUIDialog(context); 177 dialog.setShowForAllUsers(true); 178 dialog.setMessage(R.string.remove_from_settings_prompt); 179 dialog.setButton(DialogInterface.BUTTON_NEGATIVE, context.getString(R.string.cancel), 180 (OnClickListener) null); 181 dialog.setButton(DialogInterface.BUTTON_POSITIVE, 182 context.getString(R.string.guest_exit_guest_dialog_remove), new OnClickListener() { 183 @Override 184 public void onClick(DialogInterface dialog, int which) { 185 // Tell the tuner (in main SysUI process) to clear all its settings. 186 context.sendBroadcast(new Intent(TunerService.ACTION_CLEAR)); 187 // Disable access to tuner. 188 TunerService.setTunerEnabled(context, false); 189 // Make them sit through the warning dialog again. 190 Settings.Secure.putInt(context.getContentResolver(), 191 TunerFragment.SETTING_SEEN_TUNER_WARNING, 0); 192 if (onDisabled != null) { 193 onDisabled.run(); 194 } 195 } 196 }); 197 dialog.show(); 198 } 199 200 public static final void setTunerEnabled(Context context, boolean enabled) { 201 userContext(context).getPackageManager().setComponentEnabledSetting( 202 new ComponentName(context, TunerActivity.class), 203 enabled ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED 204 : PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 205 PackageManager.DONT_KILL_APP); 206 } 207 208 public static final boolean isTunerEnabled(Context context) { 209 return userContext(context).getPackageManager().getComponentEnabledSetting( 210 new ComponentName(context, TunerActivity.class)) 211 == PackageManager.COMPONENT_ENABLED_STATE_ENABLED; 212 } 213 214 private static Context userContext(Context context) { 215 try { 216 return context.createPackageContextAsUser(context.getPackageName(), 0, 217 new UserHandle(ActivityManager.getCurrentUser())); 218 } catch (NameNotFoundException e) { 219 return context; 220 } 221 } 222 223 private class Observer extends ContentObserver { 224 public Observer() { 225 super(new Handler(Looper.getMainLooper())); 226 } 227 228 @Override 229 public void onChange(boolean selfChange, Uri uri, int userId) { 230 if (userId == ActivityManager.getCurrentUser()) { 231 reloadSetting(uri); 232 } 233 } 234 } 235 236 public interface Tunable { 237 void onTuningChanged(String key, String newValue); 238 } 239 240 public static class ClearReceiver extends BroadcastReceiver { 241 @Override 242 public void onReceive(Context context, Intent intent) { 243 if (ACTION_CLEAR.equals(intent.getAction())) { 244 get(context).clearAll(); 245 } 246 } 247 } 248} 249