Fix carrier-specific Wi-Fi settings appears in search - even on non-carrier phone

The preference should be removed from the search result when
the availability status is false.

Change-Id: I979bc70ec7672b137b96a7e02db2e9ba29fbb7a8
Fixes: 112550245
Test: manual and robotests
This commit is contained in:
Stanley Wang
2018-12-03 18:51:03 +08:00
parent 32ee158e2a
commit f301f8b216
4 changed files with 17 additions and 44 deletions

View File

@@ -41,7 +41,8 @@
<SwitchPreference <SwitchPreference
android:key="wifi_cellular_data_fallback" android:key="wifi_cellular_data_fallback"
android:title="@string/wifi_cellular_data_fallback_title" android:title="@string/wifi_cellular_data_fallback_title"
android:summary="@string/wifi_cellular_data_fallback_summary" /> android:summary="@string/wifi_cellular_data_fallback_summary"
settings:controller="com.android.settings.wifi.CellularFallbackPreferenceController" />
<Preference <Preference
android:key="install_credentials" android:key="install_credentials"

View File

@@ -18,62 +18,34 @@ package com.android.settings.wifi;
import android.content.Context; import android.content.Context;
import android.provider.Settings; import android.provider.Settings;
import android.text.TextUtils;
import androidx.preference.Preference; import com.android.settings.core.TogglePreferenceController;
import androidx.preference.SwitchPreference;
import com.android.settings.core.PreferenceControllerMixin;
import com.android.settingslib.core.AbstractPreferenceController;
/** /**
* {@link AbstractPreferenceController} that controls whether we should fall back to celluar when * CellularFallbackPreferenceController controls whether we should fall back to celluar when
* wifi is bad. * wifi is bad.
*/ */
public class CellularFallbackPreferenceController extends AbstractPreferenceController public class CellularFallbackPreferenceController extends TogglePreferenceController {
implements PreferenceControllerMixin {
private static final String KEY_CELLULAR_FALLBACK = "wifi_cellular_data_fallback"; public CellularFallbackPreferenceController(Context context, String key) {
super(context, key);
public CellularFallbackPreferenceController(Context context) {
super(context);
} }
@Override @Override
public boolean isAvailable() { public int getAvailabilityStatus() {
return !avoidBadWifiConfig(); return !avoidBadWifiConfig() ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
} }
@Override @Override
public String getPreferenceKey() { public boolean isChecked() {
return KEY_CELLULAR_FALLBACK; return avoidBadWifiCurrentSettings();
} }
@Override @Override
public boolean handlePreferenceTreeClick(Preference preference) { public boolean setChecked(boolean isChecked) {
if (!TextUtils.equals(preference.getKey(), KEY_CELLULAR_FALLBACK)) {
return false;
}
if (!(preference instanceof SwitchPreference)) {
return false;
}
// On: avoid bad wifi. Off: prompt. // On: avoid bad wifi. Off: prompt.
String settingName = Settings.Global.NETWORK_AVOID_BAD_WIFI; return Settings.Global.putString(mContext.getContentResolver(),
Settings.Global.putString(mContext.getContentResolver(), settingName, Settings.Global.NETWORK_AVOID_BAD_WIFI, isChecked ? "1" : null);
((SwitchPreference) preference).isChecked() ? "1" : null);
return true;
}
@Override
public void updateState(Preference preference) {
final boolean currentSetting = avoidBadWifiCurrentSettings();
// TODO: can this ever be null? The return value of avoidBadWifiConfig() can only
// change if the resources change, but if that happens the activity will be recreated...
if (preference != null) {
SwitchPreference pref = (SwitchPreference) preference;
pref.setChecked(currentSetting);
}
} }
private boolean avoidBadWifiConfig() { private boolean avoidBadWifiConfig() {
@@ -85,4 +57,4 @@ public class CellularFallbackPreferenceController extends AbstractPreferenceCont
return "1".equals(Settings.Global.getString(mContext.getContentResolver(), return "1".equals(Settings.Global.getString(mContext.getContentResolver(),
Settings.Global.NETWORK_AVOID_BAD_WIFI)); Settings.Global.NETWORK_AVOID_BAD_WIFI));
} }
} }

View File

@@ -85,7 +85,6 @@ public class ConfigureWifiSettings extends DashboardFragment {
controllers.add(mUseOpenWifiPreferenceController); controllers.add(mUseOpenWifiPreferenceController);
controllers.add(new WifiInfoPreferenceController(context, getSettingsLifecycle(), controllers.add(new WifiInfoPreferenceController(context, getSettingsLifecycle(),
wifiManager)); wifiManager));
controllers.add(new CellularFallbackPreferenceController(context));
controllers.add(new WifiP2pPreferenceController(context, getSettingsLifecycle(), controllers.add(new WifiP2pPreferenceController(context, getSettingsLifecycle(),
wifiManager)); wifiManager));
return controllers; return controllers;

View File

@@ -33,6 +33,7 @@ import org.mockito.MockitoAnnotations;
@RunWith(SettingsRobolectricTestRunner.class) @RunWith(SettingsRobolectricTestRunner.class)
public class CellularFallbackPreferenceControllerTest { public class CellularFallbackPreferenceControllerTest {
private static final String KEY_CELLULAR_FALLBACK = "wifi_cellular_data_fallback";
@Mock(answer = Answers.RETURNS_DEEP_STUBS) @Mock(answer = Answers.RETURNS_DEEP_STUBS)
private Context mContext; private Context mContext;
@@ -42,7 +43,7 @@ public class CellularFallbackPreferenceControllerTest {
@Before @Before
public void setUp() { public void setUp() {
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
mController = new CellularFallbackPreferenceController(mContext); mController = new CellularFallbackPreferenceController(mContext, KEY_CELLULAR_FALLBACK);
} }
@Test @Test