diff options
author | Roshan Pius <rpius@google.com> | 2020-03-10 12:50:59 -0700 |
---|---|---|
committer | Roshan Pius <rpius@google.com> | 2020-03-12 07:34:35 -0700 |
commit | 79c2d06cbe4dd90990cbfb8ff51ff105d859e7c4 (patch) | |
tree | 21dae095f9a368694e381328159f603a2919a5fb | |
parent | f249ddd9c7fbe64fbf799b3650fccdb9ea685950 (diff) |
Revert "ScoredNetworkNominator: Use getActiveScorerPackage() instead of settings"
This reverts commit a9a4162adfe985e1415781d4d617055ffa98fc0a.
Uses the @hide Settings.Global key: "use_open_wifi_package" to get this status instead.
Bug: 151160317
Test: atest com.android.server.wifi
Test: Manually verified that scored nominator is disabled when "Connect
to open networks" is toggled in Wifi preferences.
Change-Id: Iafd62e592a681327c98b976f9e8b016bb6eba512
3 files changed, 61 insertions, 18 deletions
diff --git a/service/java/com/android/server/wifi/ScoredNetworkNominator.java b/service/java/com/android/server/wifi/ScoredNetworkNominator.java index 4999ef102..cf20253ce 100644 --- a/service/java/com/android/server/wifi/ScoredNetworkNominator.java +++ b/service/java/com/android/server/wifi/ScoredNetworkNominator.java @@ -18,16 +18,21 @@ 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; +import com.android.internal.annotations.VisibleForTesting; import com.android.server.wifi.util.ScanResultUtil; import com.android.server.wifi.util.WifiPermissionsUtil; @@ -40,16 +45,23 @@ import java.util.List; */ public class ScoredNetworkNominator implements WifiNetworkSelector.NetworkNominator { private static final String TAG = "ScoredNetworkNominator"; + // TODO (b/150977740): Stop using the @hide settings global flag. + @VisibleForTesting + public static final String SETTINGS_GLOBAL_USE_OPEN_WIFI_PACKAGE = + "use_open_wifi_package"; private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); private final NetworkScoreManager mNetworkScoreManager; 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(NetworkScoreManager networkScoreManager, + ScoredNetworkNominator(final Context context, Handler handler, + final FrameworkFacade frameworkFacade, NetworkScoreManager networkScoreManager, PackageManager packageManager, WifiConfigManager wifiConfigManager, LocalLog localLog, WifiNetworkScoreCache wifiNetworkScoreCache, @@ -60,19 +72,24 @@ public class ScoredNetworkNominator implements WifiNetworkSelector.NetworkNomina mPackageManager = packageManager; mWifiConfigManager = wifiConfigManager; mLocalLog = localLog; + mContentObserver = new ContentObserver(handler) { + @Override + public void onChange(boolean selfChange) { + mNetworkRecommendationsEnabled = frameworkFacade.getStringSetting(context, + SETTINGS_GLOBAL_USE_OPEN_WIFI_PACKAGE) != null; + } + }; + frameworkFacade.registerContentObserver(context, + Settings.Global.getUriFor(SETTINGS_GLOBAL_USE_OPEN_WIFI_PACKAGE), + false /* notifyForDescendents */, mContentObserver); + mContentObserver.onChange(false /* unused */); mLocalLog.log("ScoredNetworkNominator constructed. mNetworkRecommendationsEnabled: " - + isNetworkRecommendationsEnabled()); - } - - private boolean isNetworkRecommendationsEnabled() { - // Check if we have any active scorer, not enabled otherwise. - String packageName = mNetworkScoreManager.getActiveScorerPackage(); - return !TextUtils.isEmpty(packageName); + + mNetworkRecommendationsEnabled); } @Override public void update(List<ScanDetail> scanDetails) { - if (isNetworkRecommendationsEnabled()) { + if (mNetworkRecommendationsEnabled) { updateNetworkScoreCache(scanDetails); } } @@ -121,7 +138,7 @@ public class ScoredNetworkNominator implements WifiNetworkSelector.NetworkNomina WifiConfiguration currentNetwork, String currentBssid, boolean connected, boolean untrustedNetworkAllowed, @NonNull OnConnectableListener onConnectableListener) { - if (!isNetworkRecommendationsEnabled()) { + if (!mNetworkRecommendationsEnabled) { mLocalLog.log("Skipping nominateNetworks; Network recommendations disabled."); return; } diff --git a/service/java/com/android/server/wifi/WifiInjector.java b/service/java/com/android/server/wifi/WifiInjector.java index 1a364c18d..e67c06851 100644 --- a/service/java/com/android/server/wifi/WifiInjector.java +++ b/service/java/com/android/server/wifi/WifiInjector.java @@ -303,8 +303,8 @@ public class WifiInjector { mWifiConfigManager, nominateHelper, mConnectivityLocalLog, mTelephonyUtil); mNetworkSuggestionNominator = new NetworkSuggestionNominator(mWifiNetworkSuggestionsManager, mWifiConfigManager, nominateHelper, mConnectivityLocalLog, mTelephonyUtil); - mScoredNetworkNominator = new ScoredNetworkNominator(mNetworkScoreManager, - mContext.getPackageManager(), + mScoredNetworkNominator = new ScoredNetworkNominator(mContext, wifiHandler, + mFrameworkFacade, mNetworkScoreManager, mContext.getPackageManager(), mWifiConfigManager, mConnectivityLocalLog, mWifiNetworkScoreCache, mWifiPermissionsUtil); diff --git a/tests/wifitests/src/com/android/server/wifi/ScoredNetworkNominatorTest.java b/tests/wifitests/src/com/android/server/wifi/ScoredNetworkNominatorTest.java index 8cac464aa..1147d066c 100644 --- a/tests/wifitests/src/com/android/server/wifi/ScoredNetworkNominatorTest.java +++ b/tests/wifitests/src/com/android/server/wifi/ScoredNetworkNominatorTest.java @@ -16,6 +16,7 @@ package com.android.server.wifi; +import static com.android.server.wifi.ScoredNetworkNominator.SETTINGS_GLOBAL_USE_OPEN_WIFI_PACKAGE; import static com.android.server.wifi.WifiConfigurationTestUtil.SECURITY_NONE; import static com.android.server.wifi.WifiConfigurationTestUtil.SECURITY_PSK; @@ -25,10 +26,15 @@ 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.util.LocalLog; @@ -57,11 +63,15 @@ 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; @@ -80,17 +90,28 @@ public class ScoredNetworkNominatorTest extends WifiBaseTest { MockitoAnnotations.initMocks(this); + when(mFrameworkFacade.getStringSetting(mContext, + SETTINGS_GLOBAL_USE_OPEN_WIFI_PACKAGE)) + .thenReturn("test"); 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(mNetworkScoreManager, + mScoredNetworkNominator = new ScoredNetworkNominator(mContext, + new Handler(Looper.getMainLooper()), mFrameworkFacade, 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); @@ -115,10 +136,13 @@ public class ScoredNetworkNominatorTest extends WifiBaseTest { .setupScanDetailsAndConfigStore( ssids, bssids, freqs, caps, levels, securities, mWifiConfigManager, mClock); - when(mNetworkScoreManager.getActiveScorerPackage()).thenReturn(null); + when(mFrameworkFacade.getStringSetting(mContext, + SETTINGS_GLOBAL_USE_OPEN_WIFI_PACKAGE)) + .thenReturn(null); + + mContentObserver.onChange(false /* unused */); mScoredNetworkNominator.update(scanDetailsAndConfigs.getScanDetails()); - verify(mNetworkScoreManager).getActiveScorerPackage(); verifyZeroInteractions(mNetworkScoreManager); } @@ -137,7 +161,6 @@ public class ScoredNetworkNominatorTest extends WifiBaseTest { ssids, bssids, freqs, caps, levels, securities, mWifiConfigManager, mClock); mScoredNetworkNominator.update(new ArrayList<ScanDetail>()); - verify(mNetworkScoreManager).getActiveScorerPackage(); verifyZeroInteractions(mNetworkScoreManager); } @@ -202,12 +225,15 @@ public class ScoredNetworkNominatorTest extends WifiBaseTest { @Test public void testEvaluateNetworks_recommendationsDisabled() { - when(mNetworkScoreManager.getActiveScorerPackage()).thenReturn(null); + when(mFrameworkFacade.getStringSetting(mContext, + SETTINGS_GLOBAL_USE_OPEN_WIFI_PACKAGE)) + .thenReturn(null); + + mContentObserver.onChange(false /* unused */); mScoredNetworkNominator.nominateNetworks(null, null, null, false, false, mOnConnectableListener); - verify(mNetworkScoreManager).getActiveScorerPackage(); verifyZeroInteractions(mWifiConfigManager, mNetworkScoreManager); } |