TunerActivity.java revision a9f128832f3fc16c2f435f2d0c999a5154200b4c
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; 25 26import com.android.settingslib.drawer.SettingsDrawerActivity; 27import com.android.systemui.R; 28 29public class TunerActivity extends SettingsDrawerActivity implements 30 PreferenceFragment.OnPreferenceStartFragmentCallback, 31 PreferenceFragment.OnPreferenceStartScreenCallback { 32 33 private static final String TAG_TUNER = "tuner"; 34 35 protected void onCreate(Bundle savedInstanceState) { 36 super.onCreate(savedInstanceState); 37 38 if (getFragmentManager().findFragmentByTag(TAG_TUNER) == null) { 39 getFragmentManager().beginTransaction().replace(R.id.content_frame, new TunerFragment(), 40 TAG_TUNER).commit(); 41 } 42 } 43 44 @Override 45 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) { 46 try { 47 Class<?> cls = Class.forName(pref.getFragment()); 48 Fragment fragment = (Fragment) cls.newInstance(); 49 FragmentTransaction transaction = getFragmentManager().beginTransaction(); 50 setTitle(pref.getTitle()); 51 transaction.replace(R.id.content_frame, fragment); 52 transaction.addToBackStack("PreferenceFragment"); 53 transaction.commit(); 54 return true; 55 } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) { 56 Log.d("TunerActivity", "Problem launching fragment", e); 57 return false; 58 } 59 } 60 61 @Override 62 public boolean onPreferenceStartScreen(PreferenceFragment caller, PreferenceScreen pref) { 63 FragmentTransaction transaction = getFragmentManager().beginTransaction(); 64 SubSettingsFragment fragment = new SubSettingsFragment(); 65 final Bundle b = new Bundle(1); 66 b.putString(PreferenceFragment.ARG_PREFERENCE_ROOT, pref.getKey()); 67 fragment.setArguments(b); 68 fragment.setTargetFragment(caller, 0); 69 transaction.replace(R.id.content_frame, fragment); 70 transaction.addToBackStack("PreferenceFragment"); 71 transaction.commit(); 72 return true; 73 } 74 75 public static class SubSettingsFragment extends PreferenceFragment { 76 @Override 77 public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { 78 setPreferenceScreen((PreferenceScreen) ((PreferenceFragment) getTargetFragment()) 79 .getPreferenceScreen().findPreference(rootKey)); 80 } 81 } 82 83} 84