Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e549e732 authored by Stephen Chen's avatar Stephen Chen Committed by android-build-merger
Browse files

Merge "Adjust UX for toggles when active scorer is not set." into oc-dr1-dev

am: 89fdea98

Change-Id: Iddc9c6e474847f2aa54a0f695d34201a84596985
parents 4961451a 89fdea98
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -1700,6 +1700,8 @@
    <string name="wifi_wakeup_summary">Wi\u2011Fi will turn back on near high\u2011quality saved networks, like your home network</string>
    <!-- Checkbox summary for Wi-Fi wakeup option to explain that Wi-Fi wakeup is disabled because Wi-Fi scanning is turned off -->
    <string name="wifi_wakeup_summary_scanning_disabled">Unavailable because Wi\u2011Fi scanning is turned off</string>
    <!-- Checkbox summary for Wi-Fi wakeup option to explain that a network rating provider needs to be selected to use the feature. -->
    <string name="wifi_wakeup_summary_scoring_disabled">To use, select a network rating provider</string>
    <!-- Checkbox title for option to toggle poor network detection -->
    <string name="wifi_poor_network_detection">Avoid poor connections</string>
    <!-- Checkbox summary for option to toggle poor network detection -->
@@ -1710,6 +1712,10 @@
    <string name="use_open_wifi_automatically_title">Connect to open networks</string>
    <!-- Checkbox summary for option to connect to open Wi-Fi automatically  [CHAR LIMIT=100] -->
    <string name="use_open_wifi_automatically_summary">Automatically connect to high\u2011quality public networks</string>
    <!-- Checkbox summary for option to connect to open Wi-Fi automatically, to explain that a network rating provider needs to be selected to use the feature. -->
    <string name="use_open_wifi_automatically_summary_scoring_disabled">To use, select a network rating provider</string>
    <!-- Checkbox summary for option to connect to open Wi-Fi automatically, to explain that a compatible network rating provider needs to be selected to use the feature. -->
    <string name="use_open_wifi_automatically_summary_scorer_unsupported_disabled">To use, select a compatible network rating provider</string>
    <!-- Preference title for option to install certificates -->
    <string name="wifi_install_credentials">Install certificates</string>
    <!-- Message to describe "Wi-Fi scan always available feature" when Wi-Fi is off and Wi-Fi
+12 −5
Original line number Diff line number Diff line
@@ -15,7 +15,6 @@
 */
package com.android.settings.wifi;

import static android.content.Context.NETWORK_SCORE_SERVICE;
import static android.content.Context.WIFI_SERVICE;

import android.content.Context;
@@ -26,7 +25,6 @@ import android.provider.SearchIndexableResource;

import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
import com.android.settings.R;
import com.android.settings.SettingsActivity;
import com.android.settings.core.PreferenceController;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.network.NetworkScoreManagerWrapper;
@@ -44,6 +42,7 @@ public class ConfigureWifiSettings extends DashboardFragment {

    private static final String TAG = "ConfigureWifiSettings";

    private WifiWakeupPreferenceController mWifiWakeupPreferenceController;
    private UseOpenWifiPreferenceController mUseOpenWifiPreferenceController;

    @Override
@@ -59,8 +58,14 @@ public class ConfigureWifiSettings extends DashboardFragment {
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mProgressiveDisclosureMixin.setTileLimit(
            mUseOpenWifiPreferenceController.isAvailable() ? 3 : 2);
        int tileLimit = 1;
        if (mWifiWakeupPreferenceController.isAvailable()) {
            tileLimit++;
        }
        if (mUseOpenWifiPreferenceController.isAvailable()) {
            tileLimit++;
        }
        mProgressiveDisclosureMixin.setTileLimit(tileLimit);
    }

    @Override
@@ -72,11 +77,13 @@ public class ConfigureWifiSettings extends DashboardFragment {
    protected List<PreferenceController> getPreferenceControllers(Context context) {
        final NetworkScoreManagerWrapper networkScoreManagerWrapper =
                new NetworkScoreManagerWrapper(context.getSystemService(NetworkScoreManager.class));
        mWifiWakeupPreferenceController = new WifiWakeupPreferenceController(
                context, getLifecycle(), networkScoreManagerWrapper);
        mUseOpenWifiPreferenceController = new UseOpenWifiPreferenceController(context, this,
                networkScoreManagerWrapper, getLifecycle());
        final WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
        final List<PreferenceController> controllers = new ArrayList<>();
        controllers.add(new WifiWakeupPreferenceController(context, getLifecycle()));
        controllers.add(mWifiWakeupPreferenceController);
        controllers.add(new NetworkScorerPickerPreferenceController(context,
                networkScoreManagerWrapper));
        controllers.add(new NotifyOpenNetworksPreferenceController(context, getLifecycle()));
+38 −4
Original line number Diff line number Diff line
@@ -8,7 +8,6 @@ import android.content.Context;
import android.content.Intent;
import android.database.ContentObserver;
import android.net.NetworkScoreManager;

import android.net.NetworkScorerAppData;
import android.net.Uri;
import android.os.Handler;
@@ -20,13 +19,16 @@ import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;
import android.text.TextUtils;

import com.android.settings.network.NetworkScoreManagerWrapper;
import com.android.settings.R;
import com.android.settings.core.PreferenceController;
import com.android.settings.network.NetworkScoreManagerWrapper;
import com.android.settingslib.core.lifecycle.Lifecycle;
import com.android.settingslib.core.lifecycle.LifecycleObserver;
import com.android.settingslib.core.lifecycle.events.OnPause;
import com.android.settingslib.core.lifecycle.events.OnResume;

import java.util.List;

/**
 * {@link PreferenceController} that controls whether a user wants to enable the "use open networks
 * automatically" feature provider by the current network recommendation provider.
@@ -43,6 +45,7 @@ public class UseOpenWifiPreferenceController extends PreferenceController

    private Preference mPreference;
    private ComponentName mEnableUseWifiComponentName;
    private boolean mDoFeatureSupportedScorersExist;

    public UseOpenWifiPreferenceController(Context context, Fragment fragment,
            NetworkScoreManagerWrapper networkScoreManagerWrapper, Lifecycle lifecycle) {
@@ -52,6 +55,7 @@ public class UseOpenWifiPreferenceController extends PreferenceController
        mNetworkScoreManagerWrapper = networkScoreManagerWrapper;
        mSettingObserver = new SettingObserver();
        updateEnableUseWifiComponentName();
        checkForFeatureSupportedScorers();
        lifecycle.addObserver(this);
    }

@@ -61,6 +65,21 @@ public class UseOpenWifiPreferenceController extends PreferenceController
                appData == null ? null : appData.getEnableUseOpenWifiActivity();
    }

    private void checkForFeatureSupportedScorers() {
        if (mEnableUseWifiComponentName != null) {
            mDoFeatureSupportedScorersExist = true;
            return;
        }
        List<NetworkScorerAppData> scorers = mNetworkScoreManagerWrapper.getAllValidScorers();
        for (NetworkScorerAppData scorer : scorers) {
            if (scorer.getEnableUseOpenWifiActivity() != null) {
                mDoFeatureSupportedScorersExist = true;
                return;
            }
        }
        mDoFeatureSupportedScorersExist = false;
    }

    @Override
    public void displayPreference(PreferenceScreen screen) {
        super.displayPreference(screen);
@@ -79,7 +98,7 @@ public class UseOpenWifiPreferenceController extends PreferenceController

    @Override
    public boolean isAvailable() {
        return mEnableUseWifiComponentName != null;
        return mDoFeatureSupportedScorersExist;
    }

    @Override
@@ -93,8 +112,23 @@ public class UseOpenWifiPreferenceController extends PreferenceController
            return;
        }
        final SwitchPreference useOpenWifiPreference = (SwitchPreference) preference;
        useOpenWifiPreference.setVisible(isAvailable());

        boolean isScorerSet = mNetworkScoreManagerWrapper.getActiveScorerPackage() != null;
        boolean doesActiveScorerSupportFeature = mEnableUseWifiComponentName != null;

        useOpenWifiPreference.setChecked(isSettingEnabled());
        useOpenWifiPreference.setVisible(isAvailable());
        useOpenWifiPreference.setEnabled(isScorerSet && doesActiveScorerSupportFeature);

        if (!isScorerSet) {
            useOpenWifiPreference.setSummary(
                    R.string.use_open_wifi_automatically_summary_scoring_disabled);
        } else if (!doesActiveScorerSupportFeature) {
            useOpenWifiPreference.setSummary(
                    R.string.use_open_wifi_automatically_summary_scorer_unsupported_disabled);
        } else {
            useOpenWifiPreference.setSummary(R.string.use_open_wifi_automatically_summary);
        }
    }

    @Override
+16 −6
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@ import android.text.TextUtils;
import com.android.settings.R;

import com.android.settings.core.PreferenceController;
import com.android.settings.network.NetworkScoreManagerWrapper;
import com.android.settingslib.core.lifecycle.Lifecycle;
import com.android.settingslib.core.lifecycle.LifecycleObserver;
import com.android.settingslib.core.lifecycle.events.OnPause;
@@ -41,10 +42,13 @@ public class WifiWakeupPreferenceController extends PreferenceController impleme
        LifecycleObserver, OnResume, OnPause {

    private static final String KEY_ENABLE_WIFI_WAKEUP = "enable_wifi_wakeup";
    private final NetworkScoreManagerWrapper mNetworkScoreManager;
    private SettingObserver mSettingObserver;

    public WifiWakeupPreferenceController(Context context, Lifecycle lifecycle) {
    public WifiWakeupPreferenceController(
            Context context, Lifecycle lifecycle, NetworkScoreManagerWrapper networkScoreManager) {
        super(context);
        mNetworkScoreManager = networkScoreManager;
        lifecycle.addObserver(this);
    }

@@ -110,11 +114,17 @@ public class WifiWakeupPreferenceController extends PreferenceController impleme
        boolean networkRecommendationsEnabled = Settings.Global.getInt(
                mContext.getContentResolver(),
                Settings.Global.NETWORK_RECOMMENDATIONS_ENABLED, 0) == 1;
        enableWifiWakeup.setEnabled(networkRecommendationsEnabled && wifiScanningEnabled);

        enableWifiWakeup.setSummary(wifiScanningEnabled ?
                R.string.wifi_wakeup_summary :
                R.string.wifi_wakeup_summary_scanning_disabled);
        boolean activeScorerSet = mNetworkScoreManager.getActiveScorerPackage() != null;
        enableWifiWakeup.setEnabled(
                networkRecommendationsEnabled && wifiScanningEnabled && activeScorerSet);

        if (!activeScorerSet) {
            enableWifiWakeup.setSummary(R.string.wifi_wakeup_summary_scoring_disabled);
        } else if (!wifiScanningEnabled) {
            enableWifiWakeup.setSummary(R.string.wifi_wakeup_summary_scanning_disabled);
        } else {
            enableWifiWakeup.setSummary(R.string.wifi_wakeup_summary);
        }
    }

    class SettingObserver extends ContentObserver {
+67 −24
Original line number Diff line number Diff line
@@ -19,12 +19,12 @@ package com.android.settings.wifi;
import static android.provider.Settings.Global.USE_OPEN_WIFI_PACKAGE;
import static com.android.settings.wifi.UseOpenWifiPreferenceController.REQUEST_CODE_OPEN_WIFI_AUTOMATICALLY;
import static com.google.common.truth.Truth.assertThat;

import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import android.annotation.NonNull;
import android.app.Activity;
import android.app.Fragment;
import android.content.ComponentName;
@@ -36,11 +36,13 @@ import android.provider.Settings;
import android.support.v14.preference.SwitchPreference;
import android.support.v7.preference.Preference;

import com.android.settings.R;
import com.android.settings.TestConfig;
import com.android.settings.network.NetworkScoreManagerWrapper;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import com.android.settingslib.core.lifecycle.Lifecycle;

import com.google.common.collect.Lists;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -51,10 +53,14 @@ import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;

import java.util.ArrayList;
import java.util.List;

@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class UseOpenWifiPreferenceControllerTest {
    private static ComponentName ENABLE_ACTIVITY_COMPONENT = new ComponentName("package", "activityClass");
    private static ComponentName ENABLE_ACTIVITY_COMPONENT =
            new ComponentName("package", "activityClass");
    private static NetworkScorerAppData APP_DATA =
            new NetworkScorerAppData(0, null, null, ENABLE_ACTIVITY_COMPONENT, null);
    private static NetworkScorerAppData APP_DATA_NO_ACTIVITY =
@@ -79,28 +85,45 @@ public class UseOpenWifiPreferenceControllerTest {
                mContext, mFragment, mNetworkScoreManagerWrapper, mLifecycle);
    }

    @Test
    public void testIsAvailable_noScorer() {
        when(mNetworkScoreManagerWrapper.getActiveScorer()).thenReturn(null);
    /**
     * Sets the scorers.
     * @param scorers list of scorers returned by {@link NetworkScoreManager#getAllValidScorers()}.
     *                First scorer in the list is the active scorer.
     */
    private void setupScorers(@NonNull List<NetworkScorerAppData> scorers) {
        when(mNetworkScoreManagerWrapper.getActiveScorerPackage()).thenReturn(
                ENABLE_ACTIVITY_COMPONENT.getPackageName());
        when(mNetworkScoreManagerWrapper.getAllValidScorers()).thenReturn(scorers);
        when(mNetworkScoreManagerWrapper.getActiveScorer()).thenReturn(scorers.get(0));
    }

    @Test
    public void testIsAvailable_returnsFalseWhenNoScorerSet() {
        createController();

        assertThat(mController.isAvailable()).isFalse();
    }

    @Test
    public void testIsAvailable_noEnableActivity() {
        when(mNetworkScoreManagerWrapper.getActiveScorer()).thenReturn(APP_DATA_NO_ACTIVITY);

    public void testIsAvailable_returnsFalseWhenScorersNotSupported() {
        setupScorers(Lists.newArrayList(APP_DATA_NO_ACTIVITY));
        createController();

        assertThat(mController.isAvailable()).isFalse();
    }

    @Test
    public void testIsAvailable_enableActivityExists() {
        when(mNetworkScoreManagerWrapper.getActiveScorer()).thenReturn(APP_DATA);
    public void testIsAvailable_returnsTrueIfActiveScorerSupported() {
        setupScorers(Lists.newArrayList(APP_DATA, APP_DATA_NO_ACTIVITY));
        createController();

        assertThat(mController.isAvailable()).isTrue();
    }

    @Test
    public void testIsAvailable_returnsTrueIfNonActiveScorerSupported() {
        setupScorers(Lists.newArrayList(APP_DATA_NO_ACTIVITY, APP_DATA));
        when(mNetworkScoreManagerWrapper.getActiveScorer()).thenReturn(APP_DATA_NO_ACTIVITY);
        createController();

        assertThat(mController.isAvailable()).isTrue();
@@ -117,8 +140,6 @@ public class UseOpenWifiPreferenceControllerTest {

    @Test
    public void onPreferenceChange_notAvailable_shouldDoNothing() {
        when(mNetworkScoreManagerWrapper.getActiveScorer()).thenReturn(APP_DATA_NO_ACTIVITY);

        createController();

        final Preference pref = new Preference(mContext);
@@ -129,7 +150,7 @@ public class UseOpenWifiPreferenceControllerTest {

    @Test
    public void onPreferenceChange_matchingKeyAndAvailable_enableShouldStartEnableActivity() {
        when(mNetworkScoreManagerWrapper.getActiveScorer()).thenReturn(APP_DATA);
        setupScorers(Lists.newArrayList(APP_DATA, APP_DATA_NO_ACTIVITY));
        createController();

        final SwitchPreference pref = new SwitchPreference(mContext);
@@ -145,7 +166,7 @@ public class UseOpenWifiPreferenceControllerTest {

    @Test
    public void onPreferenceChange_matchingKeyAndAvailable_disableShouldUpdateSetting() {
        when(mNetworkScoreManagerWrapper.getActiveScorer()).thenReturn(APP_DATA);
        setupScorers(Lists.newArrayList(APP_DATA, APP_DATA_NO_ACTIVITY));
        Settings.Global.putString(mContext.getContentResolver(), USE_OPEN_WIFI_PACKAGE,
                ENABLE_ACTIVITY_COMPONENT.getPackageName());

@@ -161,6 +182,7 @@ public class UseOpenWifiPreferenceControllerTest {

    @Test
    public void onActivityResult_nonmatchingRequestCode_shouldDoNothing() {
        setupScorers(Lists.newArrayList(APP_DATA, APP_DATA_NO_ACTIVITY));
        createController();

        assertThat(mController.onActivityResult(234 /* requestCode */ , Activity.RESULT_OK))
@@ -171,6 +193,7 @@ public class UseOpenWifiPreferenceControllerTest {

    @Test
    public void onActivityResult_matchingRequestCode_nonOkResult_shouldDoNothing() {
        setupScorers(Lists.newArrayList(APP_DATA, APP_DATA_NO_ACTIVITY));
        createController();

        assertThat(mController
@@ -182,7 +205,7 @@ public class UseOpenWifiPreferenceControllerTest {

    @Test
    public void onActivityResult_matchingRequestCode_okResult_updatesSetting() {
        when(mNetworkScoreManagerWrapper.getActiveScorer()).thenReturn(APP_DATA);
        setupScorers(Lists.newArrayList(APP_DATA, APP_DATA_NO_ACTIVITY));
        createController();

        assertThat(mController
@@ -193,8 +216,8 @@ public class UseOpenWifiPreferenceControllerTest {
    }

    @Test
    public void updateState_preferenceSetCheckedAndSetVisibleWhenSettingsAreEnabled() {
        when(mNetworkScoreManagerWrapper.getActiveScorer()).thenReturn(APP_DATA);
    public void updateState_noEnableActivity_preferenceDisabled_summaryChanged() {
        setupScorers(Lists.newArrayList(APP_DATA_NO_ACTIVITY));
        createController();

        final SwitchPreference preference = mock(SwitchPreference.class);
@@ -203,19 +226,39 @@ public class UseOpenWifiPreferenceControllerTest {

        mController.updateState(preference);

        verify(preference).setVisible(true);
        verify(preference).setChecked(true);
        verify(preference).setChecked(false);
        verify(preference).setSummary(
                R.string.use_open_wifi_automatically_summary_scorer_unsupported_disabled);
    }

    @Test
    public void updateState_preferenceSetCheckedAndSetVisibleWhenSettingsAreDisabled() {
        final SwitchPreference preference = mock(SwitchPreference.class);
        Settings.Global.putString(mContext.getContentResolver(), USE_OPEN_WIFI_PACKAGE, "");
    public void updateState_noScorer_preferenceDisabled_summaryChanged() {
        when(mNetworkScoreManagerWrapper.getAllValidScorers()).thenReturn(new ArrayList<>());
        createController();

        final SwitchPreference preference = mock(SwitchPreference.class);
        Settings.Global.putString(mContext.getContentResolver(), USE_OPEN_WIFI_PACKAGE,
                ENABLE_ACTIVITY_COMPONENT.getPackageName());

        mController.updateState(preference);

        verify(preference).setVisible(false);
        verify(preference).setChecked(false);
        verify(preference).setSummary(
                R.string.use_open_wifi_automatically_summary_scoring_disabled);
    }

    @Test
    public void updateState_enableActivityExists_preferenceEnabled() {
        setupScorers(Lists.newArrayList(APP_DATA, APP_DATA_NO_ACTIVITY));
        createController();

        final SwitchPreference preference = mock(SwitchPreference.class);
        Settings.Global.putString(mContext.getContentResolver(), USE_OPEN_WIFI_PACKAGE,
                ENABLE_ACTIVITY_COMPONENT.getPackageName());

        mController.updateState(preference);

        verify(preference).setChecked(true);
        verify(preference).setSummary(R.string.use_open_wifi_automatically_summary);
    }
}
Loading