Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -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.statusbar.policy; |
| 18 | |
Yoshinori Hirano | adcbed7 | 2016-06-08 14:29:49 +0900 | [diff] [blame] | 19 | import android.app.ActivityManager; |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 20 | import android.content.BroadcastReceiver; |
| 21 | import android.content.Context; |
| 22 | import android.content.Intent; |
| 23 | import android.content.IntentFilter; |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 24 | import android.net.ConnectivityManager; |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 25 | import android.net.wifi.WifiManager; |
Yoshinori Hirano | adcbed7 | 2016-06-08 14:29:49 +0900 | [diff] [blame] | 26 | import android.os.UserManager; |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 27 | import android.util.Log; |
| 28 | |
Rohan Shah | e407112 | 2018-01-22 15:16:09 -0800 | [diff] [blame] | 29 | import com.android.systemui.Dependency; |
| 30 | |
Jason Monk | dd5bdc6 | 2015-07-20 12:18:38 -0400 | [diff] [blame] | 31 | import java.io.FileDescriptor; |
| 32 | import java.io.PrintWriter; |
Jason Monk | 256a226 | 2014-08-05 16:24:22 -0400 | [diff] [blame] | 33 | import java.util.ArrayList; |
| 34 | |
Rohan Shah | e407112 | 2018-01-22 15:16:09 -0800 | [diff] [blame] | 35 | public class HotspotControllerImpl implements HotspotController, WifiManager.SoftApCallback { |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 36 | |
| 37 | private static final String TAG = "HotspotController"; |
| 38 | private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); |
Jason Monk | e69e5f8 | 2014-12-10 17:24:18 -0500 | [diff] [blame] | 39 | |
Rohan Shah | e407112 | 2018-01-22 15:16:09 -0800 | [diff] [blame] | 40 | private final ArrayList<Callback> mCallbacks = new ArrayList<>(); |
| 41 | private final WifiStateReceiver mWifiStateReceiver = new WifiStateReceiver(); |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 42 | private final ConnectivityManager mConnectivityManager; |
Rohan Shah | e407112 | 2018-01-22 15:16:09 -0800 | [diff] [blame] | 43 | private final WifiManager mWifiManager; |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 44 | private final Context mContext; |
Jason Monk | dd5bdc6 | 2015-07-20 12:18:38 -0400 | [diff] [blame] | 45 | |
| 46 | private int mHotspotState; |
Rohan Shah | e407112 | 2018-01-22 15:16:09 -0800 | [diff] [blame] | 47 | private int mNumConnectedDevices; |
Jason Monk | e645aee | 2017-03-31 13:19:26 -0400 | [diff] [blame] | 48 | private boolean mWaitingForCallback; |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 49 | |
| 50 | public HotspotControllerImpl(Context context) { |
| 51 | mContext = context; |
Rohan Shah | e407112 | 2018-01-22 15:16:09 -0800 | [diff] [blame] | 52 | mConnectivityManager = |
| 53 | (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); |
| 54 | mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); |
Jason Monk | dd5bdc6 | 2015-07-20 12:18:38 -0400 | [diff] [blame] | 55 | } |
| 56 | |
Jason Monk | 28680c6 | 2016-04-19 09:31:20 -0400 | [diff] [blame] | 57 | @Override |
| 58 | public boolean isHotspotSupported() { |
| 59 | return mConnectivityManager.isTetheringSupported() |
Yoshinori Hirano | adcbed7 | 2016-06-08 14:29:49 +0900 | [diff] [blame] | 60 | && mConnectivityManager.getTetherableWifiRegexs().length != 0 |
| 61 | && UserManager.get(mContext).isUserAdmin(ActivityManager.getCurrentUser()); |
Jason Monk | 28680c6 | 2016-04-19 09:31:20 -0400 | [diff] [blame] | 62 | } |
| 63 | |
Jason Monk | dd5bdc6 | 2015-07-20 12:18:38 -0400 | [diff] [blame] | 64 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 65 | pw.println("HotspotController state:"); |
| 66 | pw.print(" mHotspotEnabled="); pw.println(stateToString(mHotspotState)); |
| 67 | } |
| 68 | |
| 69 | private static String stateToString(int hotspotState) { |
| 70 | switch (hotspotState) { |
| 71 | case WifiManager.WIFI_AP_STATE_DISABLED: |
| 72 | return "DISABLED"; |
| 73 | case WifiManager.WIFI_AP_STATE_DISABLING: |
| 74 | return "DISABLING"; |
| 75 | case WifiManager.WIFI_AP_STATE_ENABLED: |
| 76 | return "ENABLED"; |
| 77 | case WifiManager.WIFI_AP_STATE_ENABLING: |
| 78 | return "ENABLING"; |
| 79 | case WifiManager.WIFI_AP_STATE_FAILED: |
| 80 | return "FAILED"; |
| 81 | } |
| 82 | return null; |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 83 | } |
| 84 | |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 85 | @Override |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 86 | public void addCallback(Callback callback) { |
Jason Monk | ffc63f4 | 2016-03-16 15:22:15 -0400 | [diff] [blame] | 87 | synchronized (mCallbacks) { |
| 88 | if (callback == null || mCallbacks.contains(callback)) return; |
| 89 | if (DEBUG) Log.d(TAG, "addCallback " + callback); |
| 90 | mCallbacks.add(callback); |
Rohan Shah | e407112 | 2018-01-22 15:16:09 -0800 | [diff] [blame] | 91 | |
| 92 | updateWifiStateListeners(!mCallbacks.isEmpty()); |
Jason Monk | ffc63f4 | 2016-03-16 15:22:15 -0400 | [diff] [blame] | 93 | } |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 94 | } |
| 95 | |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 96 | @Override |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 97 | public void removeCallback(Callback callback) { |
| 98 | if (callback == null) return; |
| 99 | if (DEBUG) Log.d(TAG, "removeCallback " + callback); |
Jason Monk | ffc63f4 | 2016-03-16 15:22:15 -0400 | [diff] [blame] | 100 | synchronized (mCallbacks) { |
| 101 | mCallbacks.remove(callback); |
Rohan Shah | e407112 | 2018-01-22 15:16:09 -0800 | [diff] [blame] | 102 | |
| 103 | updateWifiStateListeners(!mCallbacks.isEmpty()); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Updates the wifi state receiver to either start or stop listening to get updates to the |
| 109 | * hotspot status. Additionally starts listening to wifi manager state to track the number of |
| 110 | * connected devices. |
| 111 | * |
| 112 | * @param shouldListen whether we should start listening to various wifi statuses |
| 113 | */ |
| 114 | private void updateWifiStateListeners(boolean shouldListen) { |
| 115 | mWifiStateReceiver.setListening(shouldListen); |
| 116 | if (shouldListen) { |
| 117 | mWifiManager.registerSoftApCallback( |
| 118 | this, |
| 119 | Dependency.get(Dependency.MAIN_HANDLER)); |
| 120 | } else { |
| 121 | mWifiManager.unregisterSoftApCallback(this); |
Jason Monk | ffc63f4 | 2016-03-16 15:22:15 -0400 | [diff] [blame] | 122 | } |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public boolean isHotspotEnabled() { |
Jason Monk | dd5bdc6 | 2015-07-20 12:18:38 -0400 | [diff] [blame] | 127 | return mHotspotState == WifiManager.WIFI_AP_STATE_ENABLED; |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 128 | } |
| 129 | |
Jason Monk | e645aee | 2017-03-31 13:19:26 -0400 | [diff] [blame] | 130 | @Override |
| 131 | public boolean isHotspotTransient() { |
| 132 | return mWaitingForCallback || (mHotspotState == WifiManager.WIFI_AP_STATE_ENABLING); |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 133 | } |
| 134 | |
Jason Monk | 20ef402 | 2014-10-07 14:22:59 -0400 | [diff] [blame] | 135 | @Override |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 136 | public void setHotspotEnabled(boolean enabled) { |
Amin Shaikh | d967f82 | 2018-06-19 17:58:35 -0400 | [diff] [blame] | 137 | if (mWaitingForCallback) { |
| 138 | if (DEBUG) Log.d(TAG, "Ignoring setHotspotEnabled; waiting for callback."); |
| 139 | return; |
| 140 | } |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 141 | if (enabled) { |
| 142 | OnStartTetheringCallback callback = new OnStartTetheringCallback(); |
Jason Monk | e645aee | 2017-03-31 13:19:26 -0400 | [diff] [blame] | 143 | mWaitingForCallback = true; |
| 144 | if (DEBUG) Log.d(TAG, "Starting tethering"); |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 145 | mConnectivityManager.startTethering( |
| 146 | ConnectivityManager.TETHERING_WIFI, false, callback); |
Sanket Padawe | 63224c3 | 2014-12-01 18:14:40 -0800 | [diff] [blame] | 147 | } else { |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 148 | mConnectivityManager.stopTethering(ConnectivityManager.TETHERING_WIFI); |
Jason Monk | 256a226 | 2014-08-05 16:24:22 -0400 | [diff] [blame] | 149 | } |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 150 | } |
| 151 | |
Rohan Shah | e407112 | 2018-01-22 15:16:09 -0800 | [diff] [blame] | 152 | @Override |
| 153 | public int getNumConnectedDevices() { |
| 154 | return mNumConnectedDevices; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Sends a hotspot changed callback with the new enabled status. Wraps |
| 159 | * {@link #fireHotspotChangedCallback(boolean, int)} and assumes that the number of devices has |
| 160 | * not changed. |
| 161 | * |
| 162 | * @param enabled whether the hotspot is enabled |
| 163 | */ |
| 164 | private void fireHotspotChangedCallback(boolean enabled) { |
| 165 | fireHotspotChangedCallback(enabled, mNumConnectedDevices); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Sends a hotspot changed callback with the new enabled status & the number of devices |
| 170 | * connected to the hotspot. Be careful when calling over multiple threads, especially if one of |
| 171 | * them is the main thread (as it can be blocked). |
| 172 | * |
| 173 | * @param enabled whether the hotspot is enabled |
| 174 | * @param numConnectedDevices number of devices connected to the hotspot |
| 175 | */ |
| 176 | private void fireHotspotChangedCallback(boolean enabled, int numConnectedDevices) { |
Jason Monk | ffc63f4 | 2016-03-16 15:22:15 -0400 | [diff] [blame] | 177 | synchronized (mCallbacks) { |
| 178 | for (Callback callback : mCallbacks) { |
Rohan Shah | e407112 | 2018-01-22 15:16:09 -0800 | [diff] [blame] | 179 | callback.onHotspotChanged(enabled, numConnectedDevices); |
Jason Monk | ffc63f4 | 2016-03-16 15:22:15 -0400 | [diff] [blame] | 180 | } |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | |
Rohan Shah | e407112 | 2018-01-22 15:16:09 -0800 | [diff] [blame] | 184 | @Override |
| 185 | public void onStateChanged(int state, int failureReason) { |
| 186 | // Do nothing - we don't care about changing anything here. |
| 187 | } |
| 188 | |
| 189 | @Override |
| 190 | public void onNumClientsChanged(int numConnectedDevices) { |
| 191 | mNumConnectedDevices = numConnectedDevices; |
| 192 | fireHotspotChangedCallback(isHotspotEnabled(), numConnectedDevices); |
| 193 | } |
| 194 | |
Jason Monk | e645aee | 2017-03-31 13:19:26 -0400 | [diff] [blame] | 195 | private final class OnStartTetheringCallback extends |
| 196 | ConnectivityManager.OnStartTetheringCallback { |
| 197 | @Override |
| 198 | public void onTetheringStarted() { |
| 199 | if (DEBUG) Log.d(TAG, "onTetheringStarted"); |
| 200 | mWaitingForCallback = false; |
| 201 | // Don't fire a callback here, instead wait for the next update from wifi. |
| 202 | } |
| 203 | |
| 204 | @Override |
| 205 | public void onTetheringFailed() { |
| 206 | if (DEBUG) Log.d(TAG, "onTetheringFailed"); |
| 207 | mWaitingForCallback = false; |
Rohan Shah | e407112 | 2018-01-22 15:16:09 -0800 | [diff] [blame] | 208 | fireHotspotChangedCallback(isHotspotEnabled()); |
Jason Monk | e645aee | 2017-03-31 13:19:26 -0400 | [diff] [blame] | 209 | // TODO: Show error. |
| 210 | } |
| 211 | } |
| 212 | |
Rohan Shah | e407112 | 2018-01-22 15:16:09 -0800 | [diff] [blame] | 213 | /** |
| 214 | * Class to listen in on wifi state and update the hotspot state |
| 215 | */ |
| 216 | private final class WifiStateReceiver extends BroadcastReceiver { |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 217 | private boolean mRegistered; |
| 218 | |
| 219 | public void setListening(boolean listening) { |
| 220 | if (listening && !mRegistered) { |
| 221 | if (DEBUG) Log.d(TAG, "Registering receiver"); |
| 222 | final IntentFilter filter = new IntentFilter(); |
| 223 | filter.addAction(WifiManager.WIFI_AP_STATE_CHANGED_ACTION); |
| 224 | mContext.registerReceiver(this, filter); |
| 225 | mRegistered = true; |
| 226 | } else if (!listening && mRegistered) { |
| 227 | if (DEBUG) Log.d(TAG, "Unregistering receiver"); |
| 228 | mContext.unregisterReceiver(this); |
| 229 | mRegistered = false; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | @Override |
| 234 | public void onReceive(Context context, Intent intent) { |
Julia Reynolds | b8a46a6 | 2015-07-14 13:36:30 -0400 | [diff] [blame] | 235 | int state = intent.getIntExtra( |
| 236 | WifiManager.EXTRA_WIFI_AP_STATE, WifiManager.WIFI_AP_STATE_FAILED); |
Jason Monk | e645aee | 2017-03-31 13:19:26 -0400 | [diff] [blame] | 237 | if (DEBUG) Log.d(TAG, "onReceive " + state); |
Rohan Shah | e407112 | 2018-01-22 15:16:09 -0800 | [diff] [blame] | 238 | |
| 239 | // Update internal hotspot state for tracking before using any enabled/callback methods. |
Jason Monk | dd5bdc6 | 2015-07-20 12:18:38 -0400 | [diff] [blame] | 240 | mHotspotState = state; |
Rohan Shah | e407112 | 2018-01-22 15:16:09 -0800 | [diff] [blame] | 241 | |
| 242 | if (!isHotspotEnabled()) { |
| 243 | // Reset num devices if the hotspot is no longer enabled so we don't get ghost |
| 244 | // counters. |
| 245 | mNumConnectedDevices = 0; |
| 246 | } |
| 247 | |
| 248 | fireHotspotChangedCallback(isHotspotEnabled()); |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | } |