1 /*
2  * Copyright (C) 2022 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.permissioncontroller.safetycenter.ui
18 
19 import android.content.Context
20 import android.text.TextUtils
21 import android.util.AttributeSet
22 import android.util.TypedValue
23 import androidx.preference.Preference
24 import androidx.preference.PreferenceCategory
25 import com.android.permissioncontroller.R
26 
27 /** A {@link PreferenceCategory} that implements {@link ComparablePreference} interface. */
28 internal class ComparablePreferenceCategory
29 @JvmOverloads
30 constructor(
31     context: Context,
32     attrs: AttributeSet? = null,
33     defStyleAttr: Int =
34         getAttr(
35             context,
36             androidx.preference.R.attr.preferenceCategoryStyle,
37             android.R.attr.preferenceCategoryStyle
38         ),
39     defStyleRes: Int = 0
40 ) : PreferenceCategory(context, attrs, defStyleAttr, defStyleRes), ComparablePreference {
41 
42     private companion object {
43         /* PreferenceCategory falls back to using TypedArrayUtils.getAttr()
44          * when no defStyleAttr is set. This method reuses the logic
45          * from library-private TypedArrayUtils. */
getAttrnull46         private fun getAttr(context: Context, attr: Int, fallbackAttr: Int): Int {
47             val value = TypedValue()
48             context.theme.resolveAttribute(attr, value, true)
49             return if (value.resourceId != 0) attr else fallbackAttr
50         }
51     }
52 
isSameItemnull53     override fun isSameItem(preference: Preference): Boolean =
54         preference is ComparablePreferenceCategory && TextUtils.equals(key, preference.key)
55 
56     override fun hasSameContents(preference: Preference): Boolean =
57         preference is ComparablePreferenceCategory && TextUtils.equals(title, preference.title)
58 }
59