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_PARTIAL; |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 20 | import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT; |
| 21 | |
Jason Monk | 2a7d0fc | 2017-11-15 10:10:24 -0500 | [diff] [blame] | 22 | import android.annotation.TargetApi; |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 23 | import android.content.Context; |
| 24 | import android.support.annotation.RestrictTo; |
| 25 | import android.support.v7.widget.LinearLayoutManager; |
| 26 | import android.support.v7.widget.RecyclerView; |
Mady Mellor | dc05204 | 2018-01-04 17:11:07 -0800 | [diff] [blame] | 27 | import android.util.AttributeSet; |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 28 | |
Jason Monk | dcb5e2f | 2017-11-15 20:19:43 -0500 | [diff] [blame] | 29 | import androidx.app.slice.Slice; |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 30 | import androidx.app.slice.core.SliceQuery; |
Mady Mellor | 5b2c0ce | 2017-12-08 12:16:37 -0800 | [diff] [blame] | 31 | import androidx.app.slice.view.R; |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * @hide |
| 35 | */ |
| 36 | @RestrictTo(RestrictTo.Scope.LIBRARY) |
Jason Monk | 2a7d0fc | 2017-11-15 10:10:24 -0500 | [diff] [blame] | 37 | @TargetApi(24) |
Mady Mellor | dc05204 | 2018-01-04 17:11:07 -0800 | [diff] [blame] | 38 | public class LargeTemplateView extends SliceChildView { |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 39 | |
| 40 | private final LargeSliceAdapter mAdapter; |
| 41 | private final RecyclerView mRecyclerView; |
| 42 | private final int mDefaultHeight; |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 43 | private Slice mSlice; |
| 44 | private boolean mIsScrollable; |
| 45 | |
| 46 | public LargeTemplateView(Context context) { |
| 47 | super(context); |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 48 | mRecyclerView = new RecyclerView(getContext()); |
| 49 | mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext())); |
| 50 | mAdapter = new LargeSliceAdapter(context); |
| 51 | mRecyclerView.setAdapter(mAdapter); |
| 52 | addView(mRecyclerView); |
Mady Mellor | 5b2c0ce | 2017-12-08 12:16:37 -0800 | [diff] [blame] | 53 | mDefaultHeight = getResources().getDimensionPixelSize(R.dimen.abc_slice_large_height); |
| 54 | } |
| 55 | |
| 56 | @Override |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 57 | public @SliceView.SliceMode int getMode() { |
| 58 | return SliceView.MODE_LARGE; |
| 59 | } |
| 60 | |
| 61 | @Override |
Mady Mellor | 238b9b6 | 2018-01-09 16:15:40 -0800 | [diff] [blame] | 62 | public void setSliceObserver(SliceView.SliceObserver observer) { |
| 63 | mObserver = observer; |
| 64 | if (mAdapter != null) { |
| 65 | mAdapter.setSliceObserver(mObserver); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | @Override |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 70 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 71 | mRecyclerView.getLayoutParams().height = WRAP_CONTENT; |
| 72 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
Mady Mellor | 5b2c0ce | 2017-12-08 12:16:37 -0800 | [diff] [blame] | 73 | int width = MeasureSpec.getSize(widthMeasureSpec); |
| 74 | if (mRecyclerView.getMeasuredHeight() > width |
Jason Monk | dcb5e2f | 2017-11-15 20:19:43 -0500 | [diff] [blame] | 75 | || (mSlice != null && SliceQuery.hasHints(mSlice, HINT_PARTIAL))) { |
Mady Mellor | 5b2c0ce | 2017-12-08 12:16:37 -0800 | [diff] [blame] | 76 | mRecyclerView.getLayoutParams().height = width; |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 77 | } else { |
| 78 | mRecyclerView.getLayoutParams().height = mRecyclerView.getMeasuredHeight(); |
| 79 | } |
| 80 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public void setSlice(Slice slice) { |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 85 | mSlice = slice; |
Mady Mellor | dc05204 | 2018-01-04 17:11:07 -0800 | [diff] [blame] | 86 | populate(); |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public void setStyle(AttributeSet attrs) { |
| 91 | super.setStyle(attrs); |
| 92 | mAdapter.setStyle(attrs); |
| 93 | } |
| 94 | |
| 95 | private void populate() { |
| 96 | if (mSlice == null) { |
| 97 | return; |
| 98 | } |
| 99 | ListContent lc = new ListContent(mSlice); |
| 100 | mAdapter.setSliceItems(lc.getRowItems(), mTintColor); |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Whether or not the content in this template should be scrollable. |
| 105 | */ |
| 106 | public void setScrollable(boolean isScrollable) { |
| 107 | // TODO -- restrict / enable how much this view can show |
| 108 | mIsScrollable = isScrollable; |
| 109 | } |
Mady Mellor | df26970 | 2017-12-20 15:40:00 -0800 | [diff] [blame] | 110 | |
| 111 | @Override |
| 112 | public void resetView() { |
| 113 | mSlice = null; |
Mady Mellor | dc05204 | 2018-01-04 17:11:07 -0800 | [diff] [blame] | 114 | mAdapter.setSliceItems(null, -1); |
Mady Mellor | df26970 | 2017-12-20 15:40:00 -0800 | [diff] [blame] | 115 | } |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 116 | } |