Merge "Use CarUi preference dialogs wherever possible" into pi-car-dev
diff --git a/src/com/android/car/settings/common/SettingsFragment.java b/src/com/android/car/settings/common/SettingsFragment.java
index ca9b40c..82f0f6a 100644
--- a/src/com/android/car/settings/common/SettingsFragment.java
+++ b/src/com/android/car/settings/common/SettingsFragment.java
@@ -36,14 +36,10 @@
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.Lifecycle;
-import androidx.preference.EditTextPreference;
-import androidx.preference.ListPreference;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.car.settings.R;
-import com.android.car.ui.preference.EditTextPreferenceDialogFragment;
-import com.android.car.ui.preference.ListPreferenceDialogFragment;
import com.android.car.ui.preference.PreferenceFragment;
import com.android.car.ui.toolbar.MenuItem;
import com.android.car.ui.toolbar.Toolbar;
@@ -254,27 +250,16 @@
return;
}
- DialogFragment dialogFragment;
if (preference instanceof ValidatedEditTextPreference) {
- if (preference instanceof PasswordEditTextPreference) {
- dialogFragment = PasswordEditTextPreferenceDialogFragment.newInstance(
- preference.getKey());
- } else {
- dialogFragment = ValidatedEditTextPreferenceDialogFragment.newInstance(
- preference.getKey());
- }
- } else if (preference instanceof EditTextPreference) {
- dialogFragment = EditTextPreferenceDialogFragment.newInstance(preference.getKey());
- } else if (preference instanceof ListPreference) {
- dialogFragment = ListPreferenceDialogFragment.newInstance(preference.getKey());
- } else {
- throw new IllegalArgumentException(
- "Tried to display dialog for unknown preference type. Did you forget to "
- + "override onDisplayPreferenceDialog()?");
- }
+ DialogFragment dialogFragment = preference instanceof PasswordEditTextPreference
+ ? PasswordEditTextPreferenceDialogFragment.newInstance(preference.getKey())
+ : ValidatedEditTextPreferenceDialogFragment.newInstance(preference.getKey());
- dialogFragment.setTargetFragment(/* fragment= */ this, /* requestCode= */ 0);
- showDialog(dialogFragment, DIALOG_FRAGMENT_TAG);
+ dialogFragment.setTargetFragment(/* fragment= */ this, /* requestCode= */ 0);
+ showDialog(dialogFragment, DIALOG_FRAGMENT_TAG);
+ } else {
+ super.onDisplayPreferenceDialog(preference);
+ }
}
@Override
diff --git a/tests/robotests/src/com/android/car/settings/common/SettingsFragmentTest.java b/tests/robotests/src/com/android/car/settings/common/SettingsFragmentTest.java
index 7710c95..64477ba 100644
--- a/tests/robotests/src/com/android/car/settings/common/SettingsFragmentTest.java
+++ b/tests/robotests/src/com/android/car/settings/common/SettingsFragmentTest.java
@@ -38,8 +38,6 @@
import com.android.car.settings.R;
import com.android.car.settings.testutils.DummyFragment;
import com.android.car.settings.testutils.FragmentController;
-import com.android.car.ui.preference.EditTextPreferenceDialogFragment;
-import com.android.car.ui.preference.ListPreferenceDialogFragment;
import com.android.car.ui.toolbar.Toolbar;
import org.junit.Before;
@@ -93,30 +91,6 @@
}
@Test
- public void onDisplayPreferenceDialog_editTextPreference_showsDialog() {
- mFragmentController.setup();
-
- mFragment.getPreferenceScreen().findPreference(
- mContext.getString(R.string.tpk_edit_text_preference)).performClick();
-
- assertThat(mFragment.getFragmentManager().findFragmentByTag(
- SettingsFragment.DIALOG_FRAGMENT_TAG)).isInstanceOf(
- EditTextPreferenceDialogFragment.class);
- }
-
- @Test
- public void onDisplayPreferenceDialog_listPreference_showsDialog() {
- mFragmentController.setup();
-
- mFragment.getPreferenceScreen().findPreference(
- mContext.getString(R.string.tpk_list_preference)).performClick();
-
- assertThat(mFragment.getFragmentManager().findFragmentByTag(
- SettingsFragment.DIALOG_FRAGMENT_TAG)).isInstanceOf(
- ListPreferenceDialogFragment.class);
- }
-
- @Test
public void onDisplayPreferenceDialog_unknownPreferenceType_throwsIllegalArgumentException() {
mFragmentController.setup();
@@ -125,27 +99,6 @@
}
@Test
- public void onDisplayPreferenceDialog_alreadyShowing_doesNothing() {
- mFragmentController.setup();
-
- // Show a dialog.
- mFragment.getPreferenceScreen().findPreference(
- mContext.getString(R.string.tpk_edit_text_preference)).performClick();
- assertThat(mFragment.getFragmentManager().findFragmentByTag(
- SettingsFragment.DIALOG_FRAGMENT_TAG)).isInstanceOf(
- EditTextPreferenceDialogFragment.class);
-
- // Attempt to show another.
- mFragment.getPreferenceScreen().findPreference(
- mContext.getString(R.string.tpk_list_preference)).performClick();
-
- // Assert only one shown at a time.
- assertThat(mFragment.getFragmentManager().findFragmentByTag(
- SettingsFragment.DIALOG_FRAGMENT_TAG)).isInstanceOf(
- EditTextPreferenceDialogFragment.class);
- }
-
- @Test
public void launchFragment_otherFragment_opensFragment() {
mFragmentController.setup();
TestSettingsFragment otherFragment = new TestSettingsFragment();