Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package androidx.app.slice.widget; |
| 18 | |
Jason Monk | dcb5e2f | 2017-11-15 20:19:43 -0500 | [diff] [blame] | 19 | import static android.app.slice.Slice.HINT_HORIZONTAL; |
Jason Monk | 0c76d30 | 2017-11-21 14:02:27 -0500 | [diff] [blame] | 20 | import static android.app.slice.Slice.SUBTYPE_MESSAGE; |
| 21 | import static android.app.slice.Slice.SUBTYPE_SOURCE; |
Jason Monk | 0c76d30 | 2017-11-21 14:02:27 -0500 | [diff] [blame] | 22 | import static android.app.slice.SliceItem.FORMAT_IMAGE; |
Jason Monk | 98ae4f8 | 2017-12-18 11:29:07 -0500 | [diff] [blame] | 23 | import static android.app.slice.SliceItem.FORMAT_INT; |
Jason Monk | 0c76d30 | 2017-11-21 14:02:27 -0500 | [diff] [blame] | 24 | import static android.app.slice.SliceItem.FORMAT_TEXT; |
| 25 | |
Jason Monk | 2a7d0fc | 2017-11-15 10:10:24 -0500 | [diff] [blame] | 26 | import android.annotation.TargetApi; |
Mady Mellor | c133418 | 2017-11-10 15:50:35 -0800 | [diff] [blame] | 27 | import android.app.slice.Slice; |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 28 | import android.content.Context; |
| 29 | import android.support.annotation.RestrictTo; |
| 30 | import android.support.v7.widget.RecyclerView; |
| 31 | import android.util.ArrayMap; |
Mady Mellor | dc05204 | 2018-01-04 17:11:07 -0800 | [diff] [blame] | 32 | import android.util.AttributeSet; |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 33 | import android.view.LayoutInflater; |
| 34 | import android.view.View; |
| 35 | import android.view.ViewGroup; |
| 36 | import android.view.ViewGroup.LayoutParams; |
| 37 | |
| 38 | import java.util.ArrayList; |
| 39 | import java.util.List; |
Jason Monk | 2a7d0fc | 2017-11-15 10:10:24 -0500 | [diff] [blame] | 40 | import java.util.function.Consumer; |
| 41 | import java.util.function.Function; |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 42 | import java.util.stream.Collectors; |
| 43 | |
Jason Monk | dcb5e2f | 2017-11-15 20:19:43 -0500 | [diff] [blame] | 44 | import androidx.app.slice.SliceItem; |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 45 | import androidx.app.slice.core.SliceQuery; |
| 46 | import androidx.app.slice.view.R; |
| 47 | |
| 48 | /** |
| 49 | * @hide |
| 50 | */ |
| 51 | @RestrictTo(RestrictTo.Scope.LIBRARY) |
Jason Monk | 2a7d0fc | 2017-11-15 10:10:24 -0500 | [diff] [blame] | 52 | @TargetApi(24) |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 53 | public class LargeSliceAdapter extends RecyclerView.Adapter<LargeSliceAdapter.SliceViewHolder> { |
| 54 | |
Mady Mellor | dc05204 | 2018-01-04 17:11:07 -0800 | [diff] [blame] | 55 | 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 Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 60 | |
| 61 | private final IdGenerator mIdGen = new IdGenerator(); |
| 62 | private final Context mContext; |
| 63 | private List<SliceWrapper> mSlices = new ArrayList<>(); |
Mady Mellor | dc05204 | 2018-01-04 17:11:07 -0800 | [diff] [blame] | 64 | |
Mady Mellor | 238b9b6 | 2018-01-09 16:15:40 -0800 | [diff] [blame] | 65 | private SliceView.SliceObserver mSliceObserver; |
Mady Mellor | dc05204 | 2018-01-04 17:11:07 -0800 | [diff] [blame] | 66 | private int mColor; |
| 67 | private AttributeSet mAttrs; |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 68 | |
| 69 | public LargeSliceAdapter(Context context) { |
| 70 | mContext = context; |
| 71 | setHasStableIds(true); |
| 72 | } |
| 73 | |
Mady Mellor | 238b9b6 | 2018-01-09 16:15:40 -0800 | [diff] [blame] | 74 | public void setSliceObserver(SliceView.SliceObserver observer) { |
| 75 | mSliceObserver = observer; |
| 76 | } |
| 77 | |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 78 | /** |
| 79 | * Set the {@link SliceItem}'s to be displayed in the adapter and the accent color. |
| 80 | */ |
Mady Mellor | dc05204 | 2018-01-04 17:11:07 -0800 | [diff] [blame] | 81 | public void setSliceItems(List<SliceItem> slices, int color) { |
Mady Mellor | df26970 | 2017-12-20 15:40:00 -0800 | [diff] [blame] | 82 | 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 Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 93 | mColor = color; |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 94 | notifyDataSetChanged(); |
| 95 | } |
| 96 | |
Mady Mellor | dc05204 | 2018-01-04 17:11:07 -0800 | [diff] [blame] | 97 | /** |
| 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 Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 105 | @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 Mellor | dc05204 | 2018-01-04 17:11:07 -0800 | [diff] [blame] | 131 | holder.mSliceView.setTint(mColor); |
| 132 | holder.mSliceView.setStyle(mAttrs); |
| 133 | holder.mSliceView.setSliceItem(slice.mItem, position == 0 /* isHeader */, |
| 134 | position, mSliceObserver); |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 135 | } |
| 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 Mellor | c133418 | 2017-11-10 15:50:35 -0800 | [diff] [blame] | 148 | return new RowView(mContext); |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 149 | } |
| 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 Monk | 0c76d30 | 2017-11-21 14:02:27 -0500 | [diff] [blame] | 158 | mType = getFormat(item); |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 159 | mId = idGen.getId(item); |
| 160 | } |
| 161 | |
Jason Monk | 0c76d30 | 2017-11-21 14:02:27 -0500 | [diff] [blame] | 162 | public static int getFormat(SliceItem item) { |
| 163 | if (SUBTYPE_MESSAGE.equals(item.getSubType())) { |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 164 | // TODO: Better way to determine me or not? Something more like Messaging style. |
Jason Monk | 0c76d30 | 2017-11-21 14:02:27 -0500 | [diff] [blame] | 165 | if (SliceQuery.findSubtype(item, null, SUBTYPE_SOURCE) != null) { |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 166 | return TYPE_MESSAGE; |
| 167 | } else { |
| 168 | return TYPE_MESSAGE_LOCAL; |
| 169 | } |
| 170 | } |
Jason Monk | dcb5e2f | 2017-11-15 20:19:43 -0500 | [diff] [blame] | 171 | if (item.hasHint(HINT_HORIZONTAL)) { |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 172 | return TYPE_GRID; |
| 173 | } |
Mady Mellor | c133418 | 2017-11-10 15:50:35 -0800 | [diff] [blame] | 174 | if (!item.hasHint(Slice.HINT_LIST_ITEM)) { |
| 175 | return TYPE_HEADER; |
| 176 | } |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 177 | 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 Mellor | dc05204 | 2018-01-04 17:11:07 -0800 | [diff] [blame] | 185 | public final SliceChildView mSliceView; |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 186 | |
| 187 | public SliceViewHolder(View itemView) { |
| 188 | super(itemView); |
Mady Mellor | dc05204 | 2018-01-04 17:11:07 -0800 | [diff] [blame] | 189 | mSliceView = itemView instanceof SliceChildView ? (SliceChildView) itemView : null; |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 193 | 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 Monk | 2a7d0fc | 2017-11-15 10:10:24 -0500 | [diff] [blame] | 210 | 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 Monk | 98ae4f8 | 2017-12-18 11:29:07 -0500 | [diff] [blame] | 224 | case FORMAT_INT: |
| 225 | builder.append(i.getInt()); |
Jason Monk | 2a7d0fc | 2017-11-15 10:10:24 -0500 | [diff] [blame] | 226 | break; |
| 227 | } |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 228 | } |
| 229 | }); |
| 230 | return builder.toString(); |
| 231 | } |
| 232 | |
| 233 | public void resetUsage() { |
| 234 | mUsedIds.clear(); |
| 235 | } |
| 236 | } |
| 237 | } |