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

Commit b503c936 authored by Edgar Wang's avatar Edgar Wang
Browse files

[Expressive design] Add IntroPreference

Bug: 367714364
Test: manual
Flag: EXEMPT resource only update
Change-Id: I2d6c78ab3f00707c742c193cb0575ff5be321f2c
parent 5ef9ea49
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -42,6 +42,7 @@ android_library {
        "SettingsLibFooterPreference",
        "SettingsLibHelpUtils",
        "SettingsLibIllustrationPreference",
        "SettingsLibIntroPreference",
        "SettingsLibLayoutPreference",
        "SettingsLibMainSwitchPreference",
        "SettingsLibProfileSelector",
+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: "SettingsLibIntroPreference",
    use_resource_processor: true,
    defaults: [
        "SettingsLintDefaults",
    ],

    srcs: [
        "src/**/*.java",
        "src/**/*.kt",
    ],
    resource_dirs: ["res"],

    static_libs: [
        "androidx.preference_preference",
        "SettingsLibSettingsTheme",
    ],

    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.intro">

    <uses-sdk android:minSdkVersion="21" />

</manifest>
+45 −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.
  -->

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/entity_header"
    style="@style/SettingsLibEntityHeader">

    <LinearLayout
        android:id="@+id/entity_header_content"
        style="@style/SettingsLibEntityHeaderContent">

        <ImageView
            android:id="@android:id/icon"
            android:src="@drawable/settingslib_arrow_drop_down"
            style="@style/SettingsLibEntityHeaderIcon"/>

        <TextView
            android:id="@android:id/title"
            android:text="Title"
            style="@style/SettingsLibEntityHeaderTitle"/>

        <com.android.settingslib.widget.CollapsableTextView
            android:id="@+id/collapsable_summary"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"/>

    </LinearLayout>

</RelativeLayout>
+102 −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.os.Build
import android.util.AttributeSet
import androidx.annotation.RequiresApi
import androidx.preference.Preference
import androidx.preference.PreferenceViewHolder
import com.android.settingslib.widget.preference.intro.R

class IntroPreference @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 {
        layoutResource = R.layout.settingslib_expressive_preference_intro
        isSelectable = false

        initAttributes(context, attrs, defStyleAttr)
    }

    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.isDividerAllowedBelow = false
        holder.isDividerAllowedAbove = false

        (holder.findViewById(R.id.collapsable_summary) as? CollapsableTextView)?.apply {
            setCollapsable(isCollapsable)
            setMinLines(minLines)
            setText(summary.toString())
        }
    }

    /**
     * Sets whether the summary is collapsable.
     * @param collapsable True if the summary should be collapsable, false otherwise.
     */
    @RequiresApi(Build.VERSION_CODES.VANILLA_ICE_CREAM)
    fun setCollapsable(collapsable: Boolean) {
        isCollapsable = collapsable
        minLines = if (isCollapsable) DEFAULT_MIN_LINES else DEFAULT_MAX_LINES
        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
    }
}
 No newline at end of file
Loading