Jason Monk | 5e74517 | 2015-06-02 19:14:44 -0400 | [diff] [blame] | 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 | */ |
| 16 | package com.android.systemui.tuner; |
| 17 | |
| 18 | import android.app.ActivityManager; |
Jason Monk | 431ad73 | 2015-07-16 08:58:15 -0400 | [diff] [blame] | 19 | import android.content.BroadcastReceiver; |
| 20 | import android.content.ComponentName; |
Jason Monk | 5e74517 | 2015-06-02 19:14:44 -0400 | [diff] [blame] | 21 | import android.content.ContentResolver; |
| 22 | import android.content.Context; |
Jason Monk | 431ad73 | 2015-07-16 08:58:15 -0400 | [diff] [blame] | 23 | import android.content.DialogInterface; |
| 24 | import android.content.DialogInterface.OnClickListener; |
| 25 | import android.content.Intent; |
| 26 | import android.content.pm.PackageManager; |
Jason Monk | 540542b | 2015-08-11 14:18:14 -0400 | [diff] [blame] | 27 | import android.content.pm.PackageManager.NameNotFoundException; |
Jason Monk | 5e74517 | 2015-06-02 19:14:44 -0400 | [diff] [blame] | 28 | import android.database.ContentObserver; |
| 29 | import android.net.Uri; |
| 30 | import android.os.Handler; |
| 31 | import android.os.Looper; |
Jason Monk | 540542b | 2015-08-11 14:18:14 -0400 | [diff] [blame] | 32 | import android.os.UserHandle; |
Jason Monk | 5e74517 | 2015-06-02 19:14:44 -0400 | [diff] [blame] | 33 | import android.provider.Settings; |
| 34 | import android.util.ArrayMap; |
Jason Monk | 51c444b | 2016-01-06 16:32:29 -0500 | [diff] [blame] | 35 | import android.util.ArraySet; |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 36 | import com.android.systemui.BatteryMeterDrawable; |
Jason Monk | 431ad73 | 2015-07-16 08:58:15 -0400 | [diff] [blame] | 37 | import com.android.systemui.DemoMode; |
| 38 | import com.android.systemui.R; |
Jason Monk | 5e74517 | 2015-06-02 19:14:44 -0400 | [diff] [blame] | 39 | import com.android.systemui.SystemUI; |
| 40 | import com.android.systemui.SystemUIApplication; |
| 41 | import com.android.systemui.settings.CurrentUserTracker; |
Jason Monk | 431ad73 | 2015-07-16 08:58:15 -0400 | [diff] [blame] | 42 | import com.android.systemui.statusbar.phone.SystemUIDialog; |
Jason Monk | 5e74517 | 2015-06-02 19:14:44 -0400 | [diff] [blame] | 43 | |
Jason Monk | 5e74517 | 2015-06-02 19:14:44 -0400 | [diff] [blame] | 44 | import java.util.HashMap; |
Jason Monk | 51c444b | 2016-01-06 16:32:29 -0500 | [diff] [blame] | 45 | import java.util.Set; |
Jason Monk | 5e74517 | 2015-06-02 19:14:44 -0400 | [diff] [blame] | 46 | |
| 47 | |
| 48 | public class TunerService extends SystemUI { |
| 49 | |
Jason Monk | 431ad73 | 2015-07-16 08:58:15 -0400 | [diff] [blame] | 50 | public static final String ACTION_CLEAR = "com.android.systemui.action.CLEAR_TUNER"; |
| 51 | |
Jason Monk | 5e74517 | 2015-06-02 19:14:44 -0400 | [diff] [blame] | 52 | private final Observer mObserver = new Observer(); |
| 53 | // Map of Uris we listen on to their settings keys. |
| 54 | private final ArrayMap<Uri, String> mListeningUris = new ArrayMap<>(); |
| 55 | // Map of settings keys to the listener. |
Jason Monk | 51c444b | 2016-01-06 16:32:29 -0500 | [diff] [blame] | 56 | private final HashMap<String, Set<Tunable>> mTunableLookup = new HashMap<>(); |
Jason Monk | 5e74517 | 2015-06-02 19:14:44 -0400 | [diff] [blame] | 57 | |
| 58 | private ContentResolver mContentResolver; |
| 59 | private int mCurrentUser; |
| 60 | private CurrentUserTracker mUserTracker; |
| 61 | |
| 62 | @Override |
| 63 | public void start() { |
| 64 | mContentResolver = mContext.getContentResolver(); |
| 65 | putComponent(TunerService.class, this); |
| 66 | |
| 67 | mCurrentUser = ActivityManager.getCurrentUser(); |
| 68 | mUserTracker = new CurrentUserTracker(mContext) { |
| 69 | @Override |
| 70 | public void onUserSwitched(int newUserId) { |
| 71 | mCurrentUser = newUserId; |
| 72 | reloadAll(); |
| 73 | reregisterAll(); |
| 74 | } |
| 75 | }; |
| 76 | mUserTracker.startTracking(); |
| 77 | } |
| 78 | |
Jason Monk | f0c6f64 | 2016-01-20 22:24:50 -0500 | [diff] [blame] | 79 | public String getValue(String setting) { |
| 80 | return Settings.Secure.getStringForUser(mContentResolver, setting, mCurrentUser); |
| 81 | } |
| 82 | |
| 83 | public void setValue(String setting, String value) { |
| 84 | Settings.Secure.putStringForUser(mContentResolver, setting, value, mCurrentUser); |
| 85 | } |
| 86 | |
| 87 | public int getValue(String setting, int def) { |
| 88 | return Settings.Secure.getIntForUser(mContentResolver, setting, def, mCurrentUser); |
| 89 | } |
| 90 | |
| 91 | public void setValue(String setting, int value) { |
| 92 | Settings.Secure.putIntForUser(mContentResolver, setting, value, mCurrentUser); |
| 93 | } |
| 94 | |
Jason Monk | 5e74517 | 2015-06-02 19:14:44 -0400 | [diff] [blame] | 95 | public void addTunable(Tunable tunable, String... keys) { |
| 96 | for (String key : keys) { |
| 97 | addTunable(tunable, key); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | private void addTunable(Tunable tunable, String key) { |
| 102 | if (!mTunableLookup.containsKey(key)) { |
Jason Monk | 51c444b | 2016-01-06 16:32:29 -0500 | [diff] [blame] | 103 | mTunableLookup.put(key, new ArraySet<Tunable>()); |
Jason Monk | 5e74517 | 2015-06-02 19:14:44 -0400 | [diff] [blame] | 104 | } |
| 105 | mTunableLookup.get(key).add(tunable); |
| 106 | Uri uri = Settings.Secure.getUriFor(key); |
| 107 | if (!mListeningUris.containsKey(uri)) { |
| 108 | mListeningUris.put(uri, key); |
| 109 | mContentResolver.registerContentObserver(uri, false, mObserver, mCurrentUser); |
| 110 | } |
| 111 | // Send the first state. |
| 112 | String value = Settings.Secure.getStringForUser(mContentResolver, key, mCurrentUser); |
| 113 | tunable.onTuningChanged(key, value); |
| 114 | } |
| 115 | |
| 116 | public void removeTunable(Tunable tunable) { |
Jason Monk | 51c444b | 2016-01-06 16:32:29 -0500 | [diff] [blame] | 117 | for (Set<Tunable> list : mTunableLookup.values()) { |
Jason Monk | 5e74517 | 2015-06-02 19:14:44 -0400 | [diff] [blame] | 118 | list.remove(tunable); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | protected void reregisterAll() { |
| 123 | if (mListeningUris.size() == 0) { |
| 124 | return; |
| 125 | } |
| 126 | mContentResolver.unregisterContentObserver(mObserver); |
| 127 | for (Uri uri : mListeningUris.keySet()) { |
| 128 | mContentResolver.registerContentObserver(uri, false, mObserver, mCurrentUser); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | public void reloadSetting(Uri uri) { |
| 133 | String key = mListeningUris.get(uri); |
Jason Monk | 51c444b | 2016-01-06 16:32:29 -0500 | [diff] [blame] | 134 | Set<Tunable> tunables = mTunableLookup.get(key); |
Jason Monk | 72def0d | 2015-11-10 16:03:06 -0500 | [diff] [blame] | 135 | if (tunables == null) { |
| 136 | return; |
| 137 | } |
Jason Monk | 5e74517 | 2015-06-02 19:14:44 -0400 | [diff] [blame] | 138 | String value = Settings.Secure.getStringForUser(mContentResolver, key, mCurrentUser); |
Jason Monk | 72def0d | 2015-11-10 16:03:06 -0500 | [diff] [blame] | 139 | for (Tunable tunable : tunables) { |
Jason Monk | 5e74517 | 2015-06-02 19:14:44 -0400 | [diff] [blame] | 140 | tunable.onTuningChanged(key, value); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | private void reloadAll() { |
| 145 | for (String key : mTunableLookup.keySet()) { |
| 146 | String value = Settings.Secure.getStringForUser(mContentResolver, key, |
| 147 | mCurrentUser); |
| 148 | for (Tunable tunable : mTunableLookup.get(key)) { |
| 149 | tunable.onTuningChanged(key, value); |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
Jason Monk | 431ad73 | 2015-07-16 08:58:15 -0400 | [diff] [blame] | 154 | public void clearAll() { |
| 155 | // A couple special cases. |
| 156 | Settings.Global.putString(mContentResolver, DemoMode.DEMO_MODE_ALLOWED, null); |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 157 | Settings.System.putString(mContentResolver, BatteryMeterDrawable.SHOW_PERCENT_SETTING, null); |
Jason Monk | 431ad73 | 2015-07-16 08:58:15 -0400 | [diff] [blame] | 158 | Intent intent = new Intent(DemoMode.ACTION_DEMO); |
| 159 | intent.putExtra(DemoMode.EXTRA_COMMAND, DemoMode.COMMAND_EXIT); |
| 160 | mContext.sendBroadcast(intent); |
| 161 | |
| 162 | for (String key : mTunableLookup.keySet()) { |
| 163 | Settings.Secure.putString(mContentResolver, key, null); |
| 164 | } |
| 165 | } |
| 166 | |
Jason Monk | 5e74517 | 2015-06-02 19:14:44 -0400 | [diff] [blame] | 167 | // Only used in other processes, such as the tuner. |
| 168 | private static TunerService sInstance; |
| 169 | |
| 170 | public static TunerService get(Context context) { |
Jason Monk | d5a204f | 2015-12-21 08:50:01 -0500 | [diff] [blame] | 171 | TunerService service = null; |
| 172 | if (context.getApplicationContext() instanceof SystemUIApplication) { |
| 173 | SystemUIApplication sysUi = (SystemUIApplication) context.getApplicationContext(); |
| 174 | service = sysUi.getComponent(TunerService.class); |
| 175 | } |
Jason Monk | 5e74517 | 2015-06-02 19:14:44 -0400 | [diff] [blame] | 176 | if (service == null) { |
| 177 | // Can't get it as a component, must in the tuner, lets just create one for now. |
| 178 | return getStaticService(context); |
| 179 | } |
| 180 | return service; |
| 181 | } |
| 182 | |
| 183 | private static TunerService getStaticService(Context context) { |
| 184 | if (sInstance == null) { |
| 185 | sInstance = new TunerService(); |
| 186 | sInstance.mContext = context.getApplicationContext(); |
| 187 | sInstance.mComponents = new HashMap<>(); |
| 188 | sInstance.start(); |
| 189 | } |
| 190 | return sInstance; |
| 191 | } |
| 192 | |
Jason Monk | 431ad73 | 2015-07-16 08:58:15 -0400 | [diff] [blame] | 193 | public static final void showResetRequest(final Context context, final Runnable onDisabled) { |
| 194 | SystemUIDialog dialog = new SystemUIDialog(context); |
Jason Monk | 540542b | 2015-08-11 14:18:14 -0400 | [diff] [blame] | 195 | dialog.setShowForAllUsers(true); |
Jason Monk | 431ad73 | 2015-07-16 08:58:15 -0400 | [diff] [blame] | 196 | dialog.setMessage(R.string.remove_from_settings_prompt); |
| 197 | dialog.setButton(DialogInterface.BUTTON_NEGATIVE, context.getString(R.string.cancel), |
| 198 | (OnClickListener) null); |
| 199 | dialog.setButton(DialogInterface.BUTTON_POSITIVE, |
| 200 | context.getString(R.string.guest_exit_guest_dialog_remove), new OnClickListener() { |
| 201 | @Override |
| 202 | public void onClick(DialogInterface dialog, int which) { |
| 203 | // Tell the tuner (in main SysUI process) to clear all its settings. |
| 204 | context.sendBroadcast(new Intent(TunerService.ACTION_CLEAR)); |
| 205 | // Disable access to tuner. |
| 206 | TunerService.setTunerEnabled(context, false); |
| 207 | // Make them sit through the warning dialog again. |
| 208 | Settings.Secure.putInt(context.getContentResolver(), |
| 209 | TunerFragment.SETTING_SEEN_TUNER_WARNING, 0); |
| 210 | if (onDisabled != null) { |
| 211 | onDisabled.run(); |
| 212 | } |
| 213 | } |
| 214 | }); |
| 215 | dialog.show(); |
| 216 | } |
| 217 | |
| 218 | public static final void setTunerEnabled(Context context, boolean enabled) { |
Jason Monk | 540542b | 2015-08-11 14:18:14 -0400 | [diff] [blame] | 219 | userContext(context).getPackageManager().setComponentEnabledSetting( |
Jason Monk | 431ad73 | 2015-07-16 08:58:15 -0400 | [diff] [blame] | 220 | new ComponentName(context, TunerActivity.class), |
| 221 | enabled ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED |
| 222 | : PackageManager.COMPONENT_ENABLED_STATE_DISABLED, |
| 223 | PackageManager.DONT_KILL_APP); |
| 224 | } |
| 225 | |
| 226 | public static final boolean isTunerEnabled(Context context) { |
Jason Monk | 540542b | 2015-08-11 14:18:14 -0400 | [diff] [blame] | 227 | return userContext(context).getPackageManager().getComponentEnabledSetting( |
Jason Monk | 431ad73 | 2015-07-16 08:58:15 -0400 | [diff] [blame] | 228 | new ComponentName(context, TunerActivity.class)) |
| 229 | == PackageManager.COMPONENT_ENABLED_STATE_ENABLED; |
| 230 | } |
| 231 | |
Jason Monk | 540542b | 2015-08-11 14:18:14 -0400 | [diff] [blame] | 232 | private static Context userContext(Context context) { |
| 233 | try { |
| 234 | return context.createPackageContextAsUser(context.getPackageName(), 0, |
| 235 | new UserHandle(ActivityManager.getCurrentUser())); |
| 236 | } catch (NameNotFoundException e) { |
| 237 | return context; |
| 238 | } |
| 239 | } |
| 240 | |
Jason Monk | 5e74517 | 2015-06-02 19:14:44 -0400 | [diff] [blame] | 241 | private class Observer extends ContentObserver { |
| 242 | public Observer() { |
| 243 | super(new Handler(Looper.getMainLooper())); |
| 244 | } |
| 245 | |
| 246 | @Override |
| 247 | public void onChange(boolean selfChange, Uri uri, int userId) { |
| 248 | if (userId == ActivityManager.getCurrentUser()) { |
| 249 | reloadSetting(uri); |
| 250 | } |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | public interface Tunable { |
| 255 | void onTuningChanged(String key, String newValue); |
| 256 | } |
Jason Monk | 431ad73 | 2015-07-16 08:58:15 -0400 | [diff] [blame] | 257 | |
| 258 | public static class ClearReceiver extends BroadcastReceiver { |
| 259 | @Override |
| 260 | public void onReceive(Context context, Intent intent) { |
| 261 | if (ACTION_CLEAR.equals(intent.getAction())) { |
| 262 | get(context).clearAll(); |
| 263 | } |
| 264 | } |
| 265 | } |
Jason Monk | 5e74517 | 2015-06-02 19:14:44 -0400 | [diff] [blame] | 266 | } |