diff --git a/src/com/android/settings/network/NetworkScoreManagerWrapper.java b/src/com/android/settings/network/NetworkScoreManagerWrapper.java index 0d353781797..3489640f6a2 100644 --- a/src/com/android/settings/network/NetworkScoreManagerWrapper.java +++ b/src/com/android/settings/network/NetworkScoreManagerWrapper.java @@ -56,6 +56,13 @@ public class NetworkScoreManagerWrapper { return mNetworkScoreManager.getActiveScorerPackage(); } + /** + * Returns metadata about the active scorer or null if there is no active scorer. + */ + @Nullable + public NetworkScorerAppData getActiveScorer() { + return mNetworkScoreManager.getActiveScorer(); + } /** * Set the active scorer to a new package and clear existing scores. diff --git a/src/com/android/settings/network/NetworkScorerPickerPreferenceController.java b/src/com/android/settings/network/NetworkScorerPickerPreferenceController.java new file mode 100644 index 00000000000..6ff6edb2644 --- /dev/null +++ b/src/com/android/settings/network/NetworkScorerPickerPreferenceController.java @@ -0,0 +1,69 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.android.settings.network; + +import android.content.Context; +import android.net.NetworkScorerAppData; +import android.provider.Settings; +import android.support.v7.preference.Preference; +import com.android.settings.R; +import com.android.settings.core.PreferenceController; + +/** + * {@link PreferenceController} that shows the active network scorer and toggles the preference + * based on {@link Settings.Global.NETWORK_RECOMMENDATIONS_ENABLED}. + */ +public class NetworkScorerPickerPreferenceController extends PreferenceController { + + private static final String KEY_NETWORK_SCORER_PICKER = "network_scorer_picker"; + + private final NetworkScoreManagerWrapper mNetworkScoreManager; + + public NetworkScorerPickerPreferenceController(Context context, + NetworkScoreManagerWrapper networkScoreManager) { + super(context); + mNetworkScoreManager = networkScoreManager; + } + + @Override + public String getPreferenceKey() { + return KEY_NETWORK_SCORER_PICKER; + } + + @Override + public void updateState(Preference preference) { + boolean enabled = Settings.Global.getInt(mContext.getContentResolver(), + Settings.Global.NETWORK_RECOMMENDATIONS_ENABLED, 0) == 1; + preference.setEnabled(enabled); + if (!enabled) { + preference.setSummary(null); + return; + } + + NetworkScorerAppData scorer = mNetworkScoreManager.getActiveScorer(); + if (scorer == null) { + preference.setSummary(mContext.getString( + R.string.network_scorer_picker_none_preference)); + } else { + preference.setSummary(scorer.getRecommendationServiceLabel()); + } + } + + @Override + public boolean isAvailable() { + return true; + } +} diff --git a/src/com/android/settings/wifi/ConfigureWifiSettings.java b/src/com/android/settings/wifi/ConfigureWifiSettings.java index 18cc28be20d..73bafdf1bcb 100644 --- a/src/com/android/settings/wifi/ConfigureWifiSettings.java +++ b/src/com/android/settings/wifi/ConfigureWifiSettings.java @@ -15,9 +15,11 @@ */ package com.android.settings.wifi; +import static android.content.Context.NETWORK_SCORE_SERVICE; import static android.content.Context.WIFI_SERVICE; import android.content.Context; +import android.net.NetworkScoreManager; import android.net.wifi.WifiManager; import android.provider.SearchIndexableResource; @@ -25,6 +27,8 @@ import com.android.internal.logging.nano.MetricsProto.MetricsEvent; import com.android.settings.R; import com.android.settings.core.PreferenceController; import com.android.settings.dashboard.DashboardFragment; +import com.android.settings.network.NetworkScoreManagerWrapper; +import com.android.settings.network.NetworkScorerPickerPreferenceController; import com.android.settings.network.WifiCallingPreferenceController; import com.android.settings.search.BaseSearchIndexProvider; import com.android.settings.search.Indexable; @@ -69,6 +73,9 @@ public class ConfigureWifiSettings extends DashboardFragment { controllers.add(new CellularFallbackPreferenceController(context)); controllers.add(new NotifyOpenNetworksPreferenceController(context, getLifecycle())); controllers.add(new WifiWakeupPreferenceController(context, getLifecycle())); + controllers.add(new NetworkScorerPickerPreferenceController(context, + new NetworkScoreManagerWrapper( + (NetworkScoreManager) getSystemService(NETWORK_SCORE_SERVICE)))); controllers.add(new WifiSleepPolicyPreferenceController(context)); controllers.add(new WifiP2pPreferenceController(context, getLifecycle(), mWifiManager)); controllers.add(new WifiCallingPreferenceController(context)); diff --git a/tests/robotests/src/com/android/settings/network/NetworkScorerPickerPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/network/NetworkScorerPickerPreferenceControllerTest.java new file mode 100644 index 00000000000..6bf47ac19b7 --- /dev/null +++ b/tests/robotests/src/com/android/settings/network/NetworkScorerPickerPreferenceControllerTest.java @@ -0,0 +1,103 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.settings.network; + +import static android.provider.Settings.Global.NETWORK_RECOMMENDATIONS_ENABLED; +import static com.google.common.truth.Truth.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import android.content.ComponentName; +import android.content.Context; +import android.net.NetworkScorerAppData; +import android.provider.Settings; +import android.support.v7.preference.Preference; +import com.android.settings.R; +import com.android.settings.SettingsRobolectricTestRunner; +import com.android.settings.TestConfig; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.annotation.Config; + +@RunWith(SettingsRobolectricTestRunner.class) +@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) +public class NetworkScorerPickerPreferenceControllerTest { + + private static final String TEST_SCORER_PACKAGE = "Test Package"; + private static final String TEST_SCORER_CLASS = "Test Class"; + private static final String TEST_SCORER_LABEL = "Test Label"; + + private Context mContext; + @Mock + private NetworkScoreManagerWrapper mNetworkScorer; + private NetworkScorerPickerPreferenceController mController; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + mContext = RuntimeEnvironment.application; + mController = new NetworkScorerPickerPreferenceController(mContext, mNetworkScorer); + } + + @Test + public void testIsAvailable_shouldAlwaysReturnTrue() { + assertThat(mController.isAvailable()).isTrue(); + } + + @Test + public void updateState_preferenceSetSummaryAsActiveScorerLabel() { + Settings.System.putInt(mContext.getContentResolver(), NETWORK_RECOMMENDATIONS_ENABLED, 1); + ComponentName scorer = new ComponentName(TEST_SCORER_PACKAGE, TEST_SCORER_CLASS); + NetworkScorerAppData scorerAppData = new NetworkScorerAppData( + 0, scorer, TEST_SCORER_LABEL, null /* enableUseOpenWifiActivity */); + when(mNetworkScorer.getActiveScorer()).thenReturn(scorerAppData); + Preference preference = mock(Preference.class); + + mController.updateState(preference); + + verify(preference).setSummary(TEST_SCORER_LABEL); + } + + @Test + public void updateState_noActiveScorer_preferenceSetSummaryToNone() { + Settings.System.putInt(mContext.getContentResolver(), NETWORK_RECOMMENDATIONS_ENABLED, 1); + when(mNetworkScorer.getActiveScorer()).thenReturn(null); + Preference preference = mock(Preference.class); + + mController.updateState(preference); + + verify(preference).setSummary(mContext.getString( + R.string.network_scorer_picker_none_preference)); + } + + @Test + public void updateState_networkRecommendationsDisabled_preferenceDisabled() { + Settings.System.putInt(mContext.getContentResolver(), NETWORK_RECOMMENDATIONS_ENABLED, 0); + when(mNetworkScorer.getActiveScorer()).thenReturn(null); + Preference preference = mock(Preference.class); + + mController.updateState(preference); + + verify(preference).setEnabled(false); + verify(preference).setSummary(null); + } +}