blob: 2759394af318bcf0b58859949eb217781affa385 [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_PARTIAL;
Jason Monk8a452e92017-10-31 19:21:47 -040020import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
21
Jason Monk2a7d0fc2017-11-15 10:10:24 -050022import android.annotation.TargetApi;
Jason Monk8a452e92017-10-31 19:21:47 -040023import android.content.Context;
24import android.support.annotation.RestrictTo;
25import android.support.v7.widget.LinearLayoutManager;
26import android.support.v7.widget.RecyclerView;
Mady Mellordc052042018-01-04 17:11:07 -080027import android.util.AttributeSet;
Jason Monk8a452e92017-10-31 19:21:47 -040028
Jason Monkdcb5e2f2017-11-15 20:19:43 -050029import androidx.app.slice.Slice;
Jason Monk8a452e92017-10-31 19:21:47 -040030import androidx.app.slice.core.SliceQuery;
Mady Mellor5b2c0ce2017-12-08 12:16:37 -080031import androidx.app.slice.view.R;
Jason Monk8a452e92017-10-31 19:21:47 -040032
33/**
34 * @hide
35 */
36@RestrictTo(RestrictTo.Scope.LIBRARY)
Jason Monk2a7d0fc2017-11-15 10:10:24 -050037@TargetApi(24)
Mady Mellordc052042018-01-04 17:11:07 -080038public class LargeTemplateView extends SliceChildView {
Jason Monk8a452e92017-10-31 19:21:47 -040039
40 private final LargeSliceAdapter mAdapter;
41 private final RecyclerView mRecyclerView;
42 private final int mDefaultHeight;
Jason Monk8a452e92017-10-31 19:21:47 -040043 private Slice mSlice;
44 private boolean mIsScrollable;
45
46 public LargeTemplateView(Context context) {
47 super(context);
Jason Monk8a452e92017-10-31 19:21:47 -040048 mRecyclerView = new RecyclerView(getContext());
49 mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
50 mAdapter = new LargeSliceAdapter(context);
51 mRecyclerView.setAdapter(mAdapter);
52 addView(mRecyclerView);
Mady Mellor5b2c0ce2017-12-08 12:16:37 -080053 mDefaultHeight = getResources().getDimensionPixelSize(R.dimen.abc_slice_large_height);
54 }
55
56 @Override
Jason Monk8a452e92017-10-31 19:21:47 -040057 public @SliceView.SliceMode int getMode() {
58 return SliceView.MODE_LARGE;
59 }
60
61 @Override
Mady Mellor238b9b62018-01-09 16:15:40 -080062 public void setSliceObserver(SliceView.SliceObserver observer) {
63 mObserver = observer;
64 if (mAdapter != null) {
65 mAdapter.setSliceObserver(mObserver);
66 }
67 }
68
69 @Override
Jason Monk8a452e92017-10-31 19:21:47 -040070 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
71 mRecyclerView.getLayoutParams().height = WRAP_CONTENT;
72 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
Mady Mellor5b2c0ce2017-12-08 12:16:37 -080073 int width = MeasureSpec.getSize(widthMeasureSpec);
74 if (mRecyclerView.getMeasuredHeight() > width
Jason Monkdcb5e2f2017-11-15 20:19:43 -050075 || (mSlice != null && SliceQuery.hasHints(mSlice, HINT_PARTIAL))) {
Mady Mellor5b2c0ce2017-12-08 12:16:37 -080076 mRecyclerView.getLayoutParams().height = width;
Jason Monk8a452e92017-10-31 19:21:47 -040077 } else {
78 mRecyclerView.getLayoutParams().height = mRecyclerView.getMeasuredHeight();
79 }
80 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
81 }
82
83 @Override
84 public void setSlice(Slice slice) {
Jason Monk8a452e92017-10-31 19:21:47 -040085 mSlice = slice;
Mady Mellordc052042018-01-04 17:11:07 -080086 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 Monk8a452e92017-10-31 19:21:47 -0400101 }
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 Mellordf269702017-12-20 15:40:00 -0800110
111 @Override
112 public void resetView() {
113 mSlice = null;
Mady Mellordc052042018-01-04 17:11:07 -0800114 mAdapter.setSliceItems(null, -1);
Mady Mellordf269702017-12-20 15:40:00 -0800115 }
Jason Monk8a452e92017-10-31 19:21:47 -0400116}