OSDN Git Service

Merge "Avoid checking the restricted items in the list." 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.content.Context;
19 import android.content.res.Configuration;
20 import android.support.v4.view.PagerAdapter;
21 import android.view.LayoutInflater;
22 import android.view.View;
23 import android.view.ViewGroup;
24 import android.view.animation.AccelerateInterpolator;
25 import android.view.animation.DecelerateInterpolator;
26 import android.view.animation.Interpolator;
27 import android.widget.FrameLayout;
28
29 /**
30  * A PagerAdapter used by PreviewSeekBarPreferenceFragment that for showing multiple preview screen
31  * regarding a single setting and allowing the user to swipe across them.
32  */
33 public class PreviewPagerAdapter extends PagerAdapter {
34
35     private TouchBlockingFrameLayout[] mPreviewFrames;
36
37     /** Duration to use when cross-fading between previews. */
38     private static final long CROSS_FADE_DURATION_MS = 400;
39
40     /** Interpolator to use when cross-fading between previews. */
41     private static final Interpolator FADE_IN_INTERPOLATOR = new DecelerateInterpolator();
42
43     /** Interpolator to use when cross-fading between previews. */
44     private static final Interpolator FADE_OUT_INTERPOLATOR = new AccelerateInterpolator();
45
46     public PreviewPagerAdapter(Context context, int[] previewSampleResIds,
47                                Configuration[] configurations) {
48         mPreviewFrames = new TouchBlockingFrameLayout[previewSampleResIds.length];
49
50         for (int i = 0; i < previewSampleResIds.length; ++i) {
51             mPreviewFrames[i] = (TouchBlockingFrameLayout) LayoutInflater.from(context)
52                     .inflate(R.layout.preview_frame_container, null);
53             mPreviewFrames[i].setContentDescription(
54                     context.getString(R.string.preview_page_indicator_content_description, i + 1,
55                             previewSampleResIds.length));
56
57             for (Configuration configuration : configurations) {
58                 // Create a new configuration for the specified value. It won't
59                 // have any theme set, so manually apply the current theme.
60                 final Context configContext = context.createConfigurationContext(configuration);
61                 configContext.setTheme(context.getThemeResId());
62
63                 final LayoutInflater configInflater = LayoutInflater.from(configContext);
64                 final View sampleView = configInflater.inflate(previewSampleResIds[i],
65                         mPreviewFrames[i], false);
66                 sampleView.setAlpha(0);
67                 sampleView.setVisibility(View.INVISIBLE);
68
69                 mPreviewFrames[i].addView(sampleView);
70             }
71         }
72     }
73
74     @Override
75     public void destroyItem (ViewGroup container, int position, Object object) {
76         container.removeView((View) object);
77     }
78
79     @Override
80     public int getCount() {
81         return mPreviewFrames.length;
82     }
83
84     @Override
85     public Object instantiateItem(ViewGroup container, int position) {
86         container.addView(mPreviewFrames[position]);
87         return mPreviewFrames[position];
88     }
89
90     @Override
91     public boolean isViewFromObject(View view, Object object) {
92         return (view == object);
93     }
94
95     void setPreviewLayer(int newIndex, int currentIndex, int currentItem, boolean animate) {
96         for (FrameLayout previewFrame : mPreviewFrames) {
97             if (currentIndex >= 0) {
98                 final View lastLayer = previewFrame.getChildAt(currentIndex);
99                 if (animate && previewFrame == mPreviewFrames[currentItem]) {
100                     lastLayer.animate()
101                             .alpha(0)
102                             .setInterpolator(FADE_OUT_INTERPOLATOR)
103                             .setDuration(CROSS_FADE_DURATION_MS)
104                             .withEndAction(new Runnable() {
105                                 @Override
106                                 public void run() {
107                                     lastLayer.setVisibility(View.INVISIBLE);
108                                 }
109                             });
110                 } else {
111                     lastLayer.setAlpha(0);
112                     lastLayer.setVisibility(View.INVISIBLE);
113                 }
114             }
115
116             final View nextLayer = previewFrame.getChildAt(newIndex);
117             if (animate && previewFrame == mPreviewFrames[currentItem]) {
118                 nextLayer.animate()
119                         .alpha(1)
120                         .setInterpolator(FADE_IN_INTERPOLATOR)
121                         .setDuration(CROSS_FADE_DURATION_MS)
122                         .withStartAction(new Runnable() {
123                             @Override
124                             public void run() {
125                                 nextLayer.setVisibility(View.VISIBLE);
126                             }
127                         });
128             } else {
129                 nextLayer.setVisibility(View.VISIBLE);
130                 nextLayer.setAlpha(1);
131             }
132         }
133     }
134 }