Merge "Fix carrier-specific Wi-Fi settings appears in search - even on non-carrier phone"
diff --git a/res/xml/wifi_configure_settings.xml b/res/xml/wifi_configure_settings.xml
index 8ec320a..15ab1aa 100644
--- a/res/xml/wifi_configure_settings.xml
+++ b/res/xml/wifi_configure_settings.xml
@@ -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"
diff --git a/src/com/android/settings/wifi/CellularFallbackPreferenceController.java b/src/com/android/settings/wifi/CellularFallbackPreferenceController.java
index a883826..cbb8fb8 100644
--- a/src/com/android/settings/wifi/CellularFallbackPreferenceController.java
+++ b/src/com/android/settings/wifi/CellularFallbackPreferenceController.java
@@ -18,62 +18,34 @@
 
 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 @@
         return "1".equals(Settings.Global.getString(mContext.getContentResolver(),
                 Settings.Global.NETWORK_AVOID_BAD_WIFI));
     }
-}
+}
\ No newline at end of file
diff --git a/src/com/android/settings/wifi/ConfigureWifiSettings.java b/src/com/android/settings/wifi/ConfigureWifiSettings.java
index 1d1c30c..8df4a41 100644
--- a/src/com/android/settings/wifi/ConfigureWifiSettings.java
+++ b/src/com/android/settings/wifi/ConfigureWifiSettings.java
@@ -85,7 +85,6 @@
         controllers.add(mUseOpenWifiPreferenceController);
         controllers.add(new WifiInfoPreferenceController(context, getSettingsLifecycle(),
                 wifiManager));
-        controllers.add(new CellularFallbackPreferenceController(context));
         controllers.add(new WifiP2pPreferenceController(context, getSettingsLifecycle(),
                 wifiManager));
         return controllers;
diff --git a/tests/robotests/src/com/android/settings/wifi/CellularFallbackPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/wifi/CellularFallbackPreferenceControllerTest.java
index e12053c..91598c0 100644
--- a/tests/robotests/src/com/android/settings/wifi/CellularFallbackPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/wifi/CellularFallbackPreferenceControllerTest.java
@@ -33,6 +33,7 @@
 
 @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 @@
     @Before
     public void setUp() {
         MockitoAnnotations.initMocks(this);
-        mController = new CellularFallbackPreferenceController(mContext);
+        mController = new CellularFallbackPreferenceController(mContext, KEY_CELLULAR_FALLBACK);
     }
 
     @Test