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

Commit 94b20ae3 authored by Wenyu Zhang's avatar Wenyu Zhang Committed by Android (Google) Code Review
Browse files

Merge "a11y: Change selected button styling" into main

parents 9a72466a 92f21f16
Loading
Loading
Loading
Loading
+42 −0
Original line number Diff line number Diff line
@@ -20,11 +20,14 @@ import static android.view.WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_M

import android.content.Context;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowInsets;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
@@ -51,6 +54,8 @@ public class AutoclickTypePanel {
    private final LinearLayout mDragButton;
    private final LinearLayout mScrollButton;

    private LinearLayout mSelectedButton;

    public AutoclickTypePanel(Context context, WindowManager windowManager) {
        mContext = context;
        mWindowManager = windowManager;
@@ -80,6 +85,40 @@ public class AutoclickTypePanel {
        // Initializes panel as collapsed state and only displays the left click button.
        hideAllClickTypeButtons();
        mLeftClickButton.setVisibility(View.VISIBLE);
        setSelectedButton(/* selectedButton= */ mLeftClickButton);
    }

    /** Sets the selected button and updates the newly and previously selected button styling. */
    private void setSelectedButton(@NonNull LinearLayout selectedButton) {
        // Updates the previously selected button styling.
        if (mSelectedButton != null) {
            toggleSelectedButtonStyle(mSelectedButton, /* isSelected= */ false);
        }

        mSelectedButton = selectedButton;

        // Updates the newly selected button styling.
        toggleSelectedButtonStyle(selectedButton, /* isSelected= */ true);
    }

    private void toggleSelectedButtonStyle(@NonNull LinearLayout button, boolean isSelected) {
        // Sets icon background color.
        GradientDrawable gradientDrawable = (GradientDrawable) button.getBackground();
        gradientDrawable.setColor(
                mContext.getColor(
                        isSelected
                                ? R.color.materialColorPrimary
                                : R.color.materialColorSurfaceContainer));

        // Sets icon color.
        ImageButton imageButton = (ImageButton) button.getChildAt(/* index= */ 0);
        Drawable drawable = imageButton.getDrawable();
        drawable.mutate()
                .setTint(
                        mContext.getColor(
                                isSelected
                                        ? R.color.materialColorSurfaceContainer
                                        : R.color.materialColorPrimary));
    }

    public void show() {
@@ -97,6 +136,9 @@ public class AutoclickTypePanel {
            // buttons except the one user selected.
            hideAllClickTypeButtons();
            button.setVisibility(View.VISIBLE);

            // Sets the newly selected button.
            setSelectedButton(/* selectedButton= */ button);
        } else {
            // If the panel is already collapsed, we just need to expand it.
            showAllClickTypeButtons();
+25 −0
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentat
import static com.google.common.truth.Truth.assertThat;

import android.content.Context;
import android.graphics.drawable.GradientDrawable;
import android.testing.AndroidTestingRunner;
import android.testing.TestableContext;
import android.testing.TestableLooper;
@@ -28,6 +29,8 @@ import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;

import com.android.internal.R;

import org.junit.Before;
@@ -86,6 +89,11 @@ public class AutoclickTypePanelTest {
        assertThat(mScrollButton.getVisibility()).isEqualTo(View.GONE);
    }

    @Test
    public void AutoclickTypePanel_initialState_correctButtonStyle() {
        verifyButtonHasSelectedStyle(mLeftClickButton);
    }

    @Test
    public void togglePanelExpansion_onClick_expandedTrue() {
        // On clicking left click button, the panel is expanded and all buttons are visible.
@@ -116,4 +124,21 @@ public class AutoclickTypePanelTest {
        assertThat(mDoubleClickButton.getVisibility()).isEqualTo(View.GONE);
        assertThat(mDragButton.getVisibility()).isEqualTo(View.GONE);
    }

    @Test
    public void togglePanelExpansion_selectButton_correctStyle() {
        // By first click, the panel is expanded.
        mLeftClickButton.callOnClick();

        // Clicks any button in the expanded state to select a type button.
        mScrollButton.callOnClick();

        verifyButtonHasSelectedStyle(mScrollButton);
    }

    private void verifyButtonHasSelectedStyle(@NonNull LinearLayout button) {
        GradientDrawable gradientDrawable = (GradientDrawable) button.getBackground();
        assertThat(gradientDrawable.getColor().getDefaultColor())
                .isEqualTo(mTestableContext.getColor(R.color.materialColorPrimary));
    }
}