OSDN Git Service

Adds unregisterSoftApCallback API
authorMehdi Alizadeh <mett@google.com>
Fri, 12 Jan 2018 01:56:30 +0000 (17:56 -0800)
committerMehdi Alizadeh <mett@google.com>
Thu, 18 Jan 2018 19:15:23 +0000 (11:15 -0800)
Bug: 68712383
Test: frameworks/opt/net/wifi/tests/wifitests/runtests.sh
Change-Id: I9d6a52eed722b0b0a159b73d517a9dc4d3f8107c

wifi/java/android/net/wifi/IWifiManager.aidl
wifi/java/android/net/wifi/WifiManager.java
wifi/tests/src/android/net/wifi/WifiManagerTest.java

index 6381618..e9e61a5 100644 (file)
@@ -179,6 +179,8 @@ interface IWifiManager
 
     void startSubscriptionProvisioning(in OsuProvider provider, in IProvisioningCallback callback);
 
-    void registerSoftApCallback(in IBinder binder, in ISoftApCallback callback);
+    void registerSoftApCallback(in IBinder binder, in ISoftApCallback callback, int callbackIdentifier);
+
+    void unregisterSoftApCallback(int callbackIdentifier);
 }
 
index 379e5ae..99080d6 100644 (file)
@@ -2408,14 +2408,17 @@ public class WifiManager {
      * Registers a callback for Soft AP. See {@link SoftApCallback}. Caller will receive the current
      * soft AP state and number of connected devices immediately after a successful call to this API
      * via callback. Note that receiving an immediate WIFI_AP_STATE_FAILED value for soft AP state
-     * indicates that the latest attempt to start soft AP has failed. Only one registered callback
-     * is allowed for each process, and multiple calls to this API from the same process will simply
-     * replace the previously registered callback with the new one.
+     * indicates that the latest attempt to start soft AP has failed. Caller can unregister a
+     * previously registered callback using {@link unregisterSoftApCallback}
+     * <p>
+     * Applications should have the
+     * {@link android.Manifest.permission#NETWORK_SETTINGS NETWORK_SETTINGS} permission. Callers
+     * without the permission will trigger a {@link java.lang.SecurityException}.
+     * <p>
      *
-     * @param callback A callback for the number of connected clients.
-     * @param handler  The Handler on whose thread to execute the callbacks of the {@code
-     *                 callback} object. If a null is provided then the application's main thread
-     *                 will be used.
+     * @param callback Callback for soft AP events
+     * @param handler  The Handler on whose thread to execute the callbacks of the {@code callback}
+     *                 object. If null, then the application's main thread will be used.
      *
      * @hide
      */
@@ -2423,13 +2426,33 @@ public class WifiManager {
     public void registerSoftApCallback(@NonNull SoftApCallback callback,
                                        @Nullable Handler handler) {
         if (callback == null) throw new IllegalArgumentException("callback cannot be null");
-
         Log.v(TAG, "registerSoftApCallback: callback=" + callback + ", handler=" + handler);
 
         Looper looper = (handler == null) ? mContext.getMainLooper() : handler.getLooper();
         Binder binder = new Binder();
         try {
-            mService.registerSoftApCallback(binder, new SoftApCallbackProxy(looper, callback));
+            mService.registerSoftApCallback(binder, new SoftApCallbackProxy(looper, callback),
+                    callback.hashCode());
+        } catch (RemoteException e) {
+            throw e.rethrowFromSystemServer();
+        }
+    }
+
+    /**
+     * Allow callers to unregister a previously registered callback. After calling this method,
+     * applications will no longer receive soft AP events.
+     *
+     * @param callback Callback to unregister for soft AP events
+     *
+     * @hide
+     */
+    @RequiresPermission(android.Manifest.permission.NETWORK_SETTINGS)
+    public void unregisterSoftApCallback(@NonNull SoftApCallback callback) {
+        if (callback == null) throw new IllegalArgumentException("callback cannot be null");
+        Log.v(TAG, "unregisterSoftApCallback: callback=" + callback);
+
+        try {
+            mService.unregisterSoftApCallback(callback.hashCode());
         } catch (RemoteException e) {
             throw e.rethrowFromSystemServer();
         }
index 6695522..4b5f645 100644 (file)
@@ -36,6 +36,7 @@ import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 import static org.mockito.Mockito.*;
 
 import android.content.Context;
@@ -643,9 +644,25 @@ public class WifiManagerTest {
     /**
      * Verify an IllegalArgumentException is thrown if callback is not provided.
      */
-    @Test(expected = IllegalArgumentException.class)
+    @Test
     public void registerSoftApCallbackThrowsIllegalArgumentExceptionOnNullArgumentForCallback() {
-        mWifiManager.registerSoftApCallback(null, mHandler);
+        try {
+            mWifiManager.registerSoftApCallback(null, mHandler);
+            fail("expected IllegalArgumentException");
+        } catch (IllegalArgumentException expected) {
+        }
+    }
+
+    /**
+     * Verify an IllegalArgumentException is thrown if callback is not provided.
+     */
+    @Test
+    public void unregisterSoftApCallbackThrowsIllegalArgumentExceptionOnNullArgumentForCallback() {
+        try {
+            mWifiManager.unregisterSoftApCallback(null);
+            fail("expected IllegalArgumentException");
+        } catch (IllegalArgumentException expected) {
+        }
     }
 
     /**
@@ -665,7 +682,21 @@ public class WifiManagerTest {
     public void registerSoftApCallbackCallGoesToWifiServiceImpl() throws Exception {
         mWifiManager.registerSoftApCallback(mSoftApCallback, mHandler);
         verify(mWifiService).registerSoftApCallback(any(IBinder.class),
-                any(ISoftApCallback.Stub.class));
+                any(ISoftApCallback.Stub.class), anyInt());
+    }
+
+    /**
+     * Verify the call to unregisterSoftApCallback goes to WifiServiceImpl.
+     */
+    @Test
+    public void unregisterSoftApCallbackCallGoesToWifiServiceImpl() throws Exception {
+        ArgumentCaptor<Integer> callbackIdentifier = ArgumentCaptor.forClass(Integer.class);
+        mWifiManager.registerSoftApCallback(mSoftApCallback, mHandler);
+        verify(mWifiService).registerSoftApCallback(any(IBinder.class),
+                any(ISoftApCallback.Stub.class), callbackIdentifier.capture());
+
+        mWifiManager.unregisterSoftApCallback(mSoftApCallback);
+        verify(mWifiService).unregisterSoftApCallback(eq((int) callbackIdentifier.getValue()));
     }
 
     /*
@@ -676,7 +707,8 @@ public class WifiManagerTest {
         ArgumentCaptor<ISoftApCallback.Stub> callbackCaptor =
                 ArgumentCaptor.forClass(ISoftApCallback.Stub.class);
         mWifiManager.registerSoftApCallback(mSoftApCallback, mHandler);
-        verify(mWifiService).registerSoftApCallback(any(IBinder.class), callbackCaptor.capture());
+        verify(mWifiService).registerSoftApCallback(any(IBinder.class), callbackCaptor.capture(),
+                anyInt());
 
         callbackCaptor.getValue().onStateChanged(WIFI_AP_STATE_ENABLED, 0);
         mLooper.dispatchAll();
@@ -691,7 +723,8 @@ public class WifiManagerTest {
         ArgumentCaptor<ISoftApCallback.Stub> callbackCaptor =
                 ArgumentCaptor.forClass(ISoftApCallback.Stub.class);
         mWifiManager.registerSoftApCallback(mSoftApCallback, mHandler);
-        verify(mWifiService).registerSoftApCallback(any(IBinder.class), callbackCaptor.capture());
+        verify(mWifiService).registerSoftApCallback(any(IBinder.class), callbackCaptor.capture(),
+                anyInt());
 
         final int testNumClients = 3;
         callbackCaptor.getValue().onNumClientsChanged(testNumClients);
@@ -707,7 +740,8 @@ public class WifiManagerTest {
         ArgumentCaptor<ISoftApCallback.Stub> callbackCaptor =
                 ArgumentCaptor.forClass(ISoftApCallback.Stub.class);
         mWifiManager.registerSoftApCallback(mSoftApCallback, mHandler);
-        verify(mWifiService).registerSoftApCallback(any(IBinder.class), callbackCaptor.capture());
+        verify(mWifiService).registerSoftApCallback(any(IBinder.class), callbackCaptor.capture(),
+                anyInt());
 
         final int testNumClients = 5;
         callbackCaptor.getValue().onStateChanged(WIFI_AP_STATE_ENABLING, 0);
@@ -730,7 +764,8 @@ public class WifiManagerTest {
         TestLooper altLooper = new TestLooper();
         Handler altHandler = new Handler(altLooper.getLooper());
         mWifiManager.registerSoftApCallback(mSoftApCallback, altHandler);
-        verify(mWifiService).registerSoftApCallback(any(IBinder.class), callbackCaptor.capture());
+        verify(mWifiService).registerSoftApCallback(any(IBinder.class), callbackCaptor.capture(),
+                anyInt());
 
         callbackCaptor.getValue().onStateChanged(WIFI_AP_STATE_ENABLED, 0);
         altLooper.dispatchAll();
@@ -745,7 +780,7 @@ public class WifiManagerTest {
         mWifiManager.registerSoftApCallback(mSoftApCallback, mHandler);
         mLooper.dispatchAll();
         verify(mWifiService).registerSoftApCallback(any(IBinder.class),
-                any(ISoftApCallback.Stub.class));
+                any(ISoftApCallback.Stub.class), anyInt());
         verify(mContext, never()).getMainLooper();
     }