1 /*
<lambda>null2  * Copyright (C) 2020 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 package com.android.settingslib.widget
17 
18 import android.content.Context
19 import android.util.AttributeSet
20 import android.view.View
21 import androidx.preference.Preference
22 import androidx.preference.PreferenceViewHolder
23 import com.android.settingslib.widget.preference.card.R
24 
25 /**
26  * The CardPreference shows a card like suggestion in homepage, which also support dismiss.
27  */
28 class CardPreference @JvmOverloads constructor(
29     context: Context,
30     attrs: AttributeSet? = null,
31     defStyleAttr: Int = 0,
32     defStyleRes: Int = 0
33 ) : Preference(context, attrs, defStyleAttr, defStyleRes), GroupSectionDividerMixin {
34 
35     init {
36         layoutResource = R.layout.settingslib_expressive_preference_card
37     }
38     private var dismissible = false
39         set(value) {
40             if (field != value) {
41                 field = value
42                 notifyChanged()
43             }
44         }
45 
46     override fun onBindViewHolder(holder: PreferenceViewHolder) {
47         super.onBindViewHolder(holder)
48         holder.isDividerAllowedBelow = false
49         holder.isDividerAllowedAbove = false
50 
51         holder.findViewById(android.R.id.closeButton)?.let { dismissButton ->
52             dismissButton.visibility = if (dismissible) View.VISIBLE else View.GONE
53             dismissButton.setOnClickListener {
54                 isVisible = false
55             }
56         }
57     }
58 }