John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 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 | |
| 17 | package com.android.systemui.qs.tiles; |
| 18 | |
Rohan Shah | e407112 | 2018-01-22 15:16:09 -0800 | [diff] [blame] | 19 | import android.annotation.Nullable; |
Jake Whatley | ac7c5d2 | 2017-02-02 20:25:12 -0500 | [diff] [blame] | 20 | import android.content.ComponentName; |
Jason Monk | 76c67aa | 2016-02-19 14:49:42 -0500 | [diff] [blame] | 21 | import android.content.Intent; |
Sudheer Shanka | b6fc931 | 2016-01-27 19:59:03 +0000 | [diff] [blame] | 22 | import android.os.UserManager; |
Jason Monk | 49a2796 | 2016-05-25 14:22:01 -0400 | [diff] [blame] | 23 | import android.provider.Settings.Global; |
jackqdyulei | 76471a5 | 2016-12-05 15:03:56 -0800 | [diff] [blame] | 24 | import android.service.quicksettings.Tile; |
Julia Reynolds | 20aef8a | 2016-05-04 16:44:08 -0400 | [diff] [blame] | 25 | import android.widget.Switch; |
| 26 | |
Tamas Berghammer | 383db5eb | 2016-06-22 15:21:38 +0100 | [diff] [blame] | 27 | import com.android.internal.logging.nano.MetricsProto.MetricsEvent; |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 28 | import com.android.systemui.Dependency; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 29 | import com.android.systemui.R; |
Amin Shaikh | d03a743 | 2018-03-01 15:46:55 -0500 | [diff] [blame] | 30 | import com.android.systemui.plugins.qs.QSTile.AirplaneBooleanState; |
Jason Monk | 49a2796 | 2016-05-25 14:22:01 -0400 | [diff] [blame] | 31 | import com.android.systemui.qs.GlobalSetting; |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 32 | import com.android.systemui.qs.QSHost; |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 33 | import com.android.systemui.qs.tileimpl.QSTileImpl; |
Rohan Shah | 815fa98 | 2018-03-01 13:15:13 -0800 | [diff] [blame] | 34 | import com.android.systemui.statusbar.policy.DataSaverController; |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 35 | import com.android.systemui.statusbar.policy.HotspotController; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 36 | |
Jason Monk | 5d57720 | 2018-12-26 15:43:06 -0500 | [diff] [blame^] | 37 | import javax.inject.Inject; |
| 38 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 39 | /** Quick settings tile: Hotspot **/ |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 40 | public class HotspotTile extends QSTileImpl<AirplaneBooleanState> { |
Rohan Shah | 815fa98 | 2018-03-01 13:15:13 -0800 | [diff] [blame] | 41 | private static final Intent TETHER_SETTINGS = new Intent().setComponent(new ComponentName( |
Rohan Shah | e407112 | 2018-01-22 15:16:09 -0800 | [diff] [blame] | 42 | "com.android.settings", "com.android.settings.TetherSettings")); |
Jake Whatley | ac7c5d2 | 2017-02-02 20:25:12 -0500 | [diff] [blame] | 43 | |
Evan Laird | bcdd046 | 2017-05-15 12:51:34 -0400 | [diff] [blame] | 44 | private final Icon mEnabledStatic = ResourceIcon.get(R.drawable.ic_hotspot); |
Jason Monk | ab17c98 | 2016-06-20 12:39:38 -0400 | [diff] [blame] | 45 | |
Rohan Shah | 815fa98 | 2018-03-01 13:15:13 -0800 | [diff] [blame] | 46 | private final HotspotController mHotspotController; |
| 47 | private final DataSaverController mDataSaverController; |
| 48 | |
| 49 | private final HotspotAndDataSaverCallbacks mCallbacks = new HotspotAndDataSaverCallbacks(); |
Jason Monk | 49a2796 | 2016-05-25 14:22:01 -0400 | [diff] [blame] | 50 | private final GlobalSetting mAirplaneMode; |
| 51 | private boolean mListening; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 52 | |
Jason Monk | 5d57720 | 2018-12-26 15:43:06 -0500 | [diff] [blame^] | 53 | @Inject |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 54 | public HotspotTile(QSHost host) { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 55 | super(host); |
Rohan Shah | 815fa98 | 2018-03-01 13:15:13 -0800 | [diff] [blame] | 56 | mHotspotController = Dependency.get(HotspotController.class); |
| 57 | mDataSaverController = Dependency.get(DataSaverController.class); |
Jason Monk | 49a2796 | 2016-05-25 14:22:01 -0400 | [diff] [blame] | 58 | mAirplaneMode = new GlobalSetting(mContext, mHandler, Global.AIRPLANE_MODE_ON) { |
| 59 | @Override |
| 60 | protected void handleValueChanged(int value) { |
| 61 | refreshState(); |
| 62 | } |
| 63 | }; |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | @Override |
Jason Monk | 28680c6 | 2016-04-19 09:31:20 -0400 | [diff] [blame] | 67 | public boolean isAvailable() { |
Rohan Shah | 815fa98 | 2018-03-01 13:15:13 -0800 | [diff] [blame] | 68 | return mHotspotController.isHotspotSupported(); |
Jason Monk | 28680c6 | 2016-04-19 09:31:20 -0400 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | @Override |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 72 | protected void handleDestroy() { |
| 73 | super.handleDestroy(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | @Override |
Jason Monk | ab17c98 | 2016-06-20 12:39:38 -0400 | [diff] [blame] | 77 | public AirplaneBooleanState newTileState() { |
| 78 | return new AirplaneBooleanState(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | @Override |
Jason Monk | 1c6116c | 2017-09-06 17:33:01 -0400 | [diff] [blame] | 82 | public void handleSetListening(boolean listening) { |
Jason Monk | 49a2796 | 2016-05-25 14:22:01 -0400 | [diff] [blame] | 83 | if (mListening == listening) return; |
| 84 | mListening = listening; |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 85 | if (listening) { |
Rohan Shah | 815fa98 | 2018-03-01 13:15:13 -0800 | [diff] [blame] | 86 | mHotspotController.addCallback(mCallbacks); |
| 87 | mDataSaverController.addCallback(mCallbacks); |
Jason Monk | 94f682e | 2016-06-23 13:23:01 -0400 | [diff] [blame] | 88 | refreshState(); |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 89 | } else { |
Rohan Shah | 815fa98 | 2018-03-01 13:15:13 -0800 | [diff] [blame] | 90 | mHotspotController.removeCallback(mCallbacks); |
| 91 | mDataSaverController.removeCallback(mCallbacks); |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 92 | } |
Jason Monk | 49a2796 | 2016-05-25 14:22:01 -0400 | [diff] [blame] | 93 | mAirplaneMode.setListening(listening); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | @Override |
Jason Monk | 76c67aa | 2016-02-19 14:49:42 -0500 | [diff] [blame] | 97 | public Intent getLongClickIntent() { |
Jake Whatley | ac7c5d2 | 2017-02-02 20:25:12 -0500 | [diff] [blame] | 98 | return new Intent(TETHER_SETTINGS); |
Jason Monk | 76c67aa | 2016-02-19 14:49:42 -0500 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | @Override |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 102 | protected void handleClick() { |
Amin Shaikh | d03a743 | 2018-03-01 15:46:55 -0500 | [diff] [blame] | 103 | final boolean isEnabled = mState.value; |
Amin Shaikh | c26ab75f | 2018-04-23 18:29:11 -0400 | [diff] [blame] | 104 | if (!isEnabled && |
| 105 | (mAirplaneMode.getValue() != 0 || mDataSaverController.isDataSaverEnabled())) { |
Jason Monk | 49a2796 | 2016-05-25 14:22:01 -0400 | [diff] [blame] | 106 | return; |
| 107 | } |
Amin Shaikh | d03a743 | 2018-03-01 15:46:55 -0500 | [diff] [blame] | 108 | // Immediately enter transient enabling state when turning hotspot on. |
| 109 | refreshState(isEnabled ? null : ARG_SHOW_TRANSIENT_ENABLING); |
Rohan Shah | 815fa98 | 2018-03-01 13:15:13 -0800 | [diff] [blame] | 110 | mHotspotController.setHotspotEnabled(!isEnabled); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | @Override |
Jason Monk | 39c98e6 | 2016-03-16 09:18:35 -0400 | [diff] [blame] | 114 | public CharSequence getTileLabel() { |
| 115 | return mContext.getString(R.string.quick_settings_hotspot_label); |
| 116 | } |
| 117 | |
| 118 | @Override |
Jason Monk | ab17c98 | 2016-06-20 12:39:38 -0400 | [diff] [blame] | 119 | protected void handleUpdateState(AirplaneBooleanState state, Object arg) { |
Amin Shaikh | d03a743 | 2018-03-01 15:46:55 -0500 | [diff] [blame] | 120 | final boolean transientEnabling = arg == ARG_SHOW_TRANSIENT_ENABLING; |
Jason Monk | 395617f | 2017-05-05 14:07:58 -0400 | [diff] [blame] | 121 | if (state.slash == null) { |
| 122 | state.slash = new SlashState(); |
| 123 | } |
Rohan Shah | e407112 | 2018-01-22 15:16:09 -0800 | [diff] [blame] | 124 | |
| 125 | final int numConnectedDevices; |
Rohan Shah | 815fa98 | 2018-03-01 13:15:13 -0800 | [diff] [blame] | 126 | final boolean isTransient = transientEnabling || mHotspotController.isHotspotTransient(); |
| 127 | final boolean isDataSaverEnabled; |
Rohan Shah | 1b497b0 | 2018-01-24 11:28:37 -0800 | [diff] [blame] | 128 | |
| 129 | checkIfRestrictionEnforcedByAdminOnly(state, UserManager.DISALLOW_CONFIG_TETHERING); |
Rohan Shah | 815fa98 | 2018-03-01 13:15:13 -0800 | [diff] [blame] | 130 | |
Rohan Shah | e407112 | 2018-01-22 15:16:09 -0800 | [diff] [blame] | 131 | if (arg instanceof CallbackInfo) { |
Rohan Shah | 815fa98 | 2018-03-01 13:15:13 -0800 | [diff] [blame] | 132 | final CallbackInfo info = (CallbackInfo) arg; |
| 133 | state.value = transientEnabling || info.isHotspotEnabled; |
Rohan Shah | e407112 | 2018-01-22 15:16:09 -0800 | [diff] [blame] | 134 | numConnectedDevices = info.numConnectedDevices; |
Rohan Shah | 815fa98 | 2018-03-01 13:15:13 -0800 | [diff] [blame] | 135 | isDataSaverEnabled = info.isDataSaverEnabled; |
Julia Reynolds | b8a46a6 | 2015-07-14 13:36:30 -0400 | [diff] [blame] | 136 | } else { |
Rohan Shah | 815fa98 | 2018-03-01 13:15:13 -0800 | [diff] [blame] | 137 | state.value = transientEnabling || mHotspotController.isHotspotEnabled(); |
| 138 | numConnectedDevices = mHotspotController.getNumConnectedDevices(); |
| 139 | isDataSaverEnabled = mDataSaverController.isDataSaverEnabled(); |
Julia Reynolds | b8a46a6 | 2015-07-14 13:36:30 -0400 | [diff] [blame] | 140 | } |
Rohan Shah | e407112 | 2018-01-22 15:16:09 -0800 | [diff] [blame] | 141 | |
Jason Monk | 395617f | 2017-05-05 14:07:58 -0400 | [diff] [blame] | 142 | state.icon = mEnabledStatic; |
Rohan Shah | 1b497b0 | 2018-01-24 11:28:37 -0800 | [diff] [blame] | 143 | state.label = mContext.getString(R.string.quick_settings_hotspot_label); |
Jason Monk | ab17c98 | 2016-06-20 12:39:38 -0400 | [diff] [blame] | 144 | state.isAirplaneMode = mAirplaneMode.getValue() != 0; |
Rohan Shah | 1b497b0 | 2018-01-24 11:28:37 -0800 | [diff] [blame] | 145 | state.isTransient = isTransient; |
Jason Monk | 395617f | 2017-05-05 14:07:58 -0400 | [diff] [blame] | 146 | state.slash.isSlashed = !state.value && !state.isTransient; |
Jason Monk | e645aee | 2017-03-31 13:19:26 -0400 | [diff] [blame] | 147 | if (state.isTransient) { |
| 148 | state.icon = ResourceIcon.get(R.drawable.ic_hotspot_transient_animation); |
Jason Monk | 49a2796 | 2016-05-25 14:22:01 -0400 | [diff] [blame] | 149 | } |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 150 | state.expandedAccessibilityClassName = Switch.class.getName(); |
Julia Reynolds | 20aef8a | 2016-05-04 16:44:08 -0400 | [diff] [blame] | 151 | state.contentDescription = state.label; |
Rohan Shah | 815fa98 | 2018-03-01 13:15:13 -0800 | [diff] [blame] | 152 | |
| 153 | final boolean isTileUnavailable = (state.isAirplaneMode || isDataSaverEnabled); |
| 154 | final boolean isTileActive = (state.value || state.isTransient); |
Rohan Shah | 2c23b13 | 2018-03-09 13:41:11 -0800 | [diff] [blame] | 155 | |
| 156 | if (isTileUnavailable) { |
| 157 | state.state = Tile.STATE_UNAVAILABLE; |
| 158 | } else { |
| 159 | state.state = isTileActive ? Tile.STATE_ACTIVE : Tile.STATE_INACTIVE; |
| 160 | } |
| 161 | |
| 162 | state.secondaryLabel = getSecondaryLabel( |
| 163 | isTileActive, isTransient, isDataSaverEnabled, numConnectedDevices); |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 164 | } |
| 165 | |
Rohan Shah | e407112 | 2018-01-22 15:16:09 -0800 | [diff] [blame] | 166 | @Nullable |
Rohan Shah | 2c23b13 | 2018-03-09 13:41:11 -0800 | [diff] [blame] | 167 | private String getSecondaryLabel(boolean isActive, boolean isTransient, |
| 168 | boolean isDataSaverEnabled, int numConnectedDevices) { |
Rohan Shah | 1b497b0 | 2018-01-24 11:28:37 -0800 | [diff] [blame] | 169 | if (isTransient) { |
| 170 | return mContext.getString(R.string.quick_settings_hotspot_secondary_label_transient); |
Rohan Shah | 2c23b13 | 2018-03-09 13:41:11 -0800 | [diff] [blame] | 171 | } else if (isDataSaverEnabled) { |
| 172 | return mContext.getString( |
| 173 | R.string.quick_settings_hotspot_secondary_label_data_saver_enabled); |
| 174 | } else if (numConnectedDevices > 0 && isActive) { |
Rohan Shah | e407112 | 2018-01-22 15:16:09 -0800 | [diff] [blame] | 175 | return mContext.getResources().getQuantityString( |
Rohan Shah | 1b497b0 | 2018-01-24 11:28:37 -0800 | [diff] [blame] | 176 | R.plurals.quick_settings_hotspot_secondary_label_num_devices, |
Rohan Shah | e407112 | 2018-01-22 15:16:09 -0800 | [diff] [blame] | 177 | numConnectedDevices, |
| 178 | numConnectedDevices); |
| 179 | } |
| 180 | |
| 181 | return null; |
| 182 | } |
| 183 | |
Selim Cinek | 4fda7b2 | 2014-08-18 22:07:25 +0200 | [diff] [blame] | 184 | @Override |
Chris Wren | 457a21c | 2015-05-06 17:50:34 -0400 | [diff] [blame] | 185 | public int getMetricsCategory() { |
Chris Wren | f6e9228b | 2016-01-26 18:04:35 -0500 | [diff] [blame] | 186 | return MetricsEvent.QS_HOTSPOT; |
Chris Wren | 457a21c | 2015-05-06 17:50:34 -0400 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | @Override |
Selim Cinek | 4fda7b2 | 2014-08-18 22:07:25 +0200 | [diff] [blame] | 190 | protected String composeChangeAnnouncement() { |
| 191 | if (mState.value) { |
| 192 | return mContext.getString(R.string.accessibility_quick_settings_hotspot_changed_on); |
| 193 | } else { |
| 194 | return mContext.getString(R.string.accessibility_quick_settings_hotspot_changed_off); |
| 195 | } |
| 196 | } |
| 197 | |
Rohan Shah | 815fa98 | 2018-03-01 13:15:13 -0800 | [diff] [blame] | 198 | /** |
| 199 | * Listens to changes made to hotspot and data saver states (to toggle tile availability). |
| 200 | */ |
| 201 | private final class HotspotAndDataSaverCallbacks implements HotspotController.Callback, |
| 202 | DataSaverController.Listener { |
| 203 | CallbackInfo mCallbackInfo = new CallbackInfo(); |
Rohan Shah | e407112 | 2018-01-22 15:16:09 -0800 | [diff] [blame] | 204 | |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 205 | @Override |
Rohan Shah | 815fa98 | 2018-03-01 13:15:13 -0800 | [diff] [blame] | 206 | public void onDataSaverChanged(boolean isDataSaving) { |
| 207 | mCallbackInfo.isDataSaverEnabled = isDataSaving; |
| 208 | refreshState(mCallbackInfo); |
| 209 | } |
| 210 | |
| 211 | @Override |
| 212 | public void onHotspotChanged(boolean enabled, int numDevices) { |
| 213 | mCallbackInfo.isHotspotEnabled = enabled; |
| 214 | mCallbackInfo.numConnectedDevices = numDevices; |
Rohan Shah | e407112 | 2018-01-22 15:16:09 -0800 | [diff] [blame] | 215 | refreshState(mCallbackInfo); |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 216 | } |
Rohan Shah | e407112 | 2018-01-22 15:16:09 -0800 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Holder for any hotspot state info that needs to passed from the callback to |
| 221 | * {@link #handleUpdateState(State, Object)}. |
| 222 | */ |
| 223 | protected static final class CallbackInfo { |
Rohan Shah | 815fa98 | 2018-03-01 13:15:13 -0800 | [diff] [blame] | 224 | boolean isHotspotEnabled; |
Rohan Shah | e407112 | 2018-01-22 15:16:09 -0800 | [diff] [blame] | 225 | int numConnectedDevices; |
Rohan Shah | 815fa98 | 2018-03-01 13:15:13 -0800 | [diff] [blame] | 226 | boolean isDataSaverEnabled; |
Rohan Shah | e407112 | 2018-01-22 15:16:09 -0800 | [diff] [blame] | 227 | |
| 228 | @Override |
| 229 | public String toString() { |
| 230 | return new StringBuilder("CallbackInfo[") |
Rohan Shah | 815fa98 | 2018-03-01 13:15:13 -0800 | [diff] [blame] | 231 | .append("isHotspotEnabled=").append(isHotspotEnabled) |
Rohan Shah | e407112 | 2018-01-22 15:16:09 -0800 | [diff] [blame] | 232 | .append(",numConnectedDevices=").append(numConnectedDevices) |
Rohan Shah | 815fa98 | 2018-03-01 13:15:13 -0800 | [diff] [blame] | 233 | .append(",isDataSaverEnabled=").append(isDataSaverEnabled) |
Rohan Shah | e407112 | 2018-01-22 15:16:09 -0800 | [diff] [blame] | 234 | .append(']').toString(); |
| 235 | } |
| 236 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 237 | } |