blob: 8bdbf28b5cf115e1e539a900ed6dafe8167a53a3 [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;
Jason Monk76c67aa2016-02-19 14:49:42 -050021import android.provider.Settings;
Jason Monk32508852017-01-18 09:17:13 -050022import android.service.quicksettings.Tile;
Julia Reynolds20aef8a2016-05-04 16:44:08 -040023import android.widget.Switch;
24
Tamas Berghammer383db5eb2016-06-22 15:21:38 +010025import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Jason Monk9c7844c2017-01-18 15:21:53 -050026import com.android.systemui.Dependency;
John Spurlockaf8d6c42014-05-07 17:49:08 -040027import com.android.systemui.R;
Jason Monk395617f2017-05-05 14:07:58 -040028import com.android.systemui.R.drawable;
Jason Monkec34da82017-02-24 15:57:05 -050029import com.android.systemui.plugins.ActivityStarter;
Jason Monk702e2eb2017-03-03 16:53:44 -050030import com.android.systemui.qs.QSHost;
31import com.android.systemui.plugins.qs.QSTile.BooleanState;
32import com.android.systemui.qs.tileimpl.QSTileImpl;
John Spurlock657c62c2014-07-22 12:18:09 -040033import com.android.systemui.statusbar.policy.KeyguardMonitor;
John Spurlockaf8d6c42014-05-07 17:49:08 -040034import com.android.systemui.statusbar.policy.LocationController;
Jason Monk359bb742017-04-13 10:40:40 -040035import com.android.systemui.statusbar.policy.LocationController.LocationChangeCallback;
John Spurlockaf8d6c42014-05-07 17:49:08 -040036
37/** Quick settings tile: Location **/
Jason Monk702e2eb2017-03-03 16:53:44 -050038public class LocationTile extends QSTileImpl<BooleanState> {
John Spurlockaf8d6c42014-05-07 17:49:08 -040039
Evan Lairdbcdd0462017-05-15 12:51:34 -040040 private final Icon mIcon = ResourceIcon.get(drawable.ic_signal_location);
John Spurlockc6df3cf2014-11-04 19:36:44 -050041
John Spurlockaf8d6c42014-05-07 17:49:08 -040042 private final LocationController mController;
John Spurlock657c62c2014-07-22 12:18:09 -040043 private final KeyguardMonitor mKeyguard;
44 private final Callback mCallback = new Callback();
John Spurlockaf8d6c42014-05-07 17:49:08 -040045
Jason Monk702e2eb2017-03-03 16:53:44 -050046 public LocationTile(QSHost host) {
John Spurlockaf8d6c42014-05-07 17:49:08 -040047 super(host);
Jason Monk9c7844c2017-01-18 15:21:53 -050048 mController = Dependency.get(LocationController.class);
49 mKeyguard = Dependency.get(KeyguardMonitor.class);
John Spurlockaf8d6c42014-05-07 17:49:08 -040050 }
51
52 @Override
Jason Monk62b63a02016-02-02 15:15:31 -050053 public BooleanState newTileState() {
John Spurlockaf8d6c42014-05-07 17:49:08 -040054 return new BooleanState();
55 }
56
John Spurlockccb6b9a2014-05-17 15:54:40 -040057 @Override
Jason Monk1c6116c2017-09-06 17:33:01 -040058 public void handleSetListening(boolean listening) {
John Spurlockccb6b9a2014-05-17 15:54:40 -040059 if (listening) {
Jason Monk88529052016-11-04 13:29:58 -040060 mController.addCallback(mCallback);
John Spurlock657c62c2014-07-22 12:18:09 -040061 mKeyguard.addCallback(mCallback);
John Spurlockccb6b9a2014-05-17 15:54:40 -040062 } else {
Jason Monk88529052016-11-04 13:29:58 -040063 mController.removeCallback(mCallback);
John Spurlock657c62c2014-07-22 12:18:09 -040064 mKeyguard.removeCallback(mCallback);
John Spurlockccb6b9a2014-05-17 15:54:40 -040065 }
John Spurlockaf8d6c42014-05-07 17:49:08 -040066 }
67
68 @Override
Jason Monk76c67aa2016-02-19 14:49:42 -050069 public Intent getLongClickIntent() {
70 return new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
71 }
72
73 @Override
John Spurlockaf8d6c42014-05-07 17:49:08 -040074 protected void handleClick() {
Jason Monkba2318e2015-12-08 09:04:23 -050075 if (mKeyguard.isSecure() && mKeyguard.isShowing()) {
Jason Monk9c7844c2017-01-18 15:21:53 -050076 Dependency.get(ActivityStarter.class).postQSRunnableDismissingKeyguard(() -> {
77 final boolean wasEnabled = mState.value;
78 mHost.openPanels();
Jason Monk9c7844c2017-01-18 15:21:53 -050079 mController.setLocationEnabled(!wasEnabled);
Jason Monkba2318e2015-12-08 09:04:23 -050080 });
81 return;
82 }
Jason Monk9c7844c2017-01-18 15:21:53 -050083 final boolean wasEnabled = mState.value;
John Spurlockd21df602014-08-28 14:25:05 -040084 mController.setLocationEnabled(!wasEnabled);
John Spurlockaf8d6c42014-05-07 17:49:08 -040085 }
86
87 @Override
Jason Monk39c98e62016-03-16 09:18:35 -040088 public CharSequence getTileLabel() {
89 return mContext.getString(R.string.quick_settings_location_label);
90 }
91
92 @Override
John Spurlockaf8d6c42014-05-07 17:49:08 -040093 protected void handleUpdateState(BooleanState state, Object arg) {
Jason Monk395617f2017-05-05 14:07:58 -040094 if (state.slash == null) {
95 state.slash = new SlashState();
96 }
John Spurlockaf8d6c42014-05-07 17:49:08 -040097 final boolean locationEnabled = mController.isLocationEnabled();
Tom O'Neilld5289302014-09-05 08:22:47 -070098
99 // Work around for bug 15916487: don't show location tile on top of lock screen. After the
100 // bug is fixed, this should be reverted to only hiding it on secure lock screens:
101 // state.visible = !(mKeyguard.isSecure() && mKeyguard.isShowing());
John Spurlock899f4392014-06-11 10:59:11 -0400102 state.value = locationEnabled;
Sudheer Shankaa8fbbb32016-02-11 17:17:57 +0000103 checkIfRestrictionEnforcedByAdminOnly(state, UserManager.DISALLOW_SHARE_LOCATION);
yuemingw281000b2018-01-08 22:12:05 +0000104 if (state.disabledByPolicy == false) {
105 checkIfRestrictionEnforcedByAdminOnly(state, UserManager.DISALLOW_CONFIG_LOCATION_MODE);
106 }
Jason Monk395617f2017-05-05 14:07:58 -0400107 state.icon = mIcon;
108 state.slash.isSlashed = !state.value;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400109 if (locationEnabled) {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400110 state.label = mContext.getString(R.string.quick_settings_location_label);
111 state.contentDescription = mContext.getString(
Selim Cinek4fda7b22014-08-18 22:07:25 +0200112 R.string.accessibility_quick_settings_location_on);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400113 } else {
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 }
Jason Monk32508852017-01-18 09:17:13 -0500118 state.state = state.value ? Tile.STATE_ACTIVE : Tile.STATE_INACTIVE;
Jason Monk702e2eb2017-03-03 16:53:44 -0500119 state.expandedAccessibilityClassName = 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
Jason Monk359bb742017-04-13 10:40:40 -0400136 private final class Callback implements LocationChangeCallback,
John Spurlock657c62c2014-07-22 12:18:09 -0400137 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
Jason Monkaa573e92017-01-27 17:00:29 -0500144 public void onKeyguardShowingChanged() {
John Spurlock657c62c2014-07-22 12:18:09 -0400145 refreshState();
146 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400147 };
148}