Home | History | Annotate | Download | only in walt
      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 org.chromium.latency.walt;
     18 
     19 
     20 import android.os.Bundle;
     21 import android.support.annotation.Nullable;
     22 import android.support.v4.app.DialogFragment;
     23 import android.support.v4.app.Fragment;
     24 import android.support.v4.app.FragmentTransaction;
     25 import android.support.v4.content.ContextCompat;
     26 import android.support.v7.preference.Preference;
     27 import android.support.v7.preference.PreferenceFragmentCompat;
     28 import android.support.v7.preference.PreferenceScreen;
     29 import android.support.v7.widget.Toolbar;
     30 import android.view.View;
     31 
     32 
     33 public class SettingsFragment extends PreferenceFragmentCompat implements PreferenceFragmentCompat.OnPreferenceStartScreenCallback {
     34 
     35     private Toolbar toolbar;
     36 
     37     public SettingsFragment() {
     38         // Required empty public constructor
     39     }
     40 
     41     @Override
     42     public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
     43         // Load the preferences from an XML resource
     44         setPreferencesFromResource(R.xml.preferences, rootKey);
     45 
     46         PreferenceScreen prefMidiScreen =
     47                 (PreferenceScreen) getPreferenceScreen().findPreference("pref_midi_screen");
     48         if (prefMidiScreen != null) {
     49             boolean hasMidi =
     50                     getContext().getPackageManager().hasSystemFeature("android.software.midi");
     51             prefMidiScreen.setVisible(hasMidi);
     52         }
     53     }
     54 
     55     @Override
     56     public void onCreate(Bundle savedInstanceState) {
     57         super.onCreate(savedInstanceState);
     58         toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar_main);
     59     }
     60 
     61     @Override
     62     public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
     63         super.onViewCreated(view, savedInstanceState);
     64         view.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.ColorBackground));
     65     }
     66 
     67     @Override
     68     public void onDisplayPreferenceDialog(Preference preference) {
     69         if (preference instanceof NumberPickerPreference) {
     70             DialogFragment fragment = NumberPickerPreference.
     71                     NumberPickerPreferenceDialogFragmentCompat.newInstance(preference.getKey());
     72             fragment.setTargetFragment(this, 0);
     73             fragment.show(getFragmentManager(),
     74                     "android.support.v7.preference.PreferenceFragment.DIALOG");
     75         } else {
     76             super.onDisplayPreferenceDialog(preference);
     77         }
     78     }
     79 
     80     @Override
     81     public Fragment getCallbackFragment() {
     82         return this;
     83     }
     84 
     85     @Override
     86     public boolean onPreferenceStartScreen(PreferenceFragmentCompat preferenceFragmentCompat,
     87                                            PreferenceScreen preferenceScreen) {
     88         SettingsFragment fragment = new SettingsFragment();
     89         Bundle args = new Bundle();
     90         args.putString(PreferenceFragmentCompat.ARG_PREFERENCE_ROOT, preferenceScreen.getKey());
     91         fragment.setArguments(args);
     92 
     93         FragmentTransaction ft = preferenceFragmentCompat.getFragmentManager().beginTransaction();
     94         ft.add(R.id.fragment_container, fragment, preferenceScreen.getKey());
     95         ft.addToBackStack(preferenceScreen.getTitle().toString());
     96         ft.commit();
     97 
     98         toolbar.setTitle(preferenceScreen.getTitle());
     99         return true;
    100     }
    101 
    102 }
    103