1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License
15  */
16 
17 package com.android.tv.settings;
18 
19 import android.app.Activity;
20 import android.app.Fragment;
21 import android.os.Bundle;
22 import android.support.annotation.Nullable;
23 import android.transition.Scene;
24 import android.transition.Slide;
25 import android.transition.Transition;
26 import android.transition.TransitionManager;
27 import android.view.Gravity;
28 import android.view.ViewGroup;
29 import android.view.ViewTreeObserver;
30 
31 public abstract class TvSettingsActivity extends Activity {
32 
33     private static final String SETTINGS_FRAGMENT_TAG =
34             "com.android.tv.settings.MainSettings.SETTINGS_FRAGMENT";
35 
36     @Override
onCreate(@ullable Bundle savedInstanceState)37     protected void onCreate(@Nullable Bundle savedInstanceState) {
38         super.onCreate(savedInstanceState);
39         if (savedInstanceState == null) {
40 
41             final ViewGroup root = (ViewGroup) findViewById(android.R.id.content);
42             root.getViewTreeObserver().addOnPreDrawListener(
43                     new ViewTreeObserver.OnPreDrawListener() {
44                         @Override
45                         public boolean onPreDraw() {
46                             root.getViewTreeObserver().removeOnPreDrawListener(this);
47                             final Scene scene = new Scene(root);
48                             scene.setEnterAction(new Runnable() {
49                                 @Override
50                                 public void run() {
51                                     final Fragment fragment = createSettingsFragment();
52                                     if (fragment != null) {
53                                         getFragmentManager().beginTransaction()
54                                                 .add(android.R.id.content, fragment,
55                                                         SETTINGS_FRAGMENT_TAG)
56                                                 .commitNow();
57                                     }
58                                 }
59                             });
60 
61                             final Slide slide = new Slide(Gravity.END);
62                             slide.setSlideFraction(
63                                     getResources().getDimension(R.dimen.lb_settings_pane_width)
64                                             / root.getWidth());
65                             TransitionManager.go(scene, slide);
66 
67                             // Skip the current draw, there's nothing in it
68                             return false;
69                         }
70                     });
71         }
72     }
73 
74     @Override
finish()75     public void finish() {
76         final Fragment fragment = getFragmentManager().findFragmentByTag(SETTINGS_FRAGMENT_TAG);
77         if (isResumed() && fragment != null) {
78             final ViewGroup root = (ViewGroup) findViewById(android.R.id.content);
79             final Scene scene = new Scene(root);
80             scene.setEnterAction(new Runnable() {
81                 @Override
82                 public void run() {
83                     getFragmentManager().beginTransaction()
84                             .remove(fragment)
85                             .commitNow();
86                 }
87             });
88             final Slide slide = new Slide(Gravity.END);
89             slide.setSlideFraction(
90                     getResources().getDimension(R.dimen.lb_settings_pane_width) / root.getWidth());
91             slide.addListener(new Transition.TransitionListener() {
92                 @Override
93                 public void onTransitionStart(Transition transition) {
94                     getWindow().setDimAmount(0);
95                 }
96 
97                 @Override
98                 public void onTransitionEnd(Transition transition) {
99                     transition.removeListener(this);
100                     TvSettingsActivity.super.finish();
101                 }
102 
103                 @Override
104                 public void onTransitionCancel(Transition transition) {
105                 }
106 
107                 @Override
108                 public void onTransitionPause(Transition transition) {
109                 }
110 
111                 @Override
112                 public void onTransitionResume(Transition transition) {
113                 }
114             });
115             TransitionManager.go(scene, slide);
116         } else {
117             super.finish();
118         }
119     }
120 
createSettingsFragment()121     protected abstract Fragment createSettingsFragment();
122 }
123