[Expressive design] update preference background with round corner

Bug: 366336385
Test: manual
Flag: EXEMPT resource only update
Change-Id: Ib104753c9755139480a08e9f34957dc0b268434b
diff --git a/packages/SettingsLib/ActionButtonsPreference/src/com/android/settingslib/widget/ActionButtonsPreference.java b/packages/SettingsLib/ActionButtonsPreference/src/com/android/settingslib/widget/ActionButtonsPreference.java
index b286182..601e001 100644
--- a/packages/SettingsLib/ActionButtonsPreference/src/com/android/settingslib/widget/ActionButtonsPreference.java
+++ b/packages/SettingsLib/ActionButtonsPreference/src/com/android/settingslib/widget/ActionButtonsPreference.java
@@ -57,7 +57,7 @@
  * 1. User sets invisible for button. ex: ActionButtonPreference.setButton1Visible(false)
  * 2. User doesn't set any title or icon for button.
  */
-public class ActionButtonsPreference extends Preference {
+public class ActionButtonsPreference extends Preference implements GroupSectionDividerMixin {
 
     private static final String TAG = "ActionButtonPreference";
     private static final boolean mIsAtLeastS = Build.VERSION.SDK_INT >= Build.VERSION_CODES.S;
diff --git a/packages/SettingsLib/BannerMessagePreference/src/com/android/settingslib/widget/BannerMessagePreference.java b/packages/SettingsLib/BannerMessagePreference/src/com/android/settingslib/widget/BannerMessagePreference.java
index 6cd777e..10769ec 100644
--- a/packages/SettingsLib/BannerMessagePreference/src/com/android/settingslib/widget/BannerMessagePreference.java
+++ b/packages/SettingsLib/BannerMessagePreference/src/com/android/settingslib/widget/BannerMessagePreference.java
@@ -43,7 +43,7 @@
  * Banner message is a banner displaying important information (permission request, page error etc),
  * and provide actions for user to address. It requires a user action to be dismissed.
  */
-public class BannerMessagePreference extends Preference {
+public class BannerMessagePreference extends Preference implements GroupSectionDividerMixin {
 
     public enum AttentionLevel {
         HIGH(0, R.color.banner_background_attention_high, R.color.banner_accent_attention_high),
diff --git a/packages/SettingsLib/CardPreference/src/com/android/settingslib/widget/CardPreference.kt b/packages/SettingsLib/CardPreference/src/com/android/settingslib/widget/CardPreference.kt
index eb14746..84ff1bb 100644
--- a/packages/SettingsLib/CardPreference/src/com/android/settingslib/widget/CardPreference.kt
+++ b/packages/SettingsLib/CardPreference/src/com/android/settingslib/widget/CardPreference.kt
@@ -30,7 +30,7 @@
     attrs: AttributeSet? = null,
     defStyleAttr: Int = 0,
     defStyleRes: Int = 0
-) : Preference(context, attrs, defStyleAttr, defStyleRes) {
+) : Preference(context, attrs, defStyleAttr, defStyleRes), GroupSectionDividerMixin {
 
     init {
         layoutResource = R.layout.settingslib_expressive_preference_card
diff --git a/packages/SettingsLib/IllustrationPreference/Android.bp b/packages/SettingsLib/IllustrationPreference/Android.bp
index cd8f584..12890fe 100644
--- a/packages/SettingsLib/IllustrationPreference/Android.bp
+++ b/packages/SettingsLib/IllustrationPreference/Android.bp
@@ -21,6 +21,7 @@
         "SettingsLibColor",
         "androidx.preference_preference",
         "lottie",
+        "SettingsLibSettingsTheme",
         "settingslib_illustrationpreference_flags_lib",
     ],
 
diff --git a/packages/SettingsLib/IllustrationPreference/src/com/android/settingslib/widget/IllustrationPreference.java b/packages/SettingsLib/IllustrationPreference/src/com/android/settingslib/widget/IllustrationPreference.java
index a0599bb..adc4f316 100644
--- a/packages/SettingsLib/IllustrationPreference/src/com/android/settingslib/widget/IllustrationPreference.java
+++ b/packages/SettingsLib/IllustrationPreference/src/com/android/settingslib/widget/IllustrationPreference.java
@@ -55,7 +55,7 @@
 /**
  * IllustrationPreference is a preference that can play lottie format animation
  */
-public class IllustrationPreference extends Preference {
+public class IllustrationPreference extends Preference implements GroupSectionDividerMixin {
 
     private static final String TAG = "IllustrationPreference";
 
diff --git a/packages/SettingsLib/MainSwitchPreference/res/layout-v31/settingslib_main_switch_bar.xml b/packages/SettingsLib/MainSwitchPreference/res/layout-v31/settingslib_main_switch_bar.xml
index e3f8fbb..2e3ee32 100644
--- a/packages/SettingsLib/MainSwitchPreference/res/layout-v31/settingslib_main_switch_bar.xml
+++ b/packages/SettingsLib/MainSwitchPreference/res/layout-v31/settingslib_main_switch_bar.xml
@@ -20,8 +20,6 @@
     android:layout_height="wrap_content"
     android:layout_width="match_parent"
     android:minHeight="?android:attr/listPreferredItemHeight"
-    android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
-    android:paddingStart="?android:attr/listPreferredItemPaddingStart"
     android:paddingTop="@dimen/settingslib_switchbar_margin"
     android:paddingBottom="@dimen/settingslib_switchbar_margin"
     android:orientation="vertical">
diff --git a/packages/SettingsLib/MainSwitchPreference/res/layout-v33/settingslib_main_switch_bar.xml b/packages/SettingsLib/MainSwitchPreference/res/layout-v33/settingslib_main_switch_bar.xml
index 255b2c9..3e0e184 100644
--- a/packages/SettingsLib/MainSwitchPreference/res/layout-v33/settingslib_main_switch_bar.xml
+++ b/packages/SettingsLib/MainSwitchPreference/res/layout-v33/settingslib_main_switch_bar.xml
@@ -20,8 +20,6 @@
     android:layout_height="wrap_content"
     android:layout_width="match_parent"
     android:minHeight="?android:attr/listPreferredItemHeight"
-    android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
-    android:paddingStart="?android:attr/listPreferredItemPaddingStart"
     android:paddingTop="@dimen/settingslib_switchbar_margin"
     android:paddingBottom="@dimen/settingslib_switchbar_margin"
     android:orientation="vertical">
diff --git a/packages/SettingsLib/MainSwitchPreference/res/layout-v35/settingslib_expressive_main_switch_bar.xml b/packages/SettingsLib/MainSwitchPreference/res/layout-v35/settingslib_expressive_main_switch_bar.xml
index 4425ef0..f75d9b2 100644
--- a/packages/SettingsLib/MainSwitchPreference/res/layout-v35/settingslib_expressive_main_switch_bar.xml
+++ b/packages/SettingsLib/MainSwitchPreference/res/layout-v35/settingslib_expressive_main_switch_bar.xml
@@ -20,8 +20,6 @@
     android:layout_height="wrap_content"
     android:layout_width="match_parent"
     android:minHeight="?android:attr/listPreferredItemHeight"
-    android:paddingStart="?android:attr/listPreferredItemPaddingStart"
-    android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
     android:paddingVertical="@dimen/settingslib_expressive_space_small1"
     android:orientation="vertical">
 
diff --git a/packages/SettingsLib/MainSwitchPreference/res/layout/settingslib_main_switch_bar.xml b/packages/SettingsLib/MainSwitchPreference/res/layout/settingslib_main_switch_bar.xml
index bf34db9..7c0eaea 100644
--- a/packages/SettingsLib/MainSwitchPreference/res/layout/settingslib_main_switch_bar.xml
+++ b/packages/SettingsLib/MainSwitchPreference/res/layout/settingslib_main_switch_bar.xml
@@ -18,11 +18,7 @@
 <LinearLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_height="wrap_content"
-    android:layout_width="match_parent"
-    android:paddingLeft="?android:attr/listPreferredItemPaddingLeft"
-    android:paddingStart="?android:attr/listPreferredItemPaddingStart"
-    android:paddingRight="?android:attr/listPreferredItemPaddingRight"
-    android:paddingEnd="?android:attr/listPreferredItemPaddingEnd">
+    android:layout_width="match_parent">
 
     <TextView
         android:id="@+id/switch_text"
diff --git a/packages/SettingsLib/MainSwitchPreference/res/layout/settingslib_main_switch_layout.xml b/packages/SettingsLib/MainSwitchPreference/res/layout/settingslib_main_switch_layout.xml
index bef6e35..fa908a4 100644
--- a/packages/SettingsLib/MainSwitchPreference/res/layout/settingslib_main_switch_layout.xml
+++ b/packages/SettingsLib/MainSwitchPreference/res/layout/settingslib_main_switch_layout.xml
@@ -18,6 +18,10 @@
 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_height="wrap_content"
     android:layout_width="match_parent"
+    android:paddingLeft="?android:attr/listPreferredItemPaddingLeft"
+    android:paddingStart="?android:attr/listPreferredItemPaddingStart"
+    android:paddingRight="?android:attr/listPreferredItemPaddingRight"
+    android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
     android:importantForAccessibility="no">
 
     <com.android.settingslib.widget.MainSwitchBar
diff --git a/packages/SettingsLib/MainSwitchPreference/src/com/android/settingslib/widget/MainSwitchPreference.java b/packages/SettingsLib/MainSwitchPreference/src/com/android/settingslib/widget/MainSwitchPreference.java
index d895c87..3394874 100644
--- a/packages/SettingsLib/MainSwitchPreference/src/com/android/settingslib/widget/MainSwitchPreference.java
+++ b/packages/SettingsLib/MainSwitchPreference/src/com/android/settingslib/widget/MainSwitchPreference.java
@@ -36,7 +36,8 @@
  * This component is used as the main switch of the page
  * to enable or disable the prefereces on the page.
  */
-public class MainSwitchPreference extends TwoStatePreference implements OnCheckedChangeListener {
+public class MainSwitchPreference extends TwoStatePreference
+        implements OnCheckedChangeListener, GroupSectionDividerMixin {
 
     private final List<OnCheckedChangeListener> mSwitchChangeListeners = new ArrayList<>();
 
diff --git a/packages/SettingsLib/SettingsTheme/src/com/android/settingslib/widget/GroupSectionDividerMixin.kt b/packages/SettingsLib/SettingsTheme/src/com/android/settingslib/widget/GroupSectionDividerMixin.kt
new file mode 100644
index 0000000..ba5f5cf
--- /dev/null
+++ b/packages/SettingsLib/SettingsTheme/src/com/android/settingslib/widget/GroupSectionDividerMixin.kt
@@ -0,0 +1,24 @@
+/*
+ * 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.
+ */
+
+package com.android.settingslib.widget
+
+/**
+ * A base interface to indicate that a class should not have rounded corners.
+ *
+ * Classes implementing this interface will be treated as already handle rounded corners.
+ */
+interface GroupSectionDividerMixin
\ 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
new file mode 100644
index 0000000..535d80f
--- /dev/null
+++ b/packages/SettingsLib/SettingsTheme/src/com/android/settingslib/widget/SettingsBasePreferenceFragment.kt
@@ -0,0 +1,31 @@
+/*
+ * 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.
+ */
+
+package com.android.settingslib.widget
+
+import androidx.preference.PreferenceFragmentCompat
+import androidx.preference.PreferenceScreen
+import androidx.recyclerview.widget.RecyclerView
+
+/** Base class for Settings to use PreferenceFragmentCompat */
+open abstract class SettingsBasePreferenceFragment : PreferenceFragmentCompat() {
+
+    override fun onCreateAdapter(preferenceScreen: PreferenceScreen): RecyclerView.Adapter<*> {
+        if (SettingsThemeHelper.isExpressiveTheme(requireContext()))
+            return SettingsPreferenceGroupAdapter(preferenceScreen)
+        return super.onCreateAdapter(preferenceScreen)
+    }
+}
\ No newline at end of file
diff --git a/packages/SettingsLib/SettingsTheme/src/com/android/settingslib/widget/SettingsPreferenceGroupAdapter.kt b/packages/SettingsLib/SettingsTheme/src/com/android/settingslib/widget/SettingsPreferenceGroupAdapter.kt
new file mode 100644
index 0000000..98b7f76
--- /dev/null
+++ b/packages/SettingsLib/SettingsTheme/src/com/android/settingslib/widget/SettingsPreferenceGroupAdapter.kt
@@ -0,0 +1,206 @@
+/*
+ * 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.
+ */
+
+package com.android.settingslib.widget
+
+import android.os.Handler
+import android.os.Looper
+import androidx.annotation.DrawableRes
+import androidx.preference.Preference
+import androidx.preference.PreferenceCategory
+import androidx.preference.PreferenceGroup
+import androidx.preference.PreferenceGroupAdapter
+import androidx.preference.PreferenceViewHolder
+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.
+ */
+open class SettingsPreferenceGroupAdapter @JvmOverloads constructor(
+    preferenceGroup: PreferenceGroup
+) : PreferenceGroupAdapter(preferenceGroup) {
+
+    private val mPreferenceGroup = preferenceGroup
+    private var mRoundCornerMappingList: ArrayList<Int> = ArrayList()
+
+    private var mNormalPaddingStart = 0
+    private var mGroupPaddingStart = 0
+    private var mNormalPaddingEnd = 0
+    private var mGroupPaddingEnd = 0
+
+    private val mHandler = Handler(Looper.getMainLooper())
+
+    private val syncRunnable = Runnable { updatePreferences() }
+
+    init {
+        val context = preferenceGroup.context
+        mNormalPaddingStart =
+            context.resources.getDimensionPixelSize(R.dimen.settingslib_expressive_space_small1)
+        mGroupPaddingStart = mNormalPaddingStart * 2
+        mNormalPaddingEnd =
+            context.resources.getDimensionPixelSize(R.dimen.settingslib_expressive_space_small1)
+        mGroupPaddingEnd = mNormalPaddingEnd * 2
+        updatePreferences()
+    }
+
+    override fun onPreferenceHierarchyChange(preference: Preference) {
+        super.onPreferenceHierarchyChange(preference)
+
+        // Post after super class has posted their sync runnable to update preferences.
+        mHandler.removeCallbacks(syncRunnable)
+        mHandler.post(syncRunnable)
+    }
+
+    override fun onBindViewHolder(holder: PreferenceViewHolder, position: Int) {
+        super.onBindViewHolder(holder, position)
+        updateBackground(holder, position)
+    }
+
+    private fun updatePreferences() {
+        val oldList = ArrayList(mRoundCornerMappingList)
+        mRoundCornerMappingList = ArrayList()
+        mappingPreferenceGroup(mRoundCornerMappingList, mPreferenceGroup)
+        if (mRoundCornerMappingList != oldList) {
+            notifyDataSetChanged()
+        }
+    }
+
+    private fun mappingPreferenceGroup(cornerStyles: MutableList<Int>, group: PreferenceGroup) {
+        cornerStyles.clear()
+        cornerStyles.addAll(MutableList(itemCount) { 0 })
+
+        // the first item in to group
+        var startIndex = -1
+        // the last item in the group
+        var endIndex = -1
+        var currentParent: PreferenceGroup? = group
+        for (i in 0 until itemCount) {
+            when (val pref = getItem(i)) {
+                // the preference has round corner background, so we don't need to handle it.
+                is GroupSectionDividerMixin -> {
+                    cornerStyles[i] = 0
+                    startIndex = -1
+                    endIndex = -1
+                }
+
+                // PreferenceCategory should not have round corner background.
+                is PreferenceCategory -> {
+                    cornerStyles[i] = 0
+                    startIndex = -1
+                    endIndex = -1
+                    currentParent = pref
+                }
+
+                // ExpandablePreference is PreferenceGroup but it should handle round corner
+                is Expandable -> {
+                    // When ExpandablePreference is expanded, we treat is as the first item.
+                    if (pref.isExpanded()) {
+                        currentParent = pref as? PreferenceGroup
+                        startIndex = i
+                        cornerStyles[i] = cornerStyles[i] or ROUND_CORNER_TOP or ROUND_CORNER_CENTER
+                        endIndex = -1
+                    }
+                }
+
+                else -> {
+                    val parent = pref?.parent
+
+                    // item in the group should have round corner background.
+                    cornerStyles[i] = cornerStyles[i] or ROUND_CORNER_CENTER
+                    if (parent === currentParent) {
+                        // find the first item in the group
+                        if (startIndex == -1) {
+                            startIndex = i
+                            cornerStyles[i] = cornerStyles[i] or ROUND_CORNER_TOP
+                        }
+
+                        // find the last item in the group, if we find the new last item, we should
+                        // remove the old last item round corner.
+                        if (endIndex == -1 || endIndex < i) {
+                            if (endIndex != -1) {
+                                cornerStyles[endIndex] =
+                                    cornerStyles[endIndex] and ROUND_CORNER_BOTTOM.inv()
+                            }
+                            endIndex = i
+                            cornerStyles[i] = cornerStyles[i] or ROUND_CORNER_BOTTOM
+                        }
+                    } else {
+                        // this item is new group, we should reset the index.
+                        currentParent = parent
+                        startIndex = i
+                        cornerStyles[i] = cornerStyles[i] or ROUND_CORNER_TOP
+                        endIndex = i
+                        cornerStyles[i] = cornerStyles[i] or ROUND_CORNER_BOTTOM
+                    }
+                }
+            }
+        }
+    }
+
+    /** handle roundCorner background  */
+    private fun updateBackground(holder: PreferenceViewHolder, position: Int) {
+        @DrawableRes val backgroundRes = getRoundCornerDrawableRes(position, false /* isSelected*/)
+
+        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)
+        v.setBackgroundResource(backgroundRes)
+    }
+
+    @DrawableRes
+    protected fun getRoundCornerDrawableRes(position: Int, isSelected: Boolean): Int {
+        val cornerType = mRoundCornerMappingList[position]
+
+        if ((cornerType and ROUND_CORNER_CENTER) == 0) {
+            return 0
+        }
+
+        return when {
+            (cornerType and ROUND_CORNER_TOP) != 0 && (cornerType and ROUND_CORNER_BOTTOM) == 0 -> {
+                // the first
+                if (isSelected) R.drawable.settingslib_round_background_top_selected
+                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 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 R.drawable.settingslib_round_background
+            }
+
+            else -> {
+                // in the center
+                if (isSelected) R.drawable.settingslib_round_background_center_selected
+                else R.drawable.settingslib_round_background_center
+            }
+        }
+    }
+
+    companion object {
+        private const val ROUND_CORNER_CENTER: Int = 1
+        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
diff --git a/packages/SettingsLib/ZeroStatePreference/src/com/android/settingslib/widget/ZeroStatePreference.kt b/packages/SettingsLib/ZeroStatePreference/src/com/android/settingslib/widget/ZeroStatePreference.kt
index 9b1ccef..62573fe 100644
--- a/packages/SettingsLib/ZeroStatePreference/src/com/android/settingslib/widget/ZeroStatePreference.kt
+++ b/packages/SettingsLib/ZeroStatePreference/src/com/android/settingslib/widget/ZeroStatePreference.kt
@@ -32,7 +32,7 @@
     attrs: AttributeSet? = null,
     defStyleAttr: Int = 0,
     defStyleRes: Int = 0
-) : Preference(context, attrs, defStyleAttr, defStyleRes) {
+) : Preference(context, attrs, defStyleAttr, defStyleRes), GroupSectionDividerMixin {
 
     private val iconTint: Int = context.getColor(
         com.android.settingslib.widget.theme.R.color.settingslib_materialColorOnSecondaryContainer