[automerge] Allow other layouts to be passed into the dream view adatper. 2p: 5e91b7eab3 2p: c8c07f4ac7

Original change: https://googleplex-android-review.googlesource.com/c/platform/packages/apps/Settings/+/17002670

Bug: 221227402
Change-Id: Ia78d9e429ffd3aa1078af803a9c23da3683a3906
This commit is contained in:
Lucas Silva
2022-02-24 16:45:36 +00:00
committed by Presubmit Automerger Backend
5 changed files with 83 additions and 29 deletions

View File

@@ -16,9 +16,11 @@
package com.android.settings.dream;
import android.annotation.LayoutRes;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.VectorDrawable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@@ -39,14 +41,15 @@ import java.util.List;
*/
public class DreamAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private final List<IDreamItem> mItemList;
private final @LayoutRes int mLayoutRes;
private int mLastSelectedPos = -1;
/**
* View holder for each {@link IDreamItem}.
*/
private class DreamViewHolder extends RecyclerView.ViewHolder {
private final ImageView mIconView;
private final TextView mTitleView;
private final TextView mSummaryView;
private final ImageView mPreviewView;
private final ImageView mPreviewPlaceholderView;
private final Button mCustomizeButton;
@@ -57,8 +60,8 @@ public class DreamAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
mContext = context;
mPreviewView = view.findViewById(R.id.preview);
mPreviewPlaceholderView = view.findViewById(R.id.preview_placeholder);
mIconView = view.findViewById(R.id.icon);
mTitleView = view.findViewById(R.id.title_text);
mSummaryView = view.findViewById(R.id.summary_text);
mCustomizeButton = view.findViewById(R.id.customize_button);
}
@@ -68,6 +71,14 @@ public class DreamAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
public void bindView(IDreamItem item, int position) {
mTitleView.setText(item.getTitle());
final CharSequence summary = item.getSummary();
if (TextUtils.isEmpty(summary)) {
mSummaryView.setVisibility(View.GONE);
} else {
mSummaryView.setText(summary);
mSummaryView.setVisibility(View.VISIBLE);
}
final Drawable previewImage = item.getPreviewImage();
if (previewImage != null) {
mPreviewView.setImageDrawable(previewImage);
@@ -82,7 +93,10 @@ public class DreamAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
icon.setTint(Utils.getColorAttrDefaultColor(mContext,
com.android.internal.R.attr.colorAccentPrimaryVariant));
}
mIconView.setImageDrawable(icon);
final int iconSize = mContext.getResources().getDimensionPixelSize(
R.dimen.dream_item_icon_size);
icon.setBounds(0, 0, iconSize, iconSize);
mTitleView.setCompoundDrawablesRelative(icon, null, null, null);
if (item.isActive()) {
mLastSelectedPos = position;
@@ -104,15 +118,16 @@ public class DreamAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
}
}
public DreamAdapter(List<IDreamItem> itemList) {
public DreamAdapter(@LayoutRes int layoutRes, List<IDreamItem> itemList) {
mItemList = itemList;
mLayoutRes = layoutRes;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
View view = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.dream_preference_layout, viewGroup, false);
.inflate(mLayoutRes, viewGroup, false);
return new DreamViewHolder(view, viewGroup.getContext());
}

View File

@@ -74,9 +74,10 @@ public class DreamPickerController extends BasePreferenceController {
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mAdapter = new DreamAdapter(mDreamInfos.stream()
.map(DreamItem::new)
.collect(Collectors.toList()));
mAdapter = new DreamAdapter(R.layout.dream_preference_layout,
mDreamInfos.stream()
.map(DreamItem::new)
.collect(Collectors.toList()));
final LayoutPreference pref = screen.findPreference(getPreferenceKey());
if (pref == null) {
@@ -111,6 +112,11 @@ public class DreamPickerController extends BasePreferenceController {
return mDreamInfo.caption;
}
@Override
public CharSequence getSummary() {
return mDreamInfo.description;
}
@Override
public Drawable getIcon() {
return mDreamInfo.icon;

View File

@@ -18,23 +18,52 @@ package com.android.settings.dream;
import android.graphics.drawable.Drawable;
import androidx.annotation.Nullable;
/**
* Interface representing a dream item to be displayed.
*/
public interface IDreamItem {
/**
* Gets the title of this dream.
*/
CharSequence getTitle();
/**
* Gets the summary of this dream, or null if the dream doesn't provide one.
*/
@Nullable
CharSequence getSummary();
/**
* Gets the icon for the dream.
*/
Drawable getIcon();
/**
* Callback which can be implemented to handle clicks on this dream.
*/
void onItemClicked();
/**
* Callback which can be implemented to handle the customization of this dream.
*/
default void onCustomizeClicked() {
}
/**
* Gets the preview image of this dream.
*/
Drawable getPreviewImage();
/**
* Returns whether or not this dream is currently active.
*/
boolean isActive();
/**
* Returns whether to allow customization of this dream or not.
*/
default boolean allowCustomization() {
return false;
}