Make auto Wi-Fi summary text consistent with feature availability.

"Turn Wi-Fi on automatically" feature is disabled when airplane mode or
battery saver mode is enabled, when Wi-Fi scanning is disabled, or when
network recommendations are disabled / scorer unset. This change
captures these negative cases in the summary text.

Bug: 65085700
Test: make ROBOTEST_FILTER=WifiWakeupPreferenceControllerTest
RunSettingsRoboTests -j40
Change-Id: I2f2d22b5bef3ad03a28d34e79a27e6545cac557f
This commit is contained in:
Stephen Chen
2017-09-01 11:18:36 -07:00
parent d0275e53eb
commit 8ffcbccbce
4 changed files with 27 additions and 24 deletions

View File

@@ -78,7 +78,7 @@ public class ConfigureWifiSettings extends DashboardFragment {
final NetworkScoreManagerWrapper networkScoreManagerWrapper = final NetworkScoreManagerWrapper networkScoreManagerWrapper =
new NetworkScoreManagerWrapper(context.getSystemService(NetworkScoreManager.class)); new NetworkScoreManagerWrapper(context.getSystemService(NetworkScoreManager.class));
mWifiWakeupPreferenceController = new WifiWakeupPreferenceController( mWifiWakeupPreferenceController = new WifiWakeupPreferenceController(
context, getLifecycle(), networkScoreManagerWrapper); context, getLifecycle());
mUseOpenWifiPreferenceController = new UseOpenWifiPreferenceController(context, this, mUseOpenWifiPreferenceController = new UseOpenWifiPreferenceController(context, this,
networkScoreManagerWrapper, getLifecycle()); networkScoreManagerWrapper, getLifecycle());
final WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE); final WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);

View File

@@ -38,6 +38,7 @@ import android.net.wifi.WpsInfo;
import android.nfc.NfcAdapter; import android.nfc.NfcAdapter;
import android.os.Bundle; import android.os.Bundle;
import android.os.HandlerThread; import android.os.HandlerThread;
import android.os.PowerManager;
import android.os.Process; import android.os.Process;
import android.provider.Settings; import android.provider.Settings;
import android.support.annotation.VisibleForTesting; import android.support.annotation.VisibleForTesting;
@@ -910,9 +911,8 @@ public class WifiSettings extends RestrictedSettingsFragment
getContentResolver(), Settings.Global.WIFI_WAKEUP_AVAILABLE, defaultWakeupAvailable) getContentResolver(), Settings.Global.WIFI_WAKEUP_AVAILABLE, defaultWakeupAvailable)
== 1; == 1;
if (wifiWakeupAvailable) { if (wifiWakeupAvailable) {
boolean wifiWakeupEnabled = Settings.Global.getInt( mConfigureWifiSettingsPreference.setSummary(getString(
getContentResolver(), Settings.Global.WIFI_WAKEUP_ENABLED, 0) == 1; isWifiWakeupEnabled()
mConfigureWifiSettingsPreference.setSummary(getString(wifiWakeupEnabled
? R.string.wifi_configure_settings_preference_summary_wakeup_on ? R.string.wifi_configure_settings_preference_summary_wakeup_on
: R.string.wifi_configure_settings_preference_summary_wakeup_off)); : R.string.wifi_configure_settings_preference_summary_wakeup_off));
} }
@@ -927,6 +927,20 @@ public class WifiSettings extends RestrictedSettingsFragment
} }
} }
private boolean isWifiWakeupEnabled() {
PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
ContentResolver contentResolver = getContentResolver();
return Settings.Global.getInt(contentResolver,
Settings.Global.WIFI_WAKEUP_ENABLED, 0) == 1
&& Settings.Global.getInt(contentResolver,
Settings.Global.WIFI_SCAN_ALWAYS_AVAILABLE, 0) == 1
&& Settings.Global.getInt(contentResolver,
Settings.Global.AIRPLANE_MODE_ON, 0) == 0
&& Settings.Global.getInt(contentResolver,
Settings.Global.NETWORK_RECOMMENDATIONS_ENABLED, 0) == 1
&& !powerManager.isPowerSaveMode();
}
private void setOffMessage() { private void setOffMessage() {
final CharSequence title = getText(R.string.wifi_empty_list_wifi_off); final CharSequence title = getText(R.string.wifi_empty_list_wifi_off);
// Don't use WifiManager.isScanAlwaysAvailable() to check the Wi-Fi scanning mode. Instead, // Don't use WifiManager.isScanAlwaysAvailable() to check the Wi-Fi scanning mode. Instead,

View File

@@ -44,13 +44,10 @@ public class WifiWakeupPreferenceController extends AbstractPreferenceController
implements PreferenceControllerMixin, LifecycleObserver, OnResume, OnPause { implements PreferenceControllerMixin, LifecycleObserver, OnResume, OnPause {
private static final String KEY_ENABLE_WIFI_WAKEUP = "enable_wifi_wakeup"; private static final String KEY_ENABLE_WIFI_WAKEUP = "enable_wifi_wakeup";
private final NetworkScoreManagerWrapper mNetworkScoreManager;
private SettingObserver mSettingObserver; private SettingObserver mSettingObserver;
public WifiWakeupPreferenceController( public WifiWakeupPreferenceController(Context context, Lifecycle lifecycle) {
Context context, Lifecycle lifecycle, NetworkScoreManagerWrapper networkScoreManager) {
super(context); super(context);
mNetworkScoreManager = networkScoreManager;
lifecycle.addObserver(this); lifecycle.addObserver(this);
} }
@@ -116,11 +113,9 @@ public class WifiWakeupPreferenceController extends AbstractPreferenceController
boolean networkRecommendationsEnabled = Settings.Global.getInt( boolean networkRecommendationsEnabled = Settings.Global.getInt(
mContext.getContentResolver(), mContext.getContentResolver(),
Settings.Global.NETWORK_RECOMMENDATIONS_ENABLED, 0) == 1; Settings.Global.NETWORK_RECOMMENDATIONS_ENABLED, 0) == 1;
boolean activeScorerSet = mNetworkScoreManager.getActiveScorerPackage() != null; enableWifiWakeup.setEnabled(networkRecommendationsEnabled && wifiScanningEnabled);
enableWifiWakeup.setEnabled(
networkRecommendationsEnabled && wifiScanningEnabled && activeScorerSet);
if (!activeScorerSet) { if (!networkRecommendationsEnabled) {
enableWifiWakeup.setSummary(R.string.wifi_wakeup_summary_scoring_disabled); enableWifiWakeup.setSummary(R.string.wifi_wakeup_summary_scoring_disabled);
} else if (!wifiScanningEnabled) { } else if (!wifiScanningEnabled) {
enableWifiWakeup.setSummary(R.string.wifi_wakeup_summary_scanning_disabled); enableWifiWakeup.setSummary(R.string.wifi_wakeup_summary_scanning_disabled);

View File

@@ -56,8 +56,6 @@ public class WifiWakeupPreferenceControllerTest {
private static final String TEST_SCORER_PACKAGE_NAME = "Test Scorer"; private static final String TEST_SCORER_PACKAGE_NAME = "Test Scorer";
private Context mContext; private Context mContext;
@Mock
private NetworkScoreManagerWrapper mNetworkScorer;
private WifiWakeupPreferenceController mController; private WifiWakeupPreferenceController mController;
@Before @Before
@@ -65,11 +63,11 @@ public class WifiWakeupPreferenceControllerTest {
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application; mContext = RuntimeEnvironment.application;
mController = new WifiWakeupPreferenceController( mController = new WifiWakeupPreferenceController(
mContext, mock(Lifecycle.class), mNetworkScorer); mContext, mock(Lifecycle.class));
Settings.System.putInt(mContext.getContentResolver(), WIFI_SCAN_ALWAYS_AVAILABLE, 1); Settings.System.putInt(mContext.getContentResolver(), WIFI_SCAN_ALWAYS_AVAILABLE, 1);
Settings.System.putInt(mContext.getContentResolver(), NETWORK_RECOMMENDATIONS_ENABLED, 1);
SettingsShadowResources.overrideResource( SettingsShadowResources.overrideResource(
com.android.internal.R.integer.config_wifi_wakeup_available, 0); com.android.internal.R.integer.config_wifi_wakeup_available, 0);
when(mNetworkScorer.getActiveScorerPackage()).thenReturn(TEST_SCORER_PACKAGE_NAME);
} }
@After @After
@@ -116,9 +114,8 @@ public class WifiWakeupPreferenceControllerTest {
} }
@Test @Test
public void updateState_preferenceSetCheckedAndSetEnabledWhenSettingsAreEnabled() { public void updateState_preferenceSetCheckedAndSetEnabledWhenWakeupSettingEnabled() {
final SwitchPreference preference = mock(SwitchPreference.class); final SwitchPreference preference = mock(SwitchPreference.class);
Settings.System.putInt(mContext.getContentResolver(), NETWORK_RECOMMENDATIONS_ENABLED, 1);
Settings.System.putInt(mContext.getContentResolver(), WIFI_WAKEUP_ENABLED, 1); Settings.System.putInt(mContext.getContentResolver(), WIFI_WAKEUP_ENABLED, 1);
mController.updateState(preference); mController.updateState(preference);
@@ -129,22 +126,20 @@ public class WifiWakeupPreferenceControllerTest {
} }
@Test @Test
public void updateState_preferenceSetCheckedAndSetEnabledWhenSettingsAreDisabled() { public void updateState_preferenceSetUncheckedAndSetEnabledWhenWakeupSettingDisabled() {
final SwitchPreference preference = mock(SwitchPreference.class); final SwitchPreference preference = mock(SwitchPreference.class);
Settings.System.putInt(mContext.getContentResolver(), NETWORK_RECOMMENDATIONS_ENABLED, 0);
Settings.System.putInt(mContext.getContentResolver(), WIFI_WAKEUP_ENABLED, 0); Settings.System.putInt(mContext.getContentResolver(), WIFI_WAKEUP_ENABLED, 0);
mController.updateState(preference); mController.updateState(preference);
verify(preference).setChecked(false); verify(preference).setChecked(false);
verify(preference).setEnabled(false); verify(preference).setEnabled(true);
verify(preference).setSummary(R.string.wifi_wakeup_summary); verify(preference).setSummary(R.string.wifi_wakeup_summary);
} }
@Test @Test
public void updateState_preferenceSetUncheckedAndSetDisabledWhenWifiScanningDisabled() { public void updateState_preferenceSetUncheckedAndSetDisabledWhenWifiScanningDisabled() {
final SwitchPreference preference = mock(SwitchPreference.class); final SwitchPreference preference = mock(SwitchPreference.class);
Settings.System.putInt(mContext.getContentResolver(), NETWORK_RECOMMENDATIONS_ENABLED, 1);
Settings.System.putInt(mContext.getContentResolver(), WIFI_WAKEUP_ENABLED, 1); Settings.System.putInt(mContext.getContentResolver(), WIFI_WAKEUP_ENABLED, 1);
Settings.System.putInt(mContext.getContentResolver(), WIFI_SCAN_ALWAYS_AVAILABLE, 0); Settings.System.putInt(mContext.getContentResolver(), WIFI_SCAN_ALWAYS_AVAILABLE, 0);
@@ -158,9 +153,8 @@ public class WifiWakeupPreferenceControllerTest {
@Test @Test
public void updateState_preferenceSetUncheckedAndSetDisabledWhenScoringDisabled() { public void updateState_preferenceSetUncheckedAndSetDisabledWhenScoringDisabled() {
final SwitchPreference preference = mock(SwitchPreference.class); final SwitchPreference preference = mock(SwitchPreference.class);
Settings.System.putInt(mContext.getContentResolver(), NETWORK_RECOMMENDATIONS_ENABLED, 1);
Settings.System.putInt(mContext.getContentResolver(), WIFI_WAKEUP_ENABLED, 1); Settings.System.putInt(mContext.getContentResolver(), WIFI_WAKEUP_ENABLED, 1);
when(mNetworkScorer.getActiveScorerPackage()).thenReturn(null); Settings.System.putInt(mContext.getContentResolver(), NETWORK_RECOMMENDATIONS_ENABLED, 0);
mController.updateState(preference); mController.updateState(preference);