[Expressive Design] Add SegmentedButtonPreference

Bug: 384654397
Test: manual
Flag: EXEMPT library update
Change-Id: I39f26302bcb2109432d8868578273af0436ab589
diff --git a/packages/SettingsLib/Android.bp b/packages/SettingsLib/Android.bp
index d739aaf..933c512 100644
--- a/packages/SettingsLib/Android.bp
+++ b/packages/SettingsLib/Android.bp
@@ -10,19 +10,19 @@
 android_library {
     name: "SettingsLib",
     defaults: [
-        "SettingsLintDefaults",
         "SettingsLibAvatarPickerDefaults",
+        "SettingsLintDefaults",
     ],
 
     static_libs: [
         "androidx.localbroadcastmanager_localbroadcastmanager",
         "androidx.room_room-runtime",
         "androidx.sqlite_sqlite",
-        "zxing-core",
         "guava",
+        "zxing-core",
 
-        "WifiTrackerLibRes",
         "//frameworks/libs/systemui:iconloader",
+        "WifiTrackerLibRes",
         "setupdesign",
 
         "SettingsLibActionBarShadow",
@@ -31,8 +31,8 @@
         "SettingsLibAppPreference",
         "SettingsLibBannerMessagePreference",
         "SettingsLibBarChartPreference",
-        "SettingsLibButtonPreference",
         "SettingsLibBulletPreference",
+        "SettingsLibButtonPreference",
         "SettingsLibCardPreference",
         "SettingsLibCollapsingToolbarBaseActivity",
         "SettingsLibDeviceStateRotationLock",
@@ -50,6 +50,7 @@
         "SettingsLibProgressBar",
         "SettingsLibRestrictedLockUtils",
         "SettingsLibSearchWidget",
+        "SettingsLibSegmentedButtonPreference",
         "SettingsLibSelectorWithWidgetPreference",
         "SettingsLibSettingsSpinner",
         "SettingsLibSettingsTransition",
@@ -62,7 +63,7 @@
         "SettingsLibZeroStatePreference",
         "settingslib_media_flags_lib",
     ],
-    libs:[
+    libs: [
         // This flag library has been added in frameworks jar
         "aconfig_settingslib_flags_java_lib",
         "wifi_framework_aconfig_flags_lib",
@@ -118,8 +119,8 @@
         "legacy_avatar_picker_app_enabled",
     ],
     properties: [
-        "static_libs",
         "manifest",
+        "static_libs",
     ],
 }
 
diff --git a/packages/SettingsLib/SegmentedButtonPreference/Android.bp b/packages/SettingsLib/SegmentedButtonPreference/Android.bp
new file mode 100644
index 0000000..6522e98
--- /dev/null
+++ b/packages/SettingsLib/SegmentedButtonPreference/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: "SettingsLibSegmentedButtonPreference",
+    use_resource_processor: true,
+    defaults: [
+        "SettingsLintDefaults",
+    ],
+
+    srcs: [
+        "src/**/*.java",
+        "src/**/*.kt",
+    ],
+    resource_dirs: ["res"],
+
+    static_libs: [
+        "SettingsLibSettingsTheme",
+        "androidx.annotation_annotation",
+        "androidx.preference_preference",
+    ],
+    sdk_version: "system_current",
+    min_sdk_version: "21",
+    apex_available: [
+        "//apex_available:platform",
+    ],
+}
diff --git a/packages/SettingsLib/SegmentedButtonPreference/AndroidManifest.xml b/packages/SettingsLib/SegmentedButtonPreference/AndroidManifest.xml
new file mode 100644
index 0000000..d71bc36
--- /dev/null
+++ b/packages/SettingsLib/SegmentedButtonPreference/AndroidManifest.xml
@@ -0,0 +1,23 @@
+<?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.
+  -->
+
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+          package="com.android.settingslib.widget.preference.segmentedbutton">
+
+    <uses-sdk android:minSdkVersion="21" />
+
+</manifest>
diff --git a/packages/SettingsLib/SegmentedButtonPreference/res/layout/settingslib_expressive_preference_segmentedbutton.xml b/packages/SettingsLib/SegmentedButtonPreference/res/layout/settingslib_expressive_preference_segmentedbutton.xml
new file mode 100644
index 0000000..12e2872
--- /dev/null
+++ b/packages/SettingsLib/SegmentedButtonPreference/res/layout/settingslib_expressive_preference_segmentedbutton.xml
@@ -0,0 +1,104 @@
+<?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"
+    xmlns:app="http://schemas.android.com/apk/res-auto"
+    android:layout_width="match_parent"
+    android:layout_height="wrap_content"
+    android:orientation="vertical"
+    android:paddingStart="?android:attr/listPreferredItemPaddingStart"
+    android:paddingEnd="?android:attr/listPreferredItemPaddingEnd">
+
+    <com.google.android.material.button.MaterialButtonToggleGroup
+        android:id="@+id/button_group"
+        style="@style/Widget.Material3Expressive.MaterialButtonGroup.Connected"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        app:singleSelection="true"
+        app:selectionRequired="true"
+        app:checkedButton="@id/button_1">
+        <com.google.android.material.button.MaterialButton
+            android:id="@+id/button_1"
+            android:layout_weight="1"
+            app:iconPadding="0dp"
+            app:iconGravity="textStart"
+            style="@style/SettingsLibButtonStyle.Expressive.Tonal.Large"/>
+        <com.google.android.material.button.MaterialButton
+            android:id="@+id/button_2"
+            android:layout_weight="1"
+            app:iconPadding="0dp"
+            app:iconGravity="textStart"
+            style="@style/SettingsLibButtonStyle.Expressive.Tonal.Large"/>
+        <com.google.android.material.button.MaterialButton
+            android:id="@+id/button_3"
+            android:layout_weight="1"
+            app:iconPadding="0dp"
+            app:iconGravity="textStart"
+            style="@style/SettingsLibButtonStyle.Expressive.Tonal.Large"/>
+        <com.google.android.material.button.MaterialButton
+            android:id="@+id/button_4"
+            android:layout_weight="1"
+            app:iconPadding="0dp"
+            app:iconGravity="textStart"
+            style="@style/SettingsLibButtonStyle.Expressive.Tonal.Large"/>
+    </com.google.android.material.button.MaterialButtonToggleGroup>
+
+    <LinearLayout
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:id="@+id/button_group_text"
+        android:orientation="horizontal"
+        android:gravity="center"
+        android:paddingTop="@dimen/settingslib_expressive_space_extrasmall4">
+
+        <TextView
+            android:id="@+id/button_1_text"
+            android:layout_weight="1"
+            android:layout_width="0dp"
+            android:layout_height="wrap_content"
+            android:gravity="center"
+            android:textAppearance="@style/TextAppearance.SettingsLib.TitleSmall.Emphasized"
+            android:textColor="@color/settingslib_materialColorOnSurface" />
+
+        <TextView
+            android:id="@+id/button_2_text"
+            android:layout_weight="1"
+            android:layout_width="0dp"
+            android:layout_height="wrap_content"
+            android:gravity="center"
+            android:textAppearance="@style/TextAppearance.SettingsLib.TitleSmall.Emphasized"
+            android:textColor="@color/settingslib_materialColorOnSurface" />
+
+        <TextView
+            android:id="@+id/button_3_text"
+            android:layout_weight="1"
+            android:layout_width="0dp"
+            android:layout_height="wrap_content"
+            android:gravity="center"
+            android:textAppearance="@style/TextAppearance.SettingsLib.TitleSmall.Emphasized"
+            android:textColor="@color/settingslib_materialColorOnSurface" />
+
+        <TextView
+            android:id="@+id/button_4_text"
+            android:layout_weight="1"
+            android:layout_width="0dp"
+            android:layout_height="wrap_content"
+            android:gravity="center"
+            android:textAppearance="@style/TextAppearance.SettingsLib.TitleSmall.Emphasized"
+            android:textColor="@color/settingslib_materialColorOnSurface" />
+    </LinearLayout>
+</LinearLayout>
\ No newline at end of file
diff --git a/packages/SettingsLib/SegmentedButtonPreference/src/com/android/settingslib/widget/SegmentedButtonPreference.kt b/packages/SettingsLib/SegmentedButtonPreference/src/com/android/settingslib/widget/SegmentedButtonPreference.kt
new file mode 100644
index 0000000..b133969
--- /dev/null
+++ b/packages/SettingsLib/SegmentedButtonPreference/src/com/android/settingslib/widget/SegmentedButtonPreference.kt
@@ -0,0 +1,88 @@
+/*
+ * 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.view.View.GONE
+import android.view.View.VISIBLE
+import android.widget.TextView
+import androidx.annotation.DrawableRes
+import androidx.preference.Preference
+import androidx.preference.PreferenceViewHolder
+import com.android.settingslib.widget.preference.segmentedbutton.R
+import com.google.android.material.button.MaterialButton
+import com.google.android.material.button.MaterialButtonToggleGroup
+
+class SegmentedButtonPreference @JvmOverloads constructor(
+    context: Context,
+    attrs: AttributeSet? = null,
+    defStyleAttr: Int = 0,
+    defStyleRes: Int = 0
+) : Preference(context, attrs, defStyleAttr, defStyleRes), GroupSectionDividerMixin {
+    private var buttonGroup: MaterialButtonToggleGroup? = null
+    private var buttonLabels: MutableList<TextView> = mutableListOf()
+    private var buttonCheckedListener: MaterialButtonToggleGroup.OnButtonCheckedListener? = null
+
+    init {
+        layoutResource = R.layout.settingslib_expressive_preference_segmentedbutton
+    }
+
+    override fun onBindViewHolder(holder: PreferenceViewHolder) {
+        super.onBindViewHolder(holder)
+        holder.isDividerAllowedBelow = false
+        holder.isDividerAllowedAbove = false
+
+        buttonGroup = holder.findViewById(R.id.button_group) as MaterialButtonToggleGroup?
+        buttonLabels.add(holder.findViewById(R.id.button_1_text) as TextView)
+        buttonLabels.add(holder.findViewById(R.id.button_2_text) as TextView)
+        buttonLabels.add(holder.findViewById(R.id.button_3_text) as TextView)
+        buttonLabels.add(holder.findViewById(R.id.button_4_text) as TextView)
+    }
+
+    fun setupButton(index: Int, text: String, @DrawableRes icon: Int) {
+        if (index in 0 until buttonLabels.size) {
+            (buttonGroup?.getChildAt(index) as? MaterialButton)?.setIconResource(icon)
+            buttonLabels[index].text = text
+        }
+    }
+
+    fun setButtonVisibility(index: Int, visible: Boolean) {
+        if (index in 0 until buttonLabels.size) {
+            (buttonGroup?.getChildAt(index) as? MaterialButton)?.visibility =
+                if (visible) VISIBLE else GONE
+
+            buttonLabels[index].visibility = if (visible) VISIBLE else GONE
+        }
+    }
+
+    fun setButtonEnabled(index: Int, enabled: Boolean) {
+        if (index in 0 until buttonLabels.size) {
+            (buttonGroup?.getChildAt(index) as? MaterialButton)?.isEnabled = enabled
+        }
+    }
+
+    fun setOnButtonClickListener(listener: MaterialButtonToggleGroup.OnButtonCheckedListener) {
+        buttonCheckedListener = listener
+        buttonGroup?.addOnButtonCheckedListener (listener)
+    }
+
+    fun removeOnButtonClickListener() {
+        buttonCheckedListener?.let { buttonGroup?.removeOnButtonCheckedListener(it) }
+        buttonCheckedListener = null
+    }
+}
\ No newline at end of file