content: /*
   * Copyright (C) 2015 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 androidx.leanback.leanbackshowcase.app.settings;
  
  import android.os.Bundle;
  import androidx.preference.PreferenceFragment;
  import androidx.leanback.leanbackshowcase.R;
  import androidx.leanback.preference.LeanbackPreferenceFragment;
  import androidx.leanback.preference.LeanbackSettingsFragment;
  import androidx.preference.Preference;
  import androidx.preference.PreferenceScreen;
  import android.widget.Toast;
  
  import java.util.Arrays;
  
  public class SettingsExampleFragment extends LeanbackSettingsFragment {
  
      @Override
      public void onPreferenceStartInitialScreen() {
          startPreferenceFragment(buildPreferenceFragment(R.xml.prefs, null));
      }
  
      @Override
      public boolean onPreferenceStartFragment(PreferenceFragment preferenceFragment,
                                               Preference preference) {
          return false;
      }
  
      @Override
      public boolean onPreferenceStartScreen(PreferenceFragment preferenceFragment,
                                             PreferenceScreen preferenceScreen) {
          PreferenceFragment frag = buildPreferenceFragment(R.xml.prefs, preferenceScreen.getKey());
          startPreferenceFragment(frag);
          return true;
      }
  
      private PreferenceFragment buildPreferenceFragment(int preferenceResId, String root) {
          PreferenceFragment fragment = new PrefFragment();
          Bundle args = new Bundle();
          args.putInt("preferenceResource", preferenceResId);
          args.putString("root", root);
          fragment.setArguments(args);
          return fragment;
      }
  
      public static class PrefFragment extends LeanbackPreferenceFragment {
  
          @Override
          public void onCreatePreferences(Bundle bundle, String s) {
              String root = getArguments().getString("root", null);
              int prefResId = getArguments().getInt("preferenceResource");
              if (root == null) {
                  addPreferencesFromResource(prefResId);
              } else {
                  setPreferencesFromResource(prefResId, root);
              }
          }
  
          @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);
          }
  
      }
  }
  