summaryrefslogtreecommitdiff
path: root/src/com/android/customization/picker/theme/ThemePreviewPage.java
blob: c3af91cd119d554a931160e3cc8a9cfcac1de658 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package com.android.customization.picker.theme;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.icu.text.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLayoutChangeListener;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.ColorInt;
import androidx.annotation.DrawableRes;
import androidx.annotation.LayoutRes;
import androidx.annotation.StringRes;
import androidx.constraintlayout.widget.Guideline;

import com.android.customization.picker.BasePreviewAdapter.PreviewPage;
import com.android.wallpaper.R;

import java.text.FieldPosition;
import java.util.Calendar;
import java.util.List;
import java.util.TimeZone;

abstract class ThemePreviewPage extends PreviewPage {

    public interface TimeContainer {
        void updateTime();
    }

    @StringRes
    final int nameResId;
    final Drawable icon;
    @LayoutRes
    final int contentLayoutRes;
    @ColorInt
    final int accentColor;
    protected final LayoutInflater inflater;

    public ThemePreviewPage(Context context, @StringRes int titleResId,
            @DrawableRes int iconSrc, @LayoutRes int contentLayoutRes,
            @ColorInt int accentColor) {
        super(null);
        this.nameResId = titleResId;
        if (iconSrc != Resources.ID_NULL) {
            this.icon = context.getResources().getDrawable(iconSrc, context.getTheme());
            int size = context.getResources().getDimensionPixelSize(R.dimen.card_header_icon_size);
            icon.setBounds(0, 0, size, size);
        } else {
            this.icon = null;
        }
        this.contentLayoutRes = contentLayoutRes;
        this.accentColor = accentColor;
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public void bindPreviewContent() {
        TextView header = card.findViewById(R.id.theme_preview_card_header);
        header.setText(nameResId);
        header.setCompoundDrawables(null, icon, null, null);
        header.setCompoundDrawableTintList(ColorStateList.valueOf(accentColor));
        card.findViewById(R.id.theme_preview_top_bar).setVisibility(View.GONE);
        card.findViewById(R.id.edit_label).setVisibility(View.GONE);

        ViewGroup body = card.findViewById(R.id.theme_preview_card_body_container);
        inflater.inflate(contentLayoutRes, body, true);
        bindBody(false);
    }

    protected boolean containsWallpaper() {
        return false;
    }

    protected abstract void bindBody(boolean forceRebind);

    static class ThemeCoverPage extends ThemePreviewPage implements TimeContainer {

        public static final int COVER_PAGE_WALLPAPER_ALPHA = 0x66;
        /**
         * Maps which icon from ResourceConstants#ICONS_FOR_PREVIEW to use for each icon in the
         * top bar (fake "status bar") of the cover page.
         */
        private static final int [] sTopBarIconToPreviewIcon = new int [] { 0, 6, 7 };

        private final Typeface mHeadlineFont;
        private final List<Drawable> mIcons;
        private final List<Drawable> mShapeAppIcons;
        private Drawable mShapeDrawable;
        private final int[] mColorButtonIds;
        private final int[] mColorTileIds;
        private final int[][] mColorTileIconIds;
        private final int[] mShapeIconIds;
        private final Resources mRes;
        private String mTitle;
        private OnClickListener mEditClickListener;
        private final OnLayoutChangeListener[] mListeners;
        private final int mCornerRadius;
        private final ColorStateList mTintList;

        public ThemeCoverPage(Context context, String title, int accentColor, List<Drawable> icons,
                Typeface headlineFont, int cornerRadius,
                Drawable shapeDrawable,
                List<Drawable> shapeAppIcons,
                OnClickListener editClickListener,
                int[] colorButtonIds, int[] colorTileIds, int[][] colorTileIconIds,
                int[] shapeIconIds, OnLayoutChangeListener... wallpaperListeners) {
            super(context, 0, 0, R.layout.preview_card_cover_content, accentColor);
            mRes = context.getResources();
            mTitle = title;
            mHeadlineFont = headlineFont;
            mIcons = icons;
            mCornerRadius = cornerRadius;
            mShapeDrawable = shapeDrawable;
            mShapeAppIcons = shapeAppIcons;
            mEditClickListener = editClickListener;
            mColorButtonIds = colorButtonIds;
            mColorTileIds = colorTileIds;
            mColorTileIconIds = colorTileIconIds;
            mShapeIconIds = shapeIconIds;
            mListeners = wallpaperListeners;
            // Color QS icons:
            int controlGreyColor = mRes.getColor(R.color.control_grey, null);
            mTintList = new ColorStateList(
                    new int[][]{
                            new int[]{android.R.attr.state_selected},
                            new int[]{android.R.attr.state_checked},
                            new int[]{-android.R.attr.state_enabled},
                    },
                    new int[] {
                            accentColor,
                            accentColor,
                            controlGreyColor
                    }
            );
        }

        @Override
        protected void bindBody(boolean forceRebind) {
            if (card == null) {
                return;
            }
            if (mListeners != null) {
                for (OnLayoutChangeListener listener : mListeners) {
                    if (listener != null) {
                        card.addOnLayoutChangeListener(listener);
                    }
                }
            }

            if (forceRebind) {
                card.requestLayout();
            }

            for (int i = 0; i < mColorButtonIds.length; i++) {
                CompoundButton button = card.findViewById(mColorButtonIds[i]);
                if (button != null) {
                    button.setButtonTintList(mTintList);
                }
            }
            for (int i = 0; i < 3 && i < mIcons.size(); i++) {
                Drawable icon = mIcons.get(mColorTileIconIds[i][1]).getConstantState()
                        .newDrawable().mutate();
                Drawable bgShape = mShapeDrawable.getConstantState().newDrawable();
                bgShape.setTint(accentColor);

                ImageView bg = card.findViewById(mColorTileIds[i]);
                bg.setImageDrawable(bgShape);
                ImageView fg = card.findViewById(mColorTileIconIds[i][0]);
                fg.setImageDrawable(icon);
            }

            // Shape preview icons:

            for (int i = 0; i < 3 && i < mShapeAppIcons.size(); i++) {
                ImageView iconView = card.findViewById(mShapeIconIds[i]);
                iconView.setBackground(mShapeAppIcons.get(i));
            }
        }

        @Override
        public void bindPreviewContent() {
            TextView header = card.findViewById(R.id.theme_preview_card_header);
            header.setText(mTitle);
            header.setTextAppearance(R.style.CoverTitleTextAppearance);
            header.setTypeface(mHeadlineFont);

            card.findViewById(R.id.theme_preview_top_bar).setVisibility(View.VISIBLE);
            TextView clock = card.findViewById(R.id.theme_preview_clock);
            clock.setText(getFormattedTime());
            clock.setTypeface(mHeadlineFont);

            ViewGroup iconsContainer = card.findViewById(R.id.theme_preview_top_bar_icons);

            for (int i = 0; i < iconsContainer.getChildCount(); i++) {
                int iconIndex = sTopBarIconToPreviewIcon[i];
                if (iconIndex < mIcons.size()) {
                    ((ImageView) iconsContainer.getChildAt(i))
                            .setImageDrawable(mIcons.get(iconIndex).getConstantState()
                                    .newDrawable().mutate());
                } else {
                    iconsContainer.getChildAt(i).setVisibility(View.GONE);
                }
            }

            ViewGroup body = card.findViewById(R.id.theme_preview_card_body_container);

            inflater.inflate(contentLayoutRes, body, true);

            bindBody(false);

            TextView editLabel = card.findViewById(R.id.edit_label);
            editLabel.setOnClickListener(mEditClickListener);
            card.setOnClickListener(mEditClickListener);
            card.setClickable(mEditClickListener != null);

            editLabel.setVisibility(mEditClickListener != null
                    ? View.VISIBLE : View.INVISIBLE);

            View qsb = card.findViewById(R.id.theme_qsb);
            if (qsb != null && qsb.getVisibility() == View.VISIBLE) {
                if (qsb.getBackground() instanceof GradientDrawable) {
                    GradientDrawable bg = (GradientDrawable) qsb.getBackground();
                    float cornerRadius = useRoundedQSB(mCornerRadius)
                            ? (float)qsb.getLayoutParams().height / 2 : mCornerRadius;
                    bg.setCornerRadii(new float[]{
                            cornerRadius, cornerRadius, cornerRadius, cornerRadius,
                            cornerRadius, cornerRadius, cornerRadius, cornerRadius});
                }
            }

            Guideline guideline = card.findViewById(R.id.guideline);
            if (guideline != null) {
                guideline.setGuidelineEnd(card.getResources().getDimensionPixelOffset(
                        R.dimen.preview_theme_cover_content_bottom));
            }
        }

        @Override
        public void updateTime() {
            if (card != null) {
                ((TextView) card.findViewById(R.id.theme_preview_clock)).setText(
                        getFormattedTime());
            }
        }

        private boolean useRoundedQSB(int cornerRadius) {
            return cornerRadius >=
                    card.getResources().getDimensionPixelSize(R.dimen.roundCornerThreshold);
        }

        private String getFormattedTime() {
            DateFormat df = DateFormat.getTimeInstance(DateFormat.SHORT);
            StringBuffer time = new StringBuffer();
            FieldPosition amPmPosition = new FieldPosition(DateFormat.Field.AM_PM);
            df.format(Calendar.getInstance(TimeZone.getDefault()).getTime(), time, amPmPosition);
            if (amPmPosition.getBeginIndex() > 0) {
                time.delete(amPmPosition.getBeginIndex(), amPmPosition.getEndIndex());
            }
            return time.toString();
        }

        @Override
        protected boolean containsWallpaper() {
            return true;
        }
    }
}