• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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 package com.android.customization.picker;
17 
18 import static androidx.core.view.ViewCompat.LAYOUT_DIRECTION_RTL;
19 
20 import android.app.Activity;
21 import android.content.Context;
22 import android.view.LayoutInflater;
23 import android.view.View;
24 import android.view.ViewGroup;
25 
26 import androidx.annotation.LayoutRes;
27 import androidx.annotation.NonNull;
28 import androidx.cardview.widget.CardView;
29 import androidx.core.view.ViewCompat;
30 import androidx.viewpager.widget.PagerAdapter;
31 
32 import com.android.customization.picker.BasePreviewAdapter.PreviewPage;
33 import com.android.wallpaper.util.SizeCalculator;
34 import com.android.wallpaper.widget.PreviewPager;
35 
36 import java.util.ArrayList;
37 import java.util.List;
38 
39 /**
40  * Base adapter for {@link PreviewPager}.
41  * It can be used as-is by creating an extension of {@link PreviewPage} and adding pages via
42  * {@link #addPage(PreviewPage)}
43  * @param <T> the type of {@link PreviewPage} that this adapter will handle.
44  */
45 public class BasePreviewAdapter<T extends PreviewPage> extends PagerAdapter {
46 
47     private final int mPreviewCardResId;
48     private final Context mContext;
49     private final LayoutInflater mInflater;
50     protected final List<T> mPages = new ArrayList<>();
51 
BasePreviewAdapter(Context context, @LayoutRes int previewCardResId)52     protected BasePreviewAdapter(Context context, @LayoutRes int previewCardResId) {
53         mContext = context;
54         mPreviewCardResId = previewCardResId;
55         mInflater = LayoutInflater.from(mContext);
56     }
57 
addPage(T p)58     protected void addPage(T p) {
59         mPages.add(p);
60     }
61 
62     @Override
getCount()63     public int getCount() {
64         return mPages.size();
65     }
66 
67     @Override
isViewFromObject(@onNull View view, @NonNull Object object)68     public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
69         return view == ((PreviewPage) object).card;
70     }
71 
72     @NonNull
73     @Override
instantiateItem(@onNull ViewGroup container, int position)74     public Object instantiateItem(@NonNull ViewGroup container, int position) {
75         if (ViewCompat.getLayoutDirection(container) == LAYOUT_DIRECTION_RTL) {
76             position = mPages.size() - 1 - position;
77         }
78         CardView card = (CardView) mInflater.inflate(mPreviewCardResId, container, false);
79         T page = mPages.get(position);
80 
81         page.setCard(card);
82         page.bindPreviewContent();
83         if (card.getParent() != null) {
84             container.removeView(card);
85         }
86         container.addView(card);
87         return page;
88     }
89 
90     @Override
destroyItem(@onNull ViewGroup container, int position, @NonNull Object object)91     public void destroyItem(@NonNull ViewGroup container, int position,
92             @NonNull Object object) {
93         View card = ((PreviewPage) object).card;
94         ((PreviewPage) object).card = null;
95         if (card.getParent() == container) {
96             container.removeView(card);
97         }
98     }
99 
100     /**
101      * Represents a possible page in a {@link PreviewPager}, based on a CardView container.
102      * Override {@link #bindPreviewContent()} to bind the contents of the page when instantiated.
103      */
104     public static abstract class PreviewPage {
105         protected final String title;
106         protected CardView card;
107 
108         private Activity mActivity;
109 
PreviewPage(String title, Activity activity)110         protected PreviewPage(String title, Activity activity) {
111             this.title = title;
112             mActivity = activity;
113         }
114 
setCard(CardView card)115         public void setCard(CardView card) {
116             card.addOnLayoutChangeListener(
117                     new View.OnLayoutChangeListener() {
118                         @Override
119                         public void onLayoutChange(View v, int left, int top, int right, int bottom,
120                                 int oldLeft, int oldTop, int oldRight, int oldBottom) {
121                             card.setRadius(SizeCalculator.getPreviewCornerRadius(
122                                     mActivity, card.getMeasuredWidth()));
123                             card.removeOnLayoutChangeListener(this);
124                         }
125                     });
126             this.card = card;
127         }
128 
bindPreviewContent()129         public abstract void bindPreviewContent();
130     }
131 }
132