2 * Copyright (C) 2016 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package com.android.settings;
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;
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.
33 public class PreviewPagerAdapter extends PagerAdapter {
35 private TouchBlockingFrameLayout[] mPreviewFrames;
37 /** Duration to use when cross-fading between previews. */
38 private static final long CROSS_FADE_DURATION_MS = 400;
40 /** Interpolator to use when cross-fading between previews. */
41 private static final Interpolator FADE_IN_INTERPOLATOR = new DecelerateInterpolator();
43 /** Interpolator to use when cross-fading between previews. */
44 private static final Interpolator FADE_OUT_INTERPOLATOR = new AccelerateInterpolator();
46 public PreviewPagerAdapter(Context context, int[] previewSampleResIds,
47 Configuration[] configurations) {
48 mPreviewFrames = new TouchBlockingFrameLayout[previewSampleResIds.length];
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));
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());
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);
69 mPreviewFrames[i].addView(sampleView);
75 public void destroyItem (ViewGroup container, int position, Object object) {
76 container.removeView((View) object);
80 public int getCount() {
81 return mPreviewFrames.length;
85 public Object instantiateItem(ViewGroup container, int position) {
86 container.addView(mPreviewFrames[position]);
87 return mPreviewFrames[position];
91 public boolean isViewFromObject(View view, Object object) {
92 return (view == object);
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]) {
102 .setInterpolator(FADE_OUT_INTERPOLATOR)
103 .setDuration(CROSS_FADE_DURATION_MS)
104 .withEndAction(new Runnable() {
107 lastLayer.setVisibility(View.INVISIBLE);
111 lastLayer.setAlpha(0);
112 lastLayer.setVisibility(View.INVISIBLE);
116 final View nextLayer = previewFrame.getChildAt(newIndex);
117 if (animate && previewFrame == mPreviewFrames[currentItem]) {
120 .setInterpolator(FADE_IN_INTERPOLATOR)
121 .setDuration(CROSS_FADE_DURATION_MS)
122 .withStartAction(new Runnable() {
125 nextLayer.setVisibility(View.VISIBLE);
129 nextLayer.setVisibility(View.VISIBLE);
130 nextLayer.setAlpha(1);