1// CHECKSTYLE:OFF Generated code
2/* This file is auto-generated from OnboardingDemoFragment.java.  DO NOT MODIFY. */
3
4/*
5 * Copyright (C) 2014 The Android Open Source Project
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software distributed under the License
13 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14 * or implied. See the License for the specific language governing permissions and limitations under
15 * the License.
16 */
17package com.example.android.leanback;
18
19import android.animation.Animator;
20import android.animation.AnimatorListenerAdapter;
21import android.animation.AnimatorSet;
22import android.animation.ObjectAnimator;
23import android.graphics.drawable.AnimationDrawable;
24import android.support.v17.leanback.app.OnboardingSupportFragment;
25import android.view.LayoutInflater;
26import android.view.View;
27import android.view.ViewGroup;
28import android.widget.ImageView;
29
30import java.util.ArrayList;
31
32public class OnboardingDemoSupportFragment extends OnboardingSupportFragment {
33    private static final long ANIMATION_DURATION = 1000;
34
35    private static final int[] CONTENT_BACKGROUNDS = {
36            R.drawable.tv_bg,
37            R.drawable.gallery_photo_6,
38            R.drawable.gallery_photo_8
39    };
40
41    private static final int[] CONTENT_ANIMATIONS = {
42            R.drawable.tv_content,
43            android.R.drawable.stat_sys_download,
44            android.R.drawable.ic_popup_sync
45    };
46
47    private String[] mTitles;
48    private String[] mDescriptions;
49
50    private View mBackgroundView;
51    private View mContentView;
52    private ImageView mContentBackgroundView;
53    private ImageView mContentAnimationView;
54
55    private Animator mContentAnimator;
56
57    @SuppressWarnings("deprecation")
58    @Override
59    public void onAttach(android.app.Activity activity) {
60        super.onAttach(activity);
61        mTitles = getResources().getStringArray(R.array.onboarding_page_titles);
62        mDescriptions = getResources().getStringArray(R.array.onboarding_page_descriptions);
63        setLogoResourceId(R.drawable.ic_launcher);
64    }
65
66    @Override
67    protected int getPageCount() {
68        return mTitles.length;
69    }
70
71    @Override
72    protected CharSequence getPageTitle(int i) {
73        return mTitles[i];
74    }
75
76    @Override
77    protected CharSequence getPageDescription(int i) {
78        return mDescriptions[i];
79    }
80
81    @Override
82    protected View onCreateBackgroundView(LayoutInflater layoutInflater, ViewGroup viewGroup) {
83        mBackgroundView = layoutInflater.inflate(R.layout.onboarding_image, viewGroup, false);
84        return mBackgroundView;
85    }
86
87    @Override
88    protected View onCreateContentView(LayoutInflater layoutInflater, ViewGroup viewGroup) {
89        mContentView = layoutInflater.inflate(R.layout.onboarding_content, viewGroup, false);
90        mContentBackgroundView = (ImageView) mContentView.findViewById(R.id.background_image);
91        mContentAnimationView = (ImageView) mContentView.findViewById(R.id.animation_image);
92        return mContentView;
93    }
94
95    @Override
96    protected View onCreateForegroundView(LayoutInflater layoutInflater, ViewGroup viewGroup) {
97        return null;
98    }
99
100    @Override
101    protected Animator onCreateEnterAnimation() {
102        ArrayList<Animator> animators = new ArrayList<>();
103        animators.add(createFadeInAnimator(mBackgroundView));
104        mContentBackgroundView.setImageResource(CONTENT_BACKGROUNDS[0]);
105        mContentAnimationView.setImageResource(CONTENT_ANIMATIONS[0]);
106        mContentAnimator = createFadeInAnimator(mContentView);
107        animators.add(mContentAnimator);
108        AnimatorSet set = new AnimatorSet();
109        set.playTogether(animators);
110        set.addListener(new AnimatorListenerAdapter() {
111            @Override
112            public void onAnimationEnd(Animator animation) {
113                ((AnimationDrawable) mContentAnimationView.getDrawable()).start();
114            }
115        });
116        return set;
117    }
118
119    @Override
120    protected void onPageChanged(final int newPage, int previousPage) {
121        if (mContentAnimator != null) {
122            mContentAnimator.cancel();
123        }
124        ((AnimationDrawable) mContentAnimationView.getDrawable()).stop();
125        ArrayList<Animator> animators = new ArrayList<>();
126        Animator fadeOut = createFadeOutAnimator(mContentView);
127        fadeOut.addListener(new AnimatorListenerAdapter() {
128            @Override
129            public void onAnimationEnd(Animator animation) {
130                mContentBackgroundView.setImageResource(CONTENT_BACKGROUNDS[newPage]);
131                mContentAnimationView.setImageResource(CONTENT_ANIMATIONS[newPage]);
132            }
133        });
134        Animator fadeIn = createFadeInAnimator(mContentView);
135        fadeIn.addListener(new AnimatorListenerAdapter() {
136            @Override
137            public void onAnimationEnd(Animator animation) {
138                ((AnimationDrawable) mContentAnimationView.getDrawable()).start();
139            }
140        });
141        AnimatorSet set = new AnimatorSet();
142        set.playSequentially(fadeOut, fadeIn);
143        set.start();
144        mContentAnimator = set;
145    }
146
147    private Animator createFadeInAnimator(View view) {
148        return ObjectAnimator.ofFloat(view, View.ALPHA, 0.0f, 1.0f).setDuration(ANIMATION_DURATION);
149    }
150
151    private Animator createFadeOutAnimator(View view) {
152        return ObjectAnimator.ofFloat(view, View.ALPHA, 1.0f, 0.0f).setDuration(ANIMATION_DURATION);
153    }
154
155    @Override
156    protected void onFinishFragment() {
157        getActivity().finish();
158    }
159}
160