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.view.LayoutInflater;
25import android.view.View;
26import android.view.ViewGroup;
27import android.widget.ImageView;
28
29import androidx.leanback.app.OnboardingSupportFragment;
30
31import java.util.ArrayList;
32
33public class OnboardingDemoSupportFragment extends OnboardingSupportFragment {
34    private static final long ANIMATION_DURATION = 1000;
35
36    private static final int[] CONTENT_BACKGROUNDS = {
37            R.drawable.tv_bg,
38            R.drawable.gallery_photo_6,
39            R.drawable.gallery_photo_8
40    };
41
42    private static final int[] CONTENT_ANIMATIONS = {
43            R.drawable.tv_content,
44            android.R.drawable.stat_sys_download,
45            android.R.drawable.ic_popup_sync
46    };
47
48    private String[] mTitles;
49    private String[] mDescriptions;
50
51    private View mBackgroundView;
52    private View mContentView;
53    private ImageView mContentBackgroundView;
54    private ImageView mContentAnimationView;
55
56    private Animator mContentAnimator;
57
58    @SuppressWarnings("deprecation")
59    @Override
60    public void onAttach(android.app.Activity activity) {
61        super.onAttach(activity);
62        mTitles = getResources().getStringArray(R.array.onboarding_page_titles);
63        mDescriptions = getResources().getStringArray(R.array.onboarding_page_descriptions);
64        setLogoResourceId(R.drawable.ic_launcher);
65    }
66
67    @Override
68    protected int getPageCount() {
69        return mTitles.length;
70    }
71
72    @Override
73    protected CharSequence getPageTitle(int i) {
74        return mTitles[i];
75    }
76
77    @Override
78    protected CharSequence getPageDescription(int i) {
79        return mDescriptions[i];
80    }
81
82    @Override
83    protected View onCreateBackgroundView(LayoutInflater layoutInflater, ViewGroup viewGroup) {
84        mBackgroundView = layoutInflater.inflate(R.layout.onboarding_image, viewGroup, false);
85        return mBackgroundView;
86    }
87
88    @Override
89    protected View onCreateContentView(LayoutInflater layoutInflater, ViewGroup viewGroup) {
90        mContentView = layoutInflater.inflate(R.layout.onboarding_content, viewGroup, false);
91        mContentBackgroundView = (ImageView) mContentView.findViewById(R.id.background_image);
92        mContentAnimationView = (ImageView) mContentView.findViewById(R.id.animation_image);
93        return mContentView;
94    }
95
96    @Override
97    protected View onCreateForegroundView(LayoutInflater layoutInflater, ViewGroup viewGroup) {
98        return null;
99    }
100
101    @Override
102    protected Animator onCreateEnterAnimation() {
103        ArrayList<Animator> animators = new ArrayList<>();
104        animators.add(createFadeInAnimator(mBackgroundView));
105        mContentBackgroundView.setImageResource(CONTENT_BACKGROUNDS[0]);
106        mContentAnimationView.setImageResource(CONTENT_ANIMATIONS[0]);
107        mContentAnimator = createFadeInAnimator(mContentView);
108        animators.add(mContentAnimator);
109        AnimatorSet set = new AnimatorSet();
110        set.playTogether(animators);
111        set.addListener(new AnimatorListenerAdapter() {
112            @Override
113            public void onAnimationEnd(Animator animation) {
114                ((AnimationDrawable) mContentAnimationView.getDrawable()).start();
115            }
116        });
117        return set;
118    }
119
120    @Override
121    protected void onPageChanged(final int newPage, int previousPage) {
122        if (mContentAnimator != null) {
123            mContentAnimator.cancel();
124        }
125        ((AnimationDrawable) mContentAnimationView.getDrawable()).stop();
126        ArrayList<Animator> animators = new ArrayList<>();
127        Animator fadeOut = createFadeOutAnimator(mContentView);
128        fadeOut.addListener(new AnimatorListenerAdapter() {
129            @Override
130            public void onAnimationEnd(Animator animation) {
131                mContentBackgroundView.setImageResource(CONTENT_BACKGROUNDS[newPage]);
132                mContentAnimationView.setImageResource(CONTENT_ANIMATIONS[newPage]);
133            }
134        });
135        Animator fadeIn = createFadeInAnimator(mContentView);
136        fadeIn.addListener(new AnimatorListenerAdapter() {
137            @Override
138            public void onAnimationEnd(Animator animation) {
139                ((AnimationDrawable) mContentAnimationView.getDrawable()).start();
140            }
141        });
142        AnimatorSet set = new AnimatorSet();
143        set.playSequentially(fadeOut, fadeIn);
144        set.start();
145        mContentAnimator = set;
146    }
147
148    private Animator createFadeInAnimator(View view) {
149        return ObjectAnimator.ofFloat(view, View.ALPHA, 0.0f, 1.0f).setDuration(ANIMATION_DURATION);
150    }
151
152    private Animator createFadeOutAnimator(View view) {
153        return ObjectAnimator.ofFloat(view, View.ALPHA, 1.0f, 0.0f).setDuration(ANIMATION_DURATION);
154    }
155
156    @Override
157    protected void onFinishFragment() {
158        getActivity().finish();
159    }
160}
161