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