Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 89b11031 authored by Edgar Wang's avatar Edgar Wang
Browse files

[Expressive Design] Add SegmentedButtonPreference

Bug: 384654397
Test: manual
Flag: EXEMPT library update
Change-Id: I39f26302bcb2109432d8868578273af0436ab589
parent be860b4d
Loading
Loading
Loading
Loading
+7 −6
Original line number Diff line number Diff line
@@ -10,19 +10,19 @@ package {
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 @@ android_library {
        "SettingsLibAppPreference",
        "SettingsLibBannerMessagePreference",
        "SettingsLibBarChartPreference",
        "SettingsLibButtonPreference",
        "SettingsLibBulletPreference",
        "SettingsLibButtonPreference",
        "SettingsLibCardPreference",
        "SettingsLibCollapsingToolbarBaseActivity",
        "SettingsLibDeviceStateRotationLock",
@@ -50,6 +50,7 @@ android_library {
        "SettingsLibProgressBar",
        "SettingsLibRestrictedLockUtils",
        "SettingsLibSearchWidget",
        "SettingsLibSegmentedButtonPreference",
        "SettingsLibSelectorWithWidgetPreference",
        "SettingsLibSettingsSpinner",
        "SettingsLibSettingsTransition",
@@ -118,8 +119,8 @@ soong_config_module_type {
        "legacy_avatar_picker_app_enabled",
    ],
    properties: [
        "static_libs",
        "manifest",
        "static_libs",
    ],
}

+33 −0
Original line number Diff line number Diff line
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",
    ],
}
+23 −0
Original line number Diff line number Diff line
<?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>
+104 −0
Original line number Diff line number Diff line
<?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
+88 −0
Original line number Diff line number Diff line
/*
 * 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