OSDN Git Service

Merge changes from topic 'TrustCert' into nyc-dev
[android-x86/packages-apps-Settings.git] / src / com / android / settings / PreviewPagerAdapter.java
1 /*
2  * Copyright (C) 2016 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.settings;
17
18 import android.animation.Animator;
19 import android.animation.Animator.AnimatorListener;
20 import android.content.Context;
21 import android.content.res.Configuration;
22 import android.support.v4.view.PagerAdapter;
23 import android.view.LayoutInflater;
24 import android.view.View;
25 import android.view.ViewGroup;
26 import android.view.animation.AccelerateInterpolator;
27 import android.view.animation.DecelerateInterpolator;
28 import android.view.animation.Interpolator;
29 import android.widget.FrameLayout;
30 import android.widget.LinearLayout;
31 import android.widget.ScrollView;
32
33 /**
34  * A PagerAdapter used by PreviewSeekBarPreferenceFragment that for showing multiple preview screen
35  * regarding a single setting and allowing the user to swipe across them.
36  */
37 public class PreviewPagerAdapter extends PagerAdapter {
38
39     /** Duration to use when cross-fading between previews. */
40     private static final long CROSS_FADE_DURATION_MS = 400;
41
42     /** Interpolator to use when cross-fading between previews. */
43     private static final Interpolator FADE_IN_INTERPOLATOR = new DecelerateInterpolator();
44
45     /** Interpolator to use when cross-fading between previews. */
46     private static final Interpolator FADE_OUT_INTERPOLATOR = new AccelerateInterpolator();
47
48     private final FrameLayout[] mPreviewFrames;
49
50     private Runnable mAnimationEndAction;
51
52     private int mAnimationCounter;
53
54     public PreviewPagerAdapter(Context context, int[] previewSampleResIds,
55                                Configuration[] configurations) {
56         mPreviewFrames = new FrameLayout[previewSampleResIds.length];
57
58         for (int i = 0; i < previewSampleResIds.length; ++i) {
59             mPreviewFrames[i] = new FrameLayout(context);
60             mPreviewFrames[i].setLayoutParams(new LinearLayout.LayoutParams(
61                     LinearLayout.LayoutParams.MATCH_PARENT,
62                     LinearLayout.LayoutParams.MATCH_PARENT));
63
64             for (Configuration configuration : configurations) {
65                 // Create a new configuration for the specified value. It won't
66                 // have any theme set, so manually apply the current theme.
67                 final Context configContext = context.createConfigurationContext(configuration);
68                 configContext.setTheme(context.getThemeResId());
69
70                 final LayoutInflater configInflater = LayoutInflater.from(configContext);
71                 final View sampleView = configInflater.inflate(previewSampleResIds[i],
72                         mPreviewFrames[i], false);
73                 sampleView.setAlpha(0);
74                 sampleView.setVisibility(View.INVISIBLE);
75                 mPreviewFrames[i].addView(sampleView);
76             }
77         }
78     }
79
80     @Override
81     public void destroyItem (ViewGroup container, int position, Object object) {
82         container.removeView((View) object);
83     }
84
85     @Override
86     public int getCount() {
87         return mPreviewFrames.length;
88     }
89
90     @Override
91     public Object instantiateItem(ViewGroup container, int position) {
92         container.addView(mPreviewFrames[position]);
93         return mPreviewFrames[position];
94     }
95
96     @Override
97     public boolean isViewFromObject(View view, Object object) {
98         return (view == object);
99     }
100
101     boolean isAnimating() {
102         return mAnimationCounter > 0;
103     }
104
105     void setAnimationEndAction(Runnable action) {
106         mAnimationEndAction = action;
107     }
108
109     void setPreviewLayer(int newIndex, int currentIndex, int currentItem, boolean animate) {
110         for (FrameLayout previewFrame : mPreviewFrames) {
111             if (currentIndex >= 0) {
112                 final View lastLayer = previewFrame.getChildAt(currentIndex);
113                 if (animate && previewFrame == mPreviewFrames[currentItem]) {
114                     lastLayer.animate()
115                             .alpha(0)
116                             .setInterpolator(FADE_OUT_INTERPOLATOR)
117                             .setDuration(CROSS_FADE_DURATION_MS)
118                             .setListener(new PreviewFrameAnimatorListener())
119                             .withEndAction(new Runnable() {
120                                 @Override
121                                 public void run() {
122                                     lastLayer.setVisibility(View.INVISIBLE);
123                                 }
124                             });
125                 } else {
126                     lastLayer.setAlpha(0);
127                     lastLayer.setVisibility(View.INVISIBLE);
128                 }
129             }
130
131             final View nextLayer = previewFrame.getChildAt(newIndex);
132             if (animate && previewFrame == mPreviewFrames[currentItem]) {
133                 nextLayer.animate()
134                         .alpha(1)
135                         .setInterpolator(FADE_IN_INTERPOLATOR)
136                         .setDuration(CROSS_FADE_DURATION_MS)
137                         .setListener(new PreviewFrameAnimatorListener())
138                         .withStartAction(new Runnable() {
139                             @Override
140                             public void run() {
141                                 nextLayer.setVisibility(View.VISIBLE);
142                             }
143                         });
144             } else {
145                 nextLayer.setVisibility(View.VISIBLE);
146                 nextLayer.setAlpha(1);
147             }
148         }
149     }
150
151     private void runAnimationEndAction() {
152         if (mAnimationEndAction != null && !isAnimating()) {
153             mAnimationEndAction.run();
154             mAnimationEndAction = null;
155         }
156     }
157
158     private class PreviewFrameAnimatorListener implements AnimatorListener {
159         @Override
160         public void onAnimationStart(Animator animation) {
161             mAnimationCounter++;
162         }
163
164         @Override
165         public void onAnimationEnd(Animator animation) {
166             mAnimationCounter--;
167             runAnimationEndAction();
168         }
169
170         @Override
171         public void onAnimationCancel(Animator animation) {
172             // Empty method.
173         }
174
175         @Override
176         public void onAnimationRepeat(Animator animation) {
177             // Empty method.
178         }
179     }
180 }