diff options
author | Roshan Pius <rpius@google.com> | 2019-12-05 10:21:04 -0800 |
---|---|---|
committer | Roshan Pius <rpius@google.com> | 2019-12-06 08:07:05 -0800 |
commit | 31b57a859783e6e604830ac189d27ebd69cda9c6 (patch) | |
tree | 7334284a72016195dcb11eedace79a76c96dcc25 | |
parent | b8a679668dc7c8721c556973266770d2b695da7c (diff) |
wifi: Use public WifiP2pManager, WifiAwareManager objects
Using the AIDL object requires the usage IServiceManager.getService()
method.
Note: The RemoteException catch block in RttService (which is being removed)
is redundant since both the services are hosted on the same process
(i.e system_server).
Bug: 145746458
Test: atest com.android.server.wifi
Change-Id: I7cca70d199158272f3f4c968626796c1499cf7ee
5 files changed, 43 insertions, 80 deletions
diff --git a/service/java/com/android/server/wifi/ClientModeImpl.java b/service/java/com/android/server/wifi/ClientModeImpl.java index f358e9acd..474b094cf 100644 --- a/service/java/com/android/server/wifi/ClientModeImpl.java +++ b/service/java/com/android/server/wifi/ClientModeImpl.java @@ -71,7 +71,7 @@ import android.net.wifi.WifiManager.DeviceMobilityState; import android.net.wifi.WifiNetworkAgentSpecifier; import android.net.wifi.hotspot2.IProvisioningCallback; import android.net.wifi.hotspot2.OsuProvider; -import android.net.wifi.p2p.IWifiP2pManager; +import android.net.wifi.p2p.WifiP2pManager; import android.os.BatteryStatsManager; import android.os.Bundle; import android.os.ConditionVariable; @@ -2955,14 +2955,12 @@ public class ClientModeImpl extends StateMachine { private void getAdditionalWifiServiceInterfaces() { // First set up Wifi Direct if (mP2pSupported) { - IBinder s1 = mFacade.getService(Context.WIFI_P2P_SERVICE); - WifiP2pServiceImpl wifiP2pServiceImpl = - (WifiP2pServiceImpl) IWifiP2pManager.Stub.asInterface(s1); + WifiP2pManager wifiP2pService = mContext.getSystemService(WifiP2pManager.class); - if (wifiP2pServiceImpl != null) { + if (wifiP2pService != null) { mWifiP2pChannel = new AsyncChannel(); mWifiP2pChannel.connect(mContext, getHandler(), - wifiP2pServiceImpl.getP2pStateMachineMessenger()); + wifiP2pService.getP2pStateMachineMessenger()); } } } diff --git a/service/java/com/android/server/wifi/rtt/RttService.java b/service/java/com/android/server/wifi/rtt/RttService.java index ae4cdfe8b..0d3e98943 100644 --- a/service/java/com/android/server/wifi/rtt/RttService.java +++ b/service/java/com/android/server/wifi/rtt/RttService.java @@ -17,9 +17,8 @@ package com.android.server.wifi.rtt; import android.content.Context; -import android.net.wifi.aware.IWifiAwareManager; +import android.net.wifi.aware.WifiAwareManager; import android.os.HandlerThread; -import android.os.ServiceManager; import android.util.Log; import com.android.server.SystemService; @@ -62,11 +61,10 @@ public class RttService extends SystemService { WifiPermissionsUtil wifiPermissionsUtil = wifiInjector.getWifiPermissionsUtil(); RttMetrics rttMetrics = wifiInjector.getWifiMetrics().getRttMetrics(); - IWifiAwareManager awareBinder = (IWifiAwareManager) ServiceManager.getService( - Context.WIFI_AWARE_SERVICE); + WifiAwareManager awareManager = getContext().getSystemService(WifiAwareManager.class); RttNative rttNative = new RttNative(mImpl, halDeviceManager); - mImpl.start(handlerThread.getLooper(), wifiInjector.getClock(), awareBinder, rttNative, + mImpl.start(handlerThread.getLooper(), wifiInjector.getClock(), awareManager, rttNative, rttMetrics, wifiPermissionsUtil, wifiInjector.getFrameworkFacade()); } } diff --git a/service/java/com/android/server/wifi/rtt/RttServiceImpl.java b/service/java/com/android/server/wifi/rtt/RttServiceImpl.java index 6e48f59f4..6e35eb5ad 100644 --- a/service/java/com/android/server/wifi/rtt/RttServiceImpl.java +++ b/service/java/com/android/server/wifi/rtt/RttServiceImpl.java @@ -28,7 +28,7 @@ import android.database.ContentObserver; import android.location.LocationManager; import android.net.MacAddress; import android.net.wifi.aware.IWifiAwareMacAddressProvider; -import android.net.wifi.aware.IWifiAwareManager; +import android.net.wifi.aware.WifiAwareManager; import android.net.wifi.rtt.IRttCallback; import android.net.wifi.rtt.IWifiRttManager; import android.net.wifi.rtt.RangingRequest; @@ -84,7 +84,7 @@ public class RttServiceImpl extends IWifiRttManager.Stub { private final Context mContext; private final RttShellCommand mShellCommand; private Clock mClock; - private IWifiAwareManager mAwareBinder; + private WifiAwareManager mAwareManager; private RttNative mRttNative; private RttMetrics mRttMetrics; private WifiPermissionsUtil mWifiPermissionsUtil; @@ -235,17 +235,17 @@ public class RttServiceImpl extends IWifiRttManager.Stub { * * @param looper The looper on which to synchronize operations. * @param clock A mockable clock. - * @param awareBinder The Wi-Fi Aware service (binder) if supported on the system. + * @param awareManager The Wi-Fi Aware service (binder) if supported on the system. * @param rttNative The Native interface to the HAL. * @param rttMetrics The Wi-Fi RTT metrics object. * @param wifiPermissionsUtil Utility for permission checks. * @param frameworkFacade Facade for framework classes, allows mocking. */ - public void start(Looper looper, Clock clock, IWifiAwareManager awareBinder, + public void start(Looper looper, Clock clock, WifiAwareManager awareManager, RttNative rttNative, RttMetrics rttMetrics, WifiPermissionsUtil wifiPermissionsUtil, FrameworkFacade frameworkFacade) { mClock = clock; - mAwareBinder = awareBinder; + mAwareManager = awareManager; mRttNative = rttNative; mRttMetrics = rttMetrics; mWifiPermissionsUtil = wifiPermissionsUtil; @@ -429,7 +429,7 @@ public class RttServiceImpl extends IWifiRttManager.Stub { if (callback == null) { throw new IllegalArgumentException("Callback must not be null"); } - request.enforceValidity(mAwareBinder != null); + request.enforceValidity(mAwareManager != null); if (!isAvailable()) { try { @@ -1012,33 +1012,17 @@ public class RttServiceImpl extends IWifiRttManager.Stub { } request.peerHandlesTranslated = true; - try { - mAwareBinder.requestMacAddresses(request.uid, peerIdsNeedingTranslation, - new IWifiAwareMacAddressProvider.Stub() { - @Override - public void macAddress(Map peerIdToMacMap) { - // ASYNC DOMAIN - mHandler.post(() -> { - // BACK TO SYNC DOMAIN - processReceivedAwarePeerMacAddresses(request, peerIdToMacMap); - }); - } - }); - } catch (RemoteException e1) { - Log.e(TAG, - "processAwarePeerHandles: exception while calling requestMacAddresses -- " - + e1 + ", aborting request=" + request); - try { - mRttMetrics.recordOverallStatus( - WifiMetricsProto.WifiRttLog.OVERALL_AWARE_TRANSLATION_FAILURE); - request.callback.onRangingFailure(RangingResultCallback.STATUS_CODE_FAIL); - } catch (RemoteException e2) { - Log.e(TAG, "processAwarePeerHandles: onRangingResults failure -- " + e2); - } - executeNextRangingRequestIfPossible(true); - return true; // an abort because we removed request and are executing next one - } - + mAwareManager.requestMacAddresses(request.uid, peerIdsNeedingTranslation, + new IWifiAwareMacAddressProvider.Stub() { + @Override + public void macAddress(Map peerIdToMacMap) { + // ASYNC DOMAIN + mHandler.post(() -> { + // BACK TO SYNC DOMAIN + processReceivedAwarePeerMacAddresses(request, peerIdToMacMap); + }); + } + }); return true; // a deferral } diff --git a/tests/wifitests/src/com/android/server/wifi/ClientModeImplTest.java b/tests/wifitests/src/com/android/server/wifi/ClientModeImplTest.java index 61ca10e53..726582d66 100644 --- a/tests/wifitests/src/com/android/server/wifi/ClientModeImplTest.java +++ b/tests/wifitests/src/com/android/server/wifi/ClientModeImplTest.java @@ -59,14 +59,13 @@ import android.net.wifi.WifiScanner; import android.net.wifi.WifiSsid; import android.net.wifi.hotspot2.IProvisioningCallback; import android.net.wifi.hotspot2.OsuProvider; -import android.net.wifi.p2p.IWifiP2pManager; +import android.net.wifi.p2p.WifiP2pManager; import android.os.BatteryStatsManager; import android.os.Binder; import android.os.Bundle; import android.os.Handler; import android.os.HandlerThread; import android.os.IBinder; -import android.os.IInterface; import android.os.INetworkManagementService; import android.os.IPowerManager; import android.os.Looper; @@ -94,7 +93,6 @@ import com.android.internal.util.StateMachine; import com.android.server.wifi.hotspot2.NetworkDetail; import com.android.server.wifi.hotspot2.PasspointManager; import com.android.server.wifi.hotspot2.PasspointProvisioningTestUtil; -import com.android.server.wifi.p2p.WifiP2pServiceImpl; import com.android.server.wifi.proto.nano.WifiMetricsProto; import com.android.server.wifi.proto.nano.WifiMetricsProto.StaEvent; import com.android.server.wifi.proto.nano.WifiMetricsProto.WifiIsUnusableEvent; @@ -174,15 +172,6 @@ public class ClientModeImplTest extends WifiBaseTest { return mock(class1, withSettings().extraInterfaces(interfaces)); } - private static <T, I> IBinder mockService(Class<T> class1, Class<I> iface) { - T tImpl = mockWithInterfaces(class1, iface); - IBinder binder = mock(IBinder.class); - when(((IInterface) tImpl).asBinder()).thenReturn(binder); - when(binder.queryLocalInterface(iface.getCanonicalName())) - .thenReturn((IInterface) tImpl); - return binder; - } - private void enableDebugLogs() { mCmi.enableVerboseLogging(1); } @@ -193,26 +182,6 @@ public class ClientModeImplTest extends WifiBaseTest { when(facade.getService(Context.NETWORKMANAGEMENT_SERVICE)).thenReturn( mockWithInterfaces(IBinder.class, INetworkManagementService.class)); - IBinder p2pBinder = mockService(WifiP2pServiceImpl.class, IWifiP2pManager.class); - when(facade.getService(Context.WIFI_P2P_SERVICE)).thenReturn(p2pBinder); - - WifiP2pServiceImpl p2pm = (WifiP2pServiceImpl) p2pBinder.queryLocalInterface( - IWifiP2pManager.class.getCanonicalName()); - - final CountDownLatch untilDone = new CountDownLatch(1); - mP2pThread = new HandlerThread("WifiP2pMockThread") { - @Override - protected void onLooperPrepared() { - untilDone.countDown(); - } - }; - - mP2pThread.start(); - untilDone.await(); - - Handler handler = new Handler(mP2pThread.getLooper()); - when(p2pm.getP2pStateMachineMessenger()).thenReturn(new Messenger(handler)); - doAnswer(new AnswerWithArguments() { public void answer( Context context, String ifname, IpClientCallbacks callback) { @@ -255,6 +224,20 @@ public class ClientModeImplTest extends WifiBaseTest { when(context.getSystemService(ActivityManager.class)).thenReturn( mock(ActivityManager.class)); + WifiP2pManager p2pm = mock(WifiP2pManager.class); + when(context.getSystemService(WifiP2pManager.class)).thenReturn(p2pm); + final CountDownLatch untilDone = new CountDownLatch(1); + mP2pThread = new HandlerThread("WifiP2pMockThread") { + @Override + protected void onLooperPrepared() { + untilDone.countDown(); + } + }; + mP2pThread.start(); + untilDone.await(); + Handler handler = new Handler(mP2pThread.getLooper()); + when(p2pm.getP2pStateMachineMessenger()).thenReturn(new Messenger(handler)); + return context; } diff --git a/tests/wifitests/src/com/android/server/wifi/rtt/RttServiceImplTest.java b/tests/wifitests/src/com/android/server/wifi/rtt/RttServiceImplTest.java index 8086b0cda..736f0d221 100644 --- a/tests/wifitests/src/com/android/server/wifi/rtt/RttServiceImplTest.java +++ b/tests/wifitests/src/com/android/server/wifi/rtt/RttServiceImplTest.java @@ -52,8 +52,8 @@ import android.content.pm.PackageManager; import android.location.LocationManager; import android.net.MacAddress; import android.net.wifi.aware.IWifiAwareMacAddressProvider; -import android.net.wifi.aware.IWifiAwareManager; import android.net.wifi.aware.PeerHandle; +import android.net.wifi.aware.WifiAwareManager; import android.net.wifi.rtt.IRttCallback; import android.net.wifi.rtt.RangingRequest; import android.net.wifi.rtt.RangingResult; @@ -143,7 +143,7 @@ public class RttServiceImplTest extends WifiBaseTest { public RttMetrics mockMetrics; @Mock - public IWifiAwareManager mockAwareManagerBinder; + public WifiAwareManager mockAwareManager; @Mock public WifiPermissionsUtil mockPermissionUtil; @@ -224,7 +224,7 @@ public class RttServiceImplTest extends WifiBaseTest { doAnswer(mBinderLinkToDeathCounter).when(mockIbinder).linkToDeath(any(), anyInt()); doAnswer(mBinderUnlinkToDeathCounter).when(mockIbinder).unlinkToDeath(any(), anyInt()); - mDut.start(mMockLooper.getLooper(), mockClock, mockAwareManagerBinder, mockNative, + mDut.start(mMockLooper.getLooper(), mockClock, mockAwareManager, mockNative, mockMetrics, mockPermissionUtil, mFrameworkFacade); mMockLooper.dispatchAll(); ArgumentCaptor<BroadcastReceiver> bcastRxCaptor = ArgumentCaptor.forClass( @@ -324,7 +324,7 @@ public class RttServiceImplTest extends WifiBaseTest { AwareTranslatePeerHandlesToMac answer = new AwareTranslatePeerHandlesToMac(mDefaultUid, peerHandleToMacMap); - doAnswer(answer).when(mockAwareManagerBinder).requestMacAddresses(anyInt(), any(), any()); + doAnswer(answer).when(mockAwareManager).requestMacAddresses(anyInt(), any(), any()); // issue request mDut.startRanging(mockIbinder, mPackageName, mFeatureId, null, request, mockCallback); |