blob: e1b58fe4c2d61d1a7a46a4dccaf8e8f91d6ec6a0 [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
Rohan Shahe4071122018-01-22 15:16:09 -080019import android.annotation.Nullable;
Jake Whatleyac7c5d22017-02-02 20:25:12 -050020import android.content.ComponentName;
Jason Monk76c67aa2016-02-19 14:49:42 -050021import android.content.Intent;
Jason Monk49a27962016-05-25 14:22:01 -040022import android.content.IntentFilter;
Sudheer Shankab6fc9312016-01-27 19:59:03 +000023import android.os.UserManager;
24
Jason Monk49a27962016-05-25 14:22:01 -040025import android.provider.Settings.Global;
jackqdyulei76471a52016-12-05 15:03:56 -080026import android.service.quicksettings.Tile;
Julia Reynolds20aef8a2016-05-04 16:44:08 -040027import android.widget.Switch;
28
Tamas Berghammer383db5eb2016-06-22 15:21:38 +010029import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Jason Monk9c7844c2017-01-18 15:21:53 -050030import com.android.systemui.Dependency;
John Spurlockaf8d6c42014-05-07 17:49:08 -040031import com.android.systemui.R;
Jason Monk49a27962016-05-25 14:22:01 -040032import com.android.systemui.qs.GlobalSetting;
Jason Monk702e2eb2017-03-03 16:53:44 -050033import com.android.systemui.qs.QSHost;
34import com.android.systemui.plugins.qs.QSTile.AirplaneBooleanState;
35import com.android.systemui.qs.tileimpl.QSTileImpl;
Jason Monk51e4dc02014-07-22 12:00:47 -040036import com.android.systemui.statusbar.policy.HotspotController;
John Spurlockaf8d6c42014-05-07 17:49:08 -040037
38/** Quick settings tile: Hotspot **/
Jason Monk702e2eb2017-03-03 16:53:44 -050039public class HotspotTile extends QSTileImpl<AirplaneBooleanState> {
Jake Whatleyac7c5d22017-02-02 20:25:12 -050040 static final Intent TETHER_SETTINGS = new Intent().setComponent(new ComponentName(
Rohan Shahe4071122018-01-22 15:16:09 -080041 "com.android.settings", "com.android.settings.TetherSettings"));
Jake Whatleyac7c5d22017-02-02 20:25:12 -050042
Evan Lairdbcdd0462017-05-15 12:51:34 -040043 private final Icon mEnabledStatic = ResourceIcon.get(R.drawable.ic_hotspot);
Jason Monkab17c982016-06-20 12:39:38 -040044 private final Icon mUnavailable = ResourceIcon.get(R.drawable.ic_hotspot_unavailable);
45
Jason Monk51e4dc02014-07-22 12:00:47 -040046 private final HotspotController mController;
Jason Monk51e4dc02014-07-22 12:00:47 -040047 private final Callback mCallback = new Callback();
Jason Monk49a27962016-05-25 14:22:01 -040048 private final GlobalSetting mAirplaneMode;
49 private boolean mListening;
John Spurlockaf8d6c42014-05-07 17:49:08 -040050
Jason Monk702e2eb2017-03-03 16:53:44 -050051 public HotspotTile(QSHost host) {
John Spurlockaf8d6c42014-05-07 17:49:08 -040052 super(host);
Jason Monk9c7844c2017-01-18 15:21:53 -050053 mController = Dependency.get(HotspotController.class);
Jason Monk49a27962016-05-25 14:22:01 -040054 mAirplaneMode = new GlobalSetting(mContext, mHandler, Global.AIRPLANE_MODE_ON) {
55 @Override
56 protected void handleValueChanged(int value) {
57 refreshState();
58 }
59 };
John Spurlockbceed062014-08-10 18:04:16 -040060 }
61
62 @Override
Jason Monk28680c62016-04-19 09:31:20 -040063 public boolean isAvailable() {
64 return mController.isHotspotSupported();
65 }
66
67 @Override
John Spurlockbceed062014-08-10 18:04:16 -040068 protected void handleDestroy() {
69 super.handleDestroy();
John Spurlockaf8d6c42014-05-07 17:49:08 -040070 }
71
72 @Override
Jason Monkab17c982016-06-20 12:39:38 -040073 public AirplaneBooleanState newTileState() {
74 return new AirplaneBooleanState();
John Spurlockaf8d6c42014-05-07 17:49:08 -040075 }
76
77 @Override
Jason Monk1c6116c2017-09-06 17:33:01 -040078 public void handleSetListening(boolean listening) {
Jason Monk49a27962016-05-25 14:22:01 -040079 if (mListening == listening) return;
80 mListening = listening;
Jason Monk51e4dc02014-07-22 12:00:47 -040081 if (listening) {
82 mController.addCallback(mCallback);
Jason Monk49a27962016-05-25 14:22:01 -040083 final IntentFilter filter = new IntentFilter();
84 filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
Jason Monk94f682e2016-06-23 13:23:01 -040085 refreshState();
Jason Monk51e4dc02014-07-22 12:00:47 -040086 } else {
87 mController.removeCallback(mCallback);
Jason Monk51e4dc02014-07-22 12:00:47 -040088 }
Jason Monk49a27962016-05-25 14:22:01 -040089 mAirplaneMode.setListening(listening);
John Spurlockaf8d6c42014-05-07 17:49:08 -040090 }
91
92 @Override
Jason Monk76c67aa2016-02-19 14:49:42 -050093 public Intent getLongClickIntent() {
Jake Whatleyac7c5d22017-02-02 20:25:12 -050094 return new Intent(TETHER_SETTINGS);
Jason Monk76c67aa2016-02-19 14:49:42 -050095 }
96
97 @Override
John Spurlockaf8d6c42014-05-07 17:49:08 -040098 protected void handleClick() {
Jason Monk51e4dc02014-07-22 12:00:47 -040099 final boolean isEnabled = (Boolean) mState.value;
Jason Monk49a27962016-05-25 14:22:01 -0400100 if (!isEnabled && mAirplaneMode.getValue() != 0) {
101 return;
102 }
Jason Monk51e4dc02014-07-22 12:00:47 -0400103 mController.setHotspotEnabled(!isEnabled);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400104 }
105
106 @Override
Jason Monk39c98e62016-03-16 09:18:35 -0400107 public CharSequence getTileLabel() {
108 return mContext.getString(R.string.quick_settings_hotspot_label);
109 }
110
111 @Override
Jason Monkab17c982016-06-20 12:39:38 -0400112 protected void handleUpdateState(AirplaneBooleanState state, Object arg) {
Jason Monk395617f2017-05-05 14:07:58 -0400113 if (state.slash == null) {
114 state.slash = new SlashState();
115 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400116 state.label = mContext.getString(R.string.quick_settings_hotspot_label);
Jason Monk51e4dc02014-07-22 12:00:47 -0400117
Sudheer Shankaa8fbbb32016-02-11 17:17:57 +0000118 checkIfRestrictionEnforcedByAdminOnly(state, UserManager.DISALLOW_CONFIG_TETHERING);
Rohan Shahe4071122018-01-22 15:16:09 -0800119
120 final int numConnectedDevices;
121 if (arg instanceof CallbackInfo) {
122 CallbackInfo info = (CallbackInfo) arg;
123 state.value = info.enabled;
124 numConnectedDevices = info.numConnectedDevices;
Julia Reynoldsb8a46a62015-07-14 13:36:30 -0400125 } else {
Jason Monkdd5bdc62015-07-20 12:18:38 -0400126 state.value = mController.isHotspotEnabled();
Rohan Shahe4071122018-01-22 15:16:09 -0800127 numConnectedDevices = mController.getNumConnectedDevices();
Julia Reynoldsb8a46a62015-07-14 13:36:30 -0400128 }
Rohan Shahe4071122018-01-22 15:16:09 -0800129
130 state.secondaryLabel = getSecondaryLabel(state.value, numConnectedDevices);
131
Jason Monk395617f2017-05-05 14:07:58 -0400132 state.icon = mEnabledStatic;
Jason Monkab17c982016-06-20 12:39:38 -0400133 state.isAirplaneMode = mAirplaneMode.getValue() != 0;
Jason Monke645aee2017-03-31 13:19:26 -0400134 state.isTransient = mController.isHotspotTransient();
Jason Monk395617f2017-05-05 14:07:58 -0400135 state.slash.isSlashed = !state.value && !state.isTransient;
Jason Monke645aee2017-03-31 13:19:26 -0400136 if (state.isTransient) {
137 state.icon = ResourceIcon.get(R.drawable.ic_hotspot_transient_animation);
Jason Monk49a27962016-05-25 14:22:01 -0400138 }
Jason Monk702e2eb2017-03-03 16:53:44 -0500139 state.expandedAccessibilityClassName = Switch.class.getName();
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400140 state.contentDescription = state.label;
Jason Monk32508852017-01-18 09:17:13 -0500141 state.state = state.isAirplaneMode ? Tile.STATE_UNAVAILABLE
Jason Monke645aee2017-03-31 13:19:26 -0400142 : state.value || state.isTransient ? Tile.STATE_ACTIVE : Tile.STATE_INACTIVE;
Jason Monk51e4dc02014-07-22 12:00:47 -0400143 }
144
Rohan Shahe4071122018-01-22 15:16:09 -0800145 @Nullable
146 private String getSecondaryLabel(boolean enabled, int numConnectedDevices) {
147 if (numConnectedDevices > 0 && enabled) {
148 return mContext.getResources().getQuantityString(
149 R.plurals.quick_settings_hotspot_num_devices,
150 numConnectedDevices,
151 numConnectedDevices);
152 }
153
154 return null;
155 }
156
Selim Cinek4fda7b22014-08-18 22:07:25 +0200157 @Override
Chris Wren457a21c2015-05-06 17:50:34 -0400158 public int getMetricsCategory() {
Chris Wrenf6e9228b2016-01-26 18:04:35 -0500159 return MetricsEvent.QS_HOTSPOT;
Chris Wren457a21c2015-05-06 17:50:34 -0400160 }
161
162 @Override
Selim Cinek4fda7b22014-08-18 22:07:25 +0200163 protected String composeChangeAnnouncement() {
164 if (mState.value) {
165 return mContext.getString(R.string.accessibility_quick_settings_hotspot_changed_on);
166 } else {
167 return mContext.getString(R.string.accessibility_quick_settings_hotspot_changed_off);
168 }
169 }
170
John Spurlocke2efefe2014-07-28 11:17:12 -0400171 private final class Callback implements HotspotController.Callback {
Rohan Shahe4071122018-01-22 15:16:09 -0800172 final CallbackInfo mCallbackInfo = new CallbackInfo();
173
Jason Monk51e4dc02014-07-22 12:00:47 -0400174 @Override
Rohan Shahe4071122018-01-22 15:16:09 -0800175 public void onHotspotChanged(boolean enabled, int numConnectedDevices) {
176 mCallbackInfo.enabled = enabled;
177 mCallbackInfo.numConnectedDevices = numConnectedDevices;
178 refreshState(mCallbackInfo);
Jason Monk51e4dc02014-07-22 12:00:47 -0400179 }
Rohan Shahe4071122018-01-22 15:16:09 -0800180 }
181
182 /**
183 * Holder for any hotspot state info that needs to passed from the callback to
184 * {@link #handleUpdateState(State, Object)}.
185 */
186 protected static final class CallbackInfo {
187 boolean enabled;
188 int numConnectedDevices;
189
190 @Override
191 public String toString() {
192 return new StringBuilder("CallbackInfo[")
193 .append("enabled=").append(enabled)
194 .append(",numConnectedDevices=").append(numConnectedDevices)
195 .append(']').toString();
196 }
197 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400198}