QuickSettingsModel.java revision daab6af91d4e0f7bd13679f01a982dbe57c88bf0
1/*
2 * Copyright (C) 2012 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
17package com.android.systemui.statusbar.phone;
18
19import android.bluetooth.BluetoothAdapter;
20import android.bluetooth.BluetoothAdapter.BluetoothStateChangeCallback;
21import android.content.BroadcastReceiver;
22import android.content.ContentResolver;
23import android.content.Context;
24import android.content.Intent;
25import android.content.IntentFilter;
26import android.content.pm.PackageManager;
27import android.content.res.Resources;
28import android.database.ContentObserver;
29import android.graphics.drawable.Drawable;
30import android.hardware.display.WifiDisplayStatus;
31import android.os.Handler;
32import android.os.UserHandle;
33import android.provider.Settings;
34import android.provider.Settings.SettingNotFoundException;
35import android.text.TextUtils;
36import android.view.View;
37import android.view.inputmethod.InputMethodInfo;
38import android.view.inputmethod.InputMethodManager;
39import android.view.inputmethod.InputMethodSubtype;
40
41import com.android.internal.view.RotationPolicy;
42import com.android.systemui.R;
43import com.android.systemui.statusbar.policy.BatteryController.BatteryStateChangeCallback;
44import com.android.systemui.statusbar.policy.BrightnessController.BrightnessStateChangeCallback;
45import com.android.systemui.statusbar.policy.LocationController.LocationGpsStateChangeCallback;
46import com.android.systemui.statusbar.policy.NetworkController.NetworkSignalChangedCallback;
47
48import java.util.List;
49
50
51class QuickSettingsModel implements BluetoothStateChangeCallback,
52        NetworkSignalChangedCallback,
53        BatteryStateChangeCallback,
54        LocationGpsStateChangeCallback,
55        BrightnessStateChangeCallback {
56
57    // Sett InputMethoManagerService
58    private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
59
60    /** Represents the state of a given attribute. */
61    static class State {
62        int iconId;
63        String label;
64        boolean enabled = false;
65    }
66    static class BatteryState extends State {
67        int batteryLevel;
68        boolean pluggedIn;
69    }
70    static class RSSIState extends State {
71        int signalIconId;
72        int dataTypeIconId;
73    }
74    static class UserState extends State {
75        Drawable avatar;
76    }
77    static class BrightnessState extends State {
78        boolean autoBrightness;
79    }
80    public static class BluetoothState extends State {
81        boolean connected = false;
82    }
83
84    /** The callback to update a given tile. */
85    interface RefreshCallback {
86        public void refreshView(QuickSettingsTileView view, State state);
87    }
88
89    /** Broadcast receive to determine if there is an alarm set. */
90    private BroadcastReceiver mAlarmIntentReceiver = new BroadcastReceiver() {
91        @Override
92        public void onReceive(Context context, Intent intent) {
93            String action = intent.getAction();
94            if (action.equals(Intent.ACTION_ALARM_CHANGED)) {
95                onAlarmChanged(intent);
96                onNextAlarmChanged();
97            }
98        }
99    };
100
101    /** Broadcast receiver to act on user switches to update visuals of per-user state */
102    private BroadcastReceiver mUserSwitchedReceiver = new BroadcastReceiver() {
103        @Override
104        public void onReceive(Context context, Intent intent) {
105            if (Intent.ACTION_USER_SWITCHED.equals(intent.getAction())) {
106                onUserSwitched(intent);
107            }
108        }
109    };
110
111    /** ContentObserver to determine the next alarm */
112    private class NextAlarmObserver extends ContentObserver {
113        public NextAlarmObserver(Handler handler) {
114            super(handler);
115        }
116
117        @Override public void onChange(boolean selfChange) {
118            onNextAlarmChanged();
119        }
120
121        public void startObserving() {
122            final ContentResolver cr = mContext.getContentResolver();
123            cr.registerContentObserver(
124                    Settings.System.getUriFor(Settings.System.NEXT_ALARM_FORMATTED), false, this);
125        }
126    }
127
128    /** ContentObserver to watch adb */
129    private class BugreportObserver extends ContentObserver {
130        public BugreportObserver(Handler handler) {
131            super(handler);
132        }
133
134        @Override public void onChange(boolean selfChange) {
135            onBugreportChanged();
136        }
137
138        public void startObserving() {
139            final ContentResolver cr = mContext.getContentResolver();
140            cr.registerContentObserver(
141                    Settings.Secure.getUriFor(Settings.Secure.BUGREPORT_IN_POWER_MENU), false, this);
142        }
143    }
144    private Context mContext;
145    private Handler mHandler;
146    private NextAlarmObserver mNextAlarmObserver;
147    private BugreportObserver mBugreportObserver;
148
149    private QuickSettingsTileView mUserTile;
150    private RefreshCallback mUserCallback;
151    private UserState mUserState = new UserState();
152
153    private QuickSettingsTileView mTimeTile;
154    private RefreshCallback mTimeCallback;
155    private State mTimeState = new State();
156
157    private QuickSettingsTileView mAlarmTile;
158    private RefreshCallback mAlarmCallback;
159    private State mAlarmState = new State();
160
161    private QuickSettingsTileView mAirplaneModeTile;
162    private RefreshCallback mAirplaneModeCallback;
163    private State mAirplaneModeState = new State();
164
165    private QuickSettingsTileView mWifiTile;
166    private RefreshCallback mWifiCallback;
167    private State mWifiState = new State();
168
169    private QuickSettingsTileView mWifiDisplayTile;
170    private RefreshCallback mWifiDisplayCallback;
171    private State mWifiDisplayState = new State();
172
173    private QuickSettingsTileView mRSSITile;
174    private RefreshCallback mRSSICallback;
175    private RSSIState mRSSIState = new RSSIState();
176
177    private QuickSettingsTileView mBluetoothTile;
178    private RefreshCallback mBluetoothCallback;
179    private BluetoothState mBluetoothState = new BluetoothState();
180
181    private QuickSettingsTileView mBatteryTile;
182    private RefreshCallback mBatteryCallback;
183    private BatteryState mBatteryState = new BatteryState();
184
185    private QuickSettingsTileView mLocationTile;
186    private RefreshCallback mLocationCallback;
187    private State mLocationState = new State();
188
189    private QuickSettingsTileView mImeTile;
190    private RefreshCallback mImeCallback = null;
191    private State mImeState = new State();
192
193    private QuickSettingsTileView mRotationLockTile;
194    private RefreshCallback mRotationLockCallback;
195    private State mRotationLockState = new State();
196
197    private QuickSettingsTileView mBrightnessTile;
198    private RefreshCallback mBrightnessCallback;
199    private BrightnessState mBrightnessState = new BrightnessState();
200
201    private QuickSettingsTileView mBugreportTile;
202    private RefreshCallback mBugreportCallback;
203    private State mBugreportState = new State();
204
205    private QuickSettingsTileView mSettingsTile;
206    private RefreshCallback mSettingsCallback;
207    private State mSettingsState = new State();
208
209    public QuickSettingsModel(Context context) {
210        mContext = context;
211        mHandler = new Handler();
212        mNextAlarmObserver = new NextAlarmObserver(mHandler);
213        mNextAlarmObserver.startObserving();
214        mBugreportObserver = new BugreportObserver(mHandler);
215        mBugreportObserver.startObserving();
216
217        IntentFilter alarmIntentFilter = new IntentFilter();
218        alarmIntentFilter.addAction(Intent.ACTION_ALARM_CHANGED);
219        context.registerReceiver(mAlarmIntentReceiver, alarmIntentFilter);
220
221        IntentFilter userSwitchedFilter = new IntentFilter(Intent.ACTION_USER_SWITCHED);
222        context.registerReceiver(mUserSwitchedReceiver, userSwitchedFilter);
223    }
224
225    void updateResources() {
226        refreshSettingsTile();
227        refreshBatteryTile();
228        refreshBluetoothTile();
229        refreshBrightnessTile();
230        refreshRotationLockTile();
231    }
232
233    // Settings
234    void addSettingsTile(QuickSettingsTileView view, RefreshCallback cb) {
235        mSettingsTile = view;
236        mSettingsCallback = cb;
237        refreshSettingsTile();
238    }
239    void refreshSettingsTile() {
240        Resources r = mContext.getResources();
241        mSettingsState.label = r.getString(R.string.quick_settings_settings_label);
242        mSettingsCallback.refreshView(mSettingsTile, mSettingsState);
243    }
244
245    // User
246    void addUserTile(QuickSettingsTileView view, RefreshCallback cb) {
247        mUserTile = view;
248        mUserCallback = cb;
249        mUserCallback.refreshView(mUserTile, mUserState);
250    }
251    void setUserTileInfo(String name, Drawable avatar) {
252        mUserState.label = name;
253        mUserState.avatar = avatar;
254        mUserCallback.refreshView(mUserTile, mUserState);
255    }
256
257    // Time
258    void addTimeTile(QuickSettingsTileView view, RefreshCallback cb) {
259        mTimeTile = view;
260        mTimeCallback = cb;
261        mTimeCallback.refreshView(view, mTimeState);
262    }
263
264    // Alarm
265    void addAlarmTile(QuickSettingsTileView view, RefreshCallback cb) {
266        mAlarmTile = view;
267        mAlarmCallback = cb;
268        mAlarmCallback.refreshView(view, mAlarmState);
269    }
270    void onAlarmChanged(Intent intent) {
271        mAlarmState.enabled = intent.getBooleanExtra("alarmSet", false);
272        mAlarmCallback.refreshView(mAlarmTile, mAlarmState);
273    }
274    void onNextAlarmChanged() {
275        mAlarmState.label = Settings.System.getString(mContext.getContentResolver(),
276                Settings.System.NEXT_ALARM_FORMATTED);
277        mAlarmCallback.refreshView(mAlarmTile, mAlarmState);
278    }
279
280    // Airplane Mode
281    void addAirplaneModeTile(QuickSettingsTileView view, RefreshCallback cb) {
282        mAirplaneModeTile = view;
283        mAirplaneModeTile.setOnClickListener(new View.OnClickListener() {
284            @Override
285            public void onClick(View v) {
286                if (mAirplaneModeState.enabled) {
287                    setAirplaneModeState(false);
288                } else {
289                    setAirplaneModeState(true);
290                }
291            }
292        });
293        mAirplaneModeCallback = cb;
294        int airplaneMode = Settings.Global.getInt(mContext.getContentResolver(),
295                Settings.Global.AIRPLANE_MODE_ON, 0);
296        onAirplaneModeChanged(airplaneMode != 0);
297    }
298    private void setAirplaneModeState(boolean enabled) {
299        // TODO: Sets the view to be "awaiting" if not already awaiting
300
301        // Change the system setting
302        Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON,
303                                enabled ? 1 : 0);
304
305        // Post the intent
306        Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
307        intent.putExtra("state", enabled);
308        mContext.sendBroadcast(intent);
309    }
310    // NetworkSignalChanged callback
311    @Override
312    public void onAirplaneModeChanged(boolean enabled) {
313        // TODO: If view is in awaiting state, disable
314        Resources r = mContext.getResources();
315        mAirplaneModeState.enabled = enabled;
316        mAirplaneModeState.iconId = (enabled ?
317                R.drawable.ic_qs_airplane_on :
318                R.drawable.ic_qs_airplane_off);
319        mAirplaneModeState.label = r.getString(R.string.quick_settings_airplane_mode_label);
320        mAirplaneModeCallback.refreshView(mAirplaneModeTile, mAirplaneModeState);
321    }
322
323    // Wifi
324    void addWifiTile(QuickSettingsTileView view, RefreshCallback cb) {
325        mWifiTile = view;
326        mWifiCallback = cb;
327        mWifiCallback.refreshView(mWifiTile, mWifiState);
328    }
329    // Remove the double quotes that the SSID may contain
330    public static String removeDoubleQuotes(String string) {
331        if (string == null) return null;
332        final int length = string.length();
333        if ((length > 1) && (string.charAt(0) == '"') && (string.charAt(length - 1) == '"')) {
334            return string.substring(1, length - 1);
335        }
336        return string;
337    }
338    // Remove the period from the network name
339    public static String removeTrailingPeriod(String string) {
340        if (string == null) return null;
341        final int length = string.length();
342        if (string.endsWith(".")) {
343            string.substring(0, length - 1);
344        }
345        return string;
346    }
347    // NetworkSignalChanged callback
348    @Override
349    public void onWifiSignalChanged(boolean enabled, int wifiSignalIconId, String enabledDesc) {
350        // TODO: If view is in awaiting state, disable
351        Resources r = mContext.getResources();
352        mWifiState.enabled = enabled;
353        boolean wifiConnected = enabled && (wifiSignalIconId > 0) && (enabledDesc != null);
354        boolean wifiNotConnected = (wifiSignalIconId > 0) && (enabledDesc == null);
355        if (wifiConnected) {
356            mWifiState.iconId = wifiSignalIconId;
357            mWifiState.label = removeDoubleQuotes(enabledDesc);
358        } else if (wifiNotConnected) {
359            mWifiState.iconId = R.drawable.ic_qs_wifi_0;
360            mWifiState.label = r.getString(R.string.quick_settings_wifi_label);
361        } else {
362            mWifiState.iconId = R.drawable.ic_qs_wifi_no_network;
363            mWifiState.label = r.getString(R.string.quick_settings_wifi_off_label);
364        }
365        mWifiCallback.refreshView(mWifiTile, mWifiState);
366    }
367
368    // RSSI
369    void addRSSITile(QuickSettingsTileView view, RefreshCallback cb) {
370        mRSSITile = view;
371        mRSSICallback = cb;
372        mRSSICallback.refreshView(mRSSITile, mRSSIState);
373    }
374    boolean deviceSupportsTelephony() {
375        PackageManager pm = mContext.getPackageManager();
376        return pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY);
377    }
378    // NetworkSignalChanged callback
379    @Override
380    public void onMobileDataSignalChanged(boolean enabled, int mobileSignalIconId,
381            int dataTypeIconId, String enabledDesc) {
382        if (deviceSupportsTelephony()) {
383            // TODO: If view is in awaiting state, disable
384            Resources r = mContext.getResources();
385            mRSSIState.signalIconId = enabled && (mobileSignalIconId > 0)
386                    ? mobileSignalIconId
387                    : R.drawable.ic_qs_signal_no_signal;
388            mRSSIState.dataTypeIconId = enabled && (dataTypeIconId > 0) && !mWifiState.enabled
389                    ? dataTypeIconId
390                    : 0;
391            mRSSIState.label = enabled
392                    ? removeTrailingPeriod(enabledDesc)
393                    : r.getString(R.string.quick_settings_rssi_emergency_only);
394            mRSSICallback.refreshView(mRSSITile, mRSSIState);
395        }
396    }
397
398    // Bluetooth
399    void addBluetoothTile(QuickSettingsTileView view, RefreshCallback cb) {
400        mBluetoothTile = view;
401        mBluetoothCallback = cb;
402
403        final BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
404        mBluetoothState.enabled = adapter.isEnabled();
405        mBluetoothState.connected =
406                (adapter.getConnectionState() == BluetoothAdapter.STATE_CONNECTED);
407        onBluetoothStateChange(mBluetoothState);
408    }
409    boolean deviceSupportsBluetooth() {
410        return (BluetoothAdapter.getDefaultAdapter() != null);
411    }
412    // BluetoothController callback
413    @Override
414    public void onBluetoothStateChange(boolean on) {
415        mBluetoothState.enabled = on;
416        onBluetoothStateChange(mBluetoothState);
417    }
418    public void onBluetoothStateChange(BluetoothState bluetoothStateIn) {
419        // TODO: If view is in awaiting state, disable
420        Resources r = mContext.getResources();
421        mBluetoothState.enabled = bluetoothStateIn.enabled;
422        mBluetoothState.connected = bluetoothStateIn.connected;
423        if (mBluetoothState.enabled) {
424            if (mBluetoothState.connected) {
425                mBluetoothState.iconId = R.drawable.ic_qs_bluetooth_on;
426            } else {
427                mBluetoothState.iconId = R.drawable.ic_qs_bluetooth_not_connected;
428            }
429            mBluetoothState.label = r.getString(R.string.quick_settings_bluetooth_label);
430        } else {
431            mBluetoothState.iconId = R.drawable.ic_qs_bluetooth_off;
432            mBluetoothState.label = r.getString(R.string.quick_settings_bluetooth_off_label);
433        }
434        mBluetoothCallback.refreshView(mBluetoothTile, mBluetoothState);
435    }
436    void refreshBluetoothTile() {
437        if (mBluetoothTile != null) {
438            onBluetoothStateChange(mBluetoothState.enabled);
439        }
440    }
441
442    // Battery
443    void addBatteryTile(QuickSettingsTileView view, RefreshCallback cb) {
444        mBatteryTile = view;
445        mBatteryCallback = cb;
446        mBatteryCallback.refreshView(mBatteryTile, mBatteryState);
447    }
448    // BatteryController callback
449    @Override
450    public void onBatteryLevelChanged(int level, boolean pluggedIn) {
451        mBatteryState.batteryLevel = level;
452        mBatteryState.pluggedIn = pluggedIn;
453        mBatteryCallback.refreshView(mBatteryTile, mBatteryState);
454    }
455    void refreshBatteryTile() {
456        mBatteryCallback.refreshView(mBatteryTile, mBatteryState);
457    }
458
459    // Location
460    void addLocationTile(QuickSettingsTileView view, RefreshCallback cb) {
461        mLocationTile = view;
462        mLocationCallback = cb;
463        mLocationCallback.refreshView(mLocationTile, mLocationState);
464    }
465    // LocationController callback
466    @Override
467    public void onLocationGpsStateChanged(boolean inUse, String description) {
468        mLocationState.enabled = inUse;
469        mLocationState.label = description;
470        mLocationCallback.refreshView(mLocationTile, mLocationState);
471    }
472
473    // Bug report
474    void addBugreportTile(QuickSettingsTileView view, RefreshCallback cb) {
475        mBugreportTile = view;
476        mBugreportCallback = cb;
477        onBugreportChanged();
478    }
479    // SettingsObserver callback
480    public void onBugreportChanged() {
481        final ContentResolver cr = mContext.getContentResolver();
482        boolean enabled = false;
483        try {
484            enabled = (Settings.Secure.getInt(cr, Settings.Secure.BUGREPORT_IN_POWER_MENU) != 0);
485        } catch (SettingNotFoundException e) {
486        }
487
488        mBugreportState.enabled = enabled;
489        mBugreportCallback.refreshView(mBugreportTile, mBugreportState);
490    }
491
492    // Wifi Display
493    void addWifiDisplayTile(QuickSettingsTileView view, RefreshCallback cb) {
494        mWifiDisplayTile = view;
495        mWifiDisplayCallback = cb;
496    }
497    public void onWifiDisplayStateChanged(WifiDisplayStatus status) {
498        mWifiDisplayState.enabled =
499                (status.getFeatureState() == WifiDisplayStatus.FEATURE_STATE_ON);
500        if (status.getActiveDisplay() != null) {
501            mWifiDisplayState.label = status.getActiveDisplay().getFriendlyDisplayName();
502        } else {
503            mWifiDisplayState.label = mContext.getString(
504                    R.string.quick_settings_wifi_display_no_connection_label);
505        }
506        mWifiDisplayCallback.refreshView(mWifiDisplayTile, mWifiDisplayState);
507
508    }
509
510    // IME
511    void addImeTile(QuickSettingsTileView view, RefreshCallback cb) {
512        mImeTile = view;
513        mImeCallback = cb;
514        mImeCallback.refreshView(mImeTile, mImeState);
515    }
516    /* This implementation is taken from
517       InputMethodManagerService.needsToShowImeSwitchOngoingNotification(). */
518    private boolean needsToShowImeSwitchOngoingNotification(InputMethodManager imm) {
519        List<InputMethodInfo> imis = imm.getEnabledInputMethodList();
520        final int N = imis.size();
521        if (N > 2) return true;
522        if (N < 1) return false;
523        int nonAuxCount = 0;
524        int auxCount = 0;
525        InputMethodSubtype nonAuxSubtype = null;
526        InputMethodSubtype auxSubtype = null;
527        for(int i = 0; i < N; ++i) {
528            final InputMethodInfo imi = imis.get(i);
529            final List<InputMethodSubtype> subtypes = imm.getEnabledInputMethodSubtypeList(imi,
530                    true);
531            final int subtypeCount = subtypes.size();
532            if (subtypeCount == 0) {
533                ++nonAuxCount;
534            } else {
535                for (int j = 0; j < subtypeCount; ++j) {
536                    final InputMethodSubtype subtype = subtypes.get(j);
537                    if (!subtype.isAuxiliary()) {
538                        ++nonAuxCount;
539                        nonAuxSubtype = subtype;
540                    } else {
541                        ++auxCount;
542                        auxSubtype = subtype;
543                    }
544                }
545            }
546        }
547        if (nonAuxCount > 1 || auxCount > 1) {
548            return true;
549        } else if (nonAuxCount == 1 && auxCount == 1) {
550            if (nonAuxSubtype != null && auxSubtype != null
551                    && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
552                            || auxSubtype.overridesImplicitlyEnabledSubtype()
553                            || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
554                    && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
555                return false;
556            }
557            return true;
558        }
559        return false;
560    }
561    void onImeWindowStatusChanged(boolean visible) {
562        InputMethodManager imm =
563                (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
564        List<InputMethodInfo> imis = imm.getInputMethodList();
565
566        mImeState.enabled = (visible && needsToShowImeSwitchOngoingNotification(imm));
567        mImeState.label = getCurrentInputMethodName(mContext, mContext.getContentResolver(),
568                imm, imis, mContext.getPackageManager());
569        if (mImeCallback != null) {
570            mImeCallback.refreshView(mImeTile, mImeState);
571        }
572    }
573    private static String getCurrentInputMethodName(Context context, ContentResolver resolver,
574            InputMethodManager imm, List<InputMethodInfo> imis, PackageManager pm) {
575        if (resolver == null || imis == null) return null;
576        final String currentInputMethodId = Settings.Secure.getString(resolver,
577                Settings.Secure.DEFAULT_INPUT_METHOD);
578        if (TextUtils.isEmpty(currentInputMethodId)) return null;
579        for (InputMethodInfo imi : imis) {
580            if (currentInputMethodId.equals(imi.getId())) {
581                final InputMethodSubtype subtype = imm.getCurrentInputMethodSubtype();
582                final CharSequence summary = subtype != null
583                        ? subtype.getDisplayName(context, imi.getPackageName(),
584                                imi.getServiceInfo().applicationInfo)
585                        : context.getString(R.string.quick_settings_ime_label);
586                return summary.toString();
587            }
588        }
589        return null;
590    }
591
592    // Rotation lock
593    void addRotationLockTile(QuickSettingsTileView view, RefreshCallback cb) {
594        mRotationLockTile = view;
595        mRotationLockCallback = cb;
596        onRotationLockChanged();
597    }
598    void onRotationLockChanged() {
599        boolean locked = RotationPolicy.isRotationLocked(mContext);
600        mRotationLockState.enabled = locked;
601        mRotationLockState.iconId = locked
602                ? R.drawable.ic_qs_rotation_locked
603                : R.drawable.ic_qs_auto_rotate;
604        mRotationLockState.label = locked
605                ? mContext.getString(R.string.quick_settings_rotation_locked_label)
606                : mContext.getString(R.string.quick_settings_rotation_unlocked_label);
607
608        // may be called before addRotationLockTile due to RotationPolicyListener in QuickSettings
609        if (mRotationLockTile != null && mRotationLockCallback != null) {
610            mRotationLockCallback.refreshView(mRotationLockTile, mRotationLockState);
611        }
612    }
613    void refreshRotationLockTile() {
614        if (mRotationLockTile != null) {
615            onRotationLockChanged();
616        }
617    }
618
619    // Brightness
620    void addBrightnessTile(QuickSettingsTileView view, RefreshCallback cb) {
621        mBrightnessTile = view;
622        mBrightnessCallback = cb;
623        onBrightnessLevelChanged();
624    }
625    @Override
626    public void onBrightnessLevelChanged() {
627        Resources r = mContext.getResources();
628        int mode = Settings.System.getInt(mContext.getContentResolver(),
629                Settings.System.SCREEN_BRIGHTNESS_MODE,
630                Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
631        mBrightnessState.autoBrightness =
632                (mode == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
633        mBrightnessState.iconId = mBrightnessState.autoBrightness
634                ? R.drawable.ic_qs_brightness_auto_on
635                : R.drawable.ic_qs_brightness_auto_off;
636        mBrightnessState.label = r.getString(R.string.quick_settings_brightness_label);
637        mBrightnessCallback.refreshView(mBrightnessTile, mBrightnessState);
638    }
639    void refreshBrightnessTile() {
640        onBrightnessLevelChanged();
641    }
642
643    // User switch: need to update visuals of all tiles known to have per-user state
644    void onUserSwitched(Intent intent) {
645        onRotationLockChanged();
646        onBrightnessLevelChanged();
647        onNextAlarmChanged();
648        onBugreportChanged();
649    }
650}
651