diff options
author | David Su <dysu@google.com> | 2019-12-05 01:19:27 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2019-12-05 01:19:27 +0000 |
commit | c2f16d91bb409bb2145b2e28f6ac88125bd68d1e (patch) | |
tree | 999fef18032bfd816cadde897190ddc9e2f94dbf | |
parent | 78686d95d0e41c2ce7cc75f633b24cbacf78d86f (diff) | |
parent | 6c0353b0bd3301ca6e4bcc5cab746d3bccdd5c38 (diff) |
Merge changes from topics "android-os-connectivity-wifiactivityenergyinfo", "requestactivityinfo"
* changes:
Move WifiActivityEnergyInfo to android.os.connectivity
Formalize WifiManager#requestActivityInfo as @SystemApi
-rw-r--r-- | service/java/com/android/server/wifi/WifiServiceImpl.java | 21 | ||||
-rw-r--r-- | tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java | 114 |
2 files changed, 122 insertions, 13 deletions
diff --git a/service/java/com/android/server/wifi/WifiServiceImpl.java b/service/java/com/android/server/wifi/WifiServiceImpl.java index 9778e8e5b..9529ef0ef 100644 --- a/service/java/com/android/server/wifi/WifiServiceImpl.java +++ b/service/java/com/android/server/wifi/WifiServiceImpl.java @@ -52,6 +52,7 @@ import android.net.wifi.IActionListener; import android.net.wifi.IDppCallback; import android.net.wifi.ILocalOnlyHotspotCallback; import android.net.wifi.INetworkRequestMatchCallback; +import android.net.wifi.IOnWifiActivityEnergyInfoListener; import android.net.wifi.IOnWifiUsabilityStatsListener; import android.net.wifi.IScanResultsCallback; import android.net.wifi.ISoftApCallback; @@ -61,7 +62,6 @@ import android.net.wifi.ITxPacketCountListener; import android.net.wifi.ScanResult; import android.net.wifi.SoftApConfiguration; import android.net.wifi.SoftApInfo; -import android.net.wifi.WifiActivityEnergyInfo; import android.net.wifi.WifiClient; import android.net.wifi.WifiConfiguration; import android.net.wifi.WifiInfo; @@ -76,10 +76,8 @@ import android.net.wifi.hotspot2.IProvisioningCallback; import android.net.wifi.hotspot2.OsuProvider; import android.net.wifi.hotspot2.PasspointConfiguration; import android.os.AsyncTask; -import android.os.BatteryStats; import android.os.Binder; import android.os.Build; -import android.os.Bundle; import android.os.IBinder; import android.os.Looper; import android.os.Message; @@ -91,6 +89,7 @@ import android.os.ShellCallback; import android.os.UserHandle; import android.os.UserManager; import android.os.WorkSource; +import android.os.connectivity.WifiActivityEnergyInfo; import android.provider.Settings; import android.telephony.TelephonyManager; import android.text.TextUtils; @@ -1744,13 +1743,19 @@ public class WifiServiceImpl extends BaseWifiService { } @Override - public void requestActivityInfo(ResultReceiver result) { + public void getWifiActivityEnergyInfoAsync(IOnWifiActivityEnergyInfoListener listener) { if (mVerboseLoggingEnabled) { - mLog.info("requestActivityInfo uid=%").c(Binder.getCallingUid()).flush(); + mLog.info("getWifiActivityEnergyInfoAsync uid=%") + .c(Binder.getCallingUid()) + .flush(); + } + // reportActivityInfo() performs permission checking + WifiActivityEnergyInfo info = reportActivityInfo(); + try { + listener.onWifiActivityEnergyInfo(info); + } catch (RemoteException e) { + Log.e(TAG, "onWifiActivityEnergyInfo: RemoteException -- ", e); } - Bundle bundle = new Bundle(); - bundle.putParcelable(BatteryStats.RESULT_RECEIVER_CONTROLLER_KEY, reportActivityInfo()); - result.send(0, bundle); } /** diff --git a/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java b/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java index ecd37daaf..0a2e42e7d 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java @@ -16,6 +16,7 @@ package com.android.server.wifi; +import static android.Manifest.permission.ACCESS_WIFI_STATE; import static android.net.wifi.WifiManager.DEVICE_MOBILITY_STATE_STATIONARY; import static android.net.wifi.WifiManager.IFACE_IP_MODE_CONFIGURATION_ERROR; import static android.net.wifi.WifiManager.IFACE_IP_MODE_LOCAL_ONLY; @@ -94,6 +95,7 @@ import android.net.wifi.IActionListener; import android.net.wifi.IDppCallback; import android.net.wifi.ILocalOnlyHotspotCallback; import android.net.wifi.INetworkRequestMatchCallback; +import android.net.wifi.IOnWifiActivityEnergyInfoListener; import android.net.wifi.IOnWifiUsabilityStatsListener; import android.net.wifi.IScanResultsCallback; import android.net.wifi.ISoftApCallback; @@ -128,6 +130,7 @@ import android.os.PowerManager; import android.os.Process; import android.os.RemoteException; import android.os.UserManager; +import android.os.connectivity.WifiActivityEnergyInfo; import android.os.test.TestLooper; import android.telephony.TelephonyManager; @@ -282,6 +285,7 @@ public class WifiServiceImplTest extends WifiBaseTest { @Mock ILocalOnlyHotspotCallback mLohsCallback; @Mock IScanResultsCallback mScanResultsCallback; @Mock ISuggestionConnectionStatusListener mSuggestionConnectionStatusListener; + @Mock IOnWifiActivityEnergyInfoListener mOnWifiActivityEnergyInfoListener; WifiLog mLog = new LogcatLog(TAG); @@ -1147,7 +1151,7 @@ public class WifiServiceImplTest extends WifiBaseTest { public void testGetWifiApEnabledPermissionDenied() { // we should not be able to get the state doThrow(new SecurityException()).when(mContext) - .enforceCallingOrSelfPermission(eq(android.Manifest.permission.ACCESS_WIFI_STATE), + .enforceCallingOrSelfPermission(eq(ACCESS_WIFI_STATE), eq("WifiService")); try { @@ -4585,7 +4589,7 @@ public class WifiServiceImplTest extends WifiBaseTest { @Test(expected = SecurityException.class) public void testRegisterScanResultCallbackWithMissingPermission() throws Exception { doThrow(new SecurityException()).when(mContext).enforceCallingOrSelfPermission( - eq(android.Manifest.permission.ACCESS_WIFI_STATE), eq("WifiService")); + eq(ACCESS_WIFI_STATE), eq("WifiService")); mWifiServiceImpl.registerScanResultsCallback(mScanResultsCallback); } @@ -4595,7 +4599,7 @@ public class WifiServiceImplTest extends WifiBaseTest { @Test(expected = SecurityException.class) public void testUnregisterScanResultCallbackWithMissingPermission() throws Exception { doThrow(new SecurityException()).when(mContext).enforceCallingOrSelfPermission( - eq(android.Manifest.permission.ACCESS_WIFI_STATE), eq("WifiService")); + eq(ACCESS_WIFI_STATE), eq("WifiService")); mWifiServiceImpl.unregisterScanResultsCallback(mScanResultsCallback); } @@ -4626,7 +4630,7 @@ public class WifiServiceImplTest extends WifiBaseTest { @Test(expected = SecurityException.class) public void testRegisterSuggestionNetworkCallbackWithMissingPermission() { doThrow(new SecurityException()).when(mContext).enforceCallingOrSelfPermission( - eq(android.Manifest.permission.ACCESS_WIFI_STATE), eq("WifiService")); + eq(ACCESS_WIFI_STATE), eq("WifiService")); mWifiServiceImpl.registerSuggestionConnectionStatusListener(mAppBinder, mSuggestionConnectionStatusListener, NETWORK_CALLBACK_ID, TEST_PACKAGE_NAME, TEST_FEATURE_ID); @@ -4647,7 +4651,7 @@ public class WifiServiceImplTest extends WifiBaseTest { @Test(expected = SecurityException.class) public void testUnregisterSuggestionNetworkCallbackWithMissingPermission() { doThrow(new SecurityException()).when(mContext).enforceCallingOrSelfPermission( - eq(android.Manifest.permission.ACCESS_WIFI_STATE), eq("WifiService")); + eq(ACCESS_WIFI_STATE), eq("WifiService")); mWifiServiceImpl.unregisterSuggestionConnectionStatusListener( NETWORK_CALLBACK_ID, TEST_PACKAGE_NAME); } @@ -4684,4 +4688,104 @@ public class WifiServiceImplTest extends WifiBaseTest { mWifiServiceImpl.acquireWifiLock(mAppBinder, wifiLockModeInvalid, "", null); } + + /** + * Tests that {@link WifiServiceImpl#reportActivityInfo()} throws {@link SecurityException} if + * the caller doesn't have the necessary permissions. + */ + @Test(expected = SecurityException.class) + public void reportActivityInfoNoPermission() throws Exception { + doThrow(SecurityException.class) + .when(mContext).enforceCallingOrSelfPermission(eq(ACCESS_WIFI_STATE), any()); + mWifiServiceImpl.reportActivityInfo(); + } + + /** + * Tests that {@link WifiServiceImpl#reportActivityInfo()} returns null if link layer stats is + * unsupported. + */ + @Test + public void reportActivityInfoFeatureUnsupported() throws Exception { + when(mClientModeImpl.syncGetSupportedFeatures(any())).thenReturn(0L); + assertNull(mWifiServiceImpl.reportActivityInfo()); + } + + private void setupReportActivityInfo() { + WifiLinkLayerStats stats = new WifiLinkLayerStats(); + stats.on_time = 1000; + stats.tx_time = 1; + stats.rx_time = 2; + stats.tx_time_per_level = new int[] {3, 4, 5}; + stats.on_time_scan = 6; + when(mClientModeImpl.syncGetLinkLayerStats(any())).thenReturn(stats); + when(mPowerProfile.getAveragePower(PowerProfile.POWER_WIFI_CONTROLLER_IDLE)) + .thenReturn(7.0); + when(mPowerProfile.getAveragePower(PowerProfile.POWER_WIFI_CONTROLLER_RX)) + .thenReturn(8.0); + when(mPowerProfile.getAveragePower(PowerProfile.POWER_WIFI_CONTROLLER_TX)) + .thenReturn(9.0); + when(mPowerProfile.getAveragePower(PowerProfile.POWER_WIFI_CONTROLLER_OPERATING_VOLTAGE)) + .thenReturn(10000.0); + when(mClock.getElapsedSinceBootMillis()).thenReturn(9999L); + } + + private void validateWifiActivityEnergyInfo(WifiActivityEnergyInfo info) { + assertNotNull(info); + assertEquals(9999L, info.getTimeSinceBootMillis()); + assertEquals(WifiActivityEnergyInfo.STACK_STATE_STATE_IDLE, info.getStackState()); + assertEquals(1, info.getControllerTxDurationMillis()); + assertEquals(2, info.getControllerRxDurationMillis()); + assertEquals(6, info.getControllerScanDurationMillis()); + assertEquals(997, info.getControllerIdleDurationMillis()); + assertEquals(70040, info.getControllerEnergyUsedMicroJoules()); + } + + /** + * Tests that {@link WifiServiceImpl#reportActivityInfo()} returns the expected values on + * success. + */ + @Test + public void reportActivityInfoSuccess() throws Exception { + when(mClientModeImpl.syncGetSupportedFeatures(any())).thenReturn(Long.MAX_VALUE); + setupReportActivityInfo(); + WifiActivityEnergyInfo info = mWifiServiceImpl.reportActivityInfo(); + validateWifiActivityEnergyInfo(info); + } + + /** + * Tests that {@link WifiServiceImpl#getWifiActivityEnergyInfoAsync} throws + * {@link SecurityException} if the caller doesn't have the necessary permissions. + */ + @Test(expected = SecurityException.class) + public void getWifiActivityEnergyInfoAsyncNoPermission() throws Exception { + doThrow(SecurityException.class) + .when(mContext).enforceCallingOrSelfPermission(eq(ACCESS_WIFI_STATE), any()); + mWifiServiceImpl.getWifiActivityEnergyInfoAsync(mOnWifiActivityEnergyInfoListener); + } + + /** + * Tests that {@link WifiServiceImpl#getWifiActivityEnergyInfoAsync} passes null to the listener + * if link layer stats is unsupported. + */ + @Test + public void getWifiActivityEnergyInfoAsyncFeatureUnsupported() throws Exception { + when(mClientModeImpl.syncGetSupportedFeatures(any())).thenReturn(0L); + mWifiServiceImpl.getWifiActivityEnergyInfoAsync(mOnWifiActivityEnergyInfoListener); + verify(mOnWifiActivityEnergyInfoListener).onWifiActivityEnergyInfo(null); + } + + /** + * Tests that {@link WifiServiceImpl#getWifiActivityEnergyInfoAsync} passes the expected values + * to the listener on success. + */ + @Test + public void getWifiActivityEnergyInfoAsyncSuccess() throws Exception { + when(mClientModeImpl.syncGetSupportedFeatures(any())).thenReturn(Long.MAX_VALUE); + setupReportActivityInfo(); + mWifiServiceImpl.getWifiActivityEnergyInfoAsync(mOnWifiActivityEnergyInfoListener); + ArgumentCaptor<WifiActivityEnergyInfo> infoCaptor = + ArgumentCaptor.forClass(WifiActivityEnergyInfo.class); + verify(mOnWifiActivityEnergyInfoListener).onWifiActivityEnergyInfo(infoCaptor.capture()); + validateWifiActivityEnergyInfo(infoCaptor.getValue()); + } } |