summaryrefslogtreecommitdiff
path: root/src/com/android/systemui/plugin/globalactions/wallet/WalletCardCarousel.java
blob: 1a27accb814b636ca736a1a56c0817d8475c3451 (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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.plugin.globalactions.wallet;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.HapticFeedbackConstants;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.accessibility.AccessibilityEvent;
import android.widget.ImageView;

import androidx.cardview.widget.CardView;
import androidx.core.view.ViewCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.LinearSmoothScroller;
import androidx.recyclerview.widget.PagerSnapHelper;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerViewAccessibilityDelegate;

import java.util.Collections;
import java.util.List;

/**
 * Card Carousel for displaying Quick Access Wallet cards.
 */
class WalletCardCarousel extends RecyclerView {

    // A negative card margin is required because card shrinkage pushes the cards too far apart
    private static final float CARD_MARGIN_RATIO = -.03f;
    private static final float CARD_SCREEN_WIDTH_RATIO = .69f;
    // Size of the unselected card as a ratio to size of selected card.
    private static final float UNSELECTED_CARD_SCALE = .83f;
    private static final float CORNER_RADIUS_RATIO = 25f / 700f;
    private static final float CARD_ASPECT_RATIO = 700f / 440f;
    static final int CARD_ANIM_ALPHA_DURATION = 100;
    static final int CARD_ANIM_ALPHA_DELAY = 50;

    private final int mScreenWidth;
    private final int mCardMarginPx;
    private final Rect mSystemGestureExclusionZone = new Rect();
    private final WalletCardAdapter mWalletCardAdapter;
    private final int mCardWidthPx;
    private final int mCardHeightPx;
    private final float mCornerRadiusPx;
    private final int mTotalCardWidth;
    private final float mCardEdgeToCenterDistance;

    private OnSelectionListener mSelectionListener;
    private OnCardScrollListener mCardScrollListener;
    // Adapter position of the child that is closest to the center of the recycler view.
    private int mCenteredAdapterPosition = RecyclerView.NO_POSITION;
    // Pixel distance, along y-axis, from the center of the recycler view to the nearest child.
    private float mEdgeToCenterDistance = Float.MAX_VALUE;
    private float mCardCenterToScreenCenterDistancePx = Float.MAX_VALUE;
    // When card data is loaded, this many cards should be animated as data is bound to them.
    private int mNumCardsToAnimate;
    // When card data is loaded, this is the position of the leftmost card to be animated.
    private int mCardAnimationStartPosition;
    // When card data is loaded, the animations may be delayed so that other animations can complete
    private int mExtraAnimationDelay;

    interface OnSelectionListener {
        /**
         * The card was moved to the center, thus selecting it.
         */
        void onCardSelected(@NonNull WalletCardViewInfo card);

        /**
         * The card was clicked.
         */
        void onCardClicked(@NonNull WalletCardViewInfo card);
    }

    interface OnCardScrollListener {
        void onCardScroll(WalletCardViewInfo centerCard, WalletCardViewInfo nextCard,
                float percentDistanceFromCenter);
    }

    public WalletCardCarousel(Context context) {
        this(context, null);
    }

    public WalletCardCarousel(Context context, @Nullable AttributeSet attributeSet) {
        super(context, attributeSet);
        setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));
        DisplayMetrics metrics = getResources().getDisplayMetrics();
        mScreenWidth = Math.min(metrics.widthPixels, metrics.heightPixels);
        mCardWidthPx = Math.round(mScreenWidth * CARD_SCREEN_WIDTH_RATIO);
        mCardHeightPx = Math.round(mCardWidthPx / CARD_ASPECT_RATIO);
        mCornerRadiusPx = mCardWidthPx * CORNER_RADIUS_RATIO;
        mCardMarginPx = Math.round(mScreenWidth * CARD_MARGIN_RATIO);
        mTotalCardWidth =
                mCardWidthPx + getResources().getDimensionPixelSize(R.dimen.card_margin) * 2;
        mCardEdgeToCenterDistance = mTotalCardWidth / 2f;
        addOnScrollListener(new CardCarouselScrollListener());
        new CarouselSnapHelper().attachToRecyclerView(this);
        mWalletCardAdapter = new WalletCardAdapter();
        mWalletCardAdapter.setHasStableIds(true);
        setAdapter(mWalletCardAdapter);
        ViewCompat.setAccessibilityDelegate(this, new CardCarouselAccessibilityDelegate(this));
        updatePadding(mScreenWidth);
    }

    @Override
    public void onViewAdded(View child) {
        super.onViewAdded(child);
        LayoutParams layoutParams = (LayoutParams) child.getLayoutParams();
        layoutParams.leftMargin = mCardMarginPx;
        layoutParams.rightMargin = mCardMarginPx;
        child.addOnLayoutChangeListener((v, l, t, r, b, ol, ot, or, ob) -> updateCardView(child));
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        int width = getWidth();
        if (mWalletCardAdapter.getItemCount() > 1) {
            // Whole carousel is opted out from system gesture.
            mSystemGestureExclusionZone.set(0, 0, width, getHeight());
            setSystemGestureExclusionRects(Collections.singletonList(mSystemGestureExclusionZone));
        }
        if (width != mScreenWidth) {
            updatePadding(width);
        }
    }

    /**
     * The padding pushes the first and last cards in the list to the center when they are
     * selected.
     */
    private void updatePadding(int viewWidth) {
        int paddingHorizontal = (viewWidth - mTotalCardWidth) / 2 - mCardMarginPx;
        paddingHorizontal = Math.max(0, paddingHorizontal); // just in case
        setPadding(paddingHorizontal, getPaddingTop(), paddingHorizontal, getPaddingBottom());

        // re-center selected card after changing padding (if card is selected)
        if (mWalletCardAdapter != null
                && mWalletCardAdapter.getItemCount() > 0
                && mCenteredAdapterPosition != NO_POSITION) {
            ViewHolder viewHolder = findViewHolderForAdapterPosition(mCenteredAdapterPosition);
            if (viewHolder != null) {
                View cardView = viewHolder.itemView;
                int cardCenter = (cardView.getLeft() + cardView.getRight()) / 2;
                int viewCenter = (getLeft() + getRight()) / 2;
                int scrollX = cardCenter - viewCenter;
                scrollBy(scrollX, 0);
            }
        }
    }

    void setSelectionListener(OnSelectionListener selectionListener) {
        mSelectionListener = selectionListener;
    }

    void setCardScrollListener(OnCardScrollListener scrollListener) {
        mCardScrollListener = scrollListener;
    }

    int getCardWidthPx() {
        return mCardWidthPx;
    }

    int getCardHeightPx() {
        return mCardHeightPx;
    }

    /**
     * Set card data. Returns true if carousel was empty, indicating that views will be animated
     */
    boolean setData(List<WalletCardViewInfo> data, int selectedIndex) {
        boolean wasEmpty = mWalletCardAdapter.getItemCount() == 0;
        mWalletCardAdapter.setData(data);
        if (wasEmpty) {
            scrollToPosition(selectedIndex);
            mNumCardsToAnimate = numCardsOnScreen(data.size(), selectedIndex);
            mCardAnimationStartPosition = Math.max(selectedIndex - 1, 0);
        }
        WalletCardViewInfo selectedCard = data.get(selectedIndex);
        mCardScrollListener.onCardScroll(selectedCard, selectedCard, 0);
        return wasEmpty;
    }

    @Override
    public void scrollToPosition(int position) {
        super.scrollToPosition(position);
        mSelectionListener.onCardSelected(mWalletCardAdapter.mData.get(position));
    }

    /**
     * The number of cards shown on screen when one of the cards is position in the center. This is
     * also the num
     */
    private static int numCardsOnScreen(int numCards, int selectedIndex) {
        if (numCards <= 2) {
            return numCards;
        }
        // When there are 3 or more cards, 3 cards will be shown unless the first or last card is
        // centered on screen.
        return selectedIndex > 0 && selectedIndex < (numCards - 1) ? 3 : 2;
    }

    private void updateCardView(View view) {
        WalletCardViewHolder viewHolder = (WalletCardViewHolder) view.getTag();
        CardView cardView = viewHolder.cardView;
        float center = (float) getWidth() / 2f;
        float viewCenter = (view.getRight() + view.getLeft()) / 2f;
        float viewWidth = view.getWidth();
        float position = (viewCenter - center) / viewWidth;
        float scaleFactor = Math.max(UNSELECTED_CARD_SCALE, 1f - Math.abs(position));

        cardView.setScaleX(scaleFactor);
        cardView.setScaleY(scaleFactor);

        // a card is the "centered card" until its edge has moved past the center of the recycler
        // view. note that we also need to factor in the negative margin.
        // Find the edge that is closer to the center.
        int edgePosition =
                viewCenter < center ? view.getRight() + mCardMarginPx
                        : view.getLeft() - mCardMarginPx;

        if (Math.abs(viewCenter - center) < mCardCenterToScreenCenterDistancePx) {
            int childAdapterPosition = getChildAdapterPosition(view);
            if (childAdapterPosition == RecyclerView.NO_POSITION) {
                return;
            }
            mCenteredAdapterPosition = getChildAdapterPosition(view);
            mEdgeToCenterDistance = edgePosition - center;
            mCardCenterToScreenCenterDistancePx = Math.abs(viewCenter - center);
        }
    }

    void setExtraAnimationDelay(int extraAnimationDelay) {
        mExtraAnimationDelay = extraAnimationDelay;
    }

    private class CardCarouselScrollListener extends OnScrollListener {

        private int oldState = -1;

        @Override
        public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
            if (newState == RecyclerView.SCROLL_STATE_IDLE && newState != oldState) {
                performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
            }
            oldState = newState;
        }

        /**
         * Callback method to be invoked when the RecyclerView has been scrolled. This will be
         * called after the scroll has completed.
         *
         * <p>This callback will also be called if visible item range changes after a layout
         * calculation. In that case, dx and dy will be 0.
         *
         * @param recyclerView The RecyclerView which scrolled.
         * @param dx           The amount of horizontal scroll.
         * @param dy           The amount of vertical scroll.
         */
        @Override
        public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
            mCenteredAdapterPosition = RecyclerView.NO_POSITION;
            mEdgeToCenterDistance = Float.MAX_VALUE;
            mCardCenterToScreenCenterDistancePx = Float.MAX_VALUE;
            for (int i = 0; i < getChildCount(); i++) {
                updateCardView(getChildAt(i));
            }
            if (mCenteredAdapterPosition == RecyclerView.NO_POSITION || dx == 0) {
                return;
            }

            int nextAdapterPosition =
                    mCenteredAdapterPosition + (mEdgeToCenterDistance > 0 ? 1 : -1);
            if (nextAdapterPosition < 0 || nextAdapterPosition >= mWalletCardAdapter.mData.size()) {
                return;
            }

            // Update the label text based on the currently selected card and the next one
            WalletCardViewInfo centerCard = mWalletCardAdapter.mData.get(mCenteredAdapterPosition);
            WalletCardViewInfo nextCard = mWalletCardAdapter.mData.get(nextAdapterPosition);
            float percentDistanceFromCenter =
                    Math.abs(mEdgeToCenterDistance) / mCardEdgeToCenterDistance;
            mCardScrollListener.onCardScroll(centerCard, nextCard, percentDistanceFromCenter);
        }
    }

    private class CarouselSnapHelper extends PagerSnapHelper {

        private static final float MILLISECONDS_PER_INCH = 200.0F;
        private static final int MAX_SCROLL_ON_FLING_DURATION = 80; // ms

        @Override
        public View findSnapView(LayoutManager layoutManager) {
            View view = super.findSnapView(layoutManager);
            if (view == null) {
                // implementation decides not to snap
                return null;
            }
            WalletCardViewHolder viewHolder = (WalletCardViewHolder) view.getTag();
            WalletCardViewInfo card = viewHolder.info;
            mSelectionListener.onCardSelected(card);
            mCardScrollListener.onCardScroll(card, card, 0);
            return view;
        }

        /**
         * The default SnapScroller is a little sluggish
         */
        @Override
        protected LinearSmoothScroller createSnapScroller(LayoutManager layoutManager) {
            return new LinearSmoothScroller(getContext()) {
                @Override
                protected void onTargetFound(View targetView, State state, Action action) {
                    int[] snapDistances = calculateDistanceToFinalSnap(layoutManager, targetView);
                    final int dx = snapDistances[0];
                    final int dy = snapDistances[1];
                    final int time = calculateTimeForDeceleration(
                            Math.max(Math.abs(dx), Math.abs(dy)));
                    if (time > 0) {
                        action.update(dx, dy, time, mDecelerateInterpolator);
                    }
                }

                @Override
                protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
                    return MILLISECONDS_PER_INCH / displayMetrics.densityDpi;
                }

                @Override
                protected int calculateTimeForScrolling(int dx) {
                    return Math.min(MAX_SCROLL_ON_FLING_DURATION,
                            super.calculateTimeForScrolling(dx));
                }
            };
        }
    }

    private static class WalletCardViewHolder extends ViewHolder {

        private final CardView cardView;
        private final ImageView imageView;
        private WalletCardViewInfo info;

        WalletCardViewHolder(View view) {
            super(view);
            cardView = view.requireViewById(R.id.card);
            imageView = cardView.requireViewById(R.id.image);
        }
    }

    private class WalletCardAdapter extends Adapter<WalletCardViewHolder> {

        private List<WalletCardViewInfo> mData = Collections.emptyList();

        @Override
        public int getItemViewType(int position) {
            return 0;
        }

        @NonNull
        @Override
        public WalletCardViewHolder onCreateViewHolder(
                @NonNull ViewGroup parent, int itemViewType) {
            LayoutInflater inflater = LayoutInflater.from(getContext());
            View view = inflater.inflate(R.layout.wallet_card_view, parent, false);
            WalletCardViewHolder viewHolder = new WalletCardViewHolder(view);
            CardView cardView = viewHolder.cardView;
            cardView.setRadius(mCornerRadiusPx);
            ViewGroup.LayoutParams layoutParams = cardView.getLayoutParams();
            layoutParams.width = mCardWidthPx;
            layoutParams.height = mCardHeightPx;
            view.setTag(viewHolder);
            return viewHolder;
        }

        @Override
        public void onBindViewHolder(WalletCardViewHolder viewHolder, int position) {
            WalletCardViewInfo info = mData.get(position);
            viewHolder.info = info;
            viewHolder.imageView.setImageDrawable(info.getCardDrawable());
            viewHolder.cardView.setContentDescription(info.getContentDescription());
            viewHolder.cardView.setOnClickListener(
                    v -> {
                        if (position != mCenteredAdapterPosition) {
                            smoothScrollToPosition(position);
                        } else {
                            mSelectionListener.onCardClicked(info);
                        }
                    });
            if (mNumCardsToAnimate > 0 && (position - mCardAnimationStartPosition < 2)) {
                mNumCardsToAnimate--;
                // Animation of cards is progressively delayed from left to right in 50ms increments
                // Additional delay may be added if the empty state view needs to be animated first.
                int startDelay = (position - mCardAnimationStartPosition) * CARD_ANIM_ALPHA_DELAY
                        + mExtraAnimationDelay;
                viewHolder.itemView.setAlpha(0f);
                viewHolder.itemView.animate().alpha(1f)
                        .setStartDelay(Math.max(0, startDelay))
                        .setDuration(CARD_ANIM_ALPHA_DURATION).start();
            }
        }

        @Override
        public long getItemId(int position) {
            return mData.get(position).getCardId().hashCode();
        }

        @Override
        public int getItemCount() {
            return mData.size();
        }

        private void setData(List<WalletCardViewInfo> data) {
            this.mData = data;
            notifyDataSetChanged();
        }
    }

    private class CardCarouselAccessibilityDelegate extends RecyclerViewAccessibilityDelegate {

        private CardCarouselAccessibilityDelegate(@NonNull RecyclerView recyclerView) {
            super(recyclerView);
        }

        @Override
        public boolean onRequestSendAccessibilityEvent(
                ViewGroup viewGroup, View view, AccessibilityEvent accessibilityEvent) {
            int eventType = accessibilityEvent.getEventType();
            if (eventType == AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED) {
                scrollToPosition(getChildAdapterPosition(view));
            }
            return super.onRequestSendAccessibilityEvent(viewGroup, view, accessibilityEvent);
        }
    }
}