[Expressive design] Add expandable preference

Bug: 367539940
Test: manual
Flag: EXEMPT resource only update

Change-Id: I94c9f277294477c05c0f430a0302b1f4690bf7b0
diff --git a/packages/SettingsLib/Android.bp b/packages/SettingsLib/Android.bp
index af07686..a36ec00 100644
--- a/packages/SettingsLib/Android.bp
+++ b/packages/SettingsLib/Android.bp
@@ -39,6 +39,7 @@
         "SettingsLibDisplayUtils",
         "SettingsLibEmergencyNumber",
         "SettingsLibEntityHeaderWidgets",
+        "SettingsLibExpandablePreference",
         "SettingsLibFooterPreference",
         "SettingsLibHelpUtils",
         "SettingsLibIllustrationPreference",
diff --git a/packages/SettingsLib/ExpandablePreference/Android.bp b/packages/SettingsLib/ExpandablePreference/Android.bp
new file mode 100644
index 0000000..e6de3f1
--- /dev/null
+++ b/packages/SettingsLib/ExpandablePreference/Android.bp
@@ -0,0 +1,33 @@
+package {
+    // See: http://go/android-license-faq
+    // A large-scale-change added 'default_applicable_licenses' to import
+    // all of the 'license_kinds' from "frameworks_base_license"
+    // to get the below license kinds:
+    //   SPDX-license-identifier-Apache-2.0
+    default_applicable_licenses: ["frameworks_base_license"],
+}
+
+android_library {
+    name: "SettingsLibExpandablePreference",
+    use_resource_processor: true,
+    defaults: [
+        "SettingsLintDefaults",
+    ],
+
+    srcs: [
+        "src/**/*.java",
+        "src/**/*.kt",
+    ],
+    resource_dirs: ["res"],
+
+    static_libs: [
+        "androidx.annotation_annotation",
+        "androidx.preference_preference",
+        "SettingsLibSettingsTheme",
+    ],
+    sdk_version: "system_current",
+    min_sdk_version: "21",
+    apex_available: [
+        "//apex_available:platform",
+    ],
+}
diff --git a/packages/SettingsLib/ExpandablePreference/AndroidManifest.xml b/packages/SettingsLib/ExpandablePreference/AndroidManifest.xml
new file mode 100644
index 0000000..7bfa19b
--- /dev/null
+++ b/packages/SettingsLib/ExpandablePreference/AndroidManifest.xml
@@ -0,0 +1,23 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+  Copyright (C) 2020 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.
+  -->
+
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+          package="com.android.settingslib.widget.preference.expandable">
+
+    <uses-sdk android:minSdkVersion="21" />
+
+</manifest>
diff --git a/packages/SettingsLib/ExpandablePreference/res/drawable/settingslib_ic_expand.xml b/packages/SettingsLib/ExpandablePreference/res/drawable/settingslib_ic_expand.xml
new file mode 100644
index 0000000..4502311
--- /dev/null
+++ b/packages/SettingsLib/ExpandablePreference/res/drawable/settingslib_ic_expand.xml
@@ -0,0 +1,37 @@
+<?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.
+  -->
+
+<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
+    <item>
+        <shape
+            android:shape="oval">
+            <size android:width="32dp" android:height="40dp"/>
+            <solid android:color="@color/settingslib_materialColorSurfaceContainer"/>
+        </shape>
+    </item>
+    <item>
+        <vector
+            android:width="32dp"
+            android:height="40dp"
+            android:viewportWidth="32"
+            android:viewportHeight="40">
+            <path
+                android:pathData="M16,23.063L11,18.063L12.063,17L16,20.938L19.938,17L21,18.063L16,23.063Z"
+                android:fillColor="@color/settingslib_materialColorOnSurface"/>
+        </vector>
+    </item>
+</layer-list>
\ No newline at end of file
diff --git a/packages/SettingsLib/ExpandablePreference/res/layout/settingslib_widget_expandable_icon.xml b/packages/SettingsLib/ExpandablePreference/res/layout/settingslib_widget_expandable_icon.xml
new file mode 100644
index 0000000..f7fefef
--- /dev/null
+++ b/packages/SettingsLib/ExpandablePreference/res/layout/settingslib_widget_expandable_icon.xml
@@ -0,0 +1,29 @@
+<?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.
+  -->
+
+<LinearLayout
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="wrap_content"
+    android:orientation="vertical">
+
+    <ImageView
+        android:id="@+id/expand_icon"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:src="@drawable/settingslib_ic_expand"/>
+</LinearLayout>
\ No newline at end of file
diff --git a/packages/SettingsLib/ExpandablePreference/src/com/android/settingslib/widget/ExpandablePreference.kt b/packages/SettingsLib/ExpandablePreference/src/com/android/settingslib/widget/ExpandablePreference.kt
new file mode 100644
index 0000000..1b93ebe
--- /dev/null
+++ b/packages/SettingsLib/ExpandablePreference/src/com/android/settingslib/widget/ExpandablePreference.kt
@@ -0,0 +1,95 @@
+/*
+ * 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.content.Context
+import android.util.AttributeSet
+import android.widget.ImageView
+import androidx.preference.Preference
+import androidx.preference.PreferenceGroup
+import androidx.preference.PreferenceViewHolder
+import com.android.settingslib.widget.preference.expandable.R
+
+class ExpandablePreference @JvmOverloads constructor(
+    context: Context,
+    attrs: AttributeSet? = null,
+    defStyleAttr: Int = 0,
+    defStyleRes: Int = 0
+) : PreferenceGroup(context, attrs, defStyleAttr, defStyleRes), Expandable {
+
+    private var isExpanded = false
+    private var expandIcon: ImageView? = null
+    private var isDirty = true // Flag to track changes
+
+    init {
+        layoutResource = com.android.settingslib.widget.theme.R.layout.settingslib_expressive_preference
+        widgetLayoutResource = R.layout.settingslib_widget_expandable_icon
+    }
+
+    override fun onBindViewHolder(holder: PreferenceViewHolder) {
+        super.onBindViewHolder(holder)
+
+        holder.isDividerAllowedAbove = false
+        holder.isDividerAllowedBelow = false
+
+        expandIcon = holder.findViewById(R.id.expand_icon) as ImageView?
+
+        updateExpandedState()
+
+        holder.itemView.setOnClickListener { toggleExpansion() }
+    }
+
+    override fun addPreference(preference: Preference): Boolean {
+        preference.isVisible = isExpanded
+        return super.addPreference(preference)
+    }
+
+    override fun onPrepareAddPreference(preference: Preference): Boolean {
+        preference.isVisible = isExpanded
+        return super.onPrepareAddPreference(preference)
+    }
+
+    override fun isExpanded(): Boolean {
+        return isExpanded
+    }
+
+    private fun toggleExpansion() {
+        isExpanded = !isExpanded
+        isDirty = true // Mark as dirty when expansion state changes
+        updateExpandedState()
+        notifyChanged()
+    }
+
+    private fun updateExpandedState() {
+        expandIcon?.rotation = when (isExpanded) {
+            true -> ROTATION_EXPANDED
+            false -> ROTATION_COLLAPSED
+        }
+
+        if (isDirty) {
+            (0 until preferenceCount).forEach { i ->
+                getPreference(i).isVisible = isExpanded
+            }
+            isDirty = false
+        }
+    }
+
+    companion object {
+        private const val ROTATION_EXPANDED = 180f
+        private const val ROTATION_COLLAPSED = 0f
+    }
+}
\ No newline at end of file
diff --git a/packages/SettingsLib/SettingsTheme/src/com/android/settingslib/widget/Expandable.kt b/packages/SettingsLib/SettingsTheme/src/com/android/settingslib/widget/Expandable.kt
new file mode 100644
index 0000000..1f84118
--- /dev/null
+++ b/packages/SettingsLib/SettingsTheme/src/com/android/settingslib/widget/Expandable.kt
@@ -0,0 +1,21 @@
+/*
+ * 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
+
+interface Expandable {
+    fun isExpanded(): Boolean
+}
\ No newline at end of file