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