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