diff --git a/res/xml/adaptive_connectivity_settings.xml b/res/xml/adaptive_connectivity_settings.xml index 63060065f83..0c8ff88dd47 100644 --- a/res/xml/adaptive_connectivity_settings.xml +++ b/res/xml/adaptive_connectivity_settings.xml @@ -29,7 +29,7 @@ settings:lottie_rawRes="@drawable/ic_enhanced_connectivity"/> diff --git a/src/com/android/settings/network/AdaptiveConnectivityScreen.kt b/src/com/android/settings/network/AdaptiveConnectivityScreen.kt index 99f402f3f65..0604236173f 100644 --- a/src/com/android/settings/network/AdaptiveConnectivityScreen.kt +++ b/src/com/android/settings/network/AdaptiveConnectivityScreen.kt @@ -35,7 +35,9 @@ class AdaptiveConnectivityScreen : PreferenceScreenCreator { override fun fragmentClass() = AdaptiveConnectivitySettings::class.java - override fun getPreferenceHierarchy(context: Context) = preferenceHierarchy(this) {} + override fun getPreferenceHierarchy(context: Context) = preferenceHierarchy(this) { + +AdaptiveConnectivityTogglePreference() + } override fun hasCompleteHierarchy() = false diff --git a/src/com/android/settings/network/AdaptiveConnectivityTogglePreference.kt b/src/com/android/settings/network/AdaptiveConnectivityTogglePreference.kt new file mode 100644 index 00000000000..e1aea8519aa --- /dev/null +++ b/src/com/android/settings/network/AdaptiveConnectivityTogglePreference.kt @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2024 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.wifi.WifiManager +import android.provider.Settings.Secure.ADAPTIVE_CONNECTIVITY_ENABLED +import androidx.preference.Preference +import com.android.settings.R +import com.android.settingslib.datastore.SettingsSecureStore +import com.android.settingslib.metadata.MainSwitchPreference +import com.android.settingslib.metadata.PreferenceMetadata +import com.android.settingslib.preference.MainSwitchPreferenceBinding + +// LINT.IfChange +class AdaptiveConnectivityTogglePreference : + MainSwitchPreference( + ADAPTIVE_CONNECTIVITY_ENABLED, + R.string.adaptive_connectivity_main_switch_title, + ), + MainSwitchPreferenceBinding, + Preference.OnPreferenceChangeListener { + + override fun storage(context: Context) = SettingsSecureStore.get(context) + + override fun bind(preference: Preference, metadata: PreferenceMetadata) { + super.bind(preference, metadata) + preference.onPreferenceChangeListener = this + } + + override fun onPreferenceChange(preference: Preference, newValue: Any): Boolean { + val isChecked = newValue as Boolean + preference.context + .getSystemService(WifiManager::class.java) + ?.setWifiScoringEnabled(isChecked) + return true + } +} +// LINT.ThenChange(AdaptiveConnectivityTogglePreferenceController.java) diff --git a/src/com/android/settings/network/AdaptiveConnectivityTogglePreferenceController.java b/src/com/android/settings/network/AdaptiveConnectivityTogglePreferenceController.java index ea849f65ac0..e1a3fb437ee 100644 --- a/src/com/android/settings/network/AdaptiveConnectivityTogglePreferenceController.java +++ b/src/com/android/settings/network/AdaptiveConnectivityTogglePreferenceController.java @@ -29,6 +29,7 @@ import com.android.settings.widget.SettingsMainSwitchPreferenceController; * {@link SettingsMainSwitchPreferenceController} * that controls whether Adaptive connectivity option is enabled. */ +// LINT.IfChange public class AdaptiveConnectivityTogglePreferenceController extends SettingsMainSwitchPreferenceController { @@ -69,3 +70,4 @@ public class AdaptiveConnectivityTogglePreferenceController extends return R.string.menu_key_network; } } +// LINT.ThenChange(AdaptiveConnectivityTogglePreference.kt) diff --git a/tests/robotests/src/com/android/settings/network/AdaptiveConnectivityTogglePreferenceControllerTest.java b/tests/robotests/src/com/android/settings/network/AdaptiveConnectivityTogglePreferenceControllerTest.java index c95797f3e34..02d233864e9 100644 --- a/tests/robotests/src/com/android/settings/network/AdaptiveConnectivityTogglePreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/network/AdaptiveConnectivityTogglePreferenceControllerTest.java @@ -36,10 +36,11 @@ import org.mockito.MockitoAnnotations; import org.robolectric.RobolectricTestRunner; import org.robolectric.RuntimeEnvironment; +// LINT.IfChange @RunWith(RobolectricTestRunner.class) public class AdaptiveConnectivityTogglePreferenceControllerTest { - private static final String PREF_KEY = "adaptive_connectivity"; + private static final String PREF_KEY = "adaptive_connectivity_enabled"; @Mock(answer = RETURNS_DEEP_STUBS) private Context mContext; @@ -87,3 +88,4 @@ public class AdaptiveConnectivityTogglePreferenceControllerTest { verify(mWifiManager).setWifiScoringEnabled(false); } } +// LINT.ThenChange(AdaptiveConnectivityTogglePreferenceTest.kt) diff --git a/tests/robotests/src/com/android/settings/network/AdaptiveConnectivityTogglePreferenceTest.kt b/tests/robotests/src/com/android/settings/network/AdaptiveConnectivityTogglePreferenceTest.kt new file mode 100644 index 00000000000..913adf6d7ad --- /dev/null +++ b/tests/robotests/src/com/android/settings/network/AdaptiveConnectivityTogglePreferenceTest.kt @@ -0,0 +1,87 @@ +/* + * Copyright (C) 2024 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.wifi.WifiManager +import android.platform.test.flag.junit.SetFlagsRule +import android.provider.Settings +import androidx.test.core.app.ApplicationProvider +import androidx.test.ext.junit.runners.AndroidJUnit4 +import com.android.settingslib.widget.MainSwitchPreference +import com.google.common.truth.Truth.assertThat +import org.junit.Before +import org.junit.Rule +import org.junit.Test +import org.junit.runner.RunWith +import org.mockito.kotlin.atLeastOnce +import org.mockito.kotlin.mock +import org.mockito.kotlin.spy +import org.mockito.kotlin.verify +import org.mockito.kotlin.whenever + +// LINT.IfChange +@RunWith(AndroidJUnit4::class) +class AdaptiveConnectivityTogglePreferenceTest { + @get:Rule + val setFlagsRule = SetFlagsRule() + + private val appContext: Context = spy(ApplicationProvider.getApplicationContext()){} + + private val mockWifiManager: WifiManager = mock() + + private val adaptiveConnectivityTogglePreference = AdaptiveConnectivityTogglePreference() + + @Before + fun setUp() { + whenever(appContext.getSystemService(WifiManager::class.java)).thenReturn(mockWifiManager) + } + + @Test + fun setChecked_withTrue_shouldUpdateSetting() { + Settings.Secure.putInt( + appContext.contentResolver, + Settings.Secure.ADAPTIVE_CONNECTIVITY_ENABLED, 0 + ) + + val mainSwitchPreference = getMainSwitchPreferenceCompat().apply { performClick() } + + assertThat(mainSwitchPreference.isChecked).isTrue() + verify(mockWifiManager, atLeastOnce()).setWifiScoringEnabled(true) + } + + @Test + fun setChecked_withFalse_shouldUpdateSetting() { + Settings.Secure.putInt( + appContext.contentResolver, + Settings.Secure.ADAPTIVE_CONNECTIVITY_ENABLED, 1 + ) + + val mainSwitchPreference = getMainSwitchPreferenceCompat().apply { performClick() } + + assertThat(mainSwitchPreference.isChecked).isFalse() + verify(mockWifiManager).setWifiScoringEnabled(false) + } + + private fun getMainSwitchPreferenceCompat(): MainSwitchPreference = + adaptiveConnectivityTogglePreference.run { + val preference = createWidget(appContext) + bind(preference, this) + preference as MainSwitchPreference + } +} +// LINT.ThenChange(AdaptiveConnectivityTogglePreferenceControllerTest.java)