diff options
author | 2024-09-13 20:29:29 +0000 | |
---|---|---|
committer | 2024-09-27 06:50:17 +0000 | |
commit | 620d0eaa35df739acdf44d329f439e332d9984a2 (patch) | |
tree | 9023b91cbfe1817fc192d1f0f2c28cacbc5df900 | |
parent | 7e21ef8b265daae22dad9d0fc0a6f727cabf6789 (diff) |
[Expressive design] update TopIntroPreference
- support expand/collapse
Bug: 366407433
Test: manual
Flag: EXEMPT resource only update
Change-Id: I7b417287568b06b7ea243625a0b788d5b2854454
11 files changed, 557 insertions, 61 deletions
diff --git a/packages/SettingsLib/SettingsTheme/res/drawable-v35/settingslib_expressive_icon_collapse.xml b/packages/SettingsLib/SettingsTheme/res/drawable-v35/settingslib_expressive_icon_collapse.xml new file mode 100644 index 000000000000..161ece73f21c --- /dev/null +++ b/packages/SettingsLib/SettingsTheme/res/drawable-v35/settingslib_expressive_icon_collapse.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. + --> +<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> + <item> + <shape + android:shape="oval"> + <size android:width="24dp" android:height="24dp"/> + <solid android:color="@color/settingslib_materialColorSurfaceDim"/> + </shape> + </item> + <item> + <vector + android:width="24dp" + android:height="24dp" + android:viewportWidth="960" + android:viewportHeight="960"> + <path + android:fillColor="@color/settingslib_materialColorOnSurface" + android:pathData="M480,432L296,616L240,560L480,320L720,560L664,616L480,432Z"/> + </vector> + </item> +</layer-list>
\ No newline at end of file diff --git a/packages/SettingsLib/SettingsTheme/res/drawable-v35/settingslib_expressive_icon_expand.xml b/packages/SettingsLib/SettingsTheme/res/drawable-v35/settingslib_expressive_icon_expand.xml new file mode 100644 index 000000000000..1b5d5182d9b2 --- /dev/null +++ b/packages/SettingsLib/SettingsTheme/res/drawable-v35/settingslib_expressive_icon_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="24dp" android:height="24dp"/> + <solid android:color="@color/settingslib_materialColorSurfaceDim"/> + </shape> + </item> + <item> + <vector + android:width="24dp" + android:height="24dp" + android:viewportWidth="960" + android:viewportHeight="960"> + <path + android:fillColor="@color/settingslib_materialColorOnSurface" + android:pathData="M480,616L240,376L296,320L480,504L664,320L720,376L480,616Z"/> + </vector> + </item> +</layer-list>
\ No newline at end of file diff --git a/packages/SettingsLib/SettingsTheme/res/layout-v35/settingslib_expressive_collapsable_textview.xml b/packages/SettingsLib/SettingsTheme/res/layout-v35/settingslib_expressive_collapsable_textview.xml new file mode 100644 index 000000000000..245d3682636b --- /dev/null +++ b/packages/SettingsLib/SettingsTheme/res/layout-v35/settingslib_expressive_collapsable_textview.xml @@ -0,0 +1,51 @@ +<?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. + --> + +<androidx.constraintlayout.widget.ConstraintLayout + 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:paddingBottom="@dimen/settingslib_expressive_space_small1" + android:paddingTop="@dimen/settingslib_expressive_space_extrasmall4" + android:orientation="vertical" + android:animateLayoutChanges="true" + android:background="?android:attr/selectableItemBackground" + android:clipToPadding="false"> + + <TextView + android:id="@android:id/title" + android:layout_width="0dp" + android:layout_height="wrap_content" + app:layout_constraintTop_toTopOf="parent" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintEnd_toEndOf="parent" + android:textAlignment="viewStart" + android:clickable="false" + android:longClickable="false" + android:maxLines="10" + android:ellipsize="end" + android:textAppearance="@style/TextAppearance.TopIntroText"/> + + <com.google.android.material.button.MaterialButton + android:id="@+id/collapse_button" + app:layout_constraintTop_toBottomOf="@android:id/title" + app:layout_constraintStart_toStartOf="parent" + android:text="@string/settingslib_expressive_text_expand" + app:icon="@drawable/settingslib_expressive_icon_expand" + style="@style/SettingslibTextButtonStyle.Expressive"/> +</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file diff --git a/packages/SettingsLib/SettingsTheme/res/values-v35/attrs_expressive.xml b/packages/SettingsLib/SettingsTheme/res/values-v35/attrs_expressive.xml new file mode 100644 index 000000000000..857dd7953234 --- /dev/null +++ b/packages/SettingsLib/SettingsTheme/res/values-v35/attrs_expressive.xml @@ -0,0 +1,25 @@ +<?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. +--> + +<resources> + <declare-styleable name="CollapsableTextView"> + <attr name="android:gravity"/> + <!-- The minimum number of lines when the textView collapsed. --> + <attr name="android:minLines"/> + <!-- Specifies that the textView is collapsable. --> + <attr name="isCollapsable" format="boolean"/> + </declare-styleable> +</resources>
\ No newline at end of file diff --git a/packages/SettingsLib/SettingsTheme/res/values-v35/strings.xml b/packages/SettingsLib/SettingsTheme/res/values-v35/strings.xml new file mode 100644 index 000000000000..22734068733a --- /dev/null +++ b/packages/SettingsLib/SettingsTheme/res/values-v35/strings.xml @@ -0,0 +1,24 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + Copyright (C) 2021 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. + --> + +<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"> + + <!-- text of button to indicate user the textView is expandable [CHAR LIMIT=NONE] --> + <string name="settingslib_expressive_text_expand">Expand</string> + <!-- text of button to indicate user the textView is collapsable [CHAR LIMIT=NONE] --> + <string name="settingslib_expressive_text_collapse">Collapse</string> +</resources>
\ No newline at end of file diff --git a/packages/SettingsLib/SettingsTheme/res/values-v35/styles_expressive.xml b/packages/SettingsLib/SettingsTheme/res/values-v35/styles_expressive.xml index dc2eb648f8c1..250c27e8b581 100644 --- a/packages/SettingsLib/SettingsTheme/res/values-v35/styles_expressive.xml +++ b/packages/SettingsLib/SettingsTheme/res/values-v35/styles_expressive.xml @@ -170,6 +170,52 @@ <item name="thumbIcon">@drawable/settingslib_expressive_switch_thumb_icon</item> </style> + <style name="SettingslibMainSwitchStyle.Expressive" parent="SettingslibSwitchStyle.Expressive"> + <item name="android:layout_gravity">center</item> + <item name="trackTint">@color/settingslib_expressive_color_main_switch_track</item> + </style> + + <style name="SettingsLibCardStyle" parent=""> + <item name="android:layout_width">match_parent</item> + <item name="android:layout_height">wrap_content</item> + <item name="android:layout_marginHorizontal">?android:attr/listPreferredItemPaddingStart</item> + <item name="android:layout_marginVertical">@dimen/settingslib_expressive_space_extrasmall4</item> + <item name="cardBackgroundColor">@color/settingslib_materialColorPrimary</item> + <item name="cardCornerRadius">@dimen/settingslib_expressive_radius_extralarge3</item> + <item name="cardElevation">0dp</item> + <item name="rippleColor">?android:attr/colorControlHighlight</item> + </style> + + <style name="SettingsLibButtonStyle.Expressive.Filled" + parent="@style/Widget.Material3.Button"> + <item name="android:theme">@style/Theme.Material3.DynamicColors.DayNight</item> + <item name="android:layout_width">wrap_content</item> + <item name="android:layout_height">wrap_content</item> + <item name="android:gravity">center</item> + <item name="android:minWidth">@dimen/settingslib_expressive_space_medium4</item> + <item name="android:minHeight">@dimen/settingslib_expressive_space_medium4</item> + <item name="android:paddingVertical">@dimen/settingslib_expressive_space_extrasmall5</item> + <item name="android:paddingHorizontal">@dimen/settingslib_expressive_space_small1</item> + <item name="android:backgroundTint">@color/settingslib_materialColorPrimary</item> + <item name="android:textAppearance">@android:style/TextAppearance.DeviceDefault.Medium</item> + <item name="android:textColor">@color/settingslib_materialColorOnPrimary</item> + <item name="android:textSize">14sp</item> + <item name="iconGravity">textStart</item> + <item name="iconTint">@color/settingslib_materialColorOnPrimary</item> + <item name="iconSize">@dimen/settingslib_expressive_space_small4</item> + </style> + + <style name="SettingsLibButtonStyle.Expressive.Filled.Large"> + <item name="android:paddingVertical">@dimen/settingslib_expressive_space_small1</item> + <item name="android:paddingHorizontal">@dimen/settingslib_expressive_space_small4</item> + <item name="android:textSize">16sp</item> + </style> + + <style name="SettingsLibButtonStyle.Expressive.Filled.Extra" + parent="@style/SettingsLibButtonStyle.Expressive.Filled.Large"> + <item name="android:layout_width">match_parent</item> + </style> + <style name="SettingsLibButtonStyle.Expressive.Tonal" parent="@style/Widget.Material3.Button.TonalButton"> <item name="android:theme">@style/Theme.Material3.DynamicColors.DayNight</item> @@ -189,19 +235,59 @@ <item name="iconSize">@dimen/settingslib_expressive_space_small4</item> </style> - <style name="SettingslibMainSwitchStyle.Expressive" parent="SettingslibSwitchStyle.Expressive"> - <item name="android:layout_gravity">center</item> - <item name="trackTint">@color/settingslib_expressive_color_main_switch_track</item> + <style name="SettingsLibButtonStyle.Expressive.Tonal.Large"> + <item name="android:paddingVertical">@dimen/settingslib_expressive_space_small1</item> + <item name="android:paddingHorizontal">@dimen/settingslib_expressive_space_small4</item> + <item name="android:textSize">16sp</item> </style> - <style name="SettingsLibCardStyle" parent=""> + <style name="SettingsLibButtonStyle.Expressive.Tonal.Extra" + parent="@style/SettingsLibButtonStyle.Expressive.Tonal.Large"> <item name="android:layout_width">match_parent</item> + </style> + + <style name="SettingsLibButtonStyle.Expressive.Outline" + parent="@style/Widget.Material3.Button.OutlinedButton.Icon"> + <item name="android:theme">@style/Theme.Material3.DynamicColors.DayNight</item> + <item name="android:layout_width">wrap_content</item> <item name="android:layout_height">wrap_content</item> - <item name="android:layout_marginHorizontal">?android:attr/listPreferredItemPaddingStart</item> - <item name="android:layout_marginVertical">@dimen/settingslib_expressive_space_extrasmall4</item> - <item name="cardBackgroundColor">@color/settingslib_materialColorPrimary</item> - <item name="cardCornerRadius">@dimen/settingslib_expressive_radius_extralarge3</item> - <item name="cardElevation">0dp</item> + <item name="android:gravity">center</item> + <item name="android:minWidth">@dimen/settingslib_expressive_space_medium4</item> + <item name="android:minHeight">@dimen/settingslib_expressive_space_medium4</item> + <item name="android:paddingVertical">@dimen/settingslib_expressive_space_extrasmall5</item> + <item name="android:paddingHorizontal">@dimen/settingslib_expressive_space_small1</item> + <item name="android:textAppearance">@android:style/TextAppearance.DeviceDefault.Medium</item> + <item name="android:textColor">@color/settingslib_materialColorPrimary</item> + <item name="android:textSize">14sp</item> + <item name="iconTint">@color/settingslib_materialColorPrimary</item> + <item name="iconGravity">textStart</item> + <item name="iconSize">@dimen/settingslib_expressive_space_small4</item> + <item name="iconPadding">@dimen/settingslib_expressive_space_extrasmall4</item> + <item name="strokeColor">@color/settingslib_materialColorOutlineVariant</item> + + </style> + + <style name="SettingsLibButtonStyle.Expressive.Outline.Large"> + <item name="android:paddingVertical">@dimen/settingslib_expressive_space_small1</item> + <item name="android:paddingHorizontal">@dimen/settingslib_expressive_space_small4</item> + <item name="android:textSize">16sp</item> + </style> + + <style name="SettingsLibButtonStyle.Expressive.Outline.Extra" + parent="@style/SettingsLibButtonStyle.Expressive.Outline.Large"> + <item name="android:layout_width">match_parent</item> + </style> + + <style name="SettingslibTextButtonStyle.Expressive" + parent="@style/Widget.Material3.Button.TextButton.Icon"> + <item name="android:theme">@style/Theme.Material3.DynamicColors.DayNight</item> + <item name="android:layout_width">wrap_content</item> + <item name="android:layout_height">wrap_content</item> + <item name="android:textAppearance">@android:style/TextAppearance.DeviceDefault.Medium</item> + <item name="android:textSize">16sp</item> + <item name="android:textColor">@color/settingslib_materialColorOnSurface</item> + <item name="iconTint">@null</item> + <item name="iconPadding">@dimen/settingslib_expressive_space_extrasmall4</item> <item name="rippleColor">?android:attr/colorControlHighlight</item> </style> </resources>
\ No newline at end of file diff --git a/packages/SettingsLib/SettingsTheme/src/com/android/settingslib/widget/CollapsableTextView.kt b/packages/SettingsLib/SettingsTheme/src/com/android/settingslib/widget/CollapsableTextView.kt new file mode 100644 index 000000000000..127f21a540ab --- /dev/null +++ b/packages/SettingsLib/SettingsTheme/src/com/android/settingslib/widget/CollapsableTextView.kt @@ -0,0 +1,150 @@ +/* + * 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.graphics.drawable.Drawable +import android.util.AttributeSet +import android.view.Gravity +import android.view.LayoutInflater +import android.view.View +import android.widget.TextView +import androidx.constraintlayout.widget.ConstraintLayout +import com.android.settingslib.widget.theme.R +import com.google.android.material.button.MaterialButton + +class CollapsableTextView @JvmOverloads constructor( + context: Context, + attrs: AttributeSet? = null, + defStyleAttr: Int = 0 +) : ConstraintLayout(context, attrs, defStyleAttr) { + + private var isCollapsable: Boolean = false + private var isCollapsed: Boolean = false + private var minLines: Int = DEFAULT_MIN_LINES + + private val titleTextView: TextView + private val collapseButton: MaterialButton + private val collapseButtonResources: CollapseButtonResources + + init { + LayoutInflater.from(context) + .inflate(R.layout.settingslib_expressive_collapsable_textview, this) + titleTextView = findViewById(android.R.id.title) + collapseButton = findViewById(R.id.collapse_button) + + collapseButtonResources = CollapseButtonResources( + context.getDrawable(R.drawable.settingslib_expressive_icon_collapse)!!, + context.getDrawable(R.drawable.settingslib_expressive_icon_expand)!!, + context.getString(R.string.settingslib_expressive_text_collapse), + context.getString(R.string.settingslib_expressive_text_expand) + ) + + collapseButton.setOnClickListener { + isCollapsed = !isCollapsed + updateView() + } + + initAttributes(context, attrs, defStyleAttr) + } + + private fun initAttributes(context: Context, attrs: AttributeSet?, defStyleAttr: Int) { + context.obtainStyledAttributes( + attrs, Attrs, defStyleAttr, 0 + ).apply { + val gravity = getInt(GravityAttr, Gravity.START) + when (gravity) { + Gravity.CENTER_VERTICAL, Gravity.CENTER, Gravity.CENTER_HORIZONTAL -> { + centerHorizontally(titleTextView) + centerHorizontally(collapseButton) + } + } + recycle() + } + } + + private fun centerHorizontally(view: View) { + (view.layoutParams as LayoutParams).apply { + startToStart = LayoutParams.PARENT_ID + endToEnd = LayoutParams.PARENT_ID + horizontalBias = 0.5f + } + } + + /** + * Sets the text content of the CollapsableTextView. + * @param text The text to display. + */ + fun setText(text: String) { + titleTextView.text = text + } + + /** + * Sets whether the text view is collapsable. + * @param collapsable True if the text view should be collapsable, false otherwise. + */ + fun setCollapsable(collapsable: Boolean) { + isCollapsable = collapsable + updateView() + } + + /** + * Sets the minimum number of lines to display when collapsed. + * @param lines The minimum number of lines. + */ + fun setMinLines(line: Int) { + minLines = line.coerceIn(1, DEFAULT_MAX_LINES) + updateView() + } + + private fun updateView() { + when { + isCollapsed -> { + collapseButton.apply { + text = collapseButtonResources.expandText + icon = collapseButtonResources.expandIcon + } + titleTextView.maxLines = minLines + } + + else -> { + collapseButton.apply { + text = collapseButtonResources.collapseText + icon = collapseButtonResources.collapseIcon + } + titleTextView.maxLines = DEFAULT_MAX_LINES + } + } + collapseButton.visibility = if (isCollapsable) VISIBLE else GONE + } + + private data class CollapseButtonResources( + val collapseIcon: Drawable, + val expandIcon: Drawable, + val collapseText: String, + val expandText: String + ) + + companion object { + private const val DEFAULT_MAX_LINES = 10 + private const val DEFAULT_MIN_LINES = 2 + + private val Attrs = R.styleable.CollapsableTextView + private val GravityAttr = R.styleable.CollapsableTextView_android_gravity + } +} + diff --git a/packages/SettingsLib/TopIntroPreference/Android.bp b/packages/SettingsLib/TopIntroPreference/Android.bp index e70201b0feb7..76e36dc5ff7d 100644 --- a/packages/SettingsLib/TopIntroPreference/Android.bp +++ b/packages/SettingsLib/TopIntroPreference/Android.bp @@ -14,7 +14,10 @@ android_library { "SettingsLintDefaults", ], - srcs: ["src/**/*.java"], + srcs: [ + "src/**/*.java", + "src/**/*.kt", + ], resource_dirs: ["res"], static_libs: [ diff --git a/packages/SettingsLib/TopIntroPreference/res/layout-v35/settingslib_expressive_top_intro.xml b/packages/SettingsLib/TopIntroPreference/res/layout-v35/settingslib_expressive_top_intro.xml new file mode 100644 index 000000000000..fb13ef79cc3b --- /dev/null +++ b/packages/SettingsLib/TopIntroPreference/res/layout-v35/settingslib_expressive_top_intro.xml @@ -0,0 +1,27 @@ +<?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:paddingEnd="?android:attr/listPreferredItemPaddingEnd" + android:paddingStart="?android:attr/listPreferredItemPaddingStart"> + + <com.android.settingslib.widget.CollapsableTextView + android:id="@+id/collapsable_text_view" + android:layout_width="match_parent" + android:layout_height="wrap_content" /> +</LinearLayout>
\ No newline at end of file diff --git a/packages/SettingsLib/TopIntroPreference/src/com/android/settingslib/widget/TopIntroPreference.java b/packages/SettingsLib/TopIntroPreference/src/com/android/settingslib/widget/TopIntroPreference.java deleted file mode 100644 index 1bbd76d86b7f..000000000000 --- a/packages/SettingsLib/TopIntroPreference/src/com/android/settingslib/widget/TopIntroPreference.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * 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. - */ - -package com.android.settingslib.widget; - -import android.content.Context; -import android.util.AttributeSet; - -import androidx.preference.Preference; -import androidx.preference.PreferenceViewHolder; - -import com.android.settingslib.widget.preference.topintro.R; - -/** - * The TopIntroPreference shows a text which describe a feature. Gernerally, we expect this - * preference always shows on the top of screen. - */ -public class TopIntroPreference extends Preference { - - public TopIntroPreference(Context context) { - super(context); - setLayoutResource(R.layout.top_intro_preference); - setSelectable(false); - } - - public TopIntroPreference(Context context, AttributeSet attrs) { - super(context, attrs); - setLayoutResource(R.layout.top_intro_preference); - setSelectable(false); - } - - @Override - public void onBindViewHolder(PreferenceViewHolder holder) { - super.onBindViewHolder(holder); - holder.setDividerAllowedAbove(false); - holder.setDividerAllowedBelow(false); - } -} diff --git a/packages/SettingsLib/TopIntroPreference/src/com/android/settingslib/widget/TopIntroPreference.kt b/packages/SettingsLib/TopIntroPreference/src/com/android/settingslib/widget/TopIntroPreference.kt new file mode 100644 index 000000000000..afced0c8d638 --- /dev/null +++ b/packages/SettingsLib/TopIntroPreference/src/com/android/settingslib/widget/TopIntroPreference.kt @@ -0,0 +1,108 @@ +/* + * 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.os.Build +import android.util.AttributeSet +import androidx.annotation.RequiresApi +import androidx.preference.Preference +import androidx.preference.PreferenceViewHolder +import com.android.settingslib.widget.preference.topintro.R + +open class TopIntroPreference @JvmOverloads constructor( + context: Context, + attrs: AttributeSet? = null, + defStyleAttr: Int = 0, + defStyleRes: Int = 0 +) : Preference(context, attrs, defStyleAttr, defStyleRes) { + + private var isCollapsable: Boolean = false + private var minLines: Int = 2 + + init { + if (SettingsThemeHelper.isExpressiveTheme(context)) { + layoutResource = R.layout.settingslib_expressive_top_intro + initAttributes(context, attrs, defStyleAttr) + } else { + layoutResource = R.layout.top_intro_preference + } + isSelectable = false + } + + private fun initAttributes(context: Context, attrs: AttributeSet?, defStyleAttr: Int) { + context.obtainStyledAttributes( + attrs, + COLLAPSABLE_TEXT_VIEW_ATTRS, defStyleAttr, 0 + ).apply { + isCollapsable = getBoolean(IS_COLLAPSABLE, false) + minLines = getInt( + MIN_LINES, + if (isCollapsable) DEFAULT_MIN_LINES else DEFAULT_MAX_LINES + ).coerceIn(1, DEFAULT_MAX_LINES) + recycle() + } + } + + override fun onBindViewHolder(holder: PreferenceViewHolder) { + super.onBindViewHolder(holder) + holder.isDividerAllowedAbove = false + holder.isDividerAllowedBelow = false + + if (!SettingsThemeHelper.isExpressiveTheme(context)) { + return + } + + (holder.findViewById(R.id.collapsable_text_view) as? CollapsableTextView)?.apply { + setCollapsable(isCollapsable) + setMinLines(minLines) + setText(title.toString()) + } + } + + /** + * Sets whether the text view is collapsable. + * @param collapsable True if the text view should be collapsable, false otherwise. + */ + @RequiresApi(Build.VERSION_CODES.VANILLA_ICE_CREAM) + fun setCollapsable(collapsable: Boolean) { + isCollapsable = collapsable + notifyChanged() + } + + /** + * Sets the minimum number of lines to display when collapsed. + * @param lines The minimum number of lines. + */ + @RequiresApi(Build.VERSION_CODES.VANILLA_ICE_CREAM) + fun setMinLines(lines: Int) { + minLines = lines.coerceIn(1, DEFAULT_MAX_LINES) + notifyChanged() + } + + companion object { + private const val DEFAULT_MAX_LINES = 10 + private const val DEFAULT_MIN_LINES = 2 + + private val COLLAPSABLE_TEXT_VIEW_ATTRS = + com.android.settingslib.widget.theme.R.styleable.CollapsableTextView + private val MIN_LINES = + com.android.settingslib.widget.theme.R.styleable.CollapsableTextView_android_minLines + private val IS_COLLAPSABLE = + com.android.settingslib.widget.theme.R.styleable.CollapsableTextView_isCollapsable + } +} |