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
android:key="wifi_cellular_data_fallback"
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
android:key="install_credentials"

View File

@@ -18,62 +18,34 @@ package com.android.settings.wifi;
import android.content.Context;
import android.provider.Settings;
import android.text.TextUtils;
import androidx.preference.Preference;
import androidx.preference.SwitchPreference;
import com.android.settings.core.PreferenceControllerMixin;
import com.android.settingslib.core.AbstractPreferenceController;
import com.android.settings.core.TogglePreferenceController;
/**
* {@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.
*/
public class CellularFallbackPreferenceController extends AbstractPreferenceController
implements PreferenceControllerMixin {
public class CellularFallbackPreferenceController extends TogglePreferenceController {
private static final String KEY_CELLULAR_FALLBACK = "wifi_cellular_data_fallback";
public CellularFallbackPreferenceController(Context context) {
super(context);
public CellularFallbackPreferenceController(Context context, String key) {
super(context, key);
}
@Override
public boolean isAvailable() {
return !avoidBadWifiConfig();
public int getAvailabilityStatus() {
return !avoidBadWifiConfig() ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
}
@Override
public String getPreferenceKey() {
return KEY_CELLULAR_FALLBACK;
public boolean isChecked() {
return avoidBadWifiCurrentSettings();
}
@Override
public boolean handlePreferenceTreeClick(Preference preference) {
if (!TextUtils.equals(preference.getKey(), KEY_CELLULAR_FALLBACK)) {
return false;
}
if (!(preference instanceof SwitchPreference)) {
return false;
}
public boolean setChecked(boolean isChecked) {
// On: avoid bad wifi. Off: prompt.
String settingName = Settings.Global.NETWORK_AVOID_BAD_WIFI;
Settings.Global.putString(mContext.getContentResolver(), settingName,
((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);
}
return Settings.Global.putString(mContext.getContentResolver(),
Settings.Global.NETWORK_AVOID_BAD_WIFI, isChecked ? "1" : null);
}
private boolean avoidBadWifiConfig() {
@@ -85,4 +57,4 @@ public class CellularFallbackPreferenceController extends AbstractPreferenceCont
return "1".equals(Settings.Global.getString(mContext.getContentResolver(),
Settings.Global.NETWORK_AVOID_BAD_WIFI));
}
}
}

View File

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

View File

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