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