/* * Copyright (C) 2016 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.example.android.leanback; import android.os.Bundle; import android.support.v14.preference.PreferenceFragment; import android.support.v17.preference.LeanbackPreferenceFragment; import android.support.v17.preference.LeanbackSettingsFragment; import android.support.v7.preference.Preference; import android.support.v7.preference.PreferenceScreen; import android.widget.Toast; import java.util.Arrays; public class SettingsFragment extends LeanbackSettingsFragment { private static final int sPreferenceResId = R.xml.prefs; @Override public void onPreferenceStartInitialScreen() { startPreferenceFragment(buildPreferenceFragment(null)); } @Override public boolean onPreferenceStartFragment(PreferenceFragment preferenceFragment, Preference preference) { return false; } @Override public boolean onPreferenceStartScreen(PreferenceFragment preferenceFragment, PreferenceScreen preferenceScreen) { PreferenceFragment frag = buildPreferenceFragment(preferenceScreen.getKey()); frag.setTargetFragment(preferenceFragment, 0); startPreferenceFragment(frag); return true; } private PreferenceFragment buildPreferenceFragment(String rootKey) { PreferenceFragment fragment = new PrefFragment(); Bundle args = new Bundle(); args.putString(PreferenceFragment.ARG_PREFERENCE_ROOT, rootKey); fragment.setArguments(args); return fragment; } public static class PrefFragment extends LeanbackPreferenceFragment { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public void onCreatePreferences(Bundle bundle, String rootKey) { setPreferencesFromResource(sPreferenceResId, rootKey); } @Override public boolean onPreferenceTreeClick(Preference preference) { final String[] keys = {"prefs_wifi_connect_wps", "prefs_date", "prefs_time", "prefs_date_time_use_timezone", "app_banner_sample_app", "pref_force_stop", "pref_uninstall", "pref_more_info"}; if (Arrays.asList(keys).contains(preference.getKey())) { Toast.makeText(getActivity(), "Implement your own action handler.", Toast.LENGTH_SHORT).show(); return true; } return super.onPreferenceTreeClick(preference); } } }