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 17 package com.example.android.leanback; 18 19 import android.os.Bundle; 20 import android.support.v14.preference.PreferenceFragment; 21 import android.support.v17.preference.LeanbackPreferenceFragment; 22 import android.support.v17.preference.LeanbackSettingsFragment; 23 import android.support.v7.preference.Preference; 24 import android.support.v7.preference.PreferenceScreen; 25 import android.widget.Toast; 26 27 import java.util.Arrays; 28 29 30 31 public class SettingsFragment extends LeanbackSettingsFragment { 32 33 34 private static final int sPreferenceResId = R.xml.prefs; 35 36 @Override onPreferenceStartInitialScreen()37 public void onPreferenceStartInitialScreen() { 38 startPreferenceFragment(buildPreferenceFragment(null)); 39 } 40 41 @Override onPreferenceStartFragment(PreferenceFragment preferenceFragment, Preference preference)42 public boolean onPreferenceStartFragment(PreferenceFragment preferenceFragment, 43 Preference preference) { 44 return false; 45 } 46 47 @Override onPreferenceStartScreen(PreferenceFragment preferenceFragment, PreferenceScreen preferenceScreen)48 public boolean onPreferenceStartScreen(PreferenceFragment preferenceFragment, 49 PreferenceScreen preferenceScreen) { 50 PreferenceFragment frag = buildPreferenceFragment(preferenceScreen.getKey()); 51 frag.setTargetFragment(preferenceFragment, 0); 52 startPreferenceFragment(frag); 53 return true; 54 } 55 56 buildPreferenceFragment(String rootKey)57 private PreferenceFragment buildPreferenceFragment(String rootKey) { 58 PreferenceFragment fragment = new PrefFragment(); 59 Bundle args = new Bundle(); 60 args.putString(PreferenceFragment.ARG_PREFERENCE_ROOT, rootKey); 61 fragment.setArguments(args); 62 return fragment; 63 } 64 65 public static class PrefFragment extends LeanbackPreferenceFragment { 66 67 @Override onCreate(Bundle savedInstanceState)68 public void onCreate(Bundle savedInstanceState) { 69 super.onCreate(savedInstanceState); 70 } 71 72 @Override onCreatePreferences(Bundle bundle, String rootKey)73 public void onCreatePreferences(Bundle bundle, String rootKey) { 74 setPreferencesFromResource(sPreferenceResId, rootKey); 75 } 76 77 @Override onPreferenceTreeClick(Preference preference)78 public boolean onPreferenceTreeClick(Preference preference) { 79 final String[] keys = {"prefs_wifi_connect_wps", "prefs_date", "prefs_time", 80 "prefs_date_time_use_timezone", "app_banner_sample_app", "pref_force_stop", 81 "pref_uninstall", "pref_more_info"}; 82 if (Arrays.asList(keys).contains(preference.getKey())) { 83 Toast.makeText(getActivity(), "Implement your own action handler.", 84 Toast.LENGTH_SHORT).show(); 85 return true; 86 } 87 return super.onPreferenceTreeClick(preference); 88 } 89 90 } 91 } 92