[Catalyst] Migrate AdaptiveConnection preference.

Bug: 368355361
Flag: com.android.settings.flags.catalyst_adaptive_connectivity
Test: Manual testing
atest -c AdaptiveConnectivityTogglePreferenceTest;
atest AdaptiveConnectivityScreenTest

Change-Id: I29e6d037ce5acbe03970aa519357ee1e4c021dbe
This commit is contained in:
Yuchen
2024-10-24 07:48:52 +00:00
parent 0478218dad
commit 101f0f7463
6 changed files with 149 additions and 3 deletions

View File

@@ -29,7 +29,7 @@
settings:lottie_rawRes="@drawable/ic_enhanced_connectivity"/>
<com.android.settingslib.widget.MainSwitchPreference
android:key="adaptive_connectivity"
android:key="adaptive_connectivity_enabled"
android:title="@string/adaptive_connectivity_main_switch_title"
settings:controller="com.android.settings.network.AdaptiveConnectivityTogglePreferenceController"/>

View File

@@ -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

View File

@@ -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)

View File

@@ -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)

View File

@@ -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)

View File

@@ -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)