diff options
author | Roshan Pius <rpius@google.com> | 2020-02-11 00:36:25 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2020-02-11 00:36:25 +0000 |
commit | 481f9c991fd4a4dd53eea448a568030b6b2deb3a (patch) | |
tree | 5831f589e775e56ece030b56d2b91a7e1f41d0a6 | |
parent | 26a1895ac4f7cf2eb01a72a116d542dcfec4b137 (diff) | |
parent | 857c6436a18670e870e16ec2e62c9d0126126caf (diff) |
Merge changes from topics "auto_wake_toggle_api", "network_rec_enabled"
* changes:
WifiService: Add API for auto wakeup feature toggle
ScoredNetworkNominator: Use getActiveScorerPackage() instead of settings
8 files changed, 109 insertions, 62 deletions
diff --git a/service/java/com/android/server/wifi/BaseWifiService.java b/service/java/com/android/server/wifi/BaseWifiService.java index 9a7fdad20..08a7a9e7f 100644 --- a/service/java/com/android/server/wifi/BaseWifiService.java +++ b/service/java/com/android/server/wifi/BaseWifiService.java @@ -652,4 +652,14 @@ public class BaseWifiService extends IWifiManager.Stub { getAllMatchingPasspointProfilesForScanResults(List<ScanResult> scanResults) { throw new UnsupportedOperationException(); } + + @Override + public void setAutoWakeupEnabled(boolean enable) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean isAutoWakeupEnabled() { + throw new UnsupportedOperationException(); + } } diff --git a/service/java/com/android/server/wifi/ScoredNetworkNominator.java b/service/java/com/android/server/wifi/ScoredNetworkNominator.java index e3cc2abdf..0f71e7f01 100644 --- a/service/java/com/android/server/wifi/ScoredNetworkNominator.java +++ b/service/java/com/android/server/wifi/ScoredNetworkNominator.java @@ -18,16 +18,12 @@ package com.android.server.wifi; import android.annotation.NonNull; import android.annotation.Nullable; -import android.content.Context; import android.content.pm.PackageManager; -import android.database.ContentObserver; import android.net.NetworkKey; import android.net.NetworkScoreManager; import android.net.wifi.ScanResult; import android.net.wifi.WifiConfiguration; -import android.os.Handler; import android.os.Process; -import android.provider.Settings; import android.text.TextUtils; import android.util.LocalLog; import android.util.Log; @@ -50,13 +46,10 @@ public class ScoredNetworkNominator implements WifiNetworkSelector.NetworkNomina private final PackageManager mPackageManager; private final WifiConfigManager mWifiConfigManager; private final LocalLog mLocalLog; - private final ContentObserver mContentObserver; private final WifiPermissionsUtil mWifiPermissionsUtil; - private boolean mNetworkRecommendationsEnabled; private WifiNetworkScoreCache mScoreCache; - ScoredNetworkNominator(final Context context, Handler handler, - final FrameworkFacade frameworkFacade, NetworkScoreManager networkScoreManager, + ScoredNetworkNominator(NetworkScoreManager networkScoreManager, PackageManager packageManager, WifiConfigManager wifiConfigManager, LocalLog localLog, WifiNetworkScoreCache wifiNetworkScoreCache, @@ -67,24 +60,19 @@ public class ScoredNetworkNominator implements WifiNetworkSelector.NetworkNomina mPackageManager = packageManager; mWifiConfigManager = wifiConfigManager; mLocalLog = localLog; - mContentObserver = new ContentObserver(handler) { - @Override - public void onChange(boolean selfChange) { - mNetworkRecommendationsEnabled = frameworkFacade.getIntegerSetting(context, - Settings.Global.NETWORK_RECOMMENDATIONS_ENABLED, 0) == 1; - } - }; - frameworkFacade.registerContentObserver(context, - Settings.Global.getUriFor(Settings.Global.NETWORK_RECOMMENDATIONS_ENABLED), - false /* notifyForDescendents */, mContentObserver); - mContentObserver.onChange(false /* unused */); mLocalLog.log("ScoredNetworkNominator constructed. mNetworkRecommendationsEnabled: " - + mNetworkRecommendationsEnabled); + + isNetworkRecommendationsEnabled()); + } + + private boolean isNetworkRecommendationsEnabled() { + // Check if we have any active scorer, not enabled otherwise. + String packageName = mNetworkScoreManager.getActiveScorerPackage(); + return !TextUtils.isEmpty(packageName); } @Override public void update(List<ScanDetail> scanDetails) { - if (mNetworkRecommendationsEnabled) { + if (isNetworkRecommendationsEnabled()) { updateNetworkScoreCache(scanDetails); } } @@ -133,7 +121,7 @@ public class ScoredNetworkNominator implements WifiNetworkSelector.NetworkNomina WifiConfiguration currentNetwork, String currentBssid, boolean connected, boolean untrustedNetworkAllowed, @NonNull OnConnectableListener onConnectableListener) { - if (!mNetworkRecommendationsEnabled) { + if (!isNetworkRecommendationsEnabled()) { mLocalLog.log("Skipping nominateNetworks; Network recommendations disabled."); return; } diff --git a/service/java/com/android/server/wifi/WakeupController.java b/service/java/com/android/server/wifi/WakeupController.java index f5dabf35c..1da8fe58a 100644 --- a/service/java/com/android/server/wifi/WakeupController.java +++ b/service/java/com/android/server/wifi/WakeupController.java @@ -186,6 +186,21 @@ public class WakeupController { } /** + * Enable/Disable the feature. + */ + public void setEnabled(boolean enable) { + mFrameworkFacade.setIntegerSetting( + mContext, Settings.Global.WIFI_WAKEUP_ENABLED, enable ? 1 : 0); + } + + /** + * Whether the feature is currently enabled. + */ + public boolean isEnabled() { + return mWifiWakeupEnabled; + } + + /** * Saves the SSID of the last Wifi network that was disconnected. Should only be called before * WakeupController is active. */ @@ -238,7 +253,7 @@ public class WakeupController { setActive(true); // ensure feature is enabled and store data has been read before performing work - if (isEnabled()) { + if (isEnabledAndReady()) { mWakeupOnboarding.maybeShowNotification(); List<ScanResult> scanResults = @@ -359,12 +374,12 @@ public class WakeupController { * @param scanResults The scan results with which to update the controller */ private void handleScanResults(Collection<ScanResult> scanResults) { - if (!isEnabled()) { + if (!isEnabledAndReady()) { Log.d(TAG, "Attempted to handleScanResults while not enabled"); return; } - // only count scan as handled if isEnabled + // only count scan as handled if isEnabledAndReady mNumScansHandled++; if (mVerboseLoggingEnabled) { Log.d(TAG, "Incoming scan #" + mNumScansHandled); @@ -423,7 +438,7 @@ public class WakeupController { * read. */ @VisibleForTesting - boolean isEnabled() { + boolean isEnabledAndReady() { return mWifiWakeupEnabled && mWakeupConfigStoreData.hasBeenRead(); } diff --git a/service/java/com/android/server/wifi/WifiInjector.java b/service/java/com/android/server/wifi/WifiInjector.java index fb34de55e..8246d020e 100644 --- a/service/java/com/android/server/wifi/WifiInjector.java +++ b/service/java/com/android/server/wifi/WifiInjector.java @@ -294,8 +294,8 @@ public class WifiInjector { mWifiConfigManager, nominateHelper, mConnectivityLocalLog, mTelephonyUtil); mNetworkSuggestionNominator = new NetworkSuggestionNominator(mWifiNetworkSuggestionsManager, mWifiConfigManager, nominateHelper, mConnectivityLocalLog, mTelephonyUtil); - mScoredNetworkNominator = new ScoredNetworkNominator(mContext, wifiHandler, - mFrameworkFacade, mNetworkScoreManager, mContext.getPackageManager(), + mScoredNetworkNominator = new ScoredNetworkNominator(mNetworkScoreManager, + mContext.getPackageManager(), mWifiConfigManager, mConnectivityLocalLog, mWifiNetworkScoreCache, mWifiPermissionsUtil); diff --git a/service/java/com/android/server/wifi/WifiServiceImpl.java b/service/java/com/android/server/wifi/WifiServiceImpl.java index 3bb8cb2cc..9f3532312 100644 --- a/service/java/com/android/server/wifi/WifiServiceImpl.java +++ b/service/java/com/android/server/wifi/WifiServiceImpl.java @@ -4207,4 +4207,28 @@ public class WifiServiceImpl extends BaseWifiService { } return mWifiThreadRunner.call(()-> mScanRequestProxy.isScanThrottleEnabled(), true); } + + /** + * See {@link android.net.wifi.WifiManager#setAutoWakeupEnabled(boolean)} + */ + @Override + public void setAutoWakeupEnabled(boolean enable) { + enforceNetworkSettingsPermission(); + mLog.info("setWalkeupEnabled uid=% verbose=%") + .c(Binder.getCallingUid()) + .c(enable).flush(); + mWifiThreadRunner.post(()-> mWifiInjector.getWakeupController().setEnabled(enable)); + } + + /** + * See {@link android.net.wifi.WifiManager#isAutoWakeupEnabled()} + */ + @Override + public boolean isAutoWakeupEnabled() { + enforceAccessPermission(); + if (mVerboseLoggingEnabled) { + mLog.info("isAutoWakeupEnabled uid=%").c(Binder.getCallingUid()).flush(); + } + return mWifiThreadRunner.call(()-> mWifiInjector.getWakeupController().isEnabled(), false); + } } diff --git a/tests/wifitests/src/com/android/server/wifi/ScoredNetworkNominatorTest.java b/tests/wifitests/src/com/android/server/wifi/ScoredNetworkNominatorTest.java index 2c5945173..8cac464aa 100644 --- a/tests/wifitests/src/com/android/server/wifi/ScoredNetworkNominatorTest.java +++ b/tests/wifitests/src/com/android/server/wifi/ScoredNetworkNominatorTest.java @@ -25,17 +25,11 @@ import static org.mockito.Mockito.*; import android.content.Context; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; -import android.database.ContentObserver; import android.net.NetworkKey; import android.net.NetworkScoreManager; -import android.net.NetworkScorerAppData; -import android.net.Uri; import android.net.wifi.ScanResult; import android.net.wifi.WifiConfiguration; -import android.os.Handler; -import android.os.Looper; import android.os.SystemClock; -import android.provider.Settings; import android.util.LocalLog; import androidx.test.filters.SmallTest; @@ -63,15 +57,11 @@ import java.util.List; public class ScoredNetworkNominatorTest extends WifiBaseTest { private static final String TEST_PACKAGE_NAME = "name.package.test"; private static final int TEST_UID = 12345; - private static final NetworkScorerAppData TEST_APP_DATA = new NetworkScorerAppData( - TEST_UID, null, null, null, null); - private ContentObserver mContentObserver; private int mThresholdQualifiedRssi2G; private int mThresholdQualifiedRssi5G; @Mock private Context mContext; @Mock private Clock mClock; - @Mock private FrameworkFacade mFrameworkFacade; @Mock private NetworkScoreManager mNetworkScoreManager; @Mock private PackageManager mPackageManager; @Mock private WifiConfigManager mWifiConfigManager; @@ -90,28 +80,17 @@ public class ScoredNetworkNominatorTest extends WifiBaseTest { MockitoAnnotations.initMocks(this); - when(mFrameworkFacade.getIntegerSetting(mContext, - Settings.Global.NETWORK_RECOMMENDATIONS_ENABLED, 0)) - .thenReturn(1); ApplicationInfo appInfo = new ApplicationInfo(); appInfo.uid = TEST_UID; when(mPackageManager.getApplicationInfo(any(), anyInt())) .thenReturn(appInfo); - ArgumentCaptor<ContentObserver> observerCaptor = - ArgumentCaptor.forClass(ContentObserver.class); mScoreCache = new WifiNetworkScoreCache(mContext); - mScoredNetworkNominator = new ScoredNetworkNominator(mContext, - new Handler(Looper.getMainLooper()), mFrameworkFacade, mNetworkScoreManager, + mScoredNetworkNominator = new ScoredNetworkNominator(mNetworkScoreManager, mPackageManager, mWifiConfigManager, new LocalLog(0), mScoreCache, mWifiPermissionsUtil); - verify(mFrameworkFacade).registerContentObserver(eq(mContext), any(Uri.class), eq(false), - observerCaptor.capture()); - mContentObserver = observerCaptor.getValue(); reset(mNetworkScoreManager); - when(mNetworkScoreManager.getActiveScorer()) - .thenReturn(TEST_APP_DATA); when(mNetworkScoreManager.getActiveScorerPackage()) .thenReturn(TEST_PACKAGE_NAME); @@ -136,13 +115,10 @@ public class ScoredNetworkNominatorTest extends WifiBaseTest { .setupScanDetailsAndConfigStore( ssids, bssids, freqs, caps, levels, securities, mWifiConfigManager, mClock); - when(mFrameworkFacade.getIntegerSetting(mContext, - Settings.Global.NETWORK_RECOMMENDATIONS_ENABLED, 0)) - .thenReturn(0); - - mContentObserver.onChange(false /* unused */); + when(mNetworkScoreManager.getActiveScorerPackage()).thenReturn(null); mScoredNetworkNominator.update(scanDetailsAndConfigs.getScanDetails()); + verify(mNetworkScoreManager).getActiveScorerPackage(); verifyZeroInteractions(mNetworkScoreManager); } @@ -161,6 +137,7 @@ public class ScoredNetworkNominatorTest extends WifiBaseTest { ssids, bssids, freqs, caps, levels, securities, mWifiConfigManager, mClock); mScoredNetworkNominator.update(new ArrayList<ScanDetail>()); + verify(mNetworkScoreManager).getActiveScorerPackage(); verifyZeroInteractions(mNetworkScoreManager); } @@ -225,15 +202,12 @@ public class ScoredNetworkNominatorTest extends WifiBaseTest { @Test public void testEvaluateNetworks_recommendationsDisabled() { - when(mFrameworkFacade.getIntegerSetting(mContext, - Settings.Global.NETWORK_RECOMMENDATIONS_ENABLED, 0)) - .thenReturn(0); - - mContentObserver.onChange(false /* unused */); + when(mNetworkScoreManager.getActiveScorerPackage()).thenReturn(null); mScoredNetworkNominator.nominateNetworks(null, null, null, false, false, mOnConnectableListener); + verify(mNetworkScoreManager).getActiveScorerPackage(); verifyZeroInteractions(mWifiConfigManager, mNetworkScoreManager); } diff --git a/tests/wifitests/src/com/android/server/wifi/WakeupControllerTest.java b/tests/wifitests/src/com/android/server/wifi/WakeupControllerTest.java index 11b1d255b..6ca9c30af 100644 --- a/tests/wifitests/src/com/android/server/wifi/WakeupControllerTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WakeupControllerTest.java @@ -187,7 +187,7 @@ public class WakeupControllerTest extends WifiBaseTest { public void verifyEnabledWhenToggledOn() { initializeWakeupController(true /* enabled */); - assertTrue(mWakeupController.isEnabled()); + assertTrue(mWakeupController.isEnabledAndReady()); } /** @@ -197,7 +197,7 @@ public class WakeupControllerTest extends WifiBaseTest { public void verifyDisabledWhenToggledOff() { initializeWakeupController(false /* enabled */); - assertFalse(mWakeupController.isEnabled()); + assertFalse(mWakeupController.isEnabledAndReady()); } /** diff --git a/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java b/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java index d3f7a2e9c..a289171b4 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java @@ -286,6 +286,7 @@ public class WifiServiceImplTest extends WifiBaseTest { @Mock PowerProfile mPowerProfile; @Mock WifiTrafficPoller mWifiTrafficPolller; @Mock ScanRequestProxy mScanRequestProxy; + @Mock WakeupController mWakeupController; @Mock ITrafficStateCallback mTrafficStateCallback; @Mock INetworkRequestMatchCallback mNetworkRequestMatchCallback; @Mock WifiNetworkSuggestionsManager mWifiNetworkSuggestionsManager; @@ -358,6 +359,7 @@ public class WifiServiceImplTest extends WifiBaseTest { when(mWifiInjector.getWifiSettingsStore()).thenReturn(mSettingsStore); when(mWifiInjector.getClock()).thenReturn(mClock); when(mWifiInjector.getScanRequestProxy()).thenReturn(mScanRequestProxy); + when(mWifiInjector.getWakeupController()).thenReturn(mWakeupController); when(mWifiInjector.getWifiNetworkSuggestionsManager()) .thenReturn(mWifiNetworkSuggestionsManager); when(mWifiInjector.makeTelephonyManager()).thenReturn(mTelephonyManager); @@ -5544,4 +5546,38 @@ public class WifiServiceImplTest extends WifiBaseTest { mLooper.stopAutoDispatchAndIgnoreExceptions(); verify(mScanRequestProxy).isScanThrottleEnabled(); } + + @Test + public void testSetAutoWakeupEnabledWithNetworkSettingsPermission() { + doNothing().when(mContext) + .enforceCallingOrSelfPermission(eq(android.Manifest.permission.NETWORK_SETTINGS), + eq("WifiService")); + mWifiServiceImpl.setAutoWakeupEnabled(true); + mLooper.dispatchAll(); + verify(mWakeupController).setEnabled(true); + + mWifiServiceImpl.setAutoWakeupEnabled(false); + mLooper.dispatchAll(); + verify(mWakeupController).setEnabled(false); + } + + @Test(expected = SecurityException.class) + public void testSetAutoWakeupEnabledWithNoNetworkSettingsPermission() { + doThrow(new SecurityException()).when(mContext) + .enforceCallingOrSelfPermission(eq(android.Manifest.permission.NETWORK_SETTINGS), + eq("WifiService")); + + mWifiServiceImpl.setAutoWakeupEnabled(true); + mLooper.dispatchAll(); + verify(mWakeupController, never()).setEnabled(true); + } + + @Test + public void testIsAutoWakeupEnabled() { + when(mWakeupController.isEnabled()).thenReturn(true); + mLooper.startAutoDispatch(); + assertTrue(mWifiServiceImpl.isAutoWakeupEnabled()); + mLooper.stopAutoDispatchAndIgnoreExceptions(); + verify(mWakeupController).isEnabled(); + } } |