1 /*
2  * Copyright (C) 2015 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.android.tv.settings;
18 
19 import android.app.Fragment;
20 import android.support.annotation.NonNull;
21 import android.support.v14.preference.PreferenceDialogFragment;
22 import android.support.v14.preference.PreferenceFragment;
23 import android.support.v17.preference.LeanbackSettingsFragment;
24 import android.support.v7.preference.Preference;
25 import android.support.v7.preference.PreferenceScreen;
26 
27 import com.android.tv.settings.system.LeanbackPickerDialogFragment;
28 import com.android.tv.settings.system.LeanbackPickerDialogPreference;
29 
30 /**
31  * Base class for settings fragments. Handles launching fragments and dialogs in a reasonably
32  * generic way. Subclasses should only override onPreferenceStartInitialScreen.
33  */
34 
35 public abstract class BaseSettingsFragment extends LeanbackSettingsFragment {
36     @Override
onPreferenceStartFragment(PreferenceFragment caller, Preference pref)37     public final boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
38         final Fragment f =
39                 Fragment.instantiate(getActivity(), pref.getFragment(), pref.getExtras());
40         f.setTargetFragment(caller, 0);
41         if (f instanceof PreferenceFragment || f instanceof PreferenceDialogFragment) {
42             startPreferenceFragment(f);
43         } else {
44             startImmersiveFragment(f);
45         }
46         return true;
47     }
48 
49     @Override
onPreferenceStartScreen(PreferenceFragment caller, PreferenceScreen pref)50     public final boolean onPreferenceStartScreen(PreferenceFragment caller, PreferenceScreen pref) {
51         return false;
52     }
53 
54     @Override
onPreferenceDisplayDialog(@onNull PreferenceFragment caller, Preference pref)55     public boolean onPreferenceDisplayDialog(@NonNull PreferenceFragment caller, Preference pref) {
56         final Fragment f;
57         if (pref instanceof LeanbackPickerDialogPreference) {
58             final LeanbackPickerDialogPreference dialogPreference = (LeanbackPickerDialogPreference)
59                     pref;
60             f = dialogPreference.getType().equals("date")
61                     ? LeanbackPickerDialogFragment.newDatePickerInstance(pref.getKey())
62                     : LeanbackPickerDialogFragment.newTimePickerInstance(pref.getKey());
63             f.setTargetFragment(caller, 0);
64             startPreferenceFragment(f);
65             return true;
66         } else {
67             return super.onPreferenceDisplayDialog(caller, pref);
68         }
69     }
70 }
71