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