blob: 9888bc7dd7ea7a7fa88b60130c95f907cbabc5a0 [file] [log] [blame]
Jason Monk8a452e92017-10-31 19:21:47 -04001/*
2 * Copyright 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package androidx.app.slice.widget;
18
Jason Monkdcb5e2f2017-11-15 20:19:43 -050019import static android.app.slice.Slice.HINT_HORIZONTAL;
Jason Monk0c76d302017-11-21 14:02:27 -050020import static android.app.slice.Slice.SUBTYPE_MESSAGE;
21import static android.app.slice.Slice.SUBTYPE_SOURCE;
Jason Monk0c76d302017-11-21 14:02:27 -050022import static android.app.slice.SliceItem.FORMAT_IMAGE;
Jason Monk98ae4f82017-12-18 11:29:07 -050023import static android.app.slice.SliceItem.FORMAT_INT;
Jason Monk0c76d302017-11-21 14:02:27 -050024import static android.app.slice.SliceItem.FORMAT_TEXT;
25
Jason Monk2a7d0fc2017-11-15 10:10:24 -050026import android.annotation.TargetApi;
Mady Mellorc1334182017-11-10 15:50:35 -080027import android.app.slice.Slice;
Jason Monk8a452e92017-10-31 19:21:47 -040028import android.content.Context;
29import android.support.annotation.RestrictTo;
30import android.support.v7.widget.RecyclerView;
31import android.util.ArrayMap;
Mady Mellordc052042018-01-04 17:11:07 -080032import android.util.AttributeSet;
Jason Monk8a452e92017-10-31 19:21:47 -040033import android.view.LayoutInflater;
34import android.view.View;
35import android.view.ViewGroup;
36import android.view.ViewGroup.LayoutParams;
37
38import java.util.ArrayList;
39import java.util.List;
Jason Monk2a7d0fc2017-11-15 10:10:24 -050040import java.util.function.Consumer;
41import java.util.function.Function;
Jason Monk8a452e92017-10-31 19:21:47 -040042import java.util.stream.Collectors;
43
Jason Monkdcb5e2f2017-11-15 20:19:43 -050044import androidx.app.slice.SliceItem;
Jason Monk8a452e92017-10-31 19:21:47 -040045import androidx.app.slice.core.SliceQuery;
46import androidx.app.slice.view.R;
47
48/**
49 * @hide
50 */
51@RestrictTo(RestrictTo.Scope.LIBRARY)
Jason Monk2a7d0fc2017-11-15 10:10:24 -050052@TargetApi(24)
Jason Monk8a452e92017-10-31 19:21:47 -040053public class LargeSliceAdapter extends RecyclerView.Adapter<LargeSliceAdapter.SliceViewHolder> {
54
Mady Mellordc052042018-01-04 17:11:07 -080055 static final int TYPE_DEFAULT = 1;
56 static final int TYPE_HEADER = 2; // TODO: headers shouldn't scroll off
57 static final int TYPE_GRID = 3;
58 static final int TYPE_MESSAGE = 4;
59 static final int TYPE_MESSAGE_LOCAL = 5;
Jason Monk8a452e92017-10-31 19:21:47 -040060
61 private final IdGenerator mIdGen = new IdGenerator();
62 private final Context mContext;
63 private List<SliceWrapper> mSlices = new ArrayList<>();
Mady Mellordc052042018-01-04 17:11:07 -080064
Mady Mellor238b9b62018-01-09 16:15:40 -080065 private SliceView.SliceObserver mSliceObserver;
Mady Mellordc052042018-01-04 17:11:07 -080066 private int mColor;
67 private AttributeSet mAttrs;
Jason Monk8a452e92017-10-31 19:21:47 -040068
69 public LargeSliceAdapter(Context context) {
70 mContext = context;
71 setHasStableIds(true);
72 }
73
Mady Mellor238b9b62018-01-09 16:15:40 -080074 public void setSliceObserver(SliceView.SliceObserver observer) {
75 mSliceObserver = observer;
76 }
77
Jason Monk8a452e92017-10-31 19:21:47 -040078 /**
79 * Set the {@link SliceItem}'s to be displayed in the adapter and the accent color.
80 */
Mady Mellordc052042018-01-04 17:11:07 -080081 public void setSliceItems(List<SliceItem> slices, int color) {
Mady Mellordf269702017-12-20 15:40:00 -080082 if (slices == null) {
83 mSlices.clear();
84 } else {
85 mIdGen.resetUsage();
86 mSlices = slices.stream().map(new Function<SliceItem, SliceWrapper>() {
87 @Override
88 public SliceWrapper apply(SliceItem s) {
89 return new SliceWrapper(s, mIdGen);
90 }
91 }).collect(Collectors.<SliceWrapper>toList());
92 }
Jason Monk8a452e92017-10-31 19:21:47 -040093 mColor = color;
Jason Monk8a452e92017-10-31 19:21:47 -040094 notifyDataSetChanged();
95 }
96
Mady Mellordc052042018-01-04 17:11:07 -080097 /**
98 * Sets the attribute set to use for views in the list.
99 */
100 public void setStyle(AttributeSet attrs) {
101 mAttrs = attrs;
102 notifyDataSetChanged();
103 }
104
Jason Monk8a452e92017-10-31 19:21:47 -0400105 @Override
106 public SliceViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
107 View v = inflateForType(viewType);
108 v.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
109 return new SliceViewHolder(v);
110 }
111
112 @Override
113 public int getItemViewType(int position) {
114 return mSlices.get(position).mType;
115 }
116
117 @Override
118 public long getItemId(int position) {
119 return mSlices.get(position).mId;
120 }
121
122 @Override
123 public int getItemCount() {
124 return mSlices.size();
125 }
126
127 @Override
128 public void onBindViewHolder(SliceViewHolder holder, int position) {
129 SliceWrapper slice = mSlices.get(position);
130 if (holder.mSliceView != null) {
Mady Mellordc052042018-01-04 17:11:07 -0800131 holder.mSliceView.setTint(mColor);
132 holder.mSliceView.setStyle(mAttrs);
133 holder.mSliceView.setSliceItem(slice.mItem, position == 0 /* isHeader */,
134 position, mSliceObserver);
Jason Monk8a452e92017-10-31 19:21:47 -0400135 }
136 }
137
138 private View inflateForType(int viewType) {
139 switch (viewType) {
140 case TYPE_GRID:
141 return LayoutInflater.from(mContext).inflate(R.layout.abc_slice_grid, null);
142 case TYPE_MESSAGE:
143 return LayoutInflater.from(mContext).inflate(R.layout.abc_slice_message, null);
144 case TYPE_MESSAGE_LOCAL:
145 return LayoutInflater.from(mContext).inflate(R.layout.abc_slice_message_local,
146 null);
147 }
Mady Mellorc1334182017-11-10 15:50:35 -0800148 return new RowView(mContext);
Jason Monk8a452e92017-10-31 19:21:47 -0400149 }
150
151 protected static class SliceWrapper {
152 private final SliceItem mItem;
153 private final int mType;
154 private final long mId;
155
156 public SliceWrapper(SliceItem item, IdGenerator idGen) {
157 mItem = item;
Jason Monk0c76d302017-11-21 14:02:27 -0500158 mType = getFormat(item);
Jason Monk8a452e92017-10-31 19:21:47 -0400159 mId = idGen.getId(item);
160 }
161
Jason Monk0c76d302017-11-21 14:02:27 -0500162 public static int getFormat(SliceItem item) {
163 if (SUBTYPE_MESSAGE.equals(item.getSubType())) {
Jason Monk8a452e92017-10-31 19:21:47 -0400164 // TODO: Better way to determine me or not? Something more like Messaging style.
Jason Monk0c76d302017-11-21 14:02:27 -0500165 if (SliceQuery.findSubtype(item, null, SUBTYPE_SOURCE) != null) {
Jason Monk8a452e92017-10-31 19:21:47 -0400166 return TYPE_MESSAGE;
167 } else {
168 return TYPE_MESSAGE_LOCAL;
169 }
170 }
Jason Monkdcb5e2f2017-11-15 20:19:43 -0500171 if (item.hasHint(HINT_HORIZONTAL)) {
Jason Monk8a452e92017-10-31 19:21:47 -0400172 return TYPE_GRID;
173 }
Mady Mellorc1334182017-11-10 15:50:35 -0800174 if (!item.hasHint(Slice.HINT_LIST_ITEM)) {
175 return TYPE_HEADER;
176 }
Jason Monk8a452e92017-10-31 19:21:47 -0400177 return TYPE_DEFAULT;
178 }
179 }
180
181 /**
182 * A {@link RecyclerView.ViewHolder} for presenting slices in {@link LargeSliceAdapter}.
183 */
184 public static class SliceViewHolder extends RecyclerView.ViewHolder {
Mady Mellordc052042018-01-04 17:11:07 -0800185 public final SliceChildView mSliceView;
Jason Monk8a452e92017-10-31 19:21:47 -0400186
187 public SliceViewHolder(View itemView) {
188 super(itemView);
Mady Mellordc052042018-01-04 17:11:07 -0800189 mSliceView = itemView instanceof SliceChildView ? (SliceChildView) itemView : null;
Jason Monk8a452e92017-10-31 19:21:47 -0400190 }
191 }
192
Jason Monk8a452e92017-10-31 19:21:47 -0400193 private static class IdGenerator {
194 private long mNextLong = 0;
195 private final ArrayMap<String, Long> mCurrentIds = new ArrayMap<>();
196 private final ArrayMap<String, Integer> mUsedIds = new ArrayMap<>();
197
198 public long getId(SliceItem item) {
199 String str = genString(item);
200 if (!mCurrentIds.containsKey(str)) {
201 mCurrentIds.put(str, mNextLong++);
202 }
203 long id = mCurrentIds.get(str);
204 int index = mUsedIds.getOrDefault(str, 0);
205 mUsedIds.put(str, index + 1);
206 return id + index * 10000;
207 }
208
209 private String genString(SliceItem item) {
Jason Monk2a7d0fc2017-11-15 10:10:24 -0500210 final StringBuilder builder = new StringBuilder();
211 SliceQuery.stream(item).forEach(new Consumer<SliceItem>() {
212 @Override
213 public void accept(SliceItem i) {
214 builder.append(i.getFormat());
215 //i.removeHint(Slice.HINT_SELECTED);
216 builder.append(i.getHints());
217 switch (i.getFormat()) {
218 case FORMAT_IMAGE:
219 builder.append(i.getIcon());
220 break;
221 case FORMAT_TEXT:
222 builder.append(i.getText());
223 break;
Jason Monk98ae4f82017-12-18 11:29:07 -0500224 case FORMAT_INT:
225 builder.append(i.getInt());
Jason Monk2a7d0fc2017-11-15 10:10:24 -0500226 break;
227 }
Jason Monk8a452e92017-10-31 19:21:47 -0400228 }
229 });
230 return builder.toString();
231 }
232
233 public void resetUsage() {
234 mUsedIds.clear();
235 }
236 }
237}