1 /* 2 * Copyright (C) 2023 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.settings.spa.preference 18 19 import android.content.Context 20 import android.util.AttributeSet 21 import androidx.annotation.VisibleForTesting 22 import androidx.compose.runtime.Composable 23 import androidx.compose.ui.platform.ComposeView 24 import androidx.compose.ui.platform.ViewCompositionStrategy 25 import androidx.preference.Preference 26 import androidx.preference.PreferenceViewHolder 27 import com.android.settings.R 28 import com.android.settingslib.spa.framework.theme.SettingsTheme 29 import com.android.settingslib.widget.GroupSectionDividerMixin 30 import com.android.settingslib.widget.NormalPaddingMixin 31 32 open class ComposeGroupSectionPreference 33 @JvmOverloads 34 constructor( 35 context: Context, 36 attrs: AttributeSet? = null, 37 defStyleAttr: Int = 0, 38 defStyleRes: Int = 0, 39 ) : ComposePreference(context, attrs, defStyleAttr, defStyleRes), GroupSectionDividerMixin 40 41 open class ComposePreference 42 @JvmOverloads 43 constructor( 44 context: Context, 45 attrs: AttributeSet? = null, 46 defStyleAttr: Int = 0, 47 defStyleRes: Int = 0, 48 ) : Preference(context, attrs, defStyleAttr, defStyleRes), NormalPaddingMixin { <lambda>null49 private var content: @Composable () -> Unit = {} 50 setContentnull51 fun setContent(content: @Composable () -> Unit) { 52 this.content = content 53 } 54 55 @VisibleForTesting 56 @Composable Contentnull57 fun Content() { 58 content() 59 } 60 61 init { 62 layoutResource = R.layout.preference_compose 63 isSelectable = false 64 } 65 onBindViewHoldernull66 override fun onBindViewHolder(holder: PreferenceViewHolder) { 67 super.onBindViewHolder(holder) 68 holder.isDividerAllowedAbove = false 69 holder.isDividerAllowedBelow = false 70 71 (holder.itemView as ComposeView).apply { 72 setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed) 73 setContent { 74 SettingsTheme { 75 content() 76 } 77 } 78 } 79 } 80 } 81