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

Commit 455d8259 authored by Edgar Wang's avatar Edgar Wang
Browse files

[Expressive design] Update dropdown item color

Bug: 416818825
Bug: 417587081
Test: visual
Flag: EXEMPT library update
Change-Id: Ifd6d7a4ccecc878f1919110e1ef7696282a0dbda
parent 13453c13
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -24,5 +24,5 @@ android_library {
    ],

    sdk_version: "system_current",
    min_sdk_version: "21",
    min_sdk_version: "23",
}
+1 −1
Original line number Diff line number Diff line
@@ -18,6 +18,6 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.android.settingslib.widget.spinner">

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

</manifest>
+41 −7
Original line number Diff line number Diff line
@@ -16,7 +16,11 @@

package com.android.settingslib.widget;

import static android.os.Build.VERSION.SDK_INT;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
@@ -45,6 +49,14 @@ public class SettingsSpinnerAdapter<T> extends ArrayAdapter<T> {
            R.layout.settings_expressive_spinner_dropdown_view;
    private final LayoutInflater mDefaultInflater;
    private int mSelectedPosition = -1;
    private static final int DEFAULT_DROPDOWN_COLOR =
            com.android.settingslib.widget.theme.R.color.settingslib_materialColorOnSurface;
    private static final int DEFAULT_DROPDOWN_SELECTED_COLOR =
            com.android.settingslib.widget.theme.R.color.settingslib_materialColorOnPrimaryContainer;

    private static boolean sIsExpressive;
    private ColorStateList mDropdownColor;
    private ColorStateList mDropdownSelectedColor;

    /**
     * Constructs a new SettingsSpinnerAdapter with the given context.
@@ -56,8 +68,20 @@ public class SettingsSpinnerAdapter<T> extends ArrayAdapter<T> {
    public SettingsSpinnerAdapter(Context context) {
        super(context, getDefaultResource(context, Style.NORMAL));

        sIsExpressive = SettingsThemeHelper.isExpressiveTheme(context);
        setDropDownViewResource(getDropdownResource(context));
        mDefaultInflater = LayoutInflater.from(context);

        if (sIsExpressive) {
            try {
                mDropdownColor = context.getColorStateList(DEFAULT_DROPDOWN_COLOR);
                mDropdownSelectedColor = context.getColorStateList(DEFAULT_DROPDOWN_SELECTED_COLOR);
            } catch (Resources.NotFoundException e) {
                // fallback for testing
                mDropdownColor = context.getColorStateList(android.R.color.black);
                mDropdownSelectedColor = context.getColorStateList(android.R.color.darker_gray);
            }
        }
    }

    @Override
@@ -65,19 +89,23 @@ public class SettingsSpinnerAdapter<T> extends ArrayAdapter<T> {
            int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        View view;
        if (convertView == null) {
            view =
                    mDefaultInflater.inflate(
                            getDropdownResource(getContext()), parent, false /* attachToRoot */);
            view = mDefaultInflater.inflate(getDropdownResource(getContext()), parent,
                    false /* attachToRoot */);
        } else {
            view = convertView;
        }
        TextView textView = view.findViewById(android.R.id.text1);
        ImageView iconView = view.findViewById(android.R.id.icon);
        if (iconView != null) {
            iconView.setImageTintList(mDropdownSelectedColor);
            iconView.setVisibility((position == mSelectedPosition) ? View.VISIBLE : View.GONE);
        }
        T item = getItem(position);
        textView.setText(item == null ? "" : item.toString());
        if (sIsExpressive) {
            textView.setTextColor(
                    (position == mSelectedPosition) ? mDropdownSelectedColor : mDropdownColor);
        }
        return view;
    }

@@ -88,8 +116,14 @@ public class SettingsSpinnerAdapter<T> extends ArrayAdapter<T> {
    public SettingsSpinnerAdapter(Context context, SettingsSpinnerPreference.Style style) {
        super(context, getDefaultResource(context, style));

        sIsExpressive = SettingsThemeHelper.isExpressiveTheme(context);
        setDropDownViewResource(getDropdownResource(context));
        mDefaultInflater = LayoutInflater.from(context);

        if (sIsExpressive) {
            mDropdownColor = context.getColorStateList(DEFAULT_DROPDOWN_COLOR);
            mDropdownSelectedColor = context.getColorStateList(DEFAULT_DROPDOWN_SELECTED_COLOR);
        }
    }

    private static int getDefaultResourceWithStyle(Style style) {
@@ -136,16 +170,16 @@ public class SettingsSpinnerAdapter<T> extends ArrayAdapter<T> {
    }

    private static int getDefaultResource(Context context, Style style) {
        int resId = SettingsThemeHelper.isExpressiveTheme(context)
        int resId = sIsExpressive
            ? getDefaultResourceWithStyle(style) : DEFAULT_DROPDOWN_RESOURCE;
        return (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU)
        return (SDK_INT >= Build.VERSION_CODES.TIRAMISU)
            ? resId : android.R.layout.simple_spinner_dropdown_item;
    }

    private static int getDropdownResource(Context context) {
        int resId = SettingsThemeHelper.isExpressiveTheme(context)
        int resId = sIsExpressive
            ? DEFAULT_EXPRESSIVE_DROPDOWN_RESOURCE : DEFAULT_DROPDOWN_RESOURCE;
        return (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU)
        return (SDK_INT >= Build.VERSION_CODES.TIRAMISU)
            ? resId : android.R.layout.simple_spinner_dropdown_item;
    }
}