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