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 @JvmOverloads constructor( 29 context: Context, 30 attrs: AttributeSet? = null, 31 defStyleAttr: Int = getAttr( 32 context, 33 R.attr.preferenceCategoryStyle, 34 android.R.attr.preferenceCategoryStyle), 35 defStyleRes: Int = 0 36 ) : PreferenceCategory(context, attrs, defStyleAttr, defStyleRes), ComparablePreference { 37 38 private companion object { 39 /* PreferenceCategory falls back to using TypedArrayUtils.getAttr() 40 * when no defStyleAttr is set. This method reuses the logic 41 * from library-private TypedArrayUtils. */ getAttrnull42 private fun getAttr(context: Context, attr: Int, fallbackAttr: Int): Int { 43 val value = TypedValue() 44 context.theme.resolveAttribute(attr, value, true) 45 return if (value.resourceId != 0) attr else fallbackAttr 46 } 47 } 48 isSameItemnull49 override fun isSameItem(preference: Preference): Boolean = 50 preference is ComparablePreferenceCategory && TextUtils.equals(key, preference.key) 51 52 override fun hasSameContents(preference: Preference): Boolean = 53 preference is ComparablePreferenceCategory && TextUtils.equals(title, preference.title) 54 } 55