1 /* 2 * Copyright (C) 2018 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.settingslib.widget; 18 19 import android.content.Context; 20 21 import androidx.preference.PreferenceFragmentCompat; 22 import androidx.preference.PreferenceScreen; 23 24 import com.android.settingslib.core.lifecycle.Lifecycle; 25 import com.android.settingslib.core.lifecycle.LifecycleObserver; 26 import com.android.settingslib.core.lifecycle.events.SetPreferenceScreen; 27 28 public class FooterPreferenceMixinCompat implements LifecycleObserver, SetPreferenceScreen { 29 30 private final PreferenceFragmentCompat mFragment; 31 private FooterPreference mFooterPreference; 32 33 public FooterPreferenceMixinCompat(PreferenceFragmentCompat fragment, Lifecycle lifecycle) { 34 mFragment = fragment; 35 lifecycle.addObserver(this); 36 } 37 38 @Override 39 public void setPreferenceScreen(PreferenceScreen preferenceScreen) { 40 if (mFooterPreference != null) { 41 preferenceScreen.addPreference(mFooterPreference); 42 } 43 } 44 45 /** 46 * Creates a new {@link FooterPreference}. 47 */ 48 public FooterPreference createFooterPreference() { 49 final PreferenceScreen screen = mFragment.getPreferenceScreen(); 50 if (mFooterPreference != null && screen != null) { 51 screen.removePreference(mFooterPreference); 52 } 53 mFooterPreference = new FooterPreference(getPrefContext()); 54 55 if (screen != null) { 56 screen.addPreference(mFooterPreference); 57 } 58 return mFooterPreference; 59 } 60 61 /** 62 * Returns an UI context with theme properly set for new Preference objects. 63 */ 64 private Context getPrefContext() { 65 return mFragment.getPreferenceManager().getContext(); 66 } 67 68 public boolean hasFooter() { 69 return mFooterPreference != null; 70 } 71 } 72 73