1 /*
2  * Copyright (C) 2022 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 
17 package com.android.intentresolver.grid;
18 
19 import android.animation.Animator;
20 import android.animation.AnimatorListenerAdapter;
21 import android.animation.ObjectAnimator;
22 import android.animation.ValueAnimator;
23 import android.view.View;
24 import android.view.View.MeasureSpec;
25 import android.view.ViewGroup;
26 import android.view.animation.AccelerateInterpolator;
27 
28 import java.util.Arrays;
29 import java.util.List;
30 
31 /** Holder for direct share targets in the {@link ChooserGridAdapter}. */
32 public class DirectShareViewHolder extends ItemGroupViewHolder {
33     private final ViewGroup mParent;
34     private final List<ViewGroup> mRows;
35     private final int mCellCountPerRow;
36 
37     private int mDirectShareMinHeight = 0;
38     private int mDirectShareCurrHeight = 0;
39 
40     private final boolean[] mCellVisibility;
41 
DirectShareViewHolder( ViewGroup parent, List<ViewGroup> rows, int cellCountPerRow, int viewType)42     public DirectShareViewHolder(
43             ViewGroup parent,
44             List<ViewGroup> rows,
45             int cellCountPerRow,
46             int viewType) {
47         super(rows.size() * cellCountPerRow, parent, viewType);
48 
49         this.mParent = parent;
50         this.mRows = rows;
51         this.mCellCountPerRow = cellCountPerRow;
52         this.mCellVisibility = new boolean[rows.size() * cellCountPerRow];
53         Arrays.fill(mCellVisibility, true);
54     }
55 
addView(int index, View v)56     public ViewGroup addView(int index, View v) {
57         ViewGroup row = getRowByIndex(index);
58         row.addView(v);
59         mCells[index] = v;
60 
61         return row;
62     }
63 
getViewGroup()64     public ViewGroup getViewGroup() {
65         return mParent;
66     }
67 
getRowByIndex(int index)68     public ViewGroup getRowByIndex(int index) {
69         return mRows.get(index / mCellCountPerRow);
70     }
71 
getRow(int rowNumber)72     public ViewGroup getRow(int rowNumber) {
73         return mRows.get(rowNumber);
74     }
75 
measure()76     public void measure() {
77         final int spec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
78         getRow(0).measure(spec, spec);
79         getRow(1).measure(spec, spec);
80 
81         mDirectShareMinHeight = getRow(0).getMeasuredHeight();
82         mDirectShareCurrHeight = (mDirectShareCurrHeight > 0)
83                 ? mDirectShareCurrHeight : mDirectShareMinHeight;
84     }
85 
getMeasuredRowHeight()86     public int getMeasuredRowHeight() {
87         return mDirectShareCurrHeight;
88     }
89 
getMinRowHeight()90     public int getMinRowHeight() {
91         return mDirectShareMinHeight;
92     }
93 
setViewVisibility(int i, int visibility)94     public void setViewVisibility(int i, int visibility) {
95         final View v = getView(i);
96         if (visibility == View.VISIBLE) {
97             mCellVisibility[i] = true;
98             v.setVisibility(visibility);
99             v.setAlpha(1.0f);
100         } else if (visibility == View.INVISIBLE && mCellVisibility[i]) {
101             mCellVisibility[i] = false;
102 
103             ValueAnimator fadeAnim = ObjectAnimator.ofFloat(v, "alpha", 1.0f, 0f);
104             fadeAnim.setDuration(ChooserGridAdapter.NO_DIRECT_SHARE_ANIM_IN_MILLIS);
105             fadeAnim.setInterpolator(new AccelerateInterpolator(1.0f));
106             fadeAnim.addListener(new AnimatorListenerAdapter() {
107                 public void onAnimationEnd(Animator animation) {
108                     v.setVisibility(View.INVISIBLE);
109                 }
110             });
111             fadeAnim.start();
112         }
113     }
114 }
115