1/*
2 * Copyright (C) 2016 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 */
16
17package com.example.android.supportpreference;
18
19import android.annotation.TargetApi;
20import android.app.Activity;
21import android.app.Fragment;
22import android.os.Bundle;
23import android.support.v14.preference.PreferenceDialogFragment;
24import android.support.v14.preference.PreferenceFragment;
25import android.support.v17.preference.LeanbackPreferenceFragment;
26import android.support.v17.preference.LeanbackSettingsFragment;
27import android.support.v7.preference.Preference;
28import android.support.v7.preference.PreferenceScreen;
29
30@TargetApi(17)
31public class FragmentSupportPreferencesLeanback extends Activity {
32    @Override
33    protected void onCreate(Bundle savedInstanceState) {
34        super.onCreate(savedInstanceState);
35
36        // Display the fragment as the main content.
37        if (savedInstanceState == null) {
38            getFragmentManager().beginTransaction().replace(android.R.id.content,
39                    new SettingsFragment()).commit();
40        }
41    }
42
43//BEGIN_INCLUDE(support_fragment_leanback)
44    public static class SettingsFragment extends LeanbackSettingsFragment {
45        @Override
46        public void onPreferenceStartInitialScreen() {
47            startPreferenceFragment(new PrefsFragment());
48        }
49
50        @Override
51        public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
52            final Fragment f =
53                    Fragment.instantiate(getActivity(), pref.getFragment(), pref.getExtras());
54            f.setTargetFragment(caller, 0);
55            if (f instanceof PreferenceFragment || f instanceof PreferenceDialogFragment) {
56                startPreferenceFragment(f);
57            } else {
58                startImmersiveFragment(f);
59            }
60            return true;
61        }
62
63        @Override
64        public boolean onPreferenceStartScreen(PreferenceFragment caller, PreferenceScreen pref) {
65            final Fragment f = new PrefsFragment();
66            final Bundle args = new Bundle(1);
67            args.putString(PreferenceFragment.ARG_PREFERENCE_ROOT, pref.getKey());
68            f.setArguments(args);
69            startPreferenceFragment(f);
70            return true;
71        }
72    }
73
74    public static class PrefsFragment extends LeanbackPreferenceFragment {
75
76        @Override
77        public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
78            // Load the preferences from an XML resource
79            setPreferencesFromResource(R.xml.preferences, rootKey);
80        }
81    }
82//END_INCLUDE(support_fragment_leanback)
83
84}
85