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