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 AnimationIcon mEnable =
29            new AnimationIcon(R.drawable.ic_signal_location_enable_animation);
30    private final AnimationIcon mDisable =
31            new AnimationIcon(R.drawable.ic_signal_location_disable_animation);
32
33    private final LocationController mController;
34    private final KeyguardMonitor mKeyguard;
35    private final Callback mCallback = new Callback();
36
37    public LocationTile(Host host) {
38        super(host);
39        mController = host.getLocationController();
40        mKeyguard = host.getKeyguardMonitor();
41    }
42
43    @Override
44    protected BooleanState newTileState() {
45        return new BooleanState();
46    }
47
48    @Override
49    public void setListening(boolean listening) {
50        if (listening) {
51            mController.addSettingsChangedCallback(mCallback);
52            mKeyguard.addCallback(mCallback);
53        } else {
54            mController.removeSettingsChangedCallback(mCallback);
55            mKeyguard.removeCallback(mCallback);
56        }
57    }
58
59    @Override
60    protected void handleClick() {
61        final boolean wasEnabled = (Boolean) mState.value;
62        mController.setLocationEnabled(!wasEnabled);
63        mEnable.setAllowAnimation(true);
64        mDisable.setAllowAnimation(true);
65    }
66
67    @Override
68    protected void handleUpdateState(BooleanState state, Object arg) {
69        final boolean locationEnabled =  mController.isLocationEnabled();
70
71        // Work around for bug 15916487: don't show location tile on top of lock screen. After the
72        // bug is fixed, this should be reverted to only hiding it on secure lock screens:
73        // state.visible = !(mKeyguard.isSecure() && mKeyguard.isShowing());
74        state.visible = !mKeyguard.isShowing();
75        state.value = locationEnabled;
76        if (locationEnabled) {
77            state.icon = mEnable;
78            state.label = mContext.getString(R.string.quick_settings_location_label);
79            state.contentDescription = mContext.getString(
80                    R.string.accessibility_quick_settings_location_on);
81        } else {
82            state.icon = mDisable;
83            state.label = mContext.getString(R.string.quick_settings_location_label);
84            state.contentDescription = mContext.getString(
85                    R.string.accessibility_quick_settings_location_off);
86        }
87    }
88
89    @Override
90    protected String composeChangeAnnouncement() {
91        if (mState.value) {
92            return mContext.getString(R.string.accessibility_quick_settings_location_changed_on);
93        } else {
94            return mContext.getString(R.string.accessibility_quick_settings_location_changed_off);
95        }
96    }
97
98    private final class Callback implements LocationSettingsChangeCallback,
99            KeyguardMonitor.Callback {
100        @Override
101        public void onLocationSettingsChanged(boolean enabled) {
102            refreshState();
103        }
104
105        @Override
106        public void onKeyguardChanged() {
107            refreshState();
108        }
109    };
110}
111