1 /* 2 * Copyright (C) 2024 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.preference 18 19 import android.content.Context 20 import androidx.preference.Preference 21 import androidx.preference.PreferenceCategory 22 import androidx.preference.SwitchPreferenceCompat 23 import androidx.preference.TwoStatePreference 24 import com.android.settingslib.metadata.PreferenceMetadata 25 import com.android.settingslib.widget.MainSwitchPreference 26 27 /** Binding of preference category associated with [PreferenceCategory]. */ 28 interface PreferenceCategoryBinding : PreferenceBinding { 29 createWidgetnull30 override fun createWidget(context: Context) = PreferenceCategory(context) 31 32 companion object { 33 @JvmStatic val INSTANCE = object : PreferenceCategoryBinding {} 34 } 35 } 36 37 /** A boolean value type preference associated with the abstract [TwoStatePreference]. */ 38 interface BooleanValuePreferenceBinding : PreferenceBinding { 39 bindnull40 override fun bind(preference: Preference, metadata: PreferenceMetadata) { 41 super.bind(preference, metadata) 42 (preference as TwoStatePreference).apply { 43 // MUST suppress persistent when initializing the checked state: 44 // 1. default value is written to datastore if not set (b/396260949) 45 // 2. avoid redundant read to the datastore 46 val suppressPersistent = isPersistent 47 if (suppressPersistent) isPersistent = false 48 // "false" is kind of placeholder, metadata datastore should provide the default value 49 isChecked = preferenceDataStore!!.getBoolean(key, false) 50 if (suppressPersistent) isPersistent = true 51 } 52 } 53 } 54 55 /** A boolean value type preference associated with [SwitchPreferenceCompat]. */ 56 interface SwitchPreferenceBinding : BooleanValuePreferenceBinding { 57 createWidgetnull58 override fun createWidget(context: Context): Preference = SwitchPreferenceCompat(context) 59 60 companion object { 61 @JvmStatic val INSTANCE = object : SwitchPreferenceBinding {} 62 } 63 } 64 65 /** A boolean value type preference associated with [MainSwitchPreference]. */ 66 interface MainSwitchPreferenceBinding : BooleanValuePreferenceBinding { 67 createWidgetnull68 override fun createWidget(context: Context): Preference = MainSwitchPreference(context) 69 70 companion object { 71 @JvmStatic val INSTANCE = object : MainSwitchPreferenceBinding {} 72 } 73 } 74 75 /** Default [PreferenceBinding] for [Preference]. */ 76 object DefaultPreferenceBinding : PreferenceBinding 77