blob: 4b890754eb848dba33a1f9e302f17daa3ca50c45 [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;
Julia Reynolds20aef8a2016-05-04 16:44:08 -040023import android.widget.Switch;
24
Chris Wren457a21c2015-05-06 17:50:34 -040025import com.android.internal.logging.MetricsLogger;
Tamas Berghammercbd3f0c2016-06-22 15:21:38 +010026import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
John Spurlockaf8d6c42014-05-07 17:49:08 -040027import com.android.systemui.R;
28import com.android.systemui.qs.QSTile;
John Spurlock657c62c2014-07-22 12:18:09 -040029import com.android.systemui.statusbar.policy.KeyguardMonitor;
John Spurlockaf8d6c42014-05-07 17:49:08 -040030import com.android.systemui.statusbar.policy.LocationController;
31import com.android.systemui.statusbar.policy.LocationController.LocationSettingsChangeCallback;
32
33/** Quick settings tile: Location **/
34public class LocationTile extends QSTile<QSTile.BooleanState> {
35
John Spurlockc6df3cf2014-11-04 19:36:44 -050036 private final AnimationIcon mEnable =
Jason Monk1aec93f2016-03-01 09:39:30 -050037 new AnimationIcon(R.drawable.ic_signal_location_enable_animation,
38 R.drawable.ic_signal_location_disable);
John Spurlockc6df3cf2014-11-04 19:36:44 -050039 private final AnimationIcon mDisable =
Jason Monk1aec93f2016-03-01 09:39:30 -050040 new AnimationIcon(R.drawable.ic_signal_location_disable_animation,
41 R.drawable.ic_signal_location_enable);
John Spurlockc6df3cf2014-11-04 19:36:44 -050042
John Spurlockaf8d6c42014-05-07 17:49:08 -040043 private final LocationController mController;
John Spurlock657c62c2014-07-22 12:18:09 -040044 private final KeyguardMonitor mKeyguard;
45 private final Callback mCallback = new Callback();
John Spurlockaf8d6c42014-05-07 17:49:08 -040046
47 public LocationTile(Host host) {
48 super(host);
49 mController = host.getLocationController();
John Spurlock657c62c2014-07-22 12:18:09 -040050 mKeyguard = host.getKeyguardMonitor();
John Spurlockaf8d6c42014-05-07 17:49:08 -040051 }
52
53 @Override
Jason Monk62b63a02016-02-02 15:15:31 -050054 public BooleanState newTileState() {
John Spurlockaf8d6c42014-05-07 17:49:08 -040055 return new BooleanState();
56 }
57
John Spurlockccb6b9a2014-05-17 15:54:40 -040058 @Override
59 public void setListening(boolean listening) {
60 if (listening) {
61 mController.addSettingsChangedCallback(mCallback);
John Spurlock657c62c2014-07-22 12:18:09 -040062 mKeyguard.addCallback(mCallback);
John Spurlockccb6b9a2014-05-17 15:54:40 -040063 } else {
64 mController.removeSettingsChangedCallback(mCallback);
John Spurlock657c62c2014-07-22 12:18:09 -040065 mKeyguard.removeCallback(mCallback);
John Spurlockccb6b9a2014-05-17 15:54:40 -040066 }
John Spurlockaf8d6c42014-05-07 17:49:08 -040067 }
68
69 @Override
Jason Monk76c67aa2016-02-19 14:49:42 -050070 public Intent getLongClickIntent() {
71 return new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
72 }
73
74 @Override
John Spurlockaf8d6c42014-05-07 17:49:08 -040075 protected void handleClick() {
Jason Monkba2318e2015-12-08 09:04:23 -050076 if (mKeyguard.isSecure() && mKeyguard.isShowing()) {
77 mHost.startRunnableDismissingKeyguard(new Runnable() {
78 @Override
79 public void run() {
80 final boolean wasEnabled = (Boolean) mState.value;
81 mHost.openPanels();
82 MetricsLogger.action(mContext, getMetricsCategory(), !wasEnabled);
83 mController.setLocationEnabled(!wasEnabled);
Jason Monkba2318e2015-12-08 09:04:23 -050084 }
85 });
86 return;
87 }
John Spurlockaf8d6c42014-05-07 17:49:08 -040088 final boolean wasEnabled = (Boolean) mState.value;
Chris Wren9e7283f2015-05-08 17:23:47 -040089 MetricsLogger.action(mContext, getMetricsCategory(), !wasEnabled);
John Spurlockd21df602014-08-28 14:25:05 -040090 mController.setLocationEnabled(!wasEnabled);
John Spurlockaf8d6c42014-05-07 17:49:08 -040091 }
92
93 @Override
Jason Monk39c98e62016-03-16 09:18:35 -040094 public CharSequence getTileLabel() {
95 return mContext.getString(R.string.quick_settings_location_label);
96 }
97
98 @Override
John Spurlockaf8d6c42014-05-07 17:49:08 -040099 protected void handleUpdateState(BooleanState state, Object arg) {
100 final boolean locationEnabled = mController.isLocationEnabled();
Tom O'Neilld5289302014-09-05 08:22:47 -0700101
102 // Work around for bug 15916487: don't show location tile on top of lock screen. After the
103 // bug is fixed, this should be reverted to only hiding it on secure lock screens:
104 // state.visible = !(mKeyguard.isSecure() && mKeyguard.isShowing());
John Spurlock899f4392014-06-11 10:59:11 -0400105 state.value = locationEnabled;
Sudheer Shankaa8fbbb32016-02-11 17:17:57 +0000106 checkIfRestrictionEnforcedByAdminOnly(state, UserManager.DISALLOW_SHARE_LOCATION);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400107 if (locationEnabled) {
John Spurlockc6df3cf2014-11-04 19:36:44 -0500108 state.icon = mEnable;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400109 state.label = mContext.getString(R.string.quick_settings_location_label);
110 state.contentDescription = mContext.getString(
Selim Cinek4fda7b22014-08-18 22:07:25 +0200111 R.string.accessibility_quick_settings_location_on);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400112 } else {
John Spurlockc6df3cf2014-11-04 19:36:44 -0500113 state.icon = mDisable;
John Spurlock012d4a22014-06-04 00:40:18 -0400114 state.label = mContext.getString(R.string.quick_settings_location_label);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400115 state.contentDescription = mContext.getString(
Selim Cinek4fda7b22014-08-18 22:07:25 +0200116 R.string.accessibility_quick_settings_location_off);
117 }
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400118 state.minimalAccessibilityClassName = state.expandedAccessibilityClassName
119 = Switch.class.getName();
Selim Cinek4fda7b22014-08-18 22:07:25 +0200120 }
121
122 @Override
Chris Wren457a21c2015-05-06 17:50:34 -0400123 public int getMetricsCategory() {
Chris Wrenf6e9228b2016-01-26 18:04:35 -0500124 return MetricsEvent.QS_LOCATION;
Chris Wren457a21c2015-05-06 17:50:34 -0400125 }
126
127 @Override
Selim Cinek4fda7b22014-08-18 22:07:25 +0200128 protected String composeChangeAnnouncement() {
129 if (mState.value) {
130 return mContext.getString(R.string.accessibility_quick_settings_location_changed_on);
131 } else {
132 return mContext.getString(R.string.accessibility_quick_settings_location_changed_off);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400133 }
134 }
135
John Spurlock657c62c2014-07-22 12:18:09 -0400136 private final class Callback implements LocationSettingsChangeCallback,
137 KeyguardMonitor.Callback {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400138 @Override
139 public void onLocationSettingsChanged(boolean enabled) {
140 refreshState();
141 }
John Spurlock657c62c2014-07-22 12:18:09 -0400142
143 @Override
144 public void onKeyguardChanged() {
145 refreshState();
146 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400147 };
148}