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