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