1 package com.android.settings.wifi.tether; 2 3 import android.net.wifi.WifiClient; 4 import android.net.wifi.WifiManager; 5 import android.os.Handler; 6 import android.os.HandlerExecutor; 7 8 import java.lang.ref.WeakReference; 9 import java.util.List; 10 11 /** 12 * Wrapper for {@link android.net.wifi.WifiManager.SoftApCallback} to pass the robo test 13 */ 14 public class WifiTetherSoftApManager { 15 16 private WifiManager mWifiManager; 17 private WifiTetherSoftApCallback mWifiTetherSoftApCallback; 18 19 private WifiManager.SoftApCallback mSoftApCallback; 20 private Handler mHandler; 21 WifiTetherSoftApManager(WifiManager wifiManager, WifiTetherSoftApCallback wifiTetherSoftApCallback)22 WifiTetherSoftApManager(WifiManager wifiManager, 23 WifiTetherSoftApCallback wifiTetherSoftApCallback) { 24 mWifiManager = wifiManager; 25 mWifiTetherSoftApCallback = wifiTetherSoftApCallback; 26 mSoftApCallback = new WifiManagerSoftApCallback(this); 27 mHandler = new Handler(); 28 } 29 registerSoftApCallback()30 public void registerSoftApCallback() { 31 mWifiManager.registerSoftApCallback(new HandlerExecutor(mHandler), mSoftApCallback); 32 } 33 unRegisterSoftApCallback()34 public void unRegisterSoftApCallback() { 35 mWifiManager.unregisterSoftApCallback(mSoftApCallback); 36 } 37 onStateChanged(int state, int failureReason)38 void onStateChanged(int state, int failureReason) { 39 mWifiTetherSoftApCallback.onStateChanged(state, failureReason); 40 } 41 onConnectedClientsChanged(List<WifiClient> clients)42 void onConnectedClientsChanged(List<WifiClient> clients) { 43 mWifiTetherSoftApCallback.onConnectedClientsChanged(clients); 44 } 45 46 public interface WifiTetherSoftApCallback { onStateChanged(int state, int failureReason)47 void onStateChanged(int state, int failureReason); 48 49 /** 50 * Called when the connected clients to soft AP changes. 51 * 52 * @param clients the currently connected clients 53 */ onConnectedClientsChanged(List<WifiClient> clients)54 void onConnectedClientsChanged(List<WifiClient> clients); 55 } 56 57 // TODO(b/246537032):Need to declare the service callback as static class and use 58 // WeakReference to avoid the callback link being occupied by WifiManager 59 private static final class WifiManagerSoftApCallback implements 60 WifiManager.SoftApCallback { 61 WeakReference<WifiTetherSoftApManager> mMyClass; 62 WifiManagerSoftApCallback(WifiTetherSoftApManager controller)63 WifiManagerSoftApCallback(WifiTetherSoftApManager controller) { 64 mMyClass = new WeakReference<>(controller); 65 } 66 67 @Override onStateChanged(int state, int failureReason)68 public void onStateChanged(int state, int failureReason) { 69 WifiTetherSoftApManager controller = mMyClass.get(); 70 if (controller == null) return; 71 72 controller.onStateChanged(state, failureReason); 73 } 74 75 @Override onConnectedClientsChanged(List<WifiClient> clients)76 public void onConnectedClientsChanged(List<WifiClient> clients) { 77 WifiTetherSoftApManager controller = mMyClass.get(); 78 if (controller == null) return; 79 80 controller.onConnectedClientsChanged(clients); 81 } 82 } 83 } 84