Merge "Update look and feel of multi-select preference screen." into pi-car-dev
diff --git a/car-ui-lib/res/layout/car_ui_list_preference.xml b/car-ui-lib/res/layout/car_ui_list_preference.xml
index b3c9855..0f9a367 100644
--- a/car-ui-lib/res/layout/car_ui_list_preference.xml
+++ b/car-ui-lib/res/layout/car_ui_list_preference.xml
@@ -15,22 +15,22 @@
~ limitations under the License.
-->
-<FrameLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
+<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
+ android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
- android:id="@+id/dialog_container">
+ android:background="@drawable/car_ui_activity_background">
<com.android.car.ui.recyclerview.CarUiRecyclerView
- android:id="@+id/radio_list"
+ android:id="@+id/list"
android:layout_width="match_parent"
- android:layout_height="match_parent"/>
+ android:layout_height="match_parent" />
<com.android.car.ui.toolbar.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
- app:state="subpage"/>
+ app:state="subpage" />
</FrameLayout>
\ No newline at end of file
diff --git a/car-ui-lib/res/layout/car_ui_multi_select_list_preference.xml b/car-ui-lib/res/layout/car_ui_multi_select_list_preference.xml
deleted file mode 100644
index 1cac8b8..0000000
--- a/car-ui-lib/res/layout/car_ui_multi_select_list_preference.xml
+++ /dev/null
@@ -1,63 +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"
- android:background="@drawable/car_ui_recyclerview_button_ripple_background">
-
- <FrameLayout
- android:id="@+id/container"
- android:layout_width="match_parent"
- android:layout_height="0dp"
- app:layout_constraintBottom_toTopOf="@id/positive_button"
- app:layout_constraintTop_toTopOf="parent">
-
- <com.android.car.ui.recyclerview.CarUiRecyclerView
- android:id="@+id/list"
- android:layout_width="match_parent"
- android:layout_height="match_parent" />
-
- <com.android.car.ui.toolbar.Toolbar
- android:id="@+id/toolbar"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- app:state="subpage" />
-
- </FrameLayout>
-
- <Button
- android:id="@+id/negative_button"
- style="@style/Widget.CarUi.Button"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_margin="@dimen/car_ui_padding_2"
- android:text="@string/car_ui_dialog_preference_negative"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintStart_toStartOf="parent" />
-
- <Button
- android:id="@+id/positive_button"
- style="@style/Widget.CarUi.Button"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_margin="@dimen/car_ui_padding_2"
- android:text="@string/car_ui_dialog_preference_positive"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintStart_toEndOf="@id/negative_button" />
-
-</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
diff --git a/car-ui-lib/src/com/android/car/ui/preference/CarUiMultiSelectListItemAdapter.java b/car-ui-lib/src/com/android/car/ui/preference/CarUiMultiSelectListItemAdapter.java
deleted file mode 100644
index ca8489e..0000000
--- a/car-ui-lib/src/com/android/car/ui/preference/CarUiMultiSelectListItemAdapter.java
+++ /dev/null
@@ -1,113 +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.ui.preference;
-
-import android.view.LayoutInflater;
-import android.view.View;
-import android.view.ViewGroup;
-import android.widget.CheckBox;
-import android.widget.TextView;
-
-import androidx.annotation.NonNull;
-import androidx.annotation.Nullable;
-import androidx.recyclerview.widget.RecyclerView;
-
-import com.android.car.ui.R;
-
-import java.util.List;
-
-/**
- * Adapter for {@link RecyclerView} to display items use checkboxes to allow for multi-select
- * functionality.
- */
-public class CarUiMultiSelectListItemAdapter extends
- RecyclerView.Adapter<CarUiMultiSelectListItemAdapter.ViewHolder> {
-
- /**
- * Callback that will be issued when a checkbox state is toggled.
- */
- public interface OnCheckedChangeListener {
-
- /**
- * Will be called when checkbox checked state changes.
- *
- * @param position of the checkbox.
- */
- void onCheckChanged(int position, boolean isChecked);
- }
-
- private OnCheckedChangeListener mListener;
-
- private final List<String> mList;
- private boolean[] mCheckedItems;
-
- CarUiMultiSelectListItemAdapter(List<String> list, boolean[] checkedItems) {
- if (list.size() != checkedItems.length) {
- throw new IllegalStateException("Item list must be same size and checked items list");
- }
-
- mList = list;
- mCheckedItems = checkedItems;
- }
-
- @NonNull
- @Override
- public ViewHolder onCreateViewHolder(ViewGroup parent,
- int viewType) {
- View view = LayoutInflater.from(parent.getContext())
- .inflate(R.layout.car_ui_check_box_list_item, parent, false);
- return new ViewHolder(view);
- }
-
- @Override
- public void onBindViewHolder(ViewHolder holder, int position) {
- String entry = mList.get(position);
- holder.mCheckBox.setChecked(mCheckedItems[position]);
- holder.mTextView.setText(entry);
- }
-
- @Override
- public int getItemCount() {
- return mList.size();
- }
-
- void setOnCheckedChangedListener(@Nullable OnCheckedChangeListener listener) {
- mListener = listener;
- }
-
- /** Holds views of list items that have checkboxes. */
- class ViewHolder extends RecyclerView.ViewHolder {
-
- CheckBox mCheckBox;
- TextView mTextView;
-
- ViewHolder(View view) {
- super(view);
- mCheckBox = view.findViewById(R.id.checkbox);
- mTextView = view.findViewById(R.id.text);
-
- view.setOnClickListener(v -> {
- boolean isChecked = !mCheckBox.isChecked();
- mCheckBox.setChecked(isChecked);
- mCheckedItems[getAdapterPosition()] = isChecked;
- if (mListener != null) {
- mListener.onCheckChanged(getAdapterPosition(), isChecked);
- }
- });
- }
- }
-}
diff --git a/car-ui-lib/src/com/android/car/ui/preference/ListPreferenceDialogFragment.java b/car-ui-lib/src/com/android/car/ui/preference/ListPreferenceDialogFragment.java
deleted file mode 100644
index 4af62ef..0000000
--- a/car-ui-lib/src/com/android/car/ui/preference/ListPreferenceDialogFragment.java
+++ /dev/null
@@ -1,216 +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.ui.preference;
-
-import android.app.AlertDialog;
-import android.app.Dialog;
-import android.content.Context;
-import android.content.DialogInterface;
-import android.os.Bundle;
-import android.view.LayoutInflater;
-import android.view.View;
-import android.view.ViewGroup;
-import android.view.ViewTreeObserver;
-
-import androidx.annotation.NonNull;
-import androidx.fragment.app.DialogFragment;
-import androidx.preference.ListPreference;
-
-import com.android.car.ui.R;
-import com.android.car.ui.recyclerview.CarUiRecyclerView;
-import com.android.car.ui.toolbar.Toolbar;
-
-import java.util.ArrayList;
-
-/**
- * Presents a dialog with a list of options associated with a {@link ListPreference}.
- *
- * <p>Note: this is borrowed as-is from androidx.preference.ListPreferenceDialogFragmentCompat
- * with updates to formatting to match the project style. Automotive applications should use this
- * implementations in order to launch the system themed platform {@link AlertDialog} instead of the
- * one in the support library. In addition this list preference can be shown in a full screen
- * dialog. Full screen dialog will have a custom layout returned by {@link #onCreateDialogView} and
- * the window size is changes in {@link #onStart()}.
- */
-public class ListPreferenceDialogFragment extends PreferenceDialogFragment implements
- Toolbar.OnBackListener, CarUiRecyclerViewRadioButtonAdapter.OnRadioButtonClickedListener {
-
- private static final String SAVE_STATE_INDEX = "ListPreferenceDialogFragment.index";
- private static final String SAVE_STATE_ENTRIES = "ListPreferenceDialogFragment.entries";
- private static final String SAVE_STATE_ENTRY_VALUES =
- "ListPreferenceDialogFragment.entryValues";
-
- private int mClickedDialogEntryIndex;
- private CharSequence[] mEntries;
- private CharSequence[] mEntryValues;
- private View mDialogView;
- private CarUiRecyclerView mCarUiRecyclerView;
- private boolean mShowFullScreen;
-
- /**
- * Returns a new instance of {@link ListPreferenceDialogFragment} for the {@link
- * ListPreference} with the given {@code key}.
- */
- public static ListPreferenceDialogFragment newInstance(String key) {
- ListPreferenceDialogFragment fragment = new ListPreferenceDialogFragment();
- Bundle b = new Bundle(/* capacity= */ 1);
- b.putString(ARG_KEY, key);
- fragment.setArguments(b);
- return fragment;
- }
-
- @Override
- public View onCreateDialogView(Context context) {
- return mShowFullScreen ? mDialogView : super.onCreateDialogView(context);
- }
-
- @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);
- }
-
- mShowFullScreen = getContext().getResources().getBoolean(
- R.bool.car_ui_preference_list_show_full_screen);
- if (!mShowFullScreen) {
- return;
- }
-
- setStyle(DialogFragment.STYLE_NORMAL, R.style.Preference_CarUi_ListPreference);
- LayoutInflater inflater = LayoutInflater.from(getContext());
- mDialogView = inflater.inflate(R.layout.car_ui_list_preference, null);
-
- Toolbar toolbar = mDialogView.findViewById(R.id.toolbar);
- toolbar.registerOnBackListener(this);
-
- ArrayList<String> entries = new ArrayList<>();
- for (CharSequence element : mEntries) {
- entries.add(element.toString());
- }
-
- mCarUiRecyclerView = mDialogView.findViewById(R.id.radio_list);
- CarUiRecyclerViewRadioButtonAdapter adapter = new CarUiRecyclerViewRadioButtonAdapter(
- entries, mClickedDialogEntryIndex);
- mCarUiRecyclerView.setAdapter(adapter);
- adapter.registerListener(this);
-
- mDialogView.getViewTreeObserver()
- .addOnGlobalLayoutListener(
- new ViewTreeObserver.OnGlobalLayoutListener() {
- @Override
- public void onGlobalLayout() {
- mDialogView.getViewTreeObserver()
- .removeOnGlobalLayoutListener(this);
- int recyclerViewHeight =
- getDialog().getWindow().getDecorView().getHeight();
-
- mCarUiRecyclerView.setPadding(mCarUiRecyclerView.getPaddingLeft(),
- toolbar.getHeight(),
- mCarUiRecyclerView.getPaddingRight(),
- mCarUiRecyclerView.getPaddingBottom());
-
- ViewGroup.LayoutParams params =
- mCarUiRecyclerView.getLayoutParams();
- params.height = recyclerViewHeight;
- mCarUiRecyclerView.setLayoutParams(params);
- }
- });
- }
-
- @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);
-
- if (mShowFullScreen) {
- builder.setPositiveButton(null, null);
- builder.setNegativeButton(null, null);
- builder.setTitle(null);
- return;
- } else {
- builder.setSingleChoiceItems(mEntries, mClickedDialogEntryIndex,
- (dialog, which) -> onClick(which));
-
- // 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 onStart() {
- super.onStart();
- Dialog dialog = getDialog();
- if (dialog != null && mShowFullScreen) {
- int width = ViewGroup.LayoutParams.MATCH_PARENT;
- int height = ViewGroup.LayoutParams.MATCH_PARENT;
- dialog.getWindow().setLayout(width, height);
- }
- }
-
- @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);
- }
- }
- }
-
- @Override
- public boolean onBack() {
- onClick(getDialog(), DialogInterface.BUTTON_NEGATIVE);
- getDialog().dismiss();
- return true;
- }
-
- @Override
- public void onClick(int position) {
- mClickedDialogEntryIndex = position;
- onClick(getDialog(), DialogInterface.BUTTON_POSITIVE);
- getDialog().dismiss();
- }
-}
diff --git a/car-ui-lib/src/com/android/car/ui/preference/ListPreferenceFragment.java b/car-ui-lib/src/com/android/car/ui/preference/ListPreferenceFragment.java
index b861de7..7df7411 100644
--- a/car-ui-lib/src/com/android/car/ui/preference/ListPreferenceFragment.java
+++ b/car-ui-lib/src/com/android/car/ui/preference/ListPreferenceFragment.java
@@ -70,7 +70,7 @@
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
- final CarUiRecyclerView mCarUiRecyclerView = view.requireViewById(R.id.radio_list);
+ final CarUiRecyclerView mCarUiRecyclerView = view.requireViewById(R.id.list);
final Toolbar toolbar = view.requireViewById(R.id.toolbar);
mCarUiRecyclerView.setPadding(0, toolbar.getHeight(), 0, 0);
diff --git a/car-ui-lib/src/com/android/car/ui/preference/MultiSelectListPreferenceDialogFragment.java b/car-ui-lib/src/com/android/car/ui/preference/MultiSelectListPreferenceDialogFragment.java
deleted file mode 100644
index 3a9fb7b..0000000
--- a/car-ui-lib/src/com/android/car/ui/preference/MultiSelectListPreferenceDialogFragment.java
+++ /dev/null
@@ -1,141 +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.ui.preference;
-
-import android.app.AlertDialog;
-import android.os.Bundle;
-
-import androidx.annotation.NonNull;
-import androidx.preference.MultiSelectListPreference;
-
-import java.util.ArrayList;
-import java.util.HashSet;
-import java.util.Set;
-
-/**
- * Presents a dialog with a list of options associated with a {@link MultiSelectListPreference}.
- *
- * <p>Note: this is borrowed as-is from
- * androidx.preference.MultiSelectListPreferenceDialogFragmentCompat with updates to formatting to
- * match the project style. Automotive applications should use this implementations in order to
- * launch the system themed platform {@link AlertDialog} instead of the one in the support library.
- */
-public class MultiSelectListPreferenceDialogFragment extends PreferenceDialogFragment {
-
- private static final String SAVE_STATE_VALUES =
- "MultiSelectListPreferenceDialogFragment.values";
- private static final String SAVE_STATE_CHANGED =
- "MultiSelectListPreferenceDialogFragment.changed";
- private static final String SAVE_STATE_ENTRIES =
- "MultiSelectListPreferenceDialogFragment.entries";
- private static final String SAVE_STATE_ENTRY_VALUES =
- "MultiSelectListPreferenceDialogFragment.entryValues";
-
- @SuppressWarnings("WeakerAccess") /* synthetic access */
- Set<String> mNewValues = new HashSet<>();
- @SuppressWarnings("WeakerAccess") /* synthetic access */
- boolean mPreferenceChanged;
- @SuppressWarnings("WeakerAccess") /* synthetic access */
- CharSequence[] mEntries;
- @SuppressWarnings("WeakerAccess") /* synthetic access */
- CharSequence[] mEntryValues;
-
- /**
- * Returns a new instance of {@link MultiSelectListPreferenceDialogFragment} for the {@link
- * MultiSelectListPreference} with the given {@code key}.
- */
- public static MultiSelectListPreferenceDialogFragment newInstance(String key) {
- final MultiSelectListPreferenceDialogFragment fragment =
- new MultiSelectListPreferenceDialogFragment();
- final Bundle b = new Bundle(1);
- b.putString(ARG_KEY, key);
- fragment.setArguments(b);
- return fragment;
- }
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
-
- if (savedInstanceState == null) {
- final MultiSelectListPreference preference = getListPreference();
-
- if (preference.getEntries() == null || preference.getEntryValues() == null) {
- throw new IllegalStateException(
- "MultiSelectListPreference requires an entries array and an entryValues "
- + "array.");
- }
-
- mNewValues.clear();
- mNewValues.addAll(preference.getValues());
- mPreferenceChanged = false;
- mEntries = preference.getEntries();
- mEntryValues = preference.getEntryValues();
- } else {
- mNewValues.clear();
- mNewValues.addAll(savedInstanceState.getStringArrayList(SAVE_STATE_VALUES));
- mPreferenceChanged = savedInstanceState.getBoolean(SAVE_STATE_CHANGED, false);
- mEntries = savedInstanceState.getCharSequenceArray(SAVE_STATE_ENTRIES);
- mEntryValues = savedInstanceState.getCharSequenceArray(SAVE_STATE_ENTRY_VALUES);
- }
- }
-
- @Override
- public void onSaveInstanceState(@NonNull Bundle outState) {
- super.onSaveInstanceState(outState);
- outState.putStringArrayList(SAVE_STATE_VALUES, new ArrayList<>(mNewValues));
- outState.putBoolean(SAVE_STATE_CHANGED, mPreferenceChanged);
- outState.putCharSequenceArray(SAVE_STATE_ENTRIES, mEntries);
- outState.putCharSequenceArray(SAVE_STATE_ENTRY_VALUES, mEntryValues);
- }
-
- private MultiSelectListPreference getListPreference() {
- return (MultiSelectListPreference) getPreference();
- }
-
- @Override
- protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
- super.onPrepareDialogBuilder(builder);
-
- final int entryCount = mEntryValues.length;
- final boolean[] checkedItems = new boolean[entryCount];
- for (int i = 0; i < entryCount; i++) {
- checkedItems[i] = mNewValues.contains(mEntryValues[i].toString());
- }
- builder.setMultiChoiceItems(mEntries, checkedItems,
- (dialog, which, isChecked) -> {
- if (isChecked) {
- mPreferenceChanged |= mNewValues.add(
- mEntryValues[which].toString());
- } else {
- mPreferenceChanged |= mNewValues.remove(
- mEntryValues[which].toString());
- }
- });
- }
-
- @Override
- public void onDialogClosed(boolean positiveResult) {
- if (positiveResult && mPreferenceChanged) {
- final MultiSelectListPreference preference = getListPreference();
- if (preference.callChangeListener(mNewValues)) {
- preference.setValues(mNewValues);
- }
- }
- mPreferenceChanged = false;
- }
-}
diff --git a/car-ui-lib/src/com/android/car/ui/preference/MultiSelectListPreferenceFragment.java b/car-ui-lib/src/com/android/car/ui/preference/MultiSelectListPreferenceFragment.java
index a253d38..17362f5 100644
--- a/car-ui-lib/src/com/android/car/ui/preference/MultiSelectListPreferenceFragment.java
+++ b/car-ui-lib/src/com/android/car/ui/preference/MultiSelectListPreferenceFragment.java
@@ -18,13 +18,10 @@
import static com.android.car.ui.preference.PreferenceDialogFragment.ARG_KEY;
-import static java.util.stream.Collectors.toList;
-
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
-import android.widget.Button;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
@@ -33,10 +30,13 @@
import androidx.preference.Preference;
import com.android.car.ui.R;
+import com.android.car.ui.recyclerview.CarUiContentListItem;
+import com.android.car.ui.recyclerview.CarUiListItem;
+import com.android.car.ui.recyclerview.CarUiListItemAdapter;
import com.android.car.ui.recyclerview.CarUiRecyclerView;
import com.android.car.ui.toolbar.Toolbar;
-import java.util.Arrays;
+import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@@ -45,8 +45,7 @@
* A fragment that provides a layout with a list of options associated with a {@link
* CarUiMultiSelectListPreference}.
*/
-public class MultiSelectListPreferenceFragment extends Fragment implements
- CarUiMultiSelectListItemAdapter.OnCheckedChangeListener {
+public class MultiSelectListPreferenceFragment extends Fragment {
private CarUiMultiSelectListPreference mPreference;
private Set<String> mNewValues;
@@ -68,7 +67,7 @@
public View onCreateView(
@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
- return inflater.inflate(R.layout.car_ui_multi_select_list_preference, container, false);
+ return inflater.inflate(R.layout.car_ui_list_preference, container, false);
}
@Override
@@ -109,27 +108,36 @@
+ "array length.");
}
- List<String> entryStrings = Arrays.stream(entries).map(CharSequence::toString).collect(
- toList());
+ List<CarUiListItem> listItems = new ArrayList<>();
+ boolean[] selectedItems = mPreference.getSelectedItems();
- CarUiMultiSelectListItemAdapter adapter = new CarUiMultiSelectListItemAdapter(
- entryStrings, mPreference.getSelectedItems());
+ for (int i = 0; i < entries.length; i++) {
+ String entry = entries[i].toString();
+ String entryValue = entryValues[i].toString();
+ CarUiContentListItem item = new CarUiContentListItem();
+ item.setAction(CarUiContentListItem.Action.CHECK_BOX);
+ item.setTitle(entry);
+ item.setChecked(selectedItems[i]);
+ item.setOnCheckedChangedListener(isChecked -> {
+ if (isChecked) {
+ mNewValues.add(entryValue);
+ } else {
+ mNewValues.remove(entryValue);
+ }
+ });
+
+ listItems.add(item);
+ }
+
+ CarUiListItemAdapter adapter = new CarUiListItemAdapter(listItems);
recyclerView.setAdapter(adapter);
- adapter.setOnCheckedChangedListener(this);
- Button positiveButton = view.requireViewById(R.id.positive_button);
- Button negativeButton = view.requireViewById(R.id.negative_button);
-
- positiveButton.setOnClickListener(v -> {
+ toolbar.registerOnBackListener(() -> {
if (mPreference.callChangeListener(mNewValues)) {
mPreference.setValues(mNewValues);
}
- dismissFragment();
- });
-
- negativeButton.setOnClickListener(v -> {
- dismissFragment();
+ return false;
});
}
@@ -163,31 +171,4 @@
return (CarUiMultiSelectListPreference) preference;
}
-
- private void dismissFragment() {
- if (getActivity() == null) {
- throw new IllegalStateException(
- "MultiSelectListPreference fragment is not attached to an Activity.");
- }
-
- getActivity().getSupportFragmentManager().popBackStack();
- }
-
- @Override
- public void onCheckChanged(int position, boolean isChecked) {
- CharSequence[] entryValues = mPreference.getEntryValues();
-
- if (position < 0 || position > entryValues.length - 1) {
- throw new IllegalStateException(
- "Clicked preference has invalid index.");
- }
-
- String value = entryValues[position].toString();
-
- if (isChecked) {
- mNewValues.add(value);
- } else {
- mNewValues.remove(value);
- }
- }
}
diff --git a/car-ui-lib/src/com/android/car/ui/recyclerview/CarUiRecyclerView.java b/car-ui-lib/src/com/android/car/ui/recyclerview/CarUiRecyclerView.java
index 0b608a2..345cbaf 100644
--- a/car-ui-lib/src/com/android/car/ui/recyclerview/CarUiRecyclerView.java
+++ b/car-ui-lib/src/com/android/car/ui/recyclerview/CarUiRecyclerView.java
@@ -264,7 +264,7 @@
a.getInt(R.styleable.CarUiRecyclerView_layoutStyle, CarUiRecyclerViewLayout.LINEAR);
mNumOfColumns = a.getInt(R.styleable.CarUiRecyclerView_numOfColumns, /* defValue= */ 2);
boolean enableDivider =
- a.getBoolean(R.styleable.CarUiRecyclerView_enableDivider, /* defValue= */ false);
+ a.getBoolean(R.styleable.CarUiRecyclerView_enableDivider, /* defValue= */ true);
if (mCarUiRecyclerViewLayout == CarUiRecyclerViewLayout.LINEAR) {