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