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.content.SharedPreferences;
22 import android.os.Bundle;
23 import android.transition.Scene;
24 import android.transition.Slide;
25 import android.transition.Transition;
26 import android.transition.TransitionManager;
27 import android.util.Log;
28 import android.view.Gravity;
29 import android.view.ViewGroup;
30 import android.view.ViewTreeObserver;
31 
32 import androidx.annotation.Nullable;
33 
34 import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
35 import com.android.settingslib.core.instrumentation.SharedPreferencesLogger;
36 import com.android.tv.settings.overlay.FeatureFactory;
37 
38 public abstract class TvSettingsActivity extends Activity {
39     private static final String TAG = "TvSettingsActivity";
40 
41     private static final String SETTINGS_FRAGMENT_TAG =
42             "com.android.tv.settings.MainSettings.SETTINGS_FRAGMENT";
43 
44     @Override
onCreate(@ullable Bundle savedInstanceState)45     protected void onCreate(@Nullable Bundle savedInstanceState) {
46         super.onCreate(savedInstanceState);
47         if (savedInstanceState == null) {
48 
49             final Fragment fragment = createSettingsFragment();
50             if (fragment == null) {
51                 return;
52             }
53             if (FeatureFactory.getFactory(this).isTwoPanelLayout()) {
54                 getFragmentManager().beginTransaction()
55                         .setCustomAnimations(android.R.animator.fade_in,
56                                 android.R.animator.fade_out)
57                         .add(android.R.id.content, fragment, SETTINGS_FRAGMENT_TAG)
58                         .commitNow();
59                 return;
60             }
61 
62             final ViewGroup root = findViewById(android.R.id.content);
63             root.getViewTreeObserver().addOnPreDrawListener(
64                     new ViewTreeObserver.OnPreDrawListener() {
65                         @Override
66                         public boolean onPreDraw() {
67                             root.getViewTreeObserver().removeOnPreDrawListener(this);
68                             final Scene scene = new Scene(root);
69                             scene.setEnterAction(() -> {
70                                 if (getFragmentManager().isStateSaved()
71                                         || getFragmentManager().isDestroyed()) {
72                                     Log.d(TAG, "Got torn down before adding fragment");
73                                     return;
74                                 }
75                                 getFragmentManager().beginTransaction()
76                                         .add(android.R.id.content, fragment,
77                                                 SETTINGS_FRAGMENT_TAG)
78                                         .commitNow();
79                             });
80 
81                             final Slide slide = new Slide(Gravity.END);
82                             slide.setSlideFraction(
83                                     getResources().getDimension(R.dimen.lb_settings_pane_width)
84                                             / root.getWidth());
85                             TransitionManager.go(scene, slide);
86 
87                             // Skip the current draw, there's nothing in it
88                             return false;
89                         }
90                     });
91         }
92     }
93 
94     @Override
finish()95     public void finish() {
96         final Fragment fragment = getFragmentManager().findFragmentByTag(SETTINGS_FRAGMENT_TAG);
97         if (FeatureFactory.getFactory(this).isTwoPanelLayout()) {
98             super.finish();
99             return;
100         }
101 
102         if (isResumed() && fragment != null) {
103             final ViewGroup root = findViewById(android.R.id.content);
104             final Scene scene = new Scene(root);
105             scene.setEnterAction(() -> getFragmentManager().beginTransaction()
106                     .remove(fragment)
107                     .commitNow());
108             final Slide slide = new Slide(Gravity.END);
109             slide.setSlideFraction(
110                     getResources().getDimension(R.dimen.lb_settings_pane_width) / root.getWidth());
111             slide.addListener(new Transition.TransitionListener() {
112                 @Override
113                 public void onTransitionStart(Transition transition) {
114                     getWindow().setDimAmount(0);
115                 }
116 
117                 @Override
118                 public void onTransitionEnd(Transition transition) {
119                     transition.removeListener(this);
120                     TvSettingsActivity.super.finish();
121                 }
122 
123                 @Override
124                 public void onTransitionCancel(Transition transition) {
125                 }
126 
127                 @Override
128                 public void onTransitionPause(Transition transition) {
129                 }
130 
131                 @Override
132                 public void onTransitionResume(Transition transition) {
133                 }
134             });
135             TransitionManager.go(scene, slide);
136         } else {
137             super.finish();
138         }
139     }
140 
createSettingsFragment()141     protected abstract Fragment createSettingsFragment();
142 
getMetricsTag()143     private String getMetricsTag() {
144         String tag = getClass().getName();
145         if (tag.startsWith("com.android.tv.settings.")) {
146             tag = tag.replace("com.android.tv.settings.", "");
147         }
148         return tag;
149     }
150 
151     @Override
getSharedPreferences(String name, int mode)152     public SharedPreferences getSharedPreferences(String name, int mode) {
153         if (name.equals(getPackageName() + "_preferences")) {
154             return new SharedPreferencesLogger(this, getMetricsTag(),
155                     new MetricsFeatureProvider());
156         }
157         return super.getSharedPreferences(name, mode);
158     }
159 }
160