LocationTile.java revision d528930380e9b0e6ff396d272b6837e24da555b3
1/*
2 * Copyright (C) 2014 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.qs.tiles;
18
19import com.android.systemui.R;
20import com.android.systemui.qs.QSTile;
21import com.android.systemui.statusbar.policy.KeyguardMonitor;
22import com.android.systemui.statusbar.policy.LocationController;
23import com.android.systemui.statusbar.policy.LocationController.LocationSettingsChangeCallback;
24
25/** Quick settings tile: Location **/
26public class LocationTile extends QSTile<QSTile.BooleanState> {
27
28    private final LocationController mController;
29    private final KeyguardMonitor mKeyguard;
30    private final Callback mCallback = new Callback();
31
32    public LocationTile(Host host) {
33        super(host);
34        mController = host.getLocationController();
35        mKeyguard = host.getKeyguardMonitor();
36    }
37
38    @Override
39    protected BooleanState newTileState() {
40        return new BooleanState();
41    }
42
43    @Override
44    public void setListening(boolean listening) {
45        if (listening) {
46            mController.addSettingsChangedCallback(mCallback);
47            mKeyguard.addCallback(mCallback);
48        } else {
49            mController.removeSettingsChangedCallback(mCallback);
50            mKeyguard.removeCallback(mCallback);
51        }
52    }
53
54    @Override
55    protected void handleClick() {
56        final boolean wasEnabled = (Boolean) mState.value;
57        mController.setLocationEnabled(!wasEnabled);
58    }
59
60    @Override
61    protected void handleUpdateState(BooleanState state, Object arg) {
62        final boolean locationEnabled =  mController.isLocationEnabled();
63
64        // Work around for bug 15916487: don't show location tile on top of lock screen. After the
65        // bug is fixed, this should be reverted to only hiding it on secure lock screens:
66        // state.visible = !(mKeyguard.isSecure() && mKeyguard.isShowing());
67        state.visible = !mKeyguard.isShowing();
68        state.value = locationEnabled;
69        if (locationEnabled) {
70            state.iconId = R.drawable.ic_qs_location_on;
71            state.label = mContext.getString(R.string.quick_settings_location_label);
72            state.contentDescription = mContext.getString(
73                    R.string.accessibility_quick_settings_location_on);
74        } else {
75            state.iconId = R.drawable.ic_qs_location_off;
76            state.label = mContext.getString(R.string.quick_settings_location_label);
77            state.contentDescription = mContext.getString(
78                    R.string.accessibility_quick_settings_location_off);
79        }
80    }
81
82    @Override
83    protected String composeChangeAnnouncement() {
84        if (mState.value) {
85            return mContext.getString(R.string.accessibility_quick_settings_location_changed_on);
86        } else {
87            return mContext.getString(R.string.accessibility_quick_settings_location_changed_off);
88        }
89    }
90
91    private final class Callback implements LocationSettingsChangeCallback,
92            KeyguardMonitor.Callback {
93        @Override
94        public void onLocationSettingsChanged(boolean enabled) {
95            refreshState();
96        }
97
98        @Override
99        public void onKeyguardChanged() {
100            refreshState();
101        }
102    };
103}
104