1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 * in compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 * or implied. See the License for the specific language governing permissions and limitations under
12 * the License.
13 */
14
15package android.support.v17.leanback.supportleanbackshowcase.app.settings;
16
17import android.app.Fragment;
18import android.content.Context;
19import android.os.Bundle;
20import android.support.v14.preference.PreferenceFragment;
21import android.support.v17.leanback.supportleanbackshowcase.R;
22import android.support.v17.preference.LeanbackPreferenceFragment;
23import android.support.v17.preference.LeanbackSettingsFragment;
24import android.support.v7.preference.DialogPreference;
25import android.support.v7.preference.ListPreference;
26import android.support.v7.preference.Preference;
27import android.support.v7.preference.PreferenceScreen;
28import android.widget.Toast;
29
30import java.util.Arrays;
31import java.util.Stack;
32
33public class SettingsExampleFragment extends LeanbackSettingsFragment {
34
35    @Override
36    public void onPreferenceStartInitialScreen() {
37        startPreferenceFragment(buildPreferenceFragment(R.xml.prefs, null));
38    }
39
40    @Override
41    public boolean onPreferenceStartFragment(PreferenceFragment preferenceFragment,
42                                             Preference preference) {
43        return false;
44    }
45
46    @Override
47    public boolean onPreferenceStartScreen(PreferenceFragment preferenceFragment,
48                                           PreferenceScreen preferenceScreen) {
49        PreferenceFragment frag = buildPreferenceFragment(R.xml.prefs, preferenceScreen.getKey());
50        startPreferenceFragment(frag);
51        return true;
52    }
53
54    private PreferenceFragment buildPreferenceFragment(int preferenceResId, String root) {
55        PreferenceFragment fragment = new PrefFragment();
56        Bundle args = new Bundle();
57        args.putInt("preferenceResource", preferenceResId);
58        args.putString("root", root);
59        fragment.setArguments(args);
60        return fragment;
61    }
62
63    public static class PrefFragment extends LeanbackPreferenceFragment {
64
65        @Override
66        public void onCreatePreferences(Bundle bundle, String s) {
67            String root = getArguments().getString("root", null);
68            int prefResId = getArguments().getInt("preferenceResource");
69            if (root == null) {
70                addPreferencesFromResource(prefResId);
71            } else {
72                setPreferencesFromResource(prefResId, root);
73            }
74        }
75
76        @Override
77        public boolean onPreferenceTreeClick(Preference preference) {
78            final String[] keys = {"prefs_wifi_connect_wps", "prefs_date", "prefs_time",
79                    "prefs_date_time_use_timezone", "app_banner_sample_app", "pref_force_stop",
80                    "pref_uninstall", "pref_more_info"};
81            if (Arrays.asList(keys).contains(preference.getKey())) {
82                Toast.makeText(getActivity(), "Implement your own action handler.", Toast.LENGTH_SHORT).show();
83                return true;
84            }
85            return super.onPreferenceTreeClick(preference);
86        }
87
88    }
89}
90