OnboardingDemoFragment.java revision 54746b4f93cf6de92c8f69fdc415ea2dff8083c2
1e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee/*
2e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee * Copyright (C) 2014 The Android Open Source Project
3e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee *
4e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee * in compliance with the License. You may obtain a copy of the License at
6e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee *
7e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee * http://www.apache.org/licenses/LICENSE-2.0
8e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee *
9e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee * Unless required by applicable law or agreed to in writing, software distributed under the License
10e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee * or implied. See the License for the specific language governing permissions and limitations under
12e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee * the License.
13e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee */
14e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Leepackage com.example.android.leanback;
15e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee
16e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Leeimport android.animation.Animator;
17e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Leeimport android.animation.AnimatorListenerAdapter;
18e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Leeimport android.animation.AnimatorSet;
19e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Leeimport android.animation.ObjectAnimator;
2054746b4f93cf6de92c8f69fdc415ea2dff8083c2Chulwoo Leeimport android.graphics.drawable.AnimationDrawable;
21e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Leeimport android.support.v17.leanback.app.OnboardingFragment;
22e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Leeimport android.view.LayoutInflater;
23e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Leeimport android.view.View;
24e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Leeimport android.view.ViewGroup;
25e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Leeimport android.widget.ImageView;
26e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee
27e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Leeimport java.util.ArrayList;
28e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee
29e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Leepublic class OnboardingDemoFragment extends OnboardingFragment {
30e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    private static final long ANIMATION_DURATION = 1000;
31e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee
3254746b4f93cf6de92c8f69fdc415ea2dff8083c2Chulwoo Lee    private static final int[] CONTENT_BACKGROUNDS = {
3354746b4f93cf6de92c8f69fdc415ea2dff8083c2Chulwoo Lee            R.drawable.tv_bg,
3454746b4f93cf6de92c8f69fdc415ea2dff8083c2Chulwoo Lee            R.drawable.gallery_photo_6,
3554746b4f93cf6de92c8f69fdc415ea2dff8083c2Chulwoo Lee            R.drawable.gallery_photo_8
36e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    };
3754746b4f93cf6de92c8f69fdc415ea2dff8083c2Chulwoo Lee
3854746b4f93cf6de92c8f69fdc415ea2dff8083c2Chulwoo Lee    private static final int[] CONTENT_ANIMATIONS = {
3954746b4f93cf6de92c8f69fdc415ea2dff8083c2Chulwoo Lee            R.drawable.tv_content,
4054746b4f93cf6de92c8f69fdc415ea2dff8083c2Chulwoo Lee            android.R.drawable.stat_sys_download,
4154746b4f93cf6de92c8f69fdc415ea2dff8083c2Chulwoo Lee            android.R.drawable.ic_popup_sync
4254746b4f93cf6de92c8f69fdc415ea2dff8083c2Chulwoo Lee    };
4354746b4f93cf6de92c8f69fdc415ea2dff8083c2Chulwoo Lee
44e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    private String[] mTitles;
45e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    private String[] mDescriptions;
46e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee
47e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    private View mBackgroundView;
4854746b4f93cf6de92c8f69fdc415ea2dff8083c2Chulwoo Lee    private View mContentView;
4954746b4f93cf6de92c8f69fdc415ea2dff8083c2Chulwoo Lee    private ImageView mContentBackgroundView;
5054746b4f93cf6de92c8f69fdc415ea2dff8083c2Chulwoo Lee    private ImageView mContentAnimationView;
51e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee
52e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    private Animator mContentAnimator;
53e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee
54e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    @Override
55e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    public void onAttach(android.app.Activity activity) {
56e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        super.onAttach(activity);
57e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        mTitles = getResources().getStringArray(R.array.onboarding_page_titles);
58e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        mDescriptions = getResources().getStringArray(R.array.onboarding_page_descriptions);
59e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    }
60e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee
61e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    @Override
62e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    protected int getPageCount() {
63e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        return mTitles.length;
64e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    }
65e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee
66e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    @Override
67e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    protected CharSequence getPageTitle(int i) {
68e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        return mTitles[i];
69e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    }
70e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee
71e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    @Override
72e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    protected CharSequence getPageDescription(int i) {
73e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        return mDescriptions[i];
74e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    }
75e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee
76e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    @Override
77e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    protected View onCreateBackgroundView(LayoutInflater layoutInflater, ViewGroup viewGroup) {
78e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        mBackgroundView = layoutInflater.inflate(R.layout.onboarding_image, viewGroup, false);
79e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        return mBackgroundView;
80e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    }
81e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee
82e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    @Override
83e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    protected View onCreateContentView(LayoutInflater layoutInflater, ViewGroup viewGroup) {
8454746b4f93cf6de92c8f69fdc415ea2dff8083c2Chulwoo Lee        mContentView = layoutInflater.inflate(R.layout.onboarding_content, viewGroup, false);
8554746b4f93cf6de92c8f69fdc415ea2dff8083c2Chulwoo Lee        mContentBackgroundView = (ImageView) mContentView.findViewById(R.id.background_image);
8654746b4f93cf6de92c8f69fdc415ea2dff8083c2Chulwoo Lee        mContentAnimationView = (ImageView) mContentView.findViewById(R.id.animation_image);
87e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        return mContentView;
88e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    }
89e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee
90e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    @Override
91e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    protected View onCreateForegroundView(LayoutInflater layoutInflater, ViewGroup viewGroup) {
92e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        return null;
93e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    }
94e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee
95e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    @Override
96e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    protected Animator onCreateEnterAnimation() {
97e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        ArrayList<Animator> animators = new ArrayList<>();
98e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        animators.add(createFadeInAnimator(mBackgroundView));
9954746b4f93cf6de92c8f69fdc415ea2dff8083c2Chulwoo Lee        mContentBackgroundView.setImageResource(CONTENT_BACKGROUNDS[0]);
10054746b4f93cf6de92c8f69fdc415ea2dff8083c2Chulwoo Lee        mContentAnimationView.setImageResource(CONTENT_ANIMATIONS[0]);
101e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        mContentAnimator = createFadeInAnimator(mContentView);
102e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        animators.add(mContentAnimator);
103e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        AnimatorSet set = new AnimatorSet();
104e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        set.playTogether(animators);
10554746b4f93cf6de92c8f69fdc415ea2dff8083c2Chulwoo Lee        set.addListener(new AnimatorListenerAdapter() {
10654746b4f93cf6de92c8f69fdc415ea2dff8083c2Chulwoo Lee            @Override
10754746b4f93cf6de92c8f69fdc415ea2dff8083c2Chulwoo Lee            public void onAnimationEnd(Animator animation) {
10854746b4f93cf6de92c8f69fdc415ea2dff8083c2Chulwoo Lee                ((AnimationDrawable) mContentAnimationView.getDrawable()).start();
10954746b4f93cf6de92c8f69fdc415ea2dff8083c2Chulwoo Lee            }
11054746b4f93cf6de92c8f69fdc415ea2dff8083c2Chulwoo Lee        });
111e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        return set;
112e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    }
113e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee
114e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    @Override
115e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    protected void onPageChanged(final int newPage, int previousPage) {
116e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        if (mContentAnimator != null) {
11754746b4f93cf6de92c8f69fdc415ea2dff8083c2Chulwoo Lee            mContentAnimator.cancel();
118e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        }
11954746b4f93cf6de92c8f69fdc415ea2dff8083c2Chulwoo Lee        ((AnimationDrawable) mContentAnimationView.getDrawable()).stop();
120e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        ArrayList<Animator> animators = new ArrayList<>();
121e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        Animator fadeOut = createFadeOutAnimator(mContentView);
122e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        fadeOut.addListener(new AnimatorListenerAdapter() {
123e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee            @Override
124e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee            public void onAnimationEnd(Animator animation) {
12554746b4f93cf6de92c8f69fdc415ea2dff8083c2Chulwoo Lee                mContentBackgroundView.setImageResource(CONTENT_BACKGROUNDS[newPage]);
12654746b4f93cf6de92c8f69fdc415ea2dff8083c2Chulwoo Lee                mContentAnimationView.setImageResource(CONTENT_ANIMATIONS[newPage]);
12754746b4f93cf6de92c8f69fdc415ea2dff8083c2Chulwoo Lee            }
12854746b4f93cf6de92c8f69fdc415ea2dff8083c2Chulwoo Lee        });
12954746b4f93cf6de92c8f69fdc415ea2dff8083c2Chulwoo Lee        Animator fadeIn = createFadeInAnimator(mContentView);
13054746b4f93cf6de92c8f69fdc415ea2dff8083c2Chulwoo Lee        fadeIn.addListener(new AnimatorListenerAdapter() {
13154746b4f93cf6de92c8f69fdc415ea2dff8083c2Chulwoo Lee            @Override
13254746b4f93cf6de92c8f69fdc415ea2dff8083c2Chulwoo Lee            public void onAnimationEnd(Animator animation) {
13354746b4f93cf6de92c8f69fdc415ea2dff8083c2Chulwoo Lee                ((AnimationDrawable) mContentAnimationView.getDrawable()).start();
134e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee            }
135e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        });
136e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        AnimatorSet set = new AnimatorSet();
13754746b4f93cf6de92c8f69fdc415ea2dff8083c2Chulwoo Lee        set.playSequentially(fadeOut, fadeIn);
138e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        set.start();
139e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        mContentAnimator = set;
140e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    }
141e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee
142e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    private Animator createFadeInAnimator(View view) {
143e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        return ObjectAnimator.ofFloat(view, View.ALPHA, 0.0f, 1.0f).setDuration(ANIMATION_DURATION);
144e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    }
145e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee
146e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    private Animator createFadeOutAnimator(View view) {
147e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        return ObjectAnimator.ofFloat(view, View.ALPHA, 1.0f, 0.0f).setDuration(ANIMATION_DURATION);
148e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    }
149e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee
150e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    @Override
151e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    protected void onFinishFragment() {
152e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        getActivity().finish();
153e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    }
154e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee}
155