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