LargeTemplateView.java revision 8a452e96e2308fe9515aa91b8e5b369eeefc25e7
18a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk/* 28a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk * Copyright 2017 The Android Open Source Project 38a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk * 48a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk * Licensed under the Apache License, Version 2.0 (the "License"); 58a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk * you may not use this file except in compliance with the License. 68a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk * You may obtain a copy of the License at 78a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk * 88a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk * http://www.apache.org/licenses/LICENSE-2.0 98a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk * 108a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk * Unless required by applicable law or agreed to in writing, software 118a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk * distributed under the License is distributed on an "AS IS" BASIS, 128a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 138a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk * See the License for the specific language governing permissions and 148a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk * limitations under the License. 158a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk */ 168a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk 178a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monkpackage androidx.app.slice.widget; 188a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk 198a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monkimport static android.view.ViewGroup.LayoutParams.WRAP_CONTENT; 208a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk 218a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monkimport android.app.slice.Slice; 228a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monkimport android.app.slice.SliceItem; 238a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monkimport android.content.Context; 248a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monkimport android.support.annotation.RestrictTo; 258a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monkimport android.support.v7.widget.LinearLayoutManager; 268a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monkimport android.support.v7.widget.RecyclerView; 278a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monkimport android.util.TypedValue; 288a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk 298a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monkimport java.util.ArrayList; 308a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monkimport java.util.List; 318a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk 328a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monkimport androidx.app.slice.core.SliceQuery; 338a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk 348a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk/** 358a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk * @hide 368a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk */ 378a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk@RestrictTo(RestrictTo.Scope.LIBRARY) 388a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monkpublic class LargeTemplateView extends SliceView.SliceModeView { 398a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk 408a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk private final LargeSliceAdapter mAdapter; 418a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk private final RecyclerView mRecyclerView; 428a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk private final int mDefaultHeight; 438a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk private final int mMaxHeight; 448a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk private Slice mSlice; 458a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk private boolean mIsScrollable; 468a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk 478a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk public LargeTemplateView(Context context) { 488a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk super(context); 498a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk 508a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk mRecyclerView = new RecyclerView(getContext()); 518a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext())); 528a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk mAdapter = new LargeSliceAdapter(context); 538a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk mRecyclerView.setAdapter(mAdapter); 548a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk addView(mRecyclerView); 558a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk mDefaultHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 200, 568a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk getResources().getDisplayMetrics()); 578a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk mMaxHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 200, 588a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk getResources().getDisplayMetrics()); 598a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk } 608a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk 618a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk @Override 628a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk public @SliceView.SliceMode int getMode() { 638a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk return SliceView.MODE_LARGE; 648a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk } 658a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk 668a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk @Override 678a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 688a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk mRecyclerView.getLayoutParams().height = WRAP_CONTENT; 698a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk super.onMeasure(widthMeasureSpec, heightMeasureSpec); 708a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk if (mRecyclerView.getMeasuredHeight() > mMaxHeight 718a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk || (mSlice != null && SliceQuery.hasHints(mSlice, Slice.HINT_PARTIAL))) { 728a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk mRecyclerView.getLayoutParams().height = mDefaultHeight; 738a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk } else { 748a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk mRecyclerView.getLayoutParams().height = mRecyclerView.getMeasuredHeight(); 758a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk } 768a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk super.onMeasure(widthMeasureSpec, heightMeasureSpec); 778a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk } 788a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk 798a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk @Override 808a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk public void setSlice(Slice slice) { 818a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk SliceItem color = SliceQuery.find(slice, SliceItem.TYPE_COLOR); 828a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk mSlice = slice; 838a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk List<SliceItem> items = new ArrayList<>(); 848a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk boolean[] hasHeader = new boolean[1]; 858a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk if (SliceQuery.hasHints(slice, Slice.HINT_LIST)) { 868a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk addList(slice, items); 878a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk } else { 888a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk slice.getItems().forEach(item -> { 898a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk if (item.hasHint(Slice.HINT_ACTIONS)) { 908a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk return; 918a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk } else if (item.getType() == SliceItem.TYPE_COLOR) { 928a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk return; 938a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk } else if (item.getType() == SliceItem.TYPE_SLICE 948a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk && item.hasHint(Slice.HINT_LIST)) { 958a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk addList(item.getSlice(), items); 968a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk } else if (item.hasHint(Slice.HINT_LIST_ITEM)) { 978a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk items.add(item); 988a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk } else if (!hasHeader[0]) { 998a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk hasHeader[0] = true; 1008a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk items.add(0, item); 1018a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk } else { 1028a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk items.add(item); 1038a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk } 1048a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk }); 1058a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk } 1068a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk mAdapter.setSliceItems(items, color); 1078a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk } 1088a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk 1098a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk private void addList(Slice slice, List<SliceItem> items) { 1108a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk List<SliceItem> sliceItems = slice.getItems(); 1118a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk items.addAll(sliceItems); 1128a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk } 1138a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk 1148a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk /** 1158a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk * Whether or not the content in this template should be scrollable. 1168a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk */ 1178a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk public void setScrollable(boolean isScrollable) { 1188a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk // TODO -- restrict / enable how much this view can show 1198a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk mIsScrollable = isScrollable; 1208a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk } 1218a452e96e2308fe9515aa91b8e5b369eeefc25e7Jason Monk} 122