blob: b1d1c77f9d0990d6f3b81241b49611c9312177c4 [file] [log] [blame]
John Spurlockaf8d6c42014-05-07 17:49:08 -04001/*
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
Jason Monk76c67aa2016-02-19 14:49:42 -050019import android.content.Intent;
Sudheer Shankab6fc9312016-01-27 19:59:03 +000020import android.os.UserManager;
21
Jason Monk76c67aa2016-02-19 14:49:42 -050022import android.provider.Settings;
Chris Wren457a21c2015-05-06 17:50:34 -040023import com.android.internal.logging.MetricsLogger;
Chris Wrenf6e9228b2016-01-26 18:04:35 -050024import com.android.internal.logging.MetricsProto.MetricsEvent;
John Spurlockaf8d6c42014-05-07 17:49:08 -040025import com.android.systemui.R;
26import com.android.systemui.qs.QSTile;
John Spurlock657c62c2014-07-22 12:18:09 -040027import com.android.systemui.statusbar.policy.KeyguardMonitor;
John Spurlockaf8d6c42014-05-07 17:49:08 -040028import com.android.systemui.statusbar.policy.LocationController;
29import com.android.systemui.statusbar.policy.LocationController.LocationSettingsChangeCallback;
30
31/** Quick settings tile: Location **/
32public class LocationTile extends QSTile<QSTile.BooleanState> {
33
John Spurlockc6df3cf2014-11-04 19:36:44 -050034 private final AnimationIcon mEnable =
Jason Monk1aec93f2016-03-01 09:39:30 -050035 new AnimationIcon(R.drawable.ic_signal_location_enable_animation,
36 R.drawable.ic_signal_location_disable);
John Spurlockc6df3cf2014-11-04 19:36:44 -050037 private final AnimationIcon mDisable =
Jason Monk1aec93f2016-03-01 09:39:30 -050038 new AnimationIcon(R.drawable.ic_signal_location_disable_animation,
39 R.drawable.ic_signal_location_enable);
John Spurlockc6df3cf2014-11-04 19:36:44 -050040
John Spurlockaf8d6c42014-05-07 17:49:08 -040041 private final LocationController mController;
John Spurlock657c62c2014-07-22 12:18:09 -040042 private final KeyguardMonitor mKeyguard;
43 private final Callback mCallback = new Callback();
John Spurlockaf8d6c42014-05-07 17:49:08 -040044
45 public LocationTile(Host host) {
46 super(host);
47 mController = host.getLocationController();
John Spurlock657c62c2014-07-22 12:18:09 -040048 mKeyguard = host.getKeyguardMonitor();
John Spurlockaf8d6c42014-05-07 17:49:08 -040049 }
50
51 @Override
Jason Monk62b63a02016-02-02 15:15:31 -050052 public BooleanState newTileState() {
John Spurlockaf8d6c42014-05-07 17:49:08 -040053 return new BooleanState();
54 }
55
John Spurlockccb6b9a2014-05-17 15:54:40 -040056 @Override
57 public void setListening(boolean listening) {
58 if (listening) {
59 mController.addSettingsChangedCallback(mCallback);
John Spurlock657c62c2014-07-22 12:18:09 -040060 mKeyguard.addCallback(mCallback);
John Spurlockccb6b9a2014-05-17 15:54:40 -040061 } else {
62 mController.removeSettingsChangedCallback(mCallback);
John Spurlock657c62c2014-07-22 12:18:09 -040063 mKeyguard.removeCallback(mCallback);
John Spurlockccb6b9a2014-05-17 15:54:40 -040064 }
John Spurlockaf8d6c42014-05-07 17:49:08 -040065 }
66
67 @Override
Jason Monk76c67aa2016-02-19 14:49:42 -050068 public Intent getLongClickIntent() {
69 return new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
70 }
71
72 @Override
John Spurlockaf8d6c42014-05-07 17:49:08 -040073 protected void handleClick() {
Jason Monkba2318e2015-12-08 09:04:23 -050074 if (mKeyguard.isSecure() && mKeyguard.isShowing()) {
75 mHost.startRunnableDismissingKeyguard(new Runnable() {
76 @Override
77 public void run() {
78 final boolean wasEnabled = (Boolean) mState.value;
79 mHost.openPanels();
80 MetricsLogger.action(mContext, getMetricsCategory(), !wasEnabled);
81 mController.setLocationEnabled(!wasEnabled);
Jason Monkba2318e2015-12-08 09:04:23 -050082 }
83 });
84 return;
85 }
John Spurlockaf8d6c42014-05-07 17:49:08 -040086 final boolean wasEnabled = (Boolean) mState.value;
Chris Wren9e7283f2015-05-08 17:23:47 -040087 MetricsLogger.action(mContext, getMetricsCategory(), !wasEnabled);
John Spurlockd21df602014-08-28 14:25:05 -040088 mController.setLocationEnabled(!wasEnabled);
John Spurlockaf8d6c42014-05-07 17:49:08 -040089 }
90
91 @Override
92 protected void handleUpdateState(BooleanState state, Object arg) {
93 final boolean locationEnabled = mController.isLocationEnabled();
Tom O'Neilld5289302014-09-05 08:22:47 -070094
95 // Work around for bug 15916487: don't show location tile on top of lock screen. After the
96 // bug is fixed, this should be reverted to only hiding it on secure lock screens:
97 // state.visible = !(mKeyguard.isSecure() && mKeyguard.isShowing());
John Spurlock899f4392014-06-11 10:59:11 -040098 state.value = locationEnabled;
Sudheer Shankaa8fbbb32016-02-11 17:17:57 +000099 checkIfRestrictionEnforcedByAdminOnly(state, UserManager.DISALLOW_SHARE_LOCATION);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400100 if (locationEnabled) {
John Spurlockc6df3cf2014-11-04 19:36:44 -0500101 state.icon = mEnable;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400102 state.label = mContext.getString(R.string.quick_settings_location_label);
103 state.contentDescription = mContext.getString(
Selim Cinek4fda7b22014-08-18 22:07:25 +0200104 R.string.accessibility_quick_settings_location_on);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400105 } else {
John Spurlockc6df3cf2014-11-04 19:36:44 -0500106 state.icon = mDisable;
John Spurlock012d4a22014-06-04 00:40:18 -0400107 state.label = mContext.getString(R.string.quick_settings_location_label);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400108 state.contentDescription = mContext.getString(
Selim Cinek4fda7b22014-08-18 22:07:25 +0200109 R.string.accessibility_quick_settings_location_off);
110 }
111 }
112
113 @Override
Chris Wren457a21c2015-05-06 17:50:34 -0400114 public int getMetricsCategory() {
Chris Wrenf6e9228b2016-01-26 18:04:35 -0500115 return MetricsEvent.QS_LOCATION;
Chris Wren457a21c2015-05-06 17:50:34 -0400116 }
117
118 @Override
Selim Cinek4fda7b22014-08-18 22:07:25 +0200119 protected String composeChangeAnnouncement() {
120 if (mState.value) {
121 return mContext.getString(R.string.accessibility_quick_settings_location_changed_on);
122 } else {
123 return mContext.getString(R.string.accessibility_quick_settings_location_changed_off);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400124 }
125 }
126
John Spurlock657c62c2014-07-22 12:18:09 -0400127 private final class Callback implements LocationSettingsChangeCallback,
128 KeyguardMonitor.Callback {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400129 @Override
130 public void onLocationSettingsChanged(boolean enabled) {
131 refreshState();
132 }
John Spurlock657c62c2014-07-22 12:18:09 -0400133
134 @Override
135 public void onKeyguardChanged() {
136 refreshState();
137 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400138 };
139}