Added highlighted background support
Bug: 376137920
Test: manual
Flag: EXEMPT resource only update
Change-Id: Ia20df9282c038e1a6fffd016514b5301eb7d3006
diff --git a/packages/SettingsLib/Preference/src/com/android/settingslib/preference/PreferenceFragment.kt b/packages/SettingsLib/Preference/src/com/android/settingslib/preference/PreferenceFragment.kt
index 5f4b88f..41a626f 100644
--- a/packages/SettingsLib/Preference/src/com/android/settingslib/preference/PreferenceFragment.kt
+++ b/packages/SettingsLib/Preference/src/com/android/settingslib/preference/PreferenceFragment.kt
@@ -21,16 +21,16 @@
import android.os.Bundle
import android.util.Log
import androidx.annotation.XmlRes
-import androidx.preference.PreferenceFragmentCompat
import androidx.preference.PreferenceScreen
import com.android.settingslib.metadata.EXTRA_BINDING_SCREEN_KEY
import com.android.settingslib.metadata.PreferenceScreenBindingKeyProvider
import com.android.settingslib.metadata.PreferenceScreenRegistry
import com.android.settingslib.preference.PreferenceScreenBindingHelper.Companion.bindRecursively
+import com.android.settingslib.widget.SettingsBasePreferenceFragment
/** Fragment to display a preference screen. */
open class PreferenceFragment :
- PreferenceFragmentCompat(), PreferenceScreenProvider, PreferenceScreenBindingKeyProvider {
+ SettingsBasePreferenceFragment(), PreferenceScreenProvider, PreferenceScreenBindingKeyProvider {
private var preferenceScreenBindingHelper: PreferenceScreenBindingHelper? = null
diff --git a/packages/SettingsLib/SettingsTheme/res/drawable-v35/settingslib_round_background_bottom_highlighted.xml b/packages/SettingsLib/SettingsTheme/res/drawable-v35/settingslib_round_background_bottom_highlighted.xml
new file mode 100644
index 0000000..c0c0869
--- /dev/null
+++ b/packages/SettingsLib/SettingsTheme/res/drawable-v35/settingslib_round_background_bottom_highlighted.xml
@@ -0,0 +1,36 @@
+<?xml version="1.0" encoding="utf-8"?><!--
+ Copyright (C) 2024 The Android Open Source Project
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+ -->
+
+<ripple xmlns:android="http://schemas.android.com/apk/res/android"
+ android:color="?android:colorControlHighlight">
+ <item
+ android:bottom="16dp"
+ android:end="?android:attr/listPreferredItemPaddingEnd"
+ android:start="?android:attr/listPreferredItemPaddingStart"
+ android:top="2dp">
+ <shape
+ android:shape="rectangle"
+ android:tint="?android:attr/colorAccent">
+ <corners
+ android:bottomLeftRadius="@dimen/settingslib_preference_corner_radius"
+ android:bottomRightRadius="@dimen/settingslib_preference_corner_radius"
+ android:topLeftRadius="4dp"
+ android:topRightRadius="4dp" />
+ <padding android:bottom="16dp" />
+ <solid android:color="#42000000" />
+ </shape>
+ </item>
+</ripple>
\ No newline at end of file
diff --git a/packages/SettingsLib/SettingsTheme/res/drawable-v35/settingslib_round_background_center_highlighted.xml b/packages/SettingsLib/SettingsTheme/res/drawable-v35/settingslib_round_background_center_highlighted.xml
new file mode 100644
index 0000000..8099d9b
--- /dev/null
+++ b/packages/SettingsLib/SettingsTheme/res/drawable-v35/settingslib_round_background_center_highlighted.xml
@@ -0,0 +1,30 @@
+<?xml version="1.0" encoding="utf-8"?><!--
+ Copyright (C) 2024 The Android Open Source Project
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+ -->
+
+<ripple xmlns:android="http://schemas.android.com/apk/res/android"
+ android:color="?android:colorControlHighlight">
+ <item
+ android:end="?android:attr/listPreferredItemPaddingEnd"
+ android:start="?android:attr/listPreferredItemPaddingStart"
+ android:top="2dp">
+ <shape
+ android:shape="rectangle"
+ android:tint="?android:attr/colorAccent">
+ <corners android:radius="4dp" />
+ <solid android:color="#42000000" />
+ </shape>
+ </item>
+</ripple>
\ No newline at end of file
diff --git a/packages/SettingsLib/SettingsTheme/res/drawable-v35/settingslib_round_background_highlighted.xml b/packages/SettingsLib/SettingsTheme/res/drawable-v35/settingslib_round_background_highlighted.xml
new file mode 100644
index 0000000..a119a4a
--- /dev/null
+++ b/packages/SettingsLib/SettingsTheme/res/drawable-v35/settingslib_round_background_highlighted.xml
@@ -0,0 +1,32 @@
+<?xml version="1.0" encoding="utf-8"?><!--
+ Copyright (C) 2024 The Android Open Source Project
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+ -->
+
+<ripple xmlns:android="http://schemas.android.com/apk/res/android"
+ android:color="?android:colorControlHighlight">
+ <item
+ android:bottom="16dp"
+ android:end="?android:attr/listPreferredItemPaddingEnd"
+ android:start="?android:attr/listPreferredItemPaddingStart"
+ android:top="2dp">
+ <shape
+ android:shape="rectangle"
+ android:tint="?android:attr/colorAccent">
+ <corners android:radius="@dimen/settingslib_preference_corner_radius" />
+ <padding android:bottom="16dp" />
+ <solid android:color="#42000000" />
+ </shape>
+ </item>
+</ripple>
\ No newline at end of file
diff --git a/packages/SettingsLib/SettingsTheme/res/drawable-v35/settingslib_round_background_top_highlighted.xml b/packages/SettingsLib/SettingsTheme/res/drawable-v35/settingslib_round_background_top_highlighted.xml
new file mode 100644
index 0000000..052eb01
--- /dev/null
+++ b/packages/SettingsLib/SettingsTheme/res/drawable-v35/settingslib_round_background_top_highlighted.xml
@@ -0,0 +1,35 @@
+<?xml version="1.0" encoding="utf-8"?><!--
+ Copyright (C) 2024 The Android Open Source Project
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+ -->
+
+<ripple xmlns:android="http://schemas.android.com/apk/res/android"
+ android:color="?android:colorControlHighlight">
+ <item
+ android:color="?android:attr/colorAccent"
+ android:end="?android:attr/listPreferredItemPaddingEnd"
+ android:start="?android:attr/listPreferredItemPaddingStart"
+ android:top="2dp">
+ <shape
+ android:shape="rectangle"
+ android:tint="?android:attr/colorAccent">
+ <corners
+ android:bottomLeftRadius="4dp"
+ android:bottomRightRadius="4dp"
+ android:topLeftRadius="@dimen/settingslib_preference_corner_radius"
+ android:topRightRadius="@dimen/settingslib_preference_corner_radius" />
+ <solid android:color="#42000000" />
+ </shape>
+ </item>
+</ripple>
\ No newline at end of file
diff --git a/packages/SettingsLib/SettingsTheme/src/com/android/settingslib/widget/SettingsBasePreferenceFragment.kt b/packages/SettingsLib/SettingsTheme/src/com/android/settingslib/widget/SettingsBasePreferenceFragment.kt
index 535d80f..265c065 100644
--- a/packages/SettingsLib/SettingsTheme/src/com/android/settingslib/widget/SettingsBasePreferenceFragment.kt
+++ b/packages/SettingsLib/SettingsTheme/src/com/android/settingslib/widget/SettingsBasePreferenceFragment.kt
@@ -21,7 +21,7 @@
import androidx.recyclerview.widget.RecyclerView
/** Base class for Settings to use PreferenceFragmentCompat */
-open abstract class SettingsBasePreferenceFragment : PreferenceFragmentCompat() {
+abstract class SettingsBasePreferenceFragment : PreferenceFragmentCompat() {
override fun onCreateAdapter(preferenceScreen: PreferenceScreen): RecyclerView.Adapter<*> {
if (SettingsThemeHelper.isExpressiveTheme(requireContext()))
diff --git a/packages/SettingsLib/SettingsTheme/src/com/android/settingslib/widget/SettingsPreferenceGroupAdapter.kt b/packages/SettingsLib/SettingsTheme/src/com/android/settingslib/widget/SettingsPreferenceGroupAdapter.kt
index 98b7f76..6a06320 100644
--- a/packages/SettingsLib/SettingsTheme/src/com/android/settingslib/widget/SettingsPreferenceGroupAdapter.kt
+++ b/packages/SettingsLib/SettingsTheme/src/com/android/settingslib/widget/SettingsPreferenceGroupAdapter.kt
@@ -16,8 +16,10 @@
package com.android.settingslib.widget
+import android.annotation.SuppressLint
import android.os.Handler
import android.os.Looper
+import android.util.TypedValue
import androidx.annotation.DrawableRes
import androidx.preference.Preference
import androidx.preference.PreferenceCategory
@@ -27,12 +29,12 @@
import com.android.settingslib.widget.theme.R
/**
- * A custom adapter for displaying settings preferences in a list, handling rounded corners
- * for preference items within a group.
+ * A custom adapter for displaying settings preferences in a list, handling rounded corners for
+ * preference items within a group.
*/
-open class SettingsPreferenceGroupAdapter @JvmOverloads constructor(
- preferenceGroup: PreferenceGroup
-) : PreferenceGroupAdapter(preferenceGroup) {
+@SuppressLint("RestrictedApi")
+open class SettingsPreferenceGroupAdapter(preferenceGroup: PreferenceGroup) :
+ PreferenceGroupAdapter(preferenceGroup) {
private val mPreferenceGroup = preferenceGroup
private var mRoundCornerMappingList: ArrayList<Int> = ArrayList()
@@ -41,6 +43,7 @@
private var mGroupPaddingStart = 0
private var mNormalPaddingEnd = 0
private var mGroupPaddingEnd = 0
+ @DrawableRes private var mLegacyBackgroundRes: Int
private val mHandler = Handler(Looper.getMainLooper())
@@ -54,9 +57,17 @@
mNormalPaddingEnd =
context.resources.getDimensionPixelSize(R.dimen.settingslib_expressive_space_small1)
mGroupPaddingEnd = mNormalPaddingEnd * 2
+ val outValue = TypedValue()
+ context.theme.resolveAttribute(
+ android.R.attr.selectableItemBackground,
+ outValue,
+ true, /* resolveRefs */
+ )
+ mLegacyBackgroundRes = outValue.resourceId
updatePreferences()
}
+ @SuppressLint("RestrictedApi")
override fun onPreferenceHierarchyChange(preference: Preference) {
super.onPreferenceHierarchyChange(preference)
@@ -65,6 +76,7 @@
mHandler.post(syncRunnable)
}
+ @SuppressLint("RestrictedApi")
override fun onBindViewHolder(holder: PreferenceViewHolder, position: Int) {
super.onBindViewHolder(holder, position)
updateBackground(holder, position)
@@ -79,6 +91,7 @@
}
}
+ @SuppressLint("RestrictedApi")
private fun mappingPreferenceGroup(cornerStyles: MutableList<Int>, group: PreferenceGroup) {
cornerStyles.clear()
cornerStyles.addAll(MutableList(itemCount) { 0 })
@@ -151,20 +164,38 @@
}
}
- /** handle roundCorner background */
+ /** handle roundCorner background */
private fun updateBackground(holder: PreferenceViewHolder, position: Int) {
- @DrawableRes val backgroundRes = getRoundCornerDrawableRes(position, false /* isSelected*/)
+ val context = holder.itemView.context
+ @DrawableRes
+ val backgroundRes =
+ when (SettingsThemeHelper.isExpressiveTheme(context)) {
+ true -> getRoundCornerDrawableRes(position, isSelected = false)
+ else -> mLegacyBackgroundRes
+ }
val v = holder.itemView
- val paddingStart = if (backgroundRes == 0) mNormalPaddingStart else mGroupPaddingStart
- val paddingEnd = if (backgroundRes == 0) mNormalPaddingEnd else mGroupPaddingEnd
-
- v.setPaddingRelative(paddingStart, v.paddingTop, paddingEnd, v.paddingBottom)
+ // Update padding
+ if (SettingsThemeHelper.isExpressiveTheme(context)) {
+ val paddingStart = if (backgroundRes == 0) mNormalPaddingStart else mGroupPaddingStart
+ val paddingEnd = if (backgroundRes == 0) mNormalPaddingEnd else mGroupPaddingEnd
+ v.setPaddingRelative(paddingStart, v.paddingTop, paddingEnd, v.paddingBottom)
+ }
+ // Update background
v.setBackgroundResource(backgroundRes)
}
@DrawableRes
protected fun getRoundCornerDrawableRes(position: Int, isSelected: Boolean): Int {
+ return getRoundCornerDrawableRes(position, isSelected, false)
+ }
+
+ @DrawableRes
+ protected fun getRoundCornerDrawableRes(
+ position: Int,
+ isSelected: Boolean,
+ isHighlighted: Boolean,
+ ): Int {
val cornerType = mRoundCornerMappingList[position]
if ((cornerType and ROUND_CORNER_CENTER) == 0) {
@@ -175,24 +206,28 @@
(cornerType and ROUND_CORNER_TOP) != 0 && (cornerType and ROUND_CORNER_BOTTOM) == 0 -> {
// the first
if (isSelected) R.drawable.settingslib_round_background_top_selected
+ else if (isHighlighted) R.drawable.settingslib_round_background_top_highlighted
else R.drawable.settingslib_round_background_top
}
(cornerType and ROUND_CORNER_BOTTOM) != 0 && (cornerType and ROUND_CORNER_TOP) == 0 -> {
// the last
if (isSelected) R.drawable.settingslib_round_background_bottom_selected
+ else if (isHighlighted) R.drawable.settingslib_round_background_bottom_highlighted
else R.drawable.settingslib_round_background_bottom
}
(cornerType and ROUND_CORNER_TOP) != 0 && (cornerType and ROUND_CORNER_BOTTOM) != 0 -> {
// the only one preference
if (isSelected) R.drawable.settingslib_round_background_selected
+ else if (isHighlighted) R.drawable.settingslib_round_background_highlighted
else R.drawable.settingslib_round_background
}
else -> {
// in the center
if (isSelected) R.drawable.settingslib_round_background_center_selected
+ else if (isHighlighted) R.drawable.settingslib_round_background_center_highlighted
else R.drawable.settingslib_round_background_center
}
}
@@ -203,4 +238,4 @@
private const val ROUND_CORNER_TOP: Int = 1 shl 1
private const val ROUND_CORNER_BOTTOM: Int = 1 shl 2
}
-}
\ No newline at end of file
+}