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_ACTIONS; |
| 20 | import static android.app.slice.Slice.HINT_LIST; |
| 21 | import static android.app.slice.Slice.HINT_LIST_ITEM; |
| 22 | import static android.app.slice.Slice.HINT_PARTIAL; |
Jason Monk | 0c76d30 | 2017-11-21 14:02:27 -0500 | [diff] [blame] | 23 | import static android.app.slice.SliceItem.FORMAT_COLOR; |
| 24 | import static android.app.slice.SliceItem.FORMAT_SLICE; |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 25 | import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT; |
| 26 | |
Mady Mellor | 71ef84d | 2017-12-11 13:33:36 -0800 | [diff] [blame^] | 27 | import static androidx.app.slice.core.SliceHints.HINT_SUMMARY; |
| 28 | |
Jason Monk | 2a7d0fc | 2017-11-15 10:10:24 -0500 | [diff] [blame] | 29 | import android.annotation.TargetApi; |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 30 | import android.content.Context; |
| 31 | import android.support.annotation.RestrictTo; |
| 32 | import android.support.v7.widget.LinearLayoutManager; |
| 33 | import android.support.v7.widget.RecyclerView; |
Mady Mellor | 5b2c0ce | 2017-12-08 12:16:37 -0800 | [diff] [blame] | 34 | import android.view.View; |
| 35 | import android.widget.FrameLayout; |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 36 | |
| 37 | import java.util.ArrayList; |
| 38 | import java.util.List; |
Jason Monk | 2a7d0fc | 2017-11-15 10:10:24 -0500 | [diff] [blame] | 39 | import java.util.function.Consumer; |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 40 | |
Jason Monk | dcb5e2f | 2017-11-15 20:19:43 -0500 | [diff] [blame] | 41 | import androidx.app.slice.Slice; |
| 42 | import androidx.app.slice.SliceItem; |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 43 | import androidx.app.slice.core.SliceQuery; |
Mady Mellor | 5b2c0ce | 2017-12-08 12:16:37 -0800 | [diff] [blame] | 44 | import androidx.app.slice.view.R; |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 45 | |
| 46 | /** |
| 47 | * @hide |
| 48 | */ |
| 49 | @RestrictTo(RestrictTo.Scope.LIBRARY) |
Jason Monk | 2a7d0fc | 2017-11-15 10:10:24 -0500 | [diff] [blame] | 50 | @TargetApi(24) |
Mady Mellor | 5b2c0ce | 2017-12-08 12:16:37 -0800 | [diff] [blame] | 51 | public class LargeTemplateView extends FrameLayout implements SliceView.SliceModeView { |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 52 | |
| 53 | private final LargeSliceAdapter mAdapter; |
| 54 | private final RecyclerView mRecyclerView; |
| 55 | private final int mDefaultHeight; |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 56 | private Slice mSlice; |
| 57 | private boolean mIsScrollable; |
| 58 | |
| 59 | public LargeTemplateView(Context context) { |
| 60 | super(context); |
| 61 | |
| 62 | mRecyclerView = new RecyclerView(getContext()); |
| 63 | mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext())); |
| 64 | mAdapter = new LargeSliceAdapter(context); |
| 65 | mRecyclerView.setAdapter(mAdapter); |
| 66 | addView(mRecyclerView); |
Mady Mellor | 5b2c0ce | 2017-12-08 12:16:37 -0800 | [diff] [blame] | 67 | mDefaultHeight = getResources().getDimensionPixelSize(R.dimen.abc_slice_large_height); |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public View getView() { |
| 72 | return this; |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public @SliceView.SliceMode int getMode() { |
| 77 | return SliceView.MODE_LARGE; |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 82 | mRecyclerView.getLayoutParams().height = WRAP_CONTENT; |
| 83 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
Mady Mellor | 5b2c0ce | 2017-12-08 12:16:37 -0800 | [diff] [blame] | 84 | int width = MeasureSpec.getSize(widthMeasureSpec); |
| 85 | if (mRecyclerView.getMeasuredHeight() > width |
Jason Monk | dcb5e2f | 2017-11-15 20:19:43 -0500 | [diff] [blame] | 86 | || (mSlice != null && SliceQuery.hasHints(mSlice, HINT_PARTIAL))) { |
Mady Mellor | 5b2c0ce | 2017-12-08 12:16:37 -0800 | [diff] [blame] | 87 | mRecyclerView.getLayoutParams().height = width; |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 88 | } else { |
| 89 | mRecyclerView.getLayoutParams().height = mRecyclerView.getMeasuredHeight(); |
| 90 | } |
| 91 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public void setSlice(Slice slice) { |
Jason Monk | 0c76d30 | 2017-11-21 14:02:27 -0500 | [diff] [blame] | 96 | SliceItem color = SliceQuery.find(slice, FORMAT_COLOR); |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 97 | mSlice = slice; |
Jason Monk | 2a7d0fc | 2017-11-15 10:10:24 -0500 | [diff] [blame] | 98 | final List<SliceItem> items = new ArrayList<>(); |
| 99 | final boolean[] hasHeader = new boolean[1]; |
Jason Monk | dcb5e2f | 2017-11-15 20:19:43 -0500 | [diff] [blame] | 100 | if (SliceQuery.hasHints(slice, HINT_LIST)) { |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 101 | addList(slice, items); |
| 102 | } else { |
Jason Monk | 2a7d0fc | 2017-11-15 10:10:24 -0500 | [diff] [blame] | 103 | slice.getItems().forEach(new Consumer<SliceItem>() { |
| 104 | @Override |
| 105 | public void accept(SliceItem item) { |
Mady Mellor | 71ef84d | 2017-12-11 13:33:36 -0800 | [diff] [blame^] | 106 | if (item.hasAnyHints(HINT_ACTIONS, HINT_SUMMARY)) { |
Jason Monk | 2a7d0fc | 2017-11-15 10:10:24 -0500 | [diff] [blame] | 107 | return; |
| 108 | } else if (FORMAT_COLOR.equals(item.getFormat())) { |
| 109 | return; |
| 110 | } else if (FORMAT_SLICE.equals(item.getFormat()) |
| 111 | && item.hasHint(HINT_LIST)) { |
| 112 | addList(item.getSlice(), items); |
| 113 | } else if (item.hasHint(HINT_LIST_ITEM)) { |
| 114 | items.add(item); |
| 115 | } else if (!hasHeader[0]) { |
| 116 | hasHeader[0] = true; |
| 117 | items.add(0, item); |
| 118 | } else { |
| 119 | items.add(item); |
| 120 | } |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 121 | } |
| 122 | }); |
| 123 | } |
| 124 | mAdapter.setSliceItems(items, color); |
| 125 | } |
| 126 | |
| 127 | private void addList(Slice slice, List<SliceItem> items) { |
| 128 | List<SliceItem> sliceItems = slice.getItems(); |
| 129 | items.addAll(sliceItems); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Whether or not the content in this template should be scrollable. |
| 134 | */ |
| 135 | public void setScrollable(boolean isScrollable) { |
| 136 | // TODO -- restrict / enable how much this view can show |
| 137 | mIsScrollable = isScrollable; |
| 138 | } |
| 139 | } |