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
54b7087e036a48f5a3db28d02ff7f9b97fbbc46c4fDake Gu    @SuppressWarnings("deprecation")
55e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    @Override
56e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    public void onAttach(android.app.Activity activity) {
57e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        super.onAttach(activity);
58e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        mTitles = getResources().getStringArray(R.array.onboarding_page_titles);
59e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        mDescriptions = getResources().getStringArray(R.array.onboarding_page_descriptions);
60c43a33fa69773625b09654d63a5bde79594e6f4aChulwoo Lee        setLogoResourceId(R.drawable.ic_launcher);
61e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    }
62e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee
63e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    @Override
64e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    protected int getPageCount() {
65e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        return mTitles.length;
66e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    }
67e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee
68e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    @Override
69e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    protected CharSequence getPageTitle(int i) {
70e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        return mTitles[i];
71e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    }
72e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee
73e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    @Override
74e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    protected CharSequence getPageDescription(int i) {
75e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        return mDescriptions[i];
76e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    }
77e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee
78e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    @Override
79e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    protected View onCreateBackgroundView(LayoutInflater layoutInflater, ViewGroup viewGroup) {
80e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        mBackgroundView = layoutInflater.inflate(R.layout.onboarding_image, viewGroup, false);
81e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        return mBackgroundView;
82e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    }
83e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee
84e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    @Override
85e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    protected View onCreateContentView(LayoutInflater layoutInflater, ViewGroup viewGroup) {
8654746b4f93cf6de92c8f69fdc415ea2dff8083c2Chulwoo Lee        mContentView = layoutInflater.inflate(R.layout.onboarding_content, viewGroup, false);
8754746b4f93cf6de92c8f69fdc415ea2dff8083c2Chulwoo Lee        mContentBackgroundView = (ImageView) mContentView.findViewById(R.id.background_image);
8854746b4f93cf6de92c8f69fdc415ea2dff8083c2Chulwoo Lee        mContentAnimationView = (ImageView) mContentView.findViewById(R.id.animation_image);
89e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        return mContentView;
90e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    }
91e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee
92e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    @Override
93e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    protected View onCreateForegroundView(LayoutInflater layoutInflater, ViewGroup viewGroup) {
94e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        return null;
95e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    }
96e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee
97e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    @Override
98e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    protected Animator onCreateEnterAnimation() {
99e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        ArrayList<Animator> animators = new ArrayList<>();
100e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        animators.add(createFadeInAnimator(mBackgroundView));
10154746b4f93cf6de92c8f69fdc415ea2dff8083c2Chulwoo Lee        mContentBackgroundView.setImageResource(CONTENT_BACKGROUNDS[0]);
10254746b4f93cf6de92c8f69fdc415ea2dff8083c2Chulwoo Lee        mContentAnimationView.setImageResource(CONTENT_ANIMATIONS[0]);
103e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        mContentAnimator = createFadeInAnimator(mContentView);
104e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        animators.add(mContentAnimator);
105e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        AnimatorSet set = new AnimatorSet();
106e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        set.playTogether(animators);
10754746b4f93cf6de92c8f69fdc415ea2dff8083c2Chulwoo Lee        set.addListener(new AnimatorListenerAdapter() {
10854746b4f93cf6de92c8f69fdc415ea2dff8083c2Chulwoo Lee            @Override
10954746b4f93cf6de92c8f69fdc415ea2dff8083c2Chulwoo Lee            public void onAnimationEnd(Animator animation) {
11054746b4f93cf6de92c8f69fdc415ea2dff8083c2Chulwoo Lee                ((AnimationDrawable) mContentAnimationView.getDrawable()).start();
11154746b4f93cf6de92c8f69fdc415ea2dff8083c2Chulwoo Lee            }
11254746b4f93cf6de92c8f69fdc415ea2dff8083c2Chulwoo Lee        });
113e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        return set;
114e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    }
115e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee
116e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    @Override
117e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    protected void onPageChanged(final int newPage, int previousPage) {
118e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        if (mContentAnimator != null) {
11954746b4f93cf6de92c8f69fdc415ea2dff8083c2Chulwoo Lee            mContentAnimator.cancel();
120e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        }
12154746b4f93cf6de92c8f69fdc415ea2dff8083c2Chulwoo Lee        ((AnimationDrawable) mContentAnimationView.getDrawable()).stop();
122e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        ArrayList<Animator> animators = new ArrayList<>();
123e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        Animator fadeOut = createFadeOutAnimator(mContentView);
124e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        fadeOut.addListener(new AnimatorListenerAdapter() {
125e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee            @Override
126e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee            public void onAnimationEnd(Animator animation) {
12754746b4f93cf6de92c8f69fdc415ea2dff8083c2Chulwoo Lee                mContentBackgroundView.setImageResource(CONTENT_BACKGROUNDS[newPage]);
12854746b4f93cf6de92c8f69fdc415ea2dff8083c2Chulwoo Lee                mContentAnimationView.setImageResource(CONTENT_ANIMATIONS[newPage]);
12954746b4f93cf6de92c8f69fdc415ea2dff8083c2Chulwoo Lee            }
13054746b4f93cf6de92c8f69fdc415ea2dff8083c2Chulwoo Lee        });
13154746b4f93cf6de92c8f69fdc415ea2dff8083c2Chulwoo Lee        Animator fadeIn = createFadeInAnimator(mContentView);
13254746b4f93cf6de92c8f69fdc415ea2dff8083c2Chulwoo Lee        fadeIn.addListener(new AnimatorListenerAdapter() {
13354746b4f93cf6de92c8f69fdc415ea2dff8083c2Chulwoo Lee            @Override
13454746b4f93cf6de92c8f69fdc415ea2dff8083c2Chulwoo Lee            public void onAnimationEnd(Animator animation) {
13554746b4f93cf6de92c8f69fdc415ea2dff8083c2Chulwoo Lee                ((AnimationDrawable) mContentAnimationView.getDrawable()).start();
136e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee            }
137e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        });
138e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        AnimatorSet set = new AnimatorSet();
13954746b4f93cf6de92c8f69fdc415ea2dff8083c2Chulwoo Lee        set.playSequentially(fadeOut, fadeIn);
140e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        set.start();
141e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        mContentAnimator = set;
142e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    }
143e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee
144e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    private Animator createFadeInAnimator(View view) {
145e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        return ObjectAnimator.ofFloat(view, View.ALPHA, 0.0f, 1.0f).setDuration(ANIMATION_DURATION);
146e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    }
147e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee
148e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    private Animator createFadeOutAnimator(View view) {
149e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        return ObjectAnimator.ofFloat(view, View.ALPHA, 1.0f, 0.0f).setDuration(ANIMATION_DURATION);
150e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    }
151e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee
152e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    @Override
153e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    protected void onFinishFragment() {
154e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        getActivity().finish();
155e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    }
156e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee}
157