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

Commit 50737c90 authored by Beverly's avatar Beverly Committed by Beverly Tai
Browse files

Update Toast visuals + motion

Puts Toast style/animation changes behind a FeatureFlag

Toasts can be added via Toast APIs OR legacy apps can add the
window directly. Because toast windows can be added directly by legacy
apps, DisplayPolicy.java requires that toast windows are removed
after TOAST_WINDOW_TIMEOUT.  This also means Toast windows added via
the Toast APIs are subject to this timeout. Therefore, this CL also
increases the timeout by 600ms to account for SystemUI's in/out
toast animations.

Bug: 169587378
Test: atest ToastUITest
Test: manual, rotate device while toast is showing
Change-Id: Ib17655c253f4ec4f7018cc5f7939b88a50ea5014
parent ea1ef78d
Loading
Loading
Loading
Loading
+32 −12
Original line number Diff line number Diff line
@@ -171,6 +171,22 @@ public class ToastPresenter {
        }
    }

    /**
     * Update the LayoutParameters of the currently showing toast view. This is used for layout
     * updates based on orientation changes.
     */
    public void updateLayoutParams(int xOffset, int yOffset, float horizontalMargin,
            float verticalMargin, int gravity) {
        checkState(mView != null, "Toast must be showing to update its layout parameters.");
        Configuration config = mResources.getConfiguration();
        mParams.gravity = Gravity.getAbsoluteGravity(gravity, config.getLayoutDirection());
        mParams.x = xOffset;
        mParams.y = yOffset;
        mParams.horizontalMargin = horizontalMargin;
        mParams.verticalMargin = verticalMargin;
        addToastView();
    }

    /**
     * Sets {@link WindowManager.LayoutParams#SYSTEM_FLAG_SHOW_FOR_ALL_USERS} flag if {@code
     * packageName} is a cross-user package.
@@ -221,18 +237,7 @@ public class ToastPresenter {

        adjustLayoutParams(mParams, windowToken, duration, gravity, xOffset, yOffset,
                horizontalMargin, verticalMargin, removeWindowAnimations);
        if (mView.getParent() != null) {
            mWindowManager.removeView(mView);
        }
        try {
            mWindowManager.addView(mView, mParams);
        } catch (WindowManager.BadTokenException e) {
            // Since the notification manager service cancels the token right after it notifies us
            // to cancel the toast there is an inherent race and we may attempt to add a window
            // after the token has been invalidated. Let us hedge against that.
            Log.w(TAG, "Error while attempting to show toast from " + mPackageName, e);
            return;
        }
        addToastView();
        trySendAccessibilityEvent(mView, mPackageName);
        if (callback != null) {
            try {
@@ -288,4 +293,19 @@ public class ToastPresenter {
        view.dispatchPopulateAccessibilityEvent(event);
        mAccessibilityManager.sendAccessibilityEvent(event);
    }

    private void addToastView() {
        if (mView.getParent() != null) {
            mWindowManager.removeView(mView);
        }
        try {
            mWindowManager.addView(mView, mParams);
        } catch (WindowManager.BadTokenException e) {
            // Since the notification manager service cancels the token right after it notifies us
            // to cancel the toast there is an inherent race and we may attempt to add a window
            // after the token has been invalidated. Let us hedge against that.
            Log.w(TAG, "Error while attempting to show toast from " + mPackageName, e);
            return;
        }
    }
}
+5 −0
Original line number Diff line number Diff line
@@ -103,5 +103,10 @@ public interface ToastPlugin extends Plugin {
        default Animator getOutAnimation() {
            return null;
        }

        /**
         * Called on orientation changes.
         */
        default void onOrientationChange(int orientation) {  }
    }
}
+21 −0
Original line number Diff line number Diff line
<?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.
  -->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#FFFFFFFF" />
    <corners android:radius="@dimen/toast_bg_radius" />
</shape>
+51 −0
Original line number Diff line number Diff line
<?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.
  -->

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:maxWidth="@dimen/toast_width"
    android:orientation="horizontal"
    android:background="@drawable/toast_background"
    android:backgroundTint="?android:attr/colorBackground"
    android:layout_marginEnd="16dp"
    android:layout_marginStart="16dp"
    android:gravity="center_vertical">

    <!-- Icon should be 24x24, make slightly larger to allow for shadowing, adjust via padding -->
    <ImageView
        android:id="@+id/icon"
        android:alpha="@dimen/toast_icon_alpha"
        android:padding="11.5dp"
        android:layout_width="@dimen/toast_icon_size"
        android:layout_height="@dimen/toast_icon_size"/>
    <TextView
        android:id="@+id/text"
        android:ellipsize="end"
        android:maxLines="2"
        android:paddingTop="12dp"
        android:paddingBottom="12dp"
        android:paddingStart="0dp"
        android:paddingEnd="22dp"
        android:textSize="@dimen/toast_text_size"
        android:textColor="?android:attr/textColorPrimary"
        android:fontFamily="@*android:string/config_headlineFontFamily"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>
+2 −0
Original line number Diff line number Diff line
@@ -52,4 +52,6 @@
    <!-- (footer_height -48dp)/2 -->
    <dimen name="controls_management_footer_top_margin">4dp</dimen>
    <dimen name="controls_management_favorites_top_margin">8dp</dimen>

    <dimen name="toast_y_offset">24dp</dimen>
</resources>
Loading