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