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) { |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 137 | if (enabled) { |
| 138 | OnStartTetheringCallback callback = new OnStartTetheringCallback(); |
Jason Monk | e645aee | 2017-03-31 13:19:26 -0400 | [diff] [blame] | 139 | mWaitingForCallback = true; |
| 140 | if (DEBUG) Log.d(TAG, "Starting tethering"); |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 141 | mConnectivityManager.startTethering( |
| 142 | ConnectivityManager.TETHERING_WIFI, false, callback); |
Rohan Shah | e407112 | 2018-01-22 15:16:09 -0800 | [diff] [blame] | 143 | fireHotspotChangedCallback(isHotspotEnabled()); |
Sanket Padawe | 63224c3 | 2014-12-01 18:14:40 -0800 | [diff] [blame] | 144 | } else { |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 145 | mConnectivityManager.stopTethering(ConnectivityManager.TETHERING_WIFI); |
Jason Monk | 256a226 | 2014-08-05 16:24:22 -0400 | [diff] [blame] | 146 | } |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 147 | } |
| 148 | |
Rohan Shah | e407112 | 2018-01-22 15:16:09 -0800 | [diff] [blame] | 149 | @Override |
| 150 | public int getNumConnectedDevices() { |
| 151 | return mNumConnectedDevices; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Sends a hotspot changed callback with the new enabled status. Wraps |
| 156 | * {@link #fireHotspotChangedCallback(boolean, int)} and assumes that the number of devices has |
| 157 | * not changed. |
| 158 | * |
| 159 | * @param enabled whether the hotspot is enabled |
| 160 | */ |
| 161 | private void fireHotspotChangedCallback(boolean enabled) { |
| 162 | fireHotspotChangedCallback(enabled, mNumConnectedDevices); |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Sends a hotspot changed callback with the new enabled status & the number of devices |
| 167 | * connected to the hotspot. Be careful when calling over multiple threads, especially if one of |
| 168 | * them is the main thread (as it can be blocked). |
| 169 | * |
| 170 | * @param enabled whether the hotspot is enabled |
| 171 | * @param numConnectedDevices number of devices connected to the hotspot |
| 172 | */ |
| 173 | private void fireHotspotChangedCallback(boolean enabled, int numConnectedDevices) { |
Jason Monk | ffc63f4 | 2016-03-16 15:22:15 -0400 | [diff] [blame] | 174 | synchronized (mCallbacks) { |
| 175 | for (Callback callback : mCallbacks) { |
Rohan Shah | e407112 | 2018-01-22 15:16:09 -0800 | [diff] [blame] | 176 | callback.onHotspotChanged(enabled, numConnectedDevices); |
Jason Monk | ffc63f4 | 2016-03-16 15:22:15 -0400 | [diff] [blame] | 177 | } |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | |
Rohan Shah | e407112 | 2018-01-22 15:16:09 -0800 | [diff] [blame] | 181 | @Override |
| 182 | public void onStateChanged(int state, int failureReason) { |
| 183 | // Do nothing - we don't care about changing anything here. |
| 184 | } |
| 185 | |
| 186 | @Override |
| 187 | public void onNumClientsChanged(int numConnectedDevices) { |
| 188 | mNumConnectedDevices = numConnectedDevices; |
| 189 | fireHotspotChangedCallback(isHotspotEnabled(), numConnectedDevices); |
| 190 | } |
| 191 | |
Jason Monk | e645aee | 2017-03-31 13:19:26 -0400 | [diff] [blame] | 192 | private final class OnStartTetheringCallback extends |
| 193 | ConnectivityManager.OnStartTetheringCallback { |
| 194 | @Override |
| 195 | public void onTetheringStarted() { |
| 196 | if (DEBUG) Log.d(TAG, "onTetheringStarted"); |
| 197 | mWaitingForCallback = false; |
| 198 | // Don't fire a callback here, instead wait for the next update from wifi. |
| 199 | } |
| 200 | |
| 201 | @Override |
| 202 | public void onTetheringFailed() { |
| 203 | if (DEBUG) Log.d(TAG, "onTetheringFailed"); |
| 204 | mWaitingForCallback = false; |
Rohan Shah | e407112 | 2018-01-22 15:16:09 -0800 | [diff] [blame] | 205 | fireHotspotChangedCallback(isHotspotEnabled()); |
Jason Monk | e645aee | 2017-03-31 13:19:26 -0400 | [diff] [blame] | 206 | // TODO: Show error. |
| 207 | } |
| 208 | } |
| 209 | |
Rohan Shah | e407112 | 2018-01-22 15:16:09 -0800 | [diff] [blame] | 210 | /** |
| 211 | * Class to listen in on wifi state and update the hotspot state |
| 212 | */ |
| 213 | private final class WifiStateReceiver extends BroadcastReceiver { |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 214 | private boolean mRegistered; |
| 215 | |
| 216 | public void setListening(boolean listening) { |
| 217 | if (listening && !mRegistered) { |
| 218 | if (DEBUG) Log.d(TAG, "Registering receiver"); |
| 219 | final IntentFilter filter = new IntentFilter(); |
| 220 | filter.addAction(WifiManager.WIFI_AP_STATE_CHANGED_ACTION); |
| 221 | mContext.registerReceiver(this, filter); |
| 222 | mRegistered = true; |
| 223 | } else if (!listening && mRegistered) { |
| 224 | if (DEBUG) Log.d(TAG, "Unregistering receiver"); |
| 225 | mContext.unregisterReceiver(this); |
| 226 | mRegistered = false; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | @Override |
| 231 | public void onReceive(Context context, Intent intent) { |
Julia Reynolds | b8a46a6 | 2015-07-14 13:36:30 -0400 | [diff] [blame] | 232 | int state = intent.getIntExtra( |
| 233 | WifiManager.EXTRA_WIFI_AP_STATE, WifiManager.WIFI_AP_STATE_FAILED); |
Jason Monk | e645aee | 2017-03-31 13:19:26 -0400 | [diff] [blame] | 234 | if (DEBUG) Log.d(TAG, "onReceive " + state); |
Rohan Shah | e407112 | 2018-01-22 15:16:09 -0800 | [diff] [blame] | 235 | |
| 236 | // Update internal hotspot state for tracking before using any enabled/callback methods. |
Jason Monk | dd5bdc6 | 2015-07-20 12:18:38 -0400 | [diff] [blame] | 237 | mHotspotState = state; |
Rohan Shah | e407112 | 2018-01-22 15:16:09 -0800 | [diff] [blame] | 238 | |
| 239 | if (!isHotspotEnabled()) { |
| 240 | // Reset num devices if the hotspot is no longer enabled so we don't get ghost |
| 241 | // counters. |
| 242 | mNumConnectedDevices = 0; |
| 243 | } |
| 244 | |
| 245 | fireHotspotChangedCallback(isHotspotEnabled()); |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | } |