• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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 package com.android.systemui.tuner;
17 
18 import android.app.Activity;
19 import android.app.Fragment;
20 import android.app.FragmentTransaction;
21 import android.os.Bundle;
22 import android.util.Log;
23 import android.view.MenuItem;
24 import android.view.Window;
25 import android.view.WindowManager;
26 import android.widget.Toolbar;
27 
28 import androidx.preference.Preference;
29 import androidx.preference.PreferenceFragment;
30 import androidx.preference.PreferenceScreen;
31 
32 import com.android.systemui.Dependency;
33 import com.android.systemui.R;
34 import com.android.systemui.fragments.FragmentService;
35 
36 public class TunerActivity extends Activity implements
37         PreferenceFragment.OnPreferenceStartFragmentCallback,
38         PreferenceFragment.OnPreferenceStartScreenCallback {
39 
40     private static final String TAG_TUNER = "tuner";
41 
onCreate(Bundle savedInstanceState)42     protected void onCreate(Bundle savedInstanceState) {
43         super.onCreate(savedInstanceState);
44 
45         getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
46         requestWindowFeature(Window.FEATURE_NO_TITLE);
47         setContentView(R.layout.tuner_activity);
48         Toolbar toolbar = findViewById(R.id.action_bar);
49         if (toolbar != null) {
50             setActionBar(toolbar);
51         }
52 
53         Dependency.initDependencies(this);
54 
55         if (getFragmentManager().findFragmentByTag(TAG_TUNER) == null) {
56             final String action = getIntent().getAction();
57             boolean showDemoMode = action != null && action.equals(
58                     "com.android.settings.action.DEMO_MODE");
59             final PreferenceFragment fragment = showDemoMode ? new DemoModeFragment()
60                     : new TunerFragment();
61             getFragmentManager().beginTransaction().replace(R.id.content_frame,
62                     fragment, TAG_TUNER).commit();
63         }
64     }
65 
66     @Override
onDestroy()67     protected void onDestroy() {
68         super.onDestroy();
69         Dependency.destroy(FragmentService.class, s -> s.destroyAll());
70         Dependency.clearDependencies();
71     }
72 
73     @Override
onMenuItemSelected(int featureId, MenuItem item)74     public boolean onMenuItemSelected(int featureId, MenuItem item) {
75         if (item.getItemId() == android.R.id.home) {
76             onBackPressed();
77             return true;
78         }
79         return super.onMenuItemSelected(featureId, item);
80     }
81 
82     @Override
onBackPressed()83     public void onBackPressed() {
84         if (!getFragmentManager().popBackStackImmediate()) {
85             super.onBackPressed();
86         }
87     }
88 
89     @Override
onPreferenceStartFragment(PreferenceFragment caller, Preference pref)90     public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
91         try {
92             Class<?> cls = Class.forName(pref.getFragment());
93             Fragment fragment = (Fragment) cls.newInstance();
94             final Bundle b = new Bundle(1);
95             b.putString(PreferenceFragment.ARG_PREFERENCE_ROOT, pref.getKey());
96             fragment.setArguments(b);
97             FragmentTransaction transaction = getFragmentManager().beginTransaction();
98             setTitle(pref.getTitle());
99             transaction.replace(R.id.content_frame, fragment);
100             transaction.addToBackStack("PreferenceFragment");
101             transaction.commit();
102             return true;
103         } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
104             Log.d("TunerActivity", "Problem launching fragment", e);
105             return false;
106         }
107     }
108 
109     @Override
onPreferenceStartScreen(PreferenceFragment caller, PreferenceScreen pref)110     public boolean onPreferenceStartScreen(PreferenceFragment caller, PreferenceScreen pref) {
111         FragmentTransaction transaction = getFragmentManager().beginTransaction();
112         SubSettingsFragment fragment = new SubSettingsFragment();
113         final Bundle b = new Bundle(1);
114         b.putString(PreferenceFragment.ARG_PREFERENCE_ROOT, pref.getKey());
115         fragment.setArguments(b);
116         fragment.setTargetFragment(caller, 0);
117         transaction.replace(R.id.content_frame, fragment);
118         transaction.addToBackStack("PreferenceFragment");
119         transaction.commit();
120         return true;
121     }
122 
123     public static class SubSettingsFragment extends PreferenceFragment {
124         private PreferenceScreen mParentScreen;
125 
126         @Override
onCreatePreferences(Bundle savedInstanceState, String rootKey)127         public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
128             mParentScreen =
129                     (PreferenceScreen) ((PreferenceFragment) getTargetFragment())
130                             .getPreferenceScreen().findPreference(rootKey);
131             PreferenceScreen screen =
132                     getPreferenceManager().createPreferenceScreen(
133                             getPreferenceManager().getContext());
134             setPreferenceScreen(screen);
135             // Copy all the preferences over to this screen so they go into the attached state.
136             while (mParentScreen.getPreferenceCount() > 0) {
137                 Preference p = mParentScreen.getPreference(0);
138                 mParentScreen.removePreference(p);
139                 screen.addPreference(p);
140             }
141         }
142 
143         @Override
onDestroy()144         public void onDestroy() {
145             super.onDestroy();
146             // Copy all the preferences back so we don't lose them.
147             PreferenceScreen screen = getPreferenceScreen();
148             while (screen.getPreferenceCount() > 0) {
149                 Preference p = screen.getPreference(0);
150                 screen.removePreference(p);
151                 mParentScreen.addPreference(p);
152             }
153         }
154     }
155 
156 }
157