TunerService.java revision ea05f87b253ce20a08158768951169b1cda0a623
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.content.pm.UserInfo;
29import android.database.ContentObserver;
30import android.net.Uri;
31import android.os.Handler;
32import android.os.Looper;
33import android.os.UserHandle;
34import android.os.UserManager;
35import android.provider.Settings;
36import android.provider.Settings.Secure;
37import android.text.TextUtils;
38import android.util.ArrayMap;
39import android.util.ArraySet;
40
41import com.android.systemui.BatteryMeterDrawable;
42import com.android.systemui.DemoMode;
43import com.android.systemui.R;
44import com.android.systemui.SystemUI;
45import com.android.systemui.SystemUIApplication;
46import com.android.systemui.settings.CurrentUserTracker;
47import com.android.systemui.statusbar.phone.StatusBarIconController;
48import com.android.systemui.statusbar.phone.SystemUIDialog;
49
50import java.util.HashMap;
51import java.util.Set;
52
53
54public class TunerService extends SystemUI {
55
56    public static final String ACTION_CLEAR = "com.android.systemui.action.CLEAR_TUNER";
57
58    private static final String TUNER_VERSION = "sysui_tuner_version";
59
60    private static final int CURRENT_TUNER_VERSION = 1;
61
62    private final Observer mObserver = new Observer();
63    // Map of Uris we listen on to their settings keys.
64    private final ArrayMap<Uri, String> mListeningUris = new ArrayMap<>();
65    // Map of settings keys to the listener.
66    private final HashMap<String, Set<Tunable>> mTunableLookup = new HashMap<>();
67
68    private ContentResolver mContentResolver;
69    private int mCurrentUser;
70    private CurrentUserTracker mUserTracker;
71
72    @Override
73    public void start() {
74        mContentResolver = mContext.getContentResolver();
75
76        for (UserInfo user : UserManager.get(mContext).getUsers()) {
77            mCurrentUser = user.getUserHandle().getIdentifier();
78            if (getValue(TUNER_VERSION, 0) != CURRENT_TUNER_VERSION) {
79                upgradeTuner(getValue(TUNER_VERSION, 0), CURRENT_TUNER_VERSION);
80            }
81        }
82        putComponent(TunerService.class, this);
83
84        mCurrentUser = ActivityManager.getCurrentUser();
85        mUserTracker = new CurrentUserTracker(mContext) {
86            @Override
87            public void onUserSwitched(int newUserId) {
88                mCurrentUser = newUserId;
89                reloadAll();
90                reregisterAll();
91            }
92        };
93        mUserTracker.startTracking();
94    }
95
96    public void destroy() {
97        mUserTracker.stopTracking();
98    }
99
100    private void upgradeTuner(int oldVersion, int newVersion) {
101        if (oldVersion < 1) {
102            String blacklistStr = getValue(StatusBarIconController.ICON_BLACKLIST);
103            if (blacklistStr != null) {
104                ArraySet<String> iconBlacklist =
105                        StatusBarIconController.getIconBlacklist(blacklistStr);
106
107                iconBlacklist.add("rotate");
108                iconBlacklist.add("headset");
109
110                Settings.Secure.putStringForUser(mContentResolver,
111                        StatusBarIconController.ICON_BLACKLIST,
112                        TextUtils.join(",", iconBlacklist), mCurrentUser);
113            }
114        }
115        setValue(TUNER_VERSION, newVersion);
116    }
117
118    public String getValue(String setting) {
119        return Settings.Secure.getStringForUser(mContentResolver, setting, mCurrentUser);
120    }
121
122    public void setValue(String setting, String value) {
123         Settings.Secure.putStringForUser(mContentResolver, setting, value, mCurrentUser);
124    }
125
126    public int getValue(String setting, int def) {
127        return Settings.Secure.getIntForUser(mContentResolver, setting, def, mCurrentUser);
128    }
129
130    public String getValue(String setting, String def) {
131        String ret = Secure.getStringForUser(mContentResolver, setting, mCurrentUser);
132        if (ret == null) return def;
133        return ret;
134    }
135
136    public void setValue(String setting, int value) {
137         Settings.Secure.putIntForUser(mContentResolver, setting, value, mCurrentUser);
138    }
139
140    public void addTunable(Tunable tunable, String... keys) {
141        for (String key : keys) {
142            addTunable(tunable, key);
143        }
144    }
145
146    private void addTunable(Tunable tunable, String key) {
147        if (!mTunableLookup.containsKey(key)) {
148            mTunableLookup.put(key, new ArraySet<Tunable>());
149        }
150        mTunableLookup.get(key).add(tunable);
151        Uri uri = Settings.Secure.getUriFor(key);
152        if (!mListeningUris.containsKey(uri)) {
153            mListeningUris.put(uri, key);
154            mContentResolver.registerContentObserver(uri, false, mObserver, mCurrentUser);
155        }
156        // Send the first state.
157        String value = Settings.Secure.getStringForUser(mContentResolver, key, mCurrentUser);
158        tunable.onTuningChanged(key, value);
159    }
160
161    public void removeTunable(Tunable tunable) {
162        for (Set<Tunable> list : mTunableLookup.values()) {
163            list.remove(tunable);
164        }
165    }
166
167    protected void reregisterAll() {
168        if (mListeningUris.size() == 0) {
169            return;
170        }
171        mContentResolver.unregisterContentObserver(mObserver);
172        for (Uri uri : mListeningUris.keySet()) {
173            mContentResolver.registerContentObserver(uri, false, mObserver, mCurrentUser);
174        }
175    }
176
177    public void reloadSetting(Uri uri) {
178        String key = mListeningUris.get(uri);
179        Set<Tunable> tunables = mTunableLookup.get(key);
180        if (tunables == null) {
181            return;
182        }
183        String value = Settings.Secure.getStringForUser(mContentResolver, key, mCurrentUser);
184        for (Tunable tunable : tunables) {
185            tunable.onTuningChanged(key, value);
186        }
187    }
188
189    private void reloadAll() {
190        for (String key : mTunableLookup.keySet()) {
191            String value = Settings.Secure.getStringForUser(mContentResolver, key,
192                    mCurrentUser);
193            for (Tunable tunable : mTunableLookup.get(key)) {
194                tunable.onTuningChanged(key, value);
195            }
196        }
197    }
198
199    public void clearAll() {
200        // A couple special cases.
201        Settings.Global.putString(mContentResolver, DemoMode.DEMO_MODE_ALLOWED, null);
202        Settings.System.putString(mContentResolver, BatteryMeterDrawable.SHOW_PERCENT_SETTING, null);
203        Intent intent = new Intent(DemoMode.ACTION_DEMO);
204        intent.putExtra(DemoMode.EXTRA_COMMAND, DemoMode.COMMAND_EXIT);
205        mContext.sendBroadcast(intent);
206
207        for (String key : mTunableLookup.keySet()) {
208            Settings.Secure.putString(mContentResolver, key, null);
209        }
210    }
211
212    // Only used in other processes, such as the tuner.
213    private static TunerService sInstance;
214
215    public static TunerService get(Context context) {
216        TunerService service = null;
217        if (context.getApplicationContext() instanceof SystemUIApplication) {
218            SystemUIApplication sysUi = (SystemUIApplication) context.getApplicationContext();
219            service = sysUi.getComponent(TunerService.class);
220        }
221        if (service == null) {
222            // Can't get it as a component, must in the tuner, lets just create one for now.
223            return getStaticService(context);
224        }
225        return service;
226    }
227
228    private static TunerService getStaticService(Context context) {
229        if (sInstance == null) {
230            sInstance = new TunerService();
231            sInstance.mContext = context.getApplicationContext();
232            sInstance.mComponents = new HashMap<>();
233            sInstance.start();
234        }
235        return sInstance;
236    }
237
238    public static final void showResetRequest(final Context context, final Runnable onDisabled) {
239        SystemUIDialog dialog = new SystemUIDialog(context);
240        dialog.setShowForAllUsers(true);
241        dialog.setMessage(R.string.remove_from_settings_prompt);
242        dialog.setButton(DialogInterface.BUTTON_NEGATIVE, context.getString(R.string.cancel),
243                (OnClickListener) null);
244        dialog.setButton(DialogInterface.BUTTON_POSITIVE,
245                context.getString(R.string.guest_exit_guest_dialog_remove), new OnClickListener() {
246            @Override
247            public void onClick(DialogInterface dialog, int which) {
248                // Tell the tuner (in main SysUI process) to clear all its settings.
249                context.sendBroadcast(new Intent(TunerService.ACTION_CLEAR));
250                // Disable access to tuner.
251                TunerService.setTunerEnabled(context, false);
252                // Make them sit through the warning dialog again.
253                Settings.Secure.putInt(context.getContentResolver(),
254                        TunerFragment.SETTING_SEEN_TUNER_WARNING, 0);
255                if (onDisabled != null) {
256                    onDisabled.run();
257                }
258            }
259        });
260        dialog.show();
261    }
262
263    public static final void setTunerEnabled(Context context, boolean enabled) {
264        userContext(context).getPackageManager().setComponentEnabledSetting(
265                new ComponentName(context, TunerActivity.class),
266                enabled ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
267                        : PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
268                        PackageManager.DONT_KILL_APP);
269
270        userContext(context).getPackageManager().setComponentEnabledSetting(
271                new ComponentName(context, TunerActivity.ACTIVITY_ALIAS_NAME),
272                enabled ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
273                        : PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
274                PackageManager.DONT_KILL_APP);
275    }
276
277    public static final boolean isTunerEnabled(Context context) {
278        return userContext(context).getPackageManager().getComponentEnabledSetting(
279                new ComponentName(context, TunerActivity.class))
280                == PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
281    }
282
283    private static Context userContext(Context context) {
284        try {
285            return context.createPackageContextAsUser(context.getPackageName(), 0,
286                    new UserHandle(ActivityManager.getCurrentUser()));
287        } catch (NameNotFoundException e) {
288            return context;
289        }
290    }
291
292    private class Observer extends ContentObserver {
293        public Observer() {
294            super(new Handler(Looper.getMainLooper()));
295        }
296
297        @Override
298        public void onChange(boolean selfChange, Uri uri, int userId) {
299            if (userId == ActivityManager.getCurrentUser()) {
300                reloadSetting(uri);
301            }
302        }
303    }
304
305    public interface Tunable {
306        void onTuningChanged(String key, String newValue);
307    }
308
309    public static class ClearReceiver extends BroadcastReceiver {
310        @Override
311        public void onReceive(Context context, Intent intent) {
312            if (ACTION_CLEAR.equals(intent.getAction())) {
313                get(context).clearAll();
314            }
315        }
316    }
317}
318