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

Commit 49111c68 authored by Yuhang Gong's avatar Yuhang Gong Committed by Android (Google) Code Review
Browse files

Merge "Add AppHeaderPreference"

parents fc2de301 99604ae5
Loading
Loading
Loading
Loading
+81 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!--
  Copyright (C) 2022 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:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="24dp"
    android:paddingBottom="16dp"
    android:paddingStart="?android:attr/listPreferredItemPaddingStart"
    android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:gravity="center_horizontal"
        android:orientation="vertical">

        <ImageView
            android:id="@android:id/icon"
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:scaleType="fitCenter"
            android:antialias="true"/>

        <TextView
            android:id="@android:id/title"
            style="@style/TextAppearance.EntityHeaderTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="false"
            android:gravity="center"
            android:ellipsize="marquee"
            android:textDirection="locale"
            android:layout_marginTop="8dp"/>

        <TextView
            android:id="@+id/install_type"
            style="@style/TextAppearance.EntityHeaderSummary"
            android:visibility="gone"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="2dp"
            android:text="@string/install_type_instant"/>

        <TextView
            android:id="@android:id/summary"
            style="@style/TextAppearance.EntityHeaderSummary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="2dp"
            android:singleLine="false"
            android:textAlignment="center"/>

        <TextView
            android:id="@+id/second_summary"
            style="@style/TextAppearance.EntityHeaderSummary"
            android:singleLine="false"
            android:maxLines="4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

    </LinearLayout>

</RelativeLayout>
+22 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8" ?>
<!--
  ~ Copyright (C) 2022 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">

    <!-- Added as the value of a header field indicating this is an instant app (as opposed to installed normally) -->
    <string name="install_type_instant">Instant app</string>
</resources>
+144 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 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.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.preference.Preference;
import androidx.preference.PreferenceViewHolder;

/**
 * The Preference for the pages need to show big apps icon and name in the header of the page.
 */
public class AppHeaderPreference extends Preference {

    private boolean mIsInstantApp;
    private CharSequence mSecondSummary;

    public AppHeaderPreference(Context context, AttributeSet attrs, int defStyleAttr,
                               int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    public AppHeaderPreference(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public AppHeaderPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public AppHeaderPreference(Context context) {
        super(context);
        init();
    }

    private void init() {
        setLayoutResource(R.layout.app_header_preference);
        setSelectable(false);
        setIsInstantApp(false);
    }

    /**
     * Returns the installation type of this preference.
     *
     * @return whether the app is an instant app
     * @see #setIsInstantApp(boolean)
     */
    public boolean getIsInstantApp() {
        return mIsInstantApp;
    }

    /**
     * Sets the installation type for this preference with a boolean.
     *
     * @param isInstantApp whether the app is an instant app
     */
    public void setIsInstantApp(@NonNull boolean isInstantApp) {
        if (mIsInstantApp != isInstantApp) {
            mIsInstantApp = isInstantApp;
            notifyChanged();
        }
    }

    /**
     * Returns the second summary of this preference.
     *
     * @return The second summary
     * @see #setSecondSummary(CharSequence)
     */
    @Nullable
    public CharSequence getSecondSummary() {
        return mSecondSummary;
    }

    /**
     * Sets the second summary for this preference with a CharSequence.
     *
     * @param secondSummary The second summary for the preference
     */
    public void setSecondSummary(@Nullable CharSequence secondSummary) {
        if (!TextUtils.equals(mSecondSummary, secondSummary)) {
            mSecondSummary = secondSummary;
            notifyChanged();
        }
    }

    /**
     * Sets the second summary for this preference with a resource ID.
     *
     * @param secondSummaryResId The second summary as a resource
     * @see #setSecondSummary(CharSequence)
     */
    public void setSecondSummary(@StringRes int secondSummaryResId) {
        setSecondSummary(getContext().getString(secondSummaryResId));
    }

    @Override
    public void onBindViewHolder(@NonNull PreferenceViewHolder holder) {
        super.onBindViewHolder(holder);

        final TextView installTypeView = (TextView) holder.findViewById(R.id.install_type);
        if (installTypeView != null) {
            if (mIsInstantApp) {
                installTypeView.setVisibility(View.VISIBLE);
            } else {
                installTypeView.setVisibility(View.GONE);
            }
        }

        final TextView secondSummaryView = (TextView) holder.findViewById(R.id.second_summary);
        if (secondSummaryView != null) {
            if (!TextUtils.isEmpty(mSecondSummary)) {
                secondSummaryView.setText(mSecondSummary);
                secondSummaryView.setVisibility(View.VISIBLE);
            } else {
                secondSummaryView.setVisibility(View.GONE);
            }
        }
    }
}
+1 −0
Original line number Diff line number Diff line
@@ -15,6 +15,7 @@ android_library {

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

    sdk_version: "system_current",
+0 −14
Original line number Diff line number Diff line
@@ -22,20 +22,6 @@
        <item name="android:paddingEnd">16dp</item>
    </style>

    <style name="TextAppearance.EntityHeaderTitle"
           parent="@android:style/TextAppearance.DeviceDefault.WindowTitle">
        <item name="android:textColor">?android:attr/textColorPrimary</item>
        <item name="android:textSize">20sp</item>
    </style>

    <style name="TextAppearance.EntityHeaderSummary"
           parent="@android:style/TextAppearance.DeviceDefault">
        <item name="android:textAlignment">viewStart</item>
        <item name="android:textColor">?android:attr/textColorSecondary</item>
        <item name="android:singleLine">true</item>
        <item name="android:ellipsize">marquee</item>
    </style>

    <style name="CrossProfileEntityHeaderIcon">
        <item name="android:layout_width">48dp</item>
        <item name="android:layout_height">48dp</item>
Loading