1 /* 2 * Copyright 2021 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.example.androidx.preference 18 19 import android.content.Context 20 import android.content.ContextWrapper 21 import android.os.Bundle 22 import androidx.appcompat.app.AppCompatActivity 23 import androidx.preference.PreferenceFragmentCompat 24 import androidx.preference.PreferenceHeaderFragmentCompat 25 26 class TwoPanePreferences : AppCompatActivity() { 27 28 class PreferenceHeaderFragmentCompatImpl : PreferenceHeaderFragmentCompat() { 29 30 class PreferenceHeader : PreferenceFragmentCompat() { onCreatePreferencesnull31 override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { 32 setPreferencesFromResource(R.xml.preference_headers, rootKey) 33 } 34 } 35 onCreatePreferenceHeadernull36 override fun onCreatePreferenceHeader(): PreferenceFragmentCompat { 37 return PreferenceHeader() 38 } 39 40 /** 41 * Simulate the behavior of Hilt to ensure that PreferenceHeaderFragmentCompat does not rely 42 * on the Context being an OnBackPressedDispatcherOwner instance 43 */ getContextnull44 override fun getContext(): Context { 45 return ContextWrapper(super.getContext()) 46 } 47 } 48 49 class BasicPreferences : PreferenceFragmentCompat() { onCreatePreferencesnull50 override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { 51 setPreferencesFromResource(R.xml.preferences_basic, rootKey) 52 } 53 } 54 55 class WidgetPreferences : PreferenceFragmentCompat() { onCreatePreferencesnull56 override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { 57 setPreferencesFromResource(R.xml.preferences_widget, rootKey) 58 } 59 } 60 61 class DialogPreferences : PreferenceFragmentCompat() { onCreatePreferencesnull62 override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { 63 setPreferencesFromResource(R.xml.preferences_dialog, rootKey) 64 } 65 } 66 67 class AdvancedPreferences : PreferenceFragmentCompat() { onCreatePreferencesnull68 override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { 69 setPreferencesFromResource(R.xml.preferences_advanced, rootKey) 70 } 71 } 72 73 class MultiPreferences : PreferenceFragmentCompat() { onCreatePreferencesnull74 override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { 75 setPreferencesFromResource(R.xml.preferences_advanced_next, rootKey) 76 } 77 } 78 onCreatenull79 override fun onCreate(savedInstanceState: Bundle?) { 80 super.onCreate(savedInstanceState) 81 setContentView(R.layout.two_pane_preferences) 82 } 83 } 84