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.content.ContentProviderClient; 21 import android.net.Uri; 22 import android.os.Bundle; 23 24 import androidx.annotation.NonNull; 25 import androidx.leanback.preference.LeanbackSettingsFragment; 26 import androidx.preference.Preference; 27 import androidx.preference.PreferenceDialogFragment; 28 import androidx.preference.PreferenceFragment; 29 import androidx.preference.PreferenceScreen; 30 31 import com.android.tv.settings.system.LeanbackPickerDialogFragment; 32 import com.android.tv.settings.system.LeanbackPickerDialogPreference; 33 import com.android.tv.twopanelsettings.slices.SlicePreference; 34 import com.android.tv.twopanelsettings.slices.SlicesConstants; 35 36 /** 37 * Base class for settings fragments. Handles launching fragments and dialogs in a reasonably 38 * generic way. Subclasses should only override onPreferenceStartInitialScreen. 39 */ 40 41 public abstract class BaseSettingsFragment extends LeanbackSettingsFragment { 42 @Override onPreferenceStartFragment(PreferenceFragment caller, Preference pref)43 public final boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) { 44 if (pref.getFragment() != null) { 45 if (pref instanceof SlicePreference) { 46 SlicePreference slicePref = (SlicePreference) pref; 47 if (slicePref.getUri() == null || !isUriValid(slicePref.getUri())) { 48 return false; 49 } 50 Bundle b = pref.getExtras(); 51 b.putString(SlicesConstants.TAG_TARGET_URI, slicePref.getUri()); 52 b.putCharSequence(SlicesConstants.TAG_SCREEN_TITLE, slicePref.getTitle()); 53 } 54 } 55 final Fragment f = 56 Fragment.instantiate(getActivity(), pref.getFragment(), pref.getExtras()); 57 f.setTargetFragment(caller, 0); 58 if (f instanceof PreferenceFragment || f instanceof PreferenceDialogFragment) { 59 startPreferenceFragment(f); 60 } else { 61 startImmersiveFragment(f); 62 } 63 return true; 64 } 65 isUriValid(String uri)66 private boolean isUriValid(String uri) { 67 if (uri == null) { 68 return false; 69 } 70 ContentProviderClient client = 71 getContext().getContentResolver().acquireContentProviderClient(Uri.parse(uri)); 72 if (client != null) { 73 client.close(); 74 return true; 75 } else { 76 return false; 77 } 78 } 79 80 @Override onPreferenceStartScreen(PreferenceFragment caller, PreferenceScreen pref)81 public final boolean onPreferenceStartScreen(PreferenceFragment caller, PreferenceScreen pref) { 82 return false; 83 } 84 85 @Override onPreferenceDisplayDialog(@onNull PreferenceFragment caller, Preference pref)86 public boolean onPreferenceDisplayDialog(@NonNull PreferenceFragment caller, Preference pref) { 87 final Fragment f; 88 if (pref instanceof LeanbackPickerDialogPreference) { 89 final LeanbackPickerDialogPreference dialogPreference = (LeanbackPickerDialogPreference) 90 pref; 91 f = dialogPreference.getType().equals("date") 92 ? LeanbackPickerDialogFragment.newDatePickerInstance(pref.getKey()) 93 : LeanbackPickerDialogFragment.newTimePickerInstance(pref.getKey()); 94 f.setTargetFragment(caller, 0); 95 startPreferenceFragment(f); 96 return true; 97 } else { 98 return super.onPreferenceDisplayDialog(caller, pref); 99 } 100 } 101 } 102