Merge "Handle null intent in WifiCallingPreferenceController" into qt-dev
diff --git a/res/values/strings.xml b/res/values/strings.xml
index ec15bdf..6e92a3d 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -10652,6 +10652,12 @@
     <string name="game_driver_app_preference_prerelease_driver">Prerelease Driver</string>
     <!-- The system value for Game Driver app preference [CHAR LIMIT=50] -->
     <string name="game_driver_app_preference_system">System Graphics Driver</string>
+    <!-- All the values for Game Driver all apps preference [CHAR LIMIT=50] -->
+    <string-array name="game_driver_all_apps_preference_values">
+        <item>@string/game_driver_app_preference_default</item>
+        <item>@string/game_driver_app_preference_game_driver</item>
+        <item>@string/game_driver_app_preference_prerelease_driver</item>
+    </string-array>
     <!-- All the values for Game Driver app preference [CHAR LIMIT=50] -->
     <string-array name="game_driver_app_preference_values">
         <item>@string/game_driver_app_preference_default</item>
diff --git a/res/xml/game_driver_settings.xml b/res/xml/game_driver_settings.xml
index a04724a..6daeefb 100644
--- a/res/xml/game_driver_settings.xml
+++ b/res/xml/game_driver_settings.xml
@@ -21,11 +21,14 @@
     android:key="game_driver_settings"
     android:title="@string/game_driver_dashboard_title">
 
-    <SwitchPreference
+    <ListPreference
         android:key="game_driver_all_apps_preference"
         android:title="@string/game_driver_all_apps_preference_title"
+        android:dialogTitle="@string/game_driver_all_apps_preference_title"
+        android:entries="@array/game_driver_all_apps_preference_values"
+        android:entryValues="@array/game_driver_all_apps_preference_values"
         settings:controller="com.android.settings.development.gamedriver.GameDriverEnableForAllAppsPreferenceController">
-    </SwitchPreference>
+    </ListPreference>
 
     <PreferenceCategory
         android:key="game_driver_category"
diff --git a/src/com/android/settings/development/gamedriver/GameDriverEnableForAllAppsPreferenceController.java b/src/com/android/settings/development/gamedriver/GameDriverEnableForAllAppsPreferenceController.java
index dd48f69..290e4b2 100644
--- a/src/com/android/settings/development/gamedriver/GameDriverEnableForAllAppsPreferenceController.java
+++ b/src/com/android/settings/development/gamedriver/GameDriverEnableForAllAppsPreferenceController.java
@@ -18,15 +18,17 @@
 
 import android.content.ContentResolver;
 import android.content.Context;
+import android.content.res.Resources;
 import android.os.Handler;
 import android.os.Looper;
 import android.provider.Settings;
 
 import androidx.annotation.VisibleForTesting;
+import androidx.preference.ListPreference;
 import androidx.preference.Preference;
 import androidx.preference.PreferenceScreen;
-import androidx.preference.SwitchPreference;
 
+import com.android.settings.R;
 import com.android.settings.core.BasePreferenceController;
 import com.android.settingslib.core.lifecycle.LifecycleObserver;
 import com.android.settingslib.core.lifecycle.events.OnStart;
@@ -43,19 +45,30 @@
 
     public static final int GAME_DRIVER_DEFAULT = 0;
     public static final int GAME_DRIVER_ALL_APPS = 1;
-    public static final int GAME_DRIVER_OFF = 2;
+    public static final int GAME_DRIVER_PRERELEASE_ALL_APPS = 2;
+    public static final int GAME_DRIVER_OFF = 3;
 
     private final Context mContext;
     private final ContentResolver mContentResolver;
+    private final String mPreferenceDefault;
+    private final String mPreferenceGameDriver;
+    private final String mPreferencePrereleaseDriver;
     @VisibleForTesting
     GameDriverContentObserver mGameDriverContentObserver;
 
-    private SwitchPreference mPreference;
+    private ListPreference mPreference;
 
     public GameDriverEnableForAllAppsPreferenceController(Context context, String key) {
         super(context, key);
         mContext = context;
         mContentResolver = context.getContentResolver();
+
+        final Resources resources = context.getResources();
+        mPreferenceDefault = resources.getString(R.string.game_driver_app_preference_default);
+        mPreferenceGameDriver =
+                resources.getString(R.string.game_driver_app_preference_game_driver);
+        mPreferencePrereleaseDriver =
+                resources.getString(R.string.game_driver_app_preference_prerelease_driver);
         mGameDriverContentObserver =
                 new GameDriverContentObserver(new Handler(Looper.getMainLooper()), this);
     }
@@ -89,31 +102,44 @@
 
     @Override
     public void updateState(Preference preference) {
-        final SwitchPreference switchPreference = (SwitchPreference) preference;
-        switchPreference.setVisible(isAvailable());
-        switchPreference.setChecked(
-                Settings.Global.getInt(
-                        mContentResolver, Settings.Global.GAME_DRIVER_ALL_APPS, GAME_DRIVER_DEFAULT)
-                == GAME_DRIVER_ALL_APPS);
+        final ListPreference listPref = (ListPreference) preference;
+        listPref.setVisible(isAvailable());
+        final int currentChoice = Settings.Global.getInt(
+                mContentResolver, Settings.Global.GAME_DRIVER_ALL_APPS, GAME_DRIVER_DEFAULT);
+        if (currentChoice == GAME_DRIVER_ALL_APPS) {
+            listPref.setValue(mPreferenceGameDriver);
+            listPref.setSummary(mPreferenceGameDriver);
+        } else if (currentChoice == GAME_DRIVER_PRERELEASE_ALL_APPS) {
+            listPref.setValue(mPreferencePrereleaseDriver);
+            listPref.setSummary(mPreferencePrereleaseDriver);
+        } else {
+            listPref.setValue(mPreferenceDefault);
+            listPref.setSummary(mPreferenceDefault);
+        }
     }
 
     @Override
     public boolean onPreferenceChange(Preference preference, Object newValue) {
-        final boolean isChecked = (boolean) newValue;
-        final int gameDriver = Settings.Global.getInt(
+        final ListPreference listPref = (ListPreference) preference;
+        final String value = newValue.toString();
+        final int currentChoice = Settings.Global.getInt(
                 mContentResolver, Settings.Global.GAME_DRIVER_ALL_APPS, GAME_DRIVER_DEFAULT);
-
-        if (isChecked && gameDriver == GAME_DRIVER_ALL_APPS) {
-            return true;
+        final int userChoice;
+        if (value.equals(mPreferenceGameDriver)) {
+            userChoice = GAME_DRIVER_ALL_APPS;
+        } else if (value.equals(mPreferencePrereleaseDriver)) {
+            userChoice = GAME_DRIVER_PRERELEASE_ALL_APPS;
+        } else {
+            userChoice = GAME_DRIVER_DEFAULT;
         }
+        listPref.setValue(value);
+        listPref.setSummary(value);
 
-        if (!isChecked && (gameDriver == GAME_DRIVER_DEFAULT || gameDriver == GAME_DRIVER_OFF)) {
-            return true;
+        if (userChoice != currentChoice) {
+            Settings.Global.putInt(
+                    mContentResolver, Settings.Global.GAME_DRIVER_ALL_APPS, userChoice);
         }
 
-        Settings.Global.putInt(mContentResolver, Settings.Global.GAME_DRIVER_ALL_APPS,
-                isChecked ? GAME_DRIVER_ALL_APPS : GAME_DRIVER_DEFAULT);
-
         return true;
     }
 
diff --git a/src/com/android/settings/development/gamedriver/GameDriverGlobalSwitchBarController.java b/src/com/android/settings/development/gamedriver/GameDriverGlobalSwitchBarController.java
index d84c28f..15f71e0 100644
--- a/src/com/android/settings/development/gamedriver/GameDriverGlobalSwitchBarController.java
+++ b/src/com/android/settings/development/gamedriver/GameDriverGlobalSwitchBarController.java
@@ -19,6 +19,7 @@
 import static com.android.settings.development.gamedriver.GameDriverEnableForAllAppsPreferenceController.GAME_DRIVER_ALL_APPS;
 import static com.android.settings.development.gamedriver.GameDriverEnableForAllAppsPreferenceController.GAME_DRIVER_DEFAULT;
 import static com.android.settings.development.gamedriver.GameDriverEnableForAllAppsPreferenceController.GAME_DRIVER_OFF;
+import static com.android.settings.development.gamedriver.GameDriverEnableForAllAppsPreferenceController.GAME_DRIVER_PRERELEASE_ALL_APPS;
 
 import android.content.ContentResolver;
 import android.content.Context;
@@ -83,7 +84,8 @@
                 mContentResolver, Settings.Global.GAME_DRIVER_ALL_APPS, GAME_DRIVER_DEFAULT);
 
         if (isChecked
-                && (gameDriver == GAME_DRIVER_DEFAULT || gameDriver == GAME_DRIVER_ALL_APPS)) {
+                && (gameDriver == GAME_DRIVER_DEFAULT || gameDriver == GAME_DRIVER_ALL_APPS
+                        || gameDriver == GAME_DRIVER_PRERELEASE_ALL_APPS)) {
             return true;
         }
 
diff --git a/src/com/android/settings/network/telephony/DefaultSubscriptionController.java b/src/com/android/settings/network/telephony/DefaultSubscriptionController.java
index 02a2d58..9eb5f8c 100644
--- a/src/com/android/settings/network/telephony/DefaultSubscriptionController.java
+++ b/src/com/android/settings/network/telephony/DefaultSubscriptionController.java
@@ -121,6 +121,11 @@
         }
         mPreference.setVisible(true);
 
+        // TODO(b/135142209) - for now we need to manually ensure we're registered as a change
+        // listener, because this might not have happened during displayPreference if
+        // getAvailabilityStatus returned CONDITIONALLY_UNAVAILABLE at the time.
+        mPreference.setOnPreferenceChangeListener(this);
+
         final List<SubscriptionInfo> subs = SubscriptionUtil.getActiveSubscriptions(mManager);
 
         // We'll have one entry for each available subscription, plus one for a "ask me every
diff --git a/tests/robotests/src/com/android/settings/development/gamedriver/GameDriverEnableForAllAppsPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/development/gamedriver/GameDriverEnableForAllAppsPreferenceControllerTest.java
index 3d418d9..df59971 100644
--- a/tests/robotests/src/com/android/settings/development/gamedriver/GameDriverEnableForAllAppsPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/development/gamedriver/GameDriverEnableForAllAppsPreferenceControllerTest.java
@@ -21,6 +21,7 @@
 import static com.android.settings.development.gamedriver.GameDriverEnableForAllAppsPreferenceController.GAME_DRIVER_ALL_APPS;
 import static com.android.settings.development.gamedriver.GameDriverEnableForAllAppsPreferenceController.GAME_DRIVER_DEFAULT;
 import static com.android.settings.development.gamedriver.GameDriverEnableForAllAppsPreferenceController.GAME_DRIVER_OFF;
+import static com.android.settings.development.gamedriver.GameDriverEnableForAllAppsPreferenceController.GAME_DRIVER_PRERELEASE_ALL_APPS;
 
 import static com.google.common.truth.Truth.assertThat;
 
@@ -30,10 +31,13 @@
 
 import android.content.ContentResolver;
 import android.content.Context;
+import android.content.res.Resources;
 import android.provider.Settings;
 
+import androidx.preference.ListPreference;
 import androidx.preference.PreferenceScreen;
-import androidx.preference.SwitchPreference;
+
+import com.android.settings.R;
 
 import org.junit.Before;
 import org.junit.Test;
@@ -49,13 +53,16 @@
     @Mock
     private PreferenceScreen mScreen;
     @Mock
-    private SwitchPreference mPreference;
+    private ListPreference mPreference;
     @Mock
     private GameDriverContentObserver mGameDriverContentObserver;
 
     private Context mContext;
     private ContentResolver mResolver;
     private GameDriverEnableForAllAppsPreferenceController mController;
+    private String mPreferenceDefault;
+    private String mPreferenceGameDriver;
+    private String mPreferencePrereleaseDriver;
 
     @Before
     public void setUp() {
@@ -63,6 +70,13 @@
         mContext = RuntimeEnvironment.application;
         mResolver = mContext.getContentResolver();
 
+        final Resources resources = mContext.getResources();
+        mPreferenceDefault = resources.getString(R.string.game_driver_app_preference_default);
+        mPreferenceGameDriver =
+                resources.getString(R.string.game_driver_app_preference_game_driver);
+        mPreferencePrereleaseDriver =
+                resources.getString(R.string.game_driver_app_preference_prerelease_driver);
+
         Settings.Global.putInt(mResolver, Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 1);
         Settings.Global.putInt(
                 mResolver, Settings.Global.GAME_DRIVER_ALL_APPS, GAME_DRIVER_DEFAULT);
@@ -95,12 +109,13 @@
     }
 
     @Test
-    public void displayPreference_shouldAddSwitchPreference() {
+    public void displayPreference_shouldAddListPreference() {
         Settings.Global.putInt(
                 mResolver, Settings.Global.GAME_DRIVER_ALL_APPS, GAME_DRIVER_DEFAULT);
         mController.updateState(mPreference);
 
-        verify(mPreference).setChecked(false);
+        verify(mPreference).setValue(mPreferenceDefault);
+        verify(mPreference).setSummary(mPreferenceDefault);
     }
 
     @Test
@@ -120,39 +135,64 @@
     }
 
     @Test
-    public void updateState_availableAndGameDriverDefault_visibleAndUncheck() {
+    public void updateState_availableAndDefault_visibleAndDefault() {
         Settings.Global.putInt(
                 mResolver, Settings.Global.GAME_DRIVER_ALL_APPS, GAME_DRIVER_DEFAULT);
         mController.updateState(mPreference);
 
         verify(mPreference, atLeastOnce()).setVisible(true);
-        verify(mPreference).setChecked(false);
+        verify(mPreference).setValue(mPreferenceDefault);
+        verify(mPreference).setSummary(mPreferenceDefault);
     }
 
     @Test
-    public void updateState_availableAndGameDriverAllApps_visibleAndCheck() {
+    public void updateState_availableAndGameDriver_visibleAndGameDriver() {
         Settings.Global.putInt(
                 mResolver, Settings.Global.GAME_DRIVER_ALL_APPS, GAME_DRIVER_ALL_APPS);
         mController.updateState(mPreference);
 
         verify(mPreference, atLeastOnce()).setVisible(true);
-        verify(mPreference).setChecked(true);
+        verify(mPreference).setValue(mPreferenceGameDriver);
+        verify(mPreference).setSummary(mPreferenceGameDriver);
     }
 
     @Test
-    public void updateState_gameDriverOff_notVisibleAndUncheck() {
+    public void updateState_availableAndPrereleaseDriver_visibleAndPrereleaseDriver() {
+        Settings.Global.putInt(
+                mResolver, Settings.Global.GAME_DRIVER_ALL_APPS, GAME_DRIVER_PRERELEASE_ALL_APPS);
+        mController.updateState(mPreference);
+
+        verify(mPreference, atLeastOnce()).setVisible(true);
+        verify(mPreference).setValue(mPreferencePrereleaseDriver);
+        verify(mPreference).setSummary(mPreferencePrereleaseDriver);
+    }
+
+    @Test
+    public void updateState_gameDriverOff_notVisibleAndSystemDriver() {
         Settings.Global.putInt(mResolver, Settings.Global.GAME_DRIVER_ALL_APPS, GAME_DRIVER_OFF);
         mController.updateState(mPreference);
 
         verify(mPreference).setVisible(false);
-        verify(mPreference).setChecked(false);
+        verify(mPreference).setValue(mPreferenceDefault);
+        verify(mPreference).setSummary(mPreferenceDefault);
     }
 
     @Test
-    public void onPreferenceChange_check_shouldUpdateSettingsGlobal() {
+    public void onPreferenceChange_default_shouldUpdateSettingsGlobal() {
+        Settings.Global.putInt(
+                mResolver, Settings.Global.GAME_DRIVER_ALL_APPS, GAME_DRIVER_ALL_APPS);
+        mController.onPreferenceChange(mPreference, mPreferenceDefault);
+
+        assertThat(Settings.Global.getInt(
+                mResolver, Settings.Global.GAME_DRIVER_ALL_APPS, GAME_DRIVER_DEFAULT))
+                .isEqualTo(GAME_DRIVER_DEFAULT);
+    }
+
+    @Test
+    public void onPreferenceChange_gameDriver_shouldUpdateSettingsGlobal() {
         Settings.Global.putInt(
                 mResolver, Settings.Global.GAME_DRIVER_ALL_APPS, GAME_DRIVER_DEFAULT);
-        mController.onPreferenceChange(mPreference, true);
+        mController.onPreferenceChange(mPreference, mPreferenceGameDriver);
 
         assertThat(Settings.Global.getInt(
                 mResolver, Settings.Global.GAME_DRIVER_ALL_APPS, GAME_DRIVER_DEFAULT))
@@ -160,13 +200,13 @@
     }
 
     @Test
-    public void onPreferenceChange_uncheck_shouldUpdateSettingsGlobal() {
+    public void onPreferenceChange_prereleaseDriver_shouldUpdateSettingsGlobal() {
         Settings.Global.putInt(
-                mResolver, Settings.Global.GAME_DRIVER_ALL_APPS, GAME_DRIVER_ALL_APPS);
-        mController.onPreferenceChange(mPreference, false);
+                mResolver, Settings.Global.GAME_DRIVER_ALL_APPS, GAME_DRIVER_DEFAULT);
+        mController.onPreferenceChange(mPreference, mPreferencePrereleaseDriver);
 
         assertThat(Settings.Global.getInt(
                 mResolver, Settings.Global.GAME_DRIVER_ALL_APPS, GAME_DRIVER_DEFAULT))
-                .isEqualTo(GAME_DRIVER_DEFAULT);
+                .isEqualTo(GAME_DRIVER_PRERELEASE_ALL_APPS);
     }
 }
diff --git a/tests/robotests/src/com/android/settings/network/telephony/DefaultSubscriptionControllerTest.java b/tests/robotests/src/com/android/settings/network/telephony/DefaultSubscriptionControllerTest.java
index 7dd636a..dbdad50 100644
--- a/tests/robotests/src/com/android/settings/network/telephony/DefaultSubscriptionControllerTest.java
+++ b/tests/robotests/src/com/android/settings/network/telephony/DefaultSubscriptionControllerTest.java
@@ -204,6 +204,28 @@
     }
 
     @Test
+    public void onPreferenceChange_prefBecomesAvailable_onPreferenceChangeCallbackNotNull() {
+        final SubscriptionInfo sub1 = createMockSub(111, "sub1");
+        final SubscriptionInfo sub2 = createMockSub(222, "sub2");
+
+        // Start with only one sub active, so the pref is not available
+        SubscriptionUtil.setActiveSubscriptionsForTesting(Arrays.asList(sub1));
+        doReturn(sub1.getSubscriptionId()).when(mController).getDefaultSubscriptionId();
+
+        mController.displayPreference(mScreen);
+        assertThat(mController.isAvailable()).isFalse();
+
+        // Now make two subs be active - the pref should become available, and the
+        // onPreferenceChange callback should be properly wired up.
+        SubscriptionUtil.setActiveSubscriptionsForTesting(Arrays.asList(sub1, sub2));
+        mController.onSubscriptionsChanged();
+        assertThat(mController.isAvailable()).isTrue();
+        assertThat(mListPreference.getOnPreferenceChangeListener()).isEqualTo(mController);
+        mListPreference.callChangeListener("222");
+        verify(mController).setDefaultSubscription(eq(222));
+    }
+
+    @Test
     public void onSubscriptionsChanged_twoSubscriptionsDefaultChanges_selectedEntryGetsUpdated() {
         final SubscriptionInfo sub1 = createMockSub(111, "sub1");
         final SubscriptionInfo sub2 = createMockSub(222, "sub2");