ronnie
2022-10-23 eadd9db01b24ccde96a129dafa989d4ec436cdfd
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
/*
 * Copyright (C) 2018 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.settings.display;
 
import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
import android.content.Context;
import android.content.res.Configuration;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStub;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Interpolator;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
 
import androidx.viewpager.widget.PagerAdapter;
 
/**
 * A PagerAdapter used by PreviewSeekBarPreferenceFragment that for showing multiple preview screen
 * regarding a single setting and allowing the user to swipe across them.
 */
public class PreviewPagerAdapter extends PagerAdapter {
 
    /** Duration to use when cross-fading between previews. */
    private static final long CROSS_FADE_DURATION_MS = 400;
 
    /** Interpolator to use when cross-fading between previews. */
    private static final Interpolator FADE_IN_INTERPOLATOR = new DecelerateInterpolator();
 
    /** Interpolator to use when cross-fading between previews. */
    private static final Interpolator FADE_OUT_INTERPOLATOR = new AccelerateInterpolator();
 
    private FrameLayout[] mPreviewFrames;
 
    private boolean mIsLayoutRtl;
 
    private Runnable mAnimationEndAction;
 
    private int mAnimationCounter;
 
    private boolean[][] mViewStubInflated;
 
    public PreviewPagerAdapter(Context context, boolean isLayoutRtl,
            int[] previewSampleResIds, Configuration[] configurations) {
        mIsLayoutRtl = isLayoutRtl;
        mPreviewFrames = new FrameLayout[previewSampleResIds.length];
        mViewStubInflated = new boolean[previewSampleResIds.length][configurations.length];
 
        for (int i = 0; i < previewSampleResIds.length; ++i) {
            int p = mIsLayoutRtl ? previewSampleResIds.length - 1 - i : i;
            mPreviewFrames[p] = new FrameLayout(context);
            mPreviewFrames[p].setLayoutParams(new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.MATCH_PARENT));
            mPreviewFrames[p].setClipToPadding(true);
            mPreviewFrames[p].setClipChildren(true);
            for (int j = 0; j < configurations.length; ++j) {
                // Create a new configuration for the specified value. It won't
                // have any theme set, so manually apply the current theme.
                final Context configContext = context.createConfigurationContext(configurations[j]);
                configContext.getTheme().setTo(context.getTheme());
 
                final ViewStub sampleViewStub = new ViewStub(configContext);
                sampleViewStub.setLayoutResource(previewSampleResIds[i]);
                final int fi = i, fj = j;
                sampleViewStub.setOnInflateListener((stub, inflated) -> {
                    inflated.setVisibility(stub.getVisibility());
                    mViewStubInflated[fi][fj] = true;
                });
 
                mPreviewFrames[p].addView(sampleViewStub);
            }
        }
    }
 
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }
 
    @Override
    public int getCount() {
        return mPreviewFrames.length;
    }
 
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        container.addView(mPreviewFrames[position]);
        return mPreviewFrames[position];
    }
 
    @Override
    public boolean isViewFromObject(View view, Object object) {
        return (view == object);
    }
 
    boolean isAnimating() {
        return mAnimationCounter > 0;
    }
 
    void setAnimationEndAction(Runnable action) {
        mAnimationEndAction = action;
    }
 
    void setPreviewLayer(int newLayerIndex, int currentLayerIndex, int currentFrameIndex,
            final boolean animate) {
        for (FrameLayout previewFrame : mPreviewFrames) {
            if (currentLayerIndex >= 0) {
                final View lastLayer = previewFrame.getChildAt(currentLayerIndex);
                if (mViewStubInflated[currentFrameIndex][currentLayerIndex]) {
                    // Explicitly set to INVISIBLE only when the stub has
                    // already been inflated.
                    if (previewFrame == mPreviewFrames[currentFrameIndex]) {
                        setVisibility(lastLayer, View.INVISIBLE, animate);
                    } else {
                        setVisibility(lastLayer, View.INVISIBLE, false);
                    }
                }
            }
 
            // Set next layer visible, as well as inflate necessary views.
            View nextLayer = previewFrame.getChildAt(newLayerIndex);
            if (previewFrame == mPreviewFrames[currentFrameIndex]) {
                // Inflate immediately if the stub has not yet been inflated.
                if (!mViewStubInflated[currentFrameIndex][newLayerIndex]) {
                    nextLayer = ((ViewStub) nextLayer).inflate();
                    nextLayer.setAlpha(0.0f);
                }
                setVisibility(nextLayer, View.VISIBLE, animate);
            } else {
                setVisibility(nextLayer, View.VISIBLE, false);
            }
        }
    }
 
    private void setVisibility(final View view, final int visibility, boolean animate) {
        final float alpha = (visibility == View.VISIBLE ? 1.0f : 0.0f);
        if (!animate) {
            view.setAlpha(alpha);
            view.setVisibility(visibility);
        } else {
            final Interpolator interpolator = (visibility == View.VISIBLE ? FADE_IN_INTERPOLATOR
                    : FADE_OUT_INTERPOLATOR);
            if (visibility == View.VISIBLE) {
                // Fade in animation.
                view.animate()
                        .alpha(alpha)
                        .setInterpolator(FADE_IN_INTERPOLATOR)
                        .setDuration(CROSS_FADE_DURATION_MS)
                        .setListener(new PreviewFrameAnimatorListener())
                        .withStartAction(new Runnable() {
                            @Override
                            public void run() {
                                view.setVisibility(visibility);
                            }
                        });
            } else {
                // Fade out animation.
                view.animate()
                        .alpha(alpha)
                        .setInterpolator(FADE_OUT_INTERPOLATOR)
                        .setDuration(CROSS_FADE_DURATION_MS)
                        .setListener(new PreviewFrameAnimatorListener())
                        .withEndAction(new Runnable() {
                            @Override
                            public void run() {
                                view.setVisibility(visibility);
                            }
                        });
            }
        }
    }
 
    private void runAnimationEndAction() {
        if (mAnimationEndAction != null && !isAnimating()) {
            mAnimationEndAction.run();
            mAnimationEndAction = null;
        }
    }
 
    private class PreviewFrameAnimatorListener implements AnimatorListener {
        @Override
        public void onAnimationStart(Animator animation) {
            mAnimationCounter++;
        }
 
        @Override
        public void onAnimationEnd(Animator animation) {
            mAnimationCounter--;
            runAnimationEndAction();
        }
 
        @Override
        public void onAnimationCancel(Animator animation) {
            // Empty method.
        }
 
        @Override
        public void onAnimationRepeat(Animator animation) {
            // Empty method.
        }
    }
}