OSDN Git Service

Merge "Fix UI issue in Sim Settings screen." 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 import android.widget.LinearLayout;
29 import android.widget.ScrollView;
30
31 /**
32  * A PagerAdapter used by PreviewSeekBarPreferenceFragment that for showing multiple preview screen
33  * regarding a single setting and allowing the user to swipe across them.
34  */
35 public class PreviewPagerAdapter extends PagerAdapter {
36
37     private FrameLayout[] mPreviewFrames;
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     public PreviewPagerAdapter(Context context, int[] previewSampleResIds,
49                                Configuration[] configurations) {
50         mPreviewFrames = new FrameLayout[previewSampleResIds.length];
51
52         for (int i = 0; i < previewSampleResIds.length; ++i) {
53             mPreviewFrames[i] = new FrameLayout(context);
54             mPreviewFrames[i].setLayoutParams(new LinearLayout.LayoutParams(
55                     LinearLayout.LayoutParams.MATCH_PARENT,
56                     LinearLayout.LayoutParams.MATCH_PARENT));
57             mPreviewFrames[i].setContentDescription(
58                     context.getString(R.string.preview_page_indicator_content_description, i + 1,
59                             previewSampleResIds.length));
60
61             for (Configuration configuration : configurations) {
62                 // Create a new configuration for the specified value. It won't
63                 // have any theme set, so manually apply the current theme.
64                 final Context configContext = context.createConfigurationContext(configuration);
65                 configContext.setTheme(context.getThemeResId());
66
67                 final LayoutInflater configInflater = LayoutInflater.from(configContext);
68                 final View sampleView = configInflater.inflate(previewSampleResIds[i],
69                         mPreviewFrames[i], false);
70                 sampleView.setAlpha(0);
71                 sampleView.setVisibility(View.INVISIBLE);
72                 mPreviewFrames[i].addView(sampleView);
73             }
74         }
75     }
76
77     @Override
78     public void destroyItem (ViewGroup container, int position, Object object) {
79         container.removeView((View) object);
80     }
81
82     @Override
83     public int getCount() {
84         return mPreviewFrames.length;
85     }
86
87     @Override
88     public Object instantiateItem(ViewGroup container, int position) {
89         container.addView(mPreviewFrames[position]);
90         return mPreviewFrames[position];
91     }
92
93     @Override
94     public boolean isViewFromObject(View view, Object object) {
95         return (view == object);
96     }
97
98     void setPreviewLayer(int newIndex, int currentIndex, int currentItem, boolean animate) {
99         for (FrameLayout previewFrame : mPreviewFrames) {
100             if (currentIndex >= 0) {
101                 final View lastLayer = previewFrame.getChildAt(currentIndex);
102                 if (animate && previewFrame == mPreviewFrames[currentItem]) {
103                     lastLayer.animate()
104                             .alpha(0)
105                             .setInterpolator(FADE_OUT_INTERPOLATOR)
106                             .setDuration(CROSS_FADE_DURATION_MS)
107                             .withEndAction(new Runnable() {
108                                 @Override
109                                 public void run() {
110                                     lastLayer.setVisibility(View.INVISIBLE);
111                                 }
112                             });
113                 } else {
114                     lastLayer.setAlpha(0);
115                     lastLayer.setVisibility(View.INVISIBLE);
116                 }
117             }
118
119             final View nextLayer = previewFrame.getChildAt(newIndex);
120             if (animate && previewFrame == mPreviewFrames[currentItem]) {
121                 nextLayer.animate()
122                         .alpha(1)
123                         .setInterpolator(FADE_IN_INTERPOLATOR)
124                         .setDuration(CROSS_FADE_DURATION_MS)
125                         .withStartAction(new Runnable() {
126                             @Override
127                             public void run() {
128                                 nextLayer.setVisibility(View.VISIBLE);
129                             }
130                         });
131             } else {
132                 nextLayer.setVisibility(View.VISIBLE);
133                 nextLayer.setAlpha(1);
134             }
135         }
136     }
137 }