diff options
11 files changed, 13 insertions, 928 deletions
@@ -45,6 +45,7 @@ LOCAL_STATIC_ANDROID_LIBRARIES += \ car-arch-common \ car-telephony-common \ car-theme-lib \ + car-ui-lib # Including the resources for the static android libraries allows to pick up their static overlays. LOCAL_RESOURCE_DIR += \ @@ -122,6 +123,7 @@ LOCAL_STATIC_ANDROID_LIBRARIES += \ car-arch-common \ car-telephony-common \ car-theme-lib \ + car-ui-lib LOCAL_STATIC_JAVA_LIBRARIES := \ androidx-constraintlayout_constraintlayout-solver \ diff --git a/res/layout/dialer_settings_activity.xml b/res/layout/dialer_settings_activity.xml deleted file mode 100644 index c67eefb7..00000000 --- a/res/layout/dialer_settings_activity.xml +++ /dev/null @@ -1,39 +0,0 @@ -<?xml version="1.0" encoding="utf-8"?> -<!-- - Copyright 2019 The Android Open Source Project - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. ---> - -<androidx.constraintlayout.widget.ConstraintLayout - xmlns:android="http://schemas.android.com/apk/res/android" - xmlns:app="http://schemas.android.com/apk/res-auto" - android:layout_width="match_parent" - android:layout_height="match_parent"> - - <com.android.car.apps.common.ClickThroughToolbar - android:id="@+id/settings_toolbar" - android:layout_width="match_parent" - android:layout_height="?android:attr/actionBarSize" - app:layout_constraintTop_toTopOf="parent"/> - - <FrameLayout - android:id="@+id/settings_fragment_container" - android:layout_width="match_parent" - android:layout_height="0dp" - android:layout_marginStart="@dimen/preference_list_margin" - android:layout_marginEnd="@dimen/preference_list_margin" - app:layout_constraintTop_toBottomOf="@id/settings_toolbar" - app:layout_constraintBottom_toBottomOf="parent"> - </FrameLayout> -</androidx.constraintlayout.widget.ConstraintLayout> diff --git a/res/values/dimens.xml b/res/values/dimens.xml index 83da788f..da55f710 100644 --- a/res/values/dimens.xml +++ b/res/values/dimens.xml @@ -144,7 +144,6 @@ <dimen name="large_avatar_icon_size">96dp</dimen> <dimen name="primary_icon_enclosing_circle_size">64dp</dimen> <dimen name="inline_icon_size">24dp</dimen> - <dimen name="preference_list_margin">@*android:dimen/car_margin</dimen> <dimen name="contact_avatar_corner_radius_percent" format="float">0.5</dimen> <dimen name="touch_target_width">156dp</dimen> <dimen name="subheader_list_height">76dp</dimen> diff --git a/res/values/styles_preference.xml b/res/values/styles_preference.xml deleted file mode 100644 index d9d0ebac..00000000 --- a/res/values/styles_preference.xml +++ /dev/null @@ -1,28 +0,0 @@ -<?xml version="1.0" encoding="utf-8"?> -<!-- Copyright (C) 2017 The Android Open Source Project - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. ---> - -<resources> - <style name="PreferenceFragmentList.Settings"> - <item name="android:paddingTop">0dp</item> - <item name="android:paddingBottom">0dp</item> - <item name="android:paddingStart">0dp</item> - <item name="android:paddingEnd">0dp</item> - <item name="android:paddingLeft">0dp</item> - <item name="android:paddingRight">0dp</item> - </style> - -</resources> - diff --git a/res/values/themes.xml b/res/values/themes.xml index e3b054c1..4747b6d1 100644 --- a/res/values/themes.xml +++ b/res/values/themes.xml @@ -15,7 +15,7 @@ limitations under the License. --> <resources> <!-- The base theme for the Dialer app. --> - <style name="Theme.Dialer" parent="android:Theme.DeviceDefault.NoActionBar"> + <style name="Theme.Dialer" parent="Theme.CarUi"> <item name="android:actionBarSize">@dimen/action_bar_height</item> <item name="tabBarSize">@dimen/tab_bar_height</item> <item name="android:actionBarItemBackground">@drawable/action_button_background</item> @@ -29,18 +29,12 @@ limitations under the License. <item name="android:searchViewStyle">@style/Widget.Dialer.SearchView</item> </style> - <style name="Theme.Dialer.Telecom" parent="Theme.Dialer"> + <style name="Theme.Dialer.Telecom"> <item name="toolbarTitleMode">@integer/config_toolbar_title_mode</item> </style> - <style name="Theme.Dialer.InCall" parent="Theme.Dialer"/> + <style name="Theme.Dialer.InCall"/> - <style name="Theme.Dialer.Setting" parent="Theme.Dialer"> - <item name="preferenceTheme">@style/PreferenceTheme</item> - </style> - - <style name="PreferenceTheme"> - <item name="preferenceFragmentListStyle">@style/PreferenceFragmentList.Settings</item> - </style> + <style name="Theme.Dialer.Setting"/> </resources> diff --git a/src/com/android/car/dialer/ui/settings/DialerSettingsActivity.java b/src/com/android/car/dialer/ui/settings/DialerSettingsActivity.java index c90f9a97..eb9f24f0 100644 --- a/src/com/android/car/dialer/ui/settings/DialerSettingsActivity.java +++ b/src/com/android/car/dialer/ui/settings/DialerSettingsActivity.java @@ -17,11 +17,9 @@ package com.android.car.dialer.ui.settings; import android.os.Bundle; -import android.widget.Toolbar; import androidx.fragment.app.FragmentActivity; -import com.android.car.dialer.R; import com.android.car.dialer.log.L; /** @@ -30,21 +28,15 @@ import com.android.car.dialer.log.L; public class DialerSettingsActivity extends FragmentActivity { private static final String TAG = "CD.SettingsActivity"; - private Toolbar mToolbar; - @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); L.d(TAG, "onCreate"); - setContentView(R.layout.dialer_settings_activity); - mToolbar = findViewById(R.id.settings_toolbar); - setActionBar(mToolbar); - - getSupportFragmentManager().beginTransaction() - .replace(R.id.settings_fragment_container, new DialerSettingsFragment()) - .commit(); - - getActionBar().setDisplayHomeAsUpEnabled(true); + if (savedInstanceState == null) { + getSupportFragmentManager().beginTransaction() + .replace(android.R.id.content, new DialerSettingsFragment()) + .commit(); + } } } diff --git a/src/com/android/car/dialer/ui/settings/DialerSettingsFragment.java b/src/com/android/car/dialer/ui/settings/DialerSettingsFragment.java index 8615629c..18c12191 100644 --- a/src/com/android/car/dialer/ui/settings/DialerSettingsFragment.java +++ b/src/com/android/car/dialer/ui/settings/DialerSettingsFragment.java @@ -18,63 +18,15 @@ package com.android.car.dialer.ui.settings; import android.os.Bundle; -import androidx.annotation.Nullable; -import androidx.fragment.app.DialogFragment; -import androidx.fragment.app.Fragment; -import androidx.preference.ListPreference; -import androidx.preference.Preference; -import androidx.preference.PreferenceFragmentCompat; - import com.android.car.dialer.R; -import com.android.car.dialer.ui.settings.common.SettingsListPreferenceDialogFragment; +import com.android.car.ui.preference.PreferenceFragment; /** * A fragment that displays the settings page */ -public class DialerSettingsFragment extends PreferenceFragmentCompat { - private static final String TAG = "CD.SettingsFragment"; - private static final String DIALOG_FRAGMENT_TAG = "DIALOG"; - +public class DialerSettingsFragment extends PreferenceFragment { @Override public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { setPreferencesFromResource(R.xml.settings_page, rootKey); } - - /** - * Settings needs to launch custom dialog types in order to extend the Device Default theme. - * - * @param preference The Preference object requesting the dialog. - */ - @Override - public void onDisplayPreferenceDialog(Preference preference) { - // check if dialog is already showing - if (findDialogByTag(DIALOG_FRAGMENT_TAG) != null) { - return; - } - - DialogFragment dialogFragment; - if (preference instanceof ListPreference) { - dialogFragment = SettingsListPreferenceDialogFragment.newInstance(preference.getKey()); - } else { - throw new IllegalArgumentException( - "Tried to display dialog for unknown preference type. Did you forget to " - + "override onDisplayPreferenceDialog()?"); - } - - dialogFragment.setTargetFragment(/* fragment= */ this, /* requestCode= */ 0); - showDialog(dialogFragment, DIALOG_FRAGMENT_TAG); - } - - @Nullable - private DialogFragment findDialogByTag(String tag) { - Fragment fragment = getFragmentManager().findFragmentByTag(tag); - if (fragment instanceof DialogFragment) { - return (DialogFragment) fragment; - } - return null; - } - - private void showDialog(DialogFragment dialogFragment, @Nullable String tag) { - dialogFragment.show(getFragmentManager(), tag); - } } diff --git a/src/com/android/car/dialer/ui/settings/common/SettingsListPreferenceDialogFragment.java b/src/com/android/car/dialer/ui/settings/common/SettingsListPreferenceDialogFragment.java deleted file mode 100644 index f544704e..00000000 --- a/src/com/android/car/dialer/ui/settings/common/SettingsListPreferenceDialogFragment.java +++ /dev/null @@ -1,121 +0,0 @@ -/* - * Copyright 2019 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.car.dialer.ui.settings.common; - -import android.app.AlertDialog; -import android.content.DialogInterface; -import android.os.Bundle; - -import androidx.annotation.NonNull; -import androidx.preference.ListPreference; - -/** - * Presents a dialog with a list of options associated with a {@link ListPreference}. - * - * <p>Note: this is borrowed as-is from androidx.preference.EditTextPreferenceDialogFragmentCompat - * with updates to formatting to match the project style. DialerSettings needs to use custom dialog - * implementations in order to launch the platform {@link AlertDialog} instead of the one in the - * support library. - */ -public class SettingsListPreferenceDialogFragment extends SettingsPreferenceDialogFragment { - - private static final String SAVE_STATE_INDEX = "SettingsListPreferenceDialogFragment.index"; - private static final String SAVE_STATE_ENTRIES = "SettingsListPreferenceDialogFragment.entries"; - private static final String SAVE_STATE_ENTRY_VALUES = - "SettingsListPreferenceDialogFragment.entryValues"; - - private int mClickedDialogEntryIndex; - private CharSequence[] mEntries; - private CharSequence[] mEntryValues; - - /** - * Returns a new instance of {@link SettingsListPreferenceDialogFragment} for the {@link - * ListPreference} with the given {@code key}. - */ - public static SettingsListPreferenceDialogFragment newInstance(String key) { - SettingsListPreferenceDialogFragment fragment = new SettingsListPreferenceDialogFragment(); - Bundle b = new Bundle(/* capacity= */ 1); - b.putString(ARG_KEY, key); - fragment.setArguments(b); - return fragment; - } - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - if (savedInstanceState == null) { - ListPreference preference = getListPreference(); - - if (preference.getEntries() == null || preference.getEntryValues() == null) { - throw new IllegalStateException( - "ListPreference requires an entries array and an entryValues array."); - } - - mClickedDialogEntryIndex = preference.findIndexOfValue(preference.getValue()); - mEntries = preference.getEntries(); - mEntryValues = preference.getEntryValues(); - } else { - mClickedDialogEntryIndex = savedInstanceState.getInt(SAVE_STATE_INDEX, 0); - mEntries = savedInstanceState.getCharSequenceArray(SAVE_STATE_ENTRIES); - mEntryValues = savedInstanceState.getCharSequenceArray(SAVE_STATE_ENTRY_VALUES); - } - } - - @Override - public void onSaveInstanceState(@NonNull Bundle outState) { - super.onSaveInstanceState(outState); - outState.putInt(SAVE_STATE_INDEX, mClickedDialogEntryIndex); - outState.putCharSequenceArray(SAVE_STATE_ENTRIES, mEntries); - outState.putCharSequenceArray(SAVE_STATE_ENTRY_VALUES, mEntryValues); - } - - private ListPreference getListPreference() { - return (ListPreference) getPreference(); - } - - @Override - protected void onPrepareDialogBuilder(AlertDialog.Builder builder) { - super.onPrepareDialogBuilder(builder); - - builder.setSingleChoiceItems(mEntries, mClickedDialogEntryIndex, - (dialog, which) -> { - mClickedDialogEntryIndex = which; - - // Clicking on an item simulates the positive button click, and dismisses the - // dialog. - SettingsListPreferenceDialogFragment.this.onClick(dialog, - DialogInterface.BUTTON_POSITIVE); - dialog.dismiss(); - }); - - // The typical interaction for list-based dialogs is to have click-on-an-item dismiss the - // dialog instead of the user having to press 'Ok'. - builder.setPositiveButton(null, null); - } - - @Override - public void onDialogClosed(boolean positiveResult) { - if (positiveResult && mClickedDialogEntryIndex >= 0) { - String value = mEntryValues[mClickedDialogEntryIndex].toString(); - ListPreference preference = getListPreference(); - if (preference.callChangeListener(value)) { - preference.setValue(value); - } - } - } - -} diff --git a/src/com/android/car/dialer/ui/settings/common/SettingsPreferenceDialogFragment.java b/src/com/android/car/dialer/ui/settings/common/SettingsPreferenceDialogFragment.java deleted file mode 100644 index c885f6a2..00000000 --- a/src/com/android/car/dialer/ui/settings/common/SettingsPreferenceDialogFragment.java +++ /dev/null @@ -1,286 +0,0 @@ -/* - * Copyright 2019 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.car.dialer.ui.settings.common; - -import android.app.AlertDialog; -import android.app.Dialog; -import android.content.Context; -import android.content.DialogInterface; -import android.graphics.Bitmap; -import android.graphics.Canvas; -import android.graphics.drawable.BitmapDrawable; -import android.graphics.drawable.Drawable; -import android.os.Bundle; -import android.text.TextUtils; -import android.view.LayoutInflater; -import android.view.View; -import android.view.Window; -import android.view.WindowManager; -import android.widget.TextView; - -import androidx.annotation.CallSuper; -import androidx.annotation.LayoutRes; -import androidx.annotation.NonNull; -import androidx.fragment.app.DialogFragment; -import androidx.fragment.app.Fragment; -import androidx.preference.DialogPreference; -import androidx.preference.PreferenceFragmentCompat; - -/** - * Abstract base class which presents a dialog associated with a {@link DialogPreference}. - * Since the preference object may not be available during fragment re-creation, the - * necessary information for displaying the dialog is read once during the initial call - * to {@link #onCreate(Bundle)} and saved/restored in the saved instance state. - * Custom subclasses should also follow this pattern. - * - * <p>Note: this is borrowed as-is from androidx.preference.PreferenceDialogFragmentCompat with - * updates to formatting to match the project style. Settings needs to use custom dialog - * implementations in order to launch the platform {@link AlertDialog} instead of the one in the - * support library. - */ -public abstract class SettingsPreferenceDialogFragment extends DialogFragment implements - DialogInterface.OnClickListener { - - protected static final String ARG_KEY = "key"; - - private static final String SAVE_STATE_TITLE = "SettingsPreferenceDialogFragment.title"; - private static final String SAVE_STATE_POSITIVE_TEXT = - "SettingsPreferenceDialogFragment.positiveText"; - private static final String SAVE_STATE_NEGATIVE_TEXT = - "SettingsPreferenceDialogFragment.negativeText"; - private static final String SAVE_STATE_MESSAGE = "SettingsPreferenceDialogFragment.message"; - private static final String SAVE_STATE_LAYOUT = "SettingsPreferenceDialogFragment.layout"; - private static final String SAVE_STATE_ICON = "SettingsPreferenceDialogFragment.icon"; - - private DialogPreference mPreference; - - private CharSequence mDialogTitle; - private CharSequence mPositiveButtonText; - private CharSequence mNegativeButtonText; - private CharSequence mDialogMessage; - @LayoutRes - private int mDialogLayoutRes; - - private BitmapDrawable mDialogIcon; - - /** Which button was clicked. */ - private int mWhichButtonClicked; - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - Fragment rawFragment = getTargetFragment(); - if (!(rawFragment instanceof DialogPreference.TargetFragment)) { - throw new IllegalStateException( - "Target fragment must implement TargetFragment interface"); - } - - DialogPreference.TargetFragment fragment = - (DialogPreference.TargetFragment) rawFragment; - - String key = getArguments().getString(ARG_KEY); - if (savedInstanceState == null) { - mPreference = (DialogPreference) fragment.findPreference(key); - mDialogTitle = mPreference.getDialogTitle(); - mPositiveButtonText = mPreference.getPositiveButtonText(); - mNegativeButtonText = mPreference.getNegativeButtonText(); - mDialogMessage = mPreference.getDialogMessage(); - mDialogLayoutRes = mPreference.getDialogLayoutResource(); - - Drawable icon = mPreference.getDialogIcon(); - if (icon == null || icon instanceof BitmapDrawable) { - mDialogIcon = (BitmapDrawable) icon; - } else { - Bitmap bitmap = Bitmap.createBitmap(icon.getIntrinsicWidth(), - icon.getIntrinsicHeight(), Bitmap.Config.ARGB_8888); - Canvas canvas = new Canvas(bitmap); - icon.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); - icon.draw(canvas); - mDialogIcon = new BitmapDrawable(getResources(), bitmap); - } - } else { - mDialogTitle = savedInstanceState.getCharSequence(SAVE_STATE_TITLE); - mPositiveButtonText = savedInstanceState.getCharSequence(SAVE_STATE_POSITIVE_TEXT); - mNegativeButtonText = savedInstanceState.getCharSequence(SAVE_STATE_NEGATIVE_TEXT); - mDialogMessage = savedInstanceState.getCharSequence(SAVE_STATE_MESSAGE); - mDialogLayoutRes = savedInstanceState.getInt(SAVE_STATE_LAYOUT, 0); - Bitmap bitmap = savedInstanceState.getParcelable(SAVE_STATE_ICON); - if (bitmap != null) { - mDialogIcon = new BitmapDrawable(getResources(), bitmap); - } - } - } - - @Override - public void onSaveInstanceState(@NonNull Bundle outState) { - super.onSaveInstanceState(outState); - - outState.putCharSequence(SAVE_STATE_TITLE, mDialogTitle); - outState.putCharSequence(SAVE_STATE_POSITIVE_TEXT, mPositiveButtonText); - outState.putCharSequence(SAVE_STATE_NEGATIVE_TEXT, mNegativeButtonText); - outState.putCharSequence(SAVE_STATE_MESSAGE, mDialogMessage); - outState.putInt(SAVE_STATE_LAYOUT, mDialogLayoutRes); - if (mDialogIcon != null) { - outState.putParcelable(SAVE_STATE_ICON, mDialogIcon.getBitmap()); - } - } - - @Override - @NonNull - public Dialog onCreateDialog(Bundle savedInstanceState) { - Context context = getActivity(); - mWhichButtonClicked = DialogInterface.BUTTON_NEGATIVE; - - AlertDialog.Builder builder = new AlertDialog.Builder(context) - .setTitle(mDialogTitle) - .setIcon(mDialogIcon) - .setPositiveButton(mPositiveButtonText, this) - .setNegativeButton(mNegativeButtonText, this); - - View contentView = onCreateDialogView(context); - if (contentView != null) { - onBindDialogView(contentView); - builder.setView(contentView); - } else { - builder.setMessage(mDialogMessage); - } - - onPrepareDialogBuilder(builder); - - // Create the dialog - Dialog dialog = builder.create(); - if (needInputMethod()) { - // Request input only after the dialog is shown. This is to prevent an issue where the - // dialog view collapsed the content on small displays. - dialog.setOnShowListener(d -> requestInputMethod(dialog)); - } - - return dialog; - } - - /** - * Get the preference that requested this dialog. Available after {@link #onCreate(Bundle)} has - * been called on the {@link PreferenceFragmentCompat} which launched this dialog. - * - * @return the {@link DialogPreference} associated with this dialog. - */ - public DialogPreference getPreference() { - if (mPreference == null) { - String key = getArguments().getString(ARG_KEY); - DialogPreference.TargetFragment fragment = - (DialogPreference.TargetFragment) getTargetFragment(); - mPreference = (DialogPreference) fragment.findPreference(key); - } - return mPreference; - } - - /** - * Prepares the dialog builder to be shown when the preference is clicked. Use this to set - * custom properties on the dialog. - * - * <p>Do not {@link AlertDialog.Builder#create()} or {@link AlertDialog.Builder#show()}. - */ - protected void onPrepareDialogBuilder(AlertDialog.Builder builder) { - } - - /** - * Returns whether the preference needs to display a soft input method when the dialog is - * displayed. Default is false. Subclasses should override this method if they need the soft - * input method brought up automatically. - * - * <p>Note: Ensure your subclass manually requests focus (ideally in {@link - * #onBindDialogView(View)}) for the input field in order to - * correctly attach the input method to the field. - */ - protected boolean needInputMethod() { - return false; - } - - /** - * Sets the required flags on the dialog window to enable input method window to show up. - */ - private void requestInputMethod(Dialog dialog) { - Window window = dialog.getWindow(); - window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); - } - - /** - * Creates the content view for the dialog (if a custom content view is required). By default, - * it inflates the dialog layout resource if it is set. - * - * @return the content View for the dialog. - * @see DialogPreference#setLayoutResource(int) - */ - protected View onCreateDialogView(Context context) { - int resId = mDialogLayoutRes; - if (resId == 0) { - return null; - } - - LayoutInflater inflater = LayoutInflater.from(context); - return inflater.inflate(resId, null); - } - - /** - * Binds views in the content View of the dialog to data. - * - * <p>Make sure to call through to the superclass implementation. - * - * @param view the content View of the dialog, if it is custom. - */ - @CallSuper - protected void onBindDialogView(View view) { - View dialogMessageView = view.findViewById(android.R.id.message); - - if (dialogMessageView != null) { - CharSequence message = mDialogMessage; - int newVisibility = View.GONE; - - if (!TextUtils.isEmpty(message)) { - if (dialogMessageView instanceof TextView) { - ((TextView) dialogMessageView).setText(message); - } - - newVisibility = View.VISIBLE; - } - - if (dialogMessageView.getVisibility() != newVisibility) { - dialogMessageView.setVisibility(newVisibility); - } - } - } - - @Override - public void onClick(DialogInterface dialog, int which) { - mWhichButtonClicked = which; - } - - @Override - public void onDismiss(DialogInterface dialog) { - super.onDismiss(dialog); - onDialogClosed(mWhichButtonClicked == DialogInterface.BUTTON_POSITIVE); - } - - /** - * Called when the dialog is dismissed. - * - * @param positiveResult {@code true} if the dialog was dismissed with {@link - * DialogInterface#BUTTON_POSITIVE}. - */ - protected abstract void onDialogClosed(boolean positiveResult); -} diff --git a/tests/robotests/src/com/android/car/dialer/ui/settings/common/SettingsListPreferenceDialogFragmentTest.java b/tests/robotests/src/com/android/car/dialer/ui/settings/common/SettingsListPreferenceDialogFragmentTest.java deleted file mode 100644 index 5b49713a..00000000 --- a/tests/robotests/src/com/android/car/dialer/ui/settings/common/SettingsListPreferenceDialogFragmentTest.java +++ /dev/null @@ -1,129 +0,0 @@ -/* - * Copyright 2019 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.car.dialer.ui.settings.common; - -import static com.google.common.truth.Truth.assertThat; - -import android.app.AlertDialog; -import android.content.Context; -import android.content.DialogInterface; -import android.os.Bundle; - -import androidx.preference.ListPreference; - -import com.android.car.dialer.CarDialerRobolectricTestRunner; -import com.android.car.dialer.FragmentTestActivity; -import com.android.car.dialer.R; - -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.robolectric.RuntimeEnvironment; -import org.robolectric.android.controller.ActivityController; -import org.robolectric.shadows.ShadowAlertDialog; -import org.robolectric.shadows.ShadowApplication; - -/** Unit test for {@link SettingsListPreferenceDialogFragment}. */ -@RunWith(CarDialerRobolectricTestRunner.class) -public class SettingsListPreferenceDialogFragmentTest { - - private ActivityController<FragmentTestActivity> mTestActivityController; - private FragmentTestActivity mTestActivity; - private ListPreference mPreference; - private SettingsListPreferenceDialogFragment mFragment; - - @Before - public void setUp() { - Context context = RuntimeEnvironment.application; - - mTestActivityController = ActivityController.of(new FragmentTestActivity()); - mTestActivity = mTestActivityController.get(); - mTestActivityController.setup(); - - SettingsPreferenceDialogFragmentTest.TestTargetFragment targetFragment = - new SettingsPreferenceDialogFragmentTest.TestTargetFragment(); - mTestActivity.setFragment(targetFragment); - mPreference = new ListPreference(context); - mPreference.setDialogLayoutResource(R.layout.preference_dialog_edittext); - mPreference.setKey("key"); - mPreference.setEntries(R.array.entries); - mPreference.setEntryValues(R.array.entry_values); - targetFragment.getPreferenceScreen().addPreference(mPreference); - - mFragment = SettingsListPreferenceDialogFragment.newInstance(mPreference.getKey()); - mFragment.setTargetFragment(targetFragment, /* requestCode= */ 0); - } - - @Test - public void dialogPopulatedWithPreferenceEntries() { - mTestActivity.showDialog(mFragment, /* tag= */ null); - - assertThat(getShadowAlertDialog().getItems()).isEqualTo(mPreference.getEntries()); - } - - @Test - public void itemSelected_dismissesDialog() { - mTestActivity.showDialog(mFragment, /* tag= */ null); - - getShadowAlertDialog().clickOnItem(1); - - assertThat(getShadowAlertDialog().hasBeenDismissed()).isTrue(); - } - - @Test - public void itemSelected_setsPreferenceValue() { - mPreference.setValueIndex(0); - mTestActivity.showDialog(mFragment, /* tag= */ null); - - getShadowAlertDialog().clickOnItem(1); - - assertThat(mPreference.getValue()).isEqualTo(mPreference.getEntryValues()[1]); - } - - @Test - public void onDialogClosed_negativeResult_doesNothing() { - mPreference.setValueIndex(0); - mTestActivity.showDialog(mFragment, /* tag= */ null); - AlertDialog dialog = ShadowAlertDialog.getLatestAlertDialog(); - - dialog.getButton(DialogInterface.BUTTON_NEGATIVE).performClick(); - - assertThat(mPreference.getValue()).isEqualTo(mPreference.getEntryValues()[0]); - } - - @Test - public void instanceStateRetained() { - mPreference.setValueIndex(0); - mTestActivity.showDialog(mFragment, /* tag= */ null); - - // Save instance state. - Bundle outState = new Bundle(); - mTestActivityController.pause().saveInstanceState(outState).stop(); - - // Recreate everything with saved state. - mTestActivityController = ActivityController.of(new FragmentTestActivity()); - mTestActivity = mTestActivityController.get(); - mTestActivityController.setup(outState); - - // Ensure saved entries were applied. - assertThat(getShadowAlertDialog().getItems()).isEqualTo(mPreference.getEntries()); - } - - private ShadowAlertDialog getShadowAlertDialog() { - return ShadowApplication.getInstance().getLatestAlertDialog(); - } -} diff --git a/tests/robotests/src/com/android/car/dialer/ui/settings/common/SettingsPreferenceDialogFragmentTest.java b/tests/robotests/src/com/android/car/dialer/ui/settings/common/SettingsPreferenceDialogFragmentTest.java deleted file mode 100644 index 6e49bc96..00000000 --- a/tests/robotests/src/com/android/car/dialer/ui/settings/common/SettingsPreferenceDialogFragmentTest.java +++ /dev/null @@ -1,251 +0,0 @@ -/* - * Copyright 2019 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.car.dialer.ui.settings.common; - -import static com.google.common.truth.Truth.assertThat; - -import android.app.AlertDialog; -import android.content.Context; -import android.content.DialogInterface; -import android.os.Bundle; -import android.view.View; -import android.view.WindowManager; - -import androidx.preference.DialogPreference; -import androidx.preference.PreferenceFragmentCompat; - -import com.android.car.dialer.CarDialerRobolectricTestRunner; -import com.android.car.dialer.FragmentTestActivity; - -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.robolectric.RuntimeEnvironment; -import org.robolectric.android.controller.ActivityController; -import org.robolectric.shadow.api.Shadow; -import org.robolectric.shadows.ShadowAlertDialog; -import org.robolectric.shadows.ShadowApplication; -import org.robolectric.shadows.ShadowWindow; - -/** Unit test for {@link SettingsPreferenceDialogFragment}. */ -@RunWith(CarDialerRobolectricTestRunner.class) -public class SettingsPreferenceDialogFragmentTest { - - private ActivityController<FragmentTestActivity> mTestActivityController; - private FragmentTestActivity mTestActivity; - private DialogPreference mPreference; - private TestSettingsPreferenceDialogFragment mFragment; - - @Before - public void setUp() { - Context context = RuntimeEnvironment.application; - - mTestActivityController = ActivityController.of(new FragmentTestActivity()); - mTestActivity = mTestActivityController.get(); - mTestActivityController.setup(); - - TestTargetFragment targetFragment = new TestTargetFragment(); - mTestActivity.setFragment(targetFragment); - mPreference = new TestDialogPreference(context); - mPreference.setKey("key"); - targetFragment.getPreferenceScreen().addPreference(mPreference); - - mFragment = TestSettingsPreferenceDialogFragment.newInstance(mPreference.getKey()); - mFragment.setTargetFragment(targetFragment, /* requestCode= */ 0); - } - - @Test - public void dialogFieldsPopulatedWithPreferenceFields() { - mPreference.setDialogTitle("title"); - mPreference.setPositiveButtonText("positive button text"); - mPreference.setNegativeButtonText("negative button text"); - mPreference.setDialogMessage("dialog message"); - - mTestActivity.showDialog(mFragment, /* tag= */ null); - - assertThat(getShadowAlertDialog().getTitle()).isEqualTo(mPreference.getDialogTitle()); - assertThat(ShadowAlertDialog.getLatestAlertDialog().getButton( - DialogInterface.BUTTON_POSITIVE).getText()).isEqualTo( - mPreference.getPositiveButtonText()); - assertThat(ShadowAlertDialog.getLatestAlertDialog().getButton( - DialogInterface.BUTTON_NEGATIVE).getText()).isEqualTo( - mPreference.getNegativeButtonText()); - assertThat(getShadowAlertDialog().getMessage()).isEqualTo(mPreference.getDialogMessage()); - } - - @Test - public void dialogMessage_messageViewShown() { - mPreference.setDialogTitle("title"); - mPreference.setPositiveButtonText("positive button text"); - mPreference.setNegativeButtonText("negative button text"); - mPreference.setDialogMessage("dialog message"); - - mTestActivity.showDialog(mFragment, /* tag= */ null); - View messageView = ShadowAlertDialog.getLatestAlertDialog().findViewById( - android.R.id.message); - - assertThat(messageView.getVisibility()).isEqualTo(View.VISIBLE); - } - - @Test - public void noDialogMessage_messageViewHidden() { - mPreference.setDialogTitle("title"); - mPreference.setPositiveButtonText("positive button text"); - mPreference.setNegativeButtonText("negative button text"); - - mTestActivity.showDialog(mFragment, /* tag= */ null); - View messageView = ShadowAlertDialog.getLatestAlertDialog().findViewById( - android.R.id.message); - - assertThat(messageView.getVisibility()).isEqualTo(View.GONE); - } - - @Test - public void getPreference_returnsDialogRequestingPreference() { - mTestActivity.showDialog(mFragment, /* tag= */ null); - - assertThat(mFragment.getPreference()).isEqualTo(mPreference); - } - - @Test - public void dialogClosed_positiveButton_callsOnDialogClosed() { - mTestActivity.showDialog(mFragment, /* tag= */ null); - AlertDialog dialog = ShadowAlertDialog.getLatestAlertDialog(); - - dialog.getButton(DialogInterface.BUTTON_POSITIVE).performClick(); - - assertThat(mFragment.getDialogClosedResult()).isEqualTo(Boolean.TRUE); - } - - @Test - public void dialogClosed_negativeButton_callsOnDialogClosed() { - mTestActivity.showDialog(mFragment, /* tag= */ null); - AlertDialog dialog = ShadowAlertDialog.getLatestAlertDialog(); - - dialog.getButton(DialogInterface.BUTTON_NEGATIVE).performClick(); - - assertThat(mFragment.getDialogClosedResult()).isEqualTo(Boolean.FALSE); - } - - @Test - public void subclassNeedsInputMethod_softInputModeSetOnWindow() { - mFragment.setNeedsInputMethod(true); - mTestActivity.showDialog(mFragment, /* tag= */ null); - - assertThat(getShadowWindowFromDialog( - ShadowAlertDialog.getLatestAlertDialog()).getSoftInputMode()).isEqualTo( - WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); - } - - @Test - public void subclassDoesNotNeedInputMethod_noWindowSoftInputMode() { - mFragment.setNeedsInputMethod(false); - mTestActivity.showDialog(mFragment, /* tag= */ null); - - assertThat(getShadowWindowFromDialog( - ShadowAlertDialog.getLatestAlertDialog()).getSoftInputMode()).isEqualTo(0); - } - - @Test - public void instanceStateRetained() { - String dialogTitle = "dialog title"; - String positiveButtonText = "positive button text"; - String negativeButtonText = "negative button text"; - String dialogMessage = "dialog message"; - mPreference.setDialogTitle(dialogTitle); - mPreference.setPositiveButtonText(positiveButtonText); - mPreference.setNegativeButtonText(negativeButtonText); - mPreference.setDialogMessage(dialogMessage); - - mTestActivity.showDialog(mFragment, /* tag= */ null); - - // Save instance state. - Bundle outState = new Bundle(); - mTestActivityController.pause().saveInstanceState(outState).stop(); - - // Recreate everything with saved state. - mTestActivityController = ActivityController.of(new FragmentTestActivity()); - mTestActivity = mTestActivityController.get(); - mTestActivityController.setup(outState); - - // Ensure saved fields were applied. - assertThat(getShadowAlertDialog().getTitle()).isEqualTo(dialogTitle); - assertThat(ShadowAlertDialog.getLatestAlertDialog().getButton( - DialogInterface.BUTTON_POSITIVE).getText()).isEqualTo(positiveButtonText); - assertThat(ShadowAlertDialog.getLatestAlertDialog().getButton( - DialogInterface.BUTTON_NEGATIVE).getText()).isEqualTo(negativeButtonText); - assertThat(getShadowAlertDialog().getMessage()).isEqualTo(dialogMessage); - } - - private ShadowAlertDialog getShadowAlertDialog() { - return ShadowApplication.getInstance().getLatestAlertDialog(); - } - - private ShadowWindow getShadowWindowFromDialog(AlertDialog dialog) { - return (ShadowWindow) Shadow.extract(dialog.getWindow()); - } - - /** Concrete implementation of the fragment under test. */ - public static class TestSettingsPreferenceDialogFragment extends - SettingsPreferenceDialogFragment { - - private Boolean mDialogClosedResult; - private boolean mNeedsInputMethod; - - static TestSettingsPreferenceDialogFragment newInstance(String key) { - TestSettingsPreferenceDialogFragment fragment = - new TestSettingsPreferenceDialogFragment(); - Bundle b = new Bundle(/* capacity= */ 1); - b.putString(ARG_KEY, key); - fragment.setArguments(b); - return fragment; - } - - @Override - protected boolean needInputMethod() { - return mNeedsInputMethod; - } - - void setNeedsInputMethod(boolean needsInputMethod) { - mNeedsInputMethod = needsInputMethod; - } - - @Override - protected void onDialogClosed(boolean positiveResult) { - mDialogClosedResult = positiveResult; - } - - Boolean getDialogClosedResult() { - return mDialogClosedResult; - } - } - - /** Concrete implementation of {@link DialogPreference} for testing use. */ - private static class TestDialogPreference extends DialogPreference { - TestDialogPreference(Context context) { - super(context); - } - } - - /** Simple {@link PreferenceFragmentCompat} implementation to serve as the target fragment. */ - public static class TestTargetFragment extends PreferenceFragmentCompat { - @Override - public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { - setPreferenceScreen(getPreferenceManager().createPreferenceScreen(getContext())); - } - } -} |