Add a "Don't ask again" checkbox to the avoid bad wifi dialog.

Also update the settings code to reflect the new tristate
semantics of the NETWORK_AVOID_BAD_WIFI setting.

Bug: 31075769
Change-Id: Icd21d8272abe6afd42ee8a41e2c7e1a6af77f0b3
diff --git a/src/com/android/settings/wifi/ConfigureWifiSettings.java b/src/com/android/settings/wifi/ConfigureWifiSettings.java
index 5ac5a7b..077d601 100644
--- a/src/com/android/settings/wifi/ConfigureWifiSettings.java
+++ b/src/com/android/settings/wifi/ConfigureWifiSettings.java
@@ -170,8 +170,8 @@
     }
 
     private boolean avoidBadWifiCurrentSettings() {
-        return Settings.Global.getInt(getContentResolver(),
-                Settings.Global.NETWORK_AVOID_BAD_WIFI, 0) == 1;
+        return "1".equals(Settings.Global.getString(getContentResolver(),
+                Settings.Global.NETWORK_AVOID_BAD_WIFI));
     }
 
     @Override
@@ -183,17 +183,10 @@
                     Settings.Global.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON,
                     ((SwitchPreference) preference).isChecked() ? 1 : 0);
         } else if (KEY_CELLULAR_FALLBACK.equals(key)) {
+            // On: avoid bad wifi. Off: prompt.
             String settingName = Settings.Global.NETWORK_AVOID_BAD_WIFI;
-            if (((SwitchPreference) preference).isChecked()) {
-                // The user wants to avoid bad wifi networks. Remember the choice.
-                Settings.Global.putInt(getContentResolver(), settingName, 1);
-            } else {
-                // Unset the setting. ConnectivityService interprets null to mean "use the carrier
-                // default". We don't set the setting to 0 because if we do, and the user switches
-                // to a carrier that does not restrict cellular fallback, then there is no way to
-                // set it to 1 again because on such a carrier the toggle is never shown.
-                Settings.Global.putString(getContentResolver(), settingName, null);
-            }
+            Settings.Global.putString(getContentResolver(), settingName,
+                    ((SwitchPreference) preference).isChecked() ? "1" : null);
         } else {
             return super.onPreferenceTreeClick(preference);
         }
diff --git a/src/com/android/settings/wifi/WifiNoInternetDialog.java b/src/com/android/settings/wifi/WifiNoInternetDialog.java
index a8f9e33..6b7b2db 100644
--- a/src/com/android/settings/wifi/WifiNoInternetDialog.java
+++ b/src/com/android/settings/wifi/WifiNoInternetDialog.java
@@ -164,24 +164,26 @@
 
     public void onClick(DialogInterface dialog, int which) {
         if (which != BUTTON_NEGATIVE && which != BUTTON_POSITIVE) return;
-        final boolean accept = (which == BUTTON_POSITIVE);
+        final boolean always = mAlwaysAllow.isChecked();
+        final String what, action;
 
         if (ACTION_PROMPT_UNVALIDATED.equals(mAction)) {
-            final String action = (accept ? "Connect" : "Ignore");
-            final boolean always = mAlwaysAllow.isChecked();
+            what = "NO_INTERNET";
+            final boolean accept = (which == BUTTON_POSITIVE);
+            action = (accept ? "Connect" : "Ignore");
             mCM.setAcceptUnvalidated(mNetwork, accept, always);
-            Log.d(TAG, "NO_INTERNET: " + action +  " network=" + mNetwork +
-                    (always ? " and remember" : ""));
         } else {
-            final String action = (accept ? "Switch" : "Cancel");
-            Log.d(TAG, "LOST_INTERNET: " + action);
-            // Only ever set the setting to 1. The values understood by ConnectivityService are null
-            // (use carrier default) or 1 (avoid bad networks regardless of carrier).
-            // TODO: Use a value other than 1 here to indicate a persisted "yes" or "no" given mAlwaysAllow.
-            if (accept) {
-                Settings.Global.putInt(mAlertParams.mContext.getContentResolver(),
-                        Settings.Global.NETWORK_AVOID_BAD_WIFI, 1);
+            what = "LOST_INTERNET";
+            final boolean avoid = (which == BUTTON_POSITIVE);
+            action = (avoid ? "Switch away" : "Get stuck");
+            if (always) {
+                Settings.Global.putString(mAlertParams.mContext.getContentResolver(),
+                        Settings.Global.NETWORK_AVOID_BAD_WIFI, avoid ? "1" : "0");
+            } else if (avoid) {
+                mCM.setAvoidUnvalidated(mNetwork);
             }
         }
+        Log.d(TAG, what + ": " + action +  " network=" + mNetwork +
+                (always ? " and remember" : ""));
     }
 }