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

Commit c0055b9b authored by Iris Yang's avatar Iris Yang
Browse files

Wire display context for text toasts

Toast doesn't work correctly for multi-display. When default display's
screen off, the toast wouldn't be able to show on secondary display.
Without this change, default display screen state will cause text toasts
doesn't correctly show up.

Bug: 233717651
Test: atest ToastUITest
Change-Id: I1705bc71062fa28c378bf7cc02d104c0f53ce865
parent 606074be
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -242,7 +242,8 @@ oneway interface IStatusBar
     * Displays a text toast.
     */
    void showToast(int uid, String packageName, IBinder token, CharSequence text,
            IBinder windowToken, int duration, @nullable ITransientNotificationCallback callback);
            IBinder windowToken, int duration, @nullable ITransientNotificationCallback callback,
            int displayId);

    /**
     * Cancels toast with token {@code token} in {@code packageName}.
+7 −4
Original line number Diff line number Diff line
@@ -398,11 +398,11 @@ public class CommandQueue extends IStatusBar.Stub implements

        /**
         * @see IStatusBar#showToast(int, String, IBinder, CharSequence, IBinder, int,
         * ITransientNotificationCallback)
         * ITransientNotificationCallback, int)
         */
        default void showToast(int uid, String packageName, IBinder token, CharSequence text,
                IBinder windowToken, int duration,
                @Nullable ITransientNotificationCallback callback) { }
                @Nullable ITransientNotificationCallback callback, int displayId) { }

        /**
         * @see IStatusBar#hideToast(String, IBinder) (String, IBinder)
@@ -944,7 +944,8 @@ public class CommandQueue extends IStatusBar.Stub implements

    @Override
    public void showToast(int uid, String packageName, IBinder token, CharSequence text,
            IBinder windowToken, int duration, @Nullable ITransientNotificationCallback callback) {
            IBinder windowToken, int duration, @Nullable ITransientNotificationCallback callback,
            int displayId) {
        synchronized (mLock) {
            SomeArgs args = SomeArgs.obtain();
            args.arg1 = packageName;
@@ -954,6 +955,7 @@ public class CommandQueue extends IStatusBar.Stub implements
            args.arg5 = callback;
            args.argi1 = uid;
            args.argi2 = duration;
            args.argi3 = displayId;
            mHandler.obtainMessage(MSG_SHOW_TOAST, args).sendToTarget();
        }
    }
@@ -1600,9 +1602,10 @@ public class CommandQueue extends IStatusBar.Stub implements
                            (ITransientNotificationCallback) args.arg5;
                    int uid = args.argi1;
                    int duration = args.argi2;
                    int displayId = args.argi3;
                    for (Callbacks callbacks : mCallbacks) {
                        callbacks.showToast(uid, packageName, token, text, windowToken, duration,
                                callback);
                                callback, displayId);
                    }
                    break;
                }
+8 −2
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@ import android.app.INotificationManager;
import android.app.ITransientNotificationCallback;
import android.content.Context;
import android.content.res.Configuration;
import android.hardware.display.DisplayManager;
import android.os.IBinder;
import android.os.ServiceManager;
import android.os.UserHandle;
@@ -106,10 +107,15 @@ public class ToastUI extends CoreStartable implements CommandQueue.Callbacks {
    @Override
    @MainThread
    public void showToast(int uid, String packageName, IBinder token, CharSequence text,
            IBinder windowToken, int duration, @Nullable ITransientNotificationCallback callback) {
            IBinder windowToken, int duration, @Nullable ITransientNotificationCallback callback,
            int displayId) {
        Runnable showToastRunnable = () -> {
            UserHandle userHandle = UserHandle.getUserHandleForUid(uid);
            Context context = mContext.createContextAsUser(userHandle, 0);

            DisplayManager mDisplayManager = mContext.getSystemService(DisplayManager.class);
            Context displayContext = context.createDisplayContext(
                    mDisplayManager.getDisplay(displayId));
            mToast = mToastFactory.createToast(mContext /* sysuiContext */, text, packageName,
                    userHandle.getIdentifier(), mOrientation);

@@ -118,7 +124,7 @@ public class ToastUI extends CoreStartable implements CommandQueue.Callbacks {
            }

            mCallback = callback;
            mPresenter = new ToastPresenter(context, mIAccessibilityManager,
            mPresenter = new ToastPresenter(displayContext, mIAccessibilityManager,
                    mNotificationManager, packageName);
            // Set as trusted overlay so touches can pass through toasts
            mPresenter.getLayoutParams().setTrustedOverlay();
+20 −18
Original line number Diff line number Diff line
@@ -44,6 +44,7 @@ import android.os.RemoteException;
import android.os.UserHandle;
import android.testing.AndroidTestingRunner;
import android.testing.TestableLooper;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@@ -122,6 +123,7 @@ public class ToastUITest extends SysuiTestCase {
        mContextSpy = spy(mContext);
        when(mContextSpy.getPackageManager()).thenReturn(mPackageManager);
        doReturn(mContextSpy).when(mContextSpy).createContextAsUser(any(), anyInt());
        doReturn(mContextSpy).when(mContextSpy).createDisplayContext(any());
        mToastUI = new ToastUI(
                mContextSpy,
                mCommandQueue,
@@ -144,7 +146,7 @@ public class ToastUITest extends SysuiTestCase {
    @Test
    public void testShowToast_addsCorrectViewToWindowManager() {
        mToastUI.showToast(UID_1, PACKAGE_NAME_1, TOKEN_1, TEXT, WINDOW_TOKEN_1, Toast.LENGTH_LONG,
                null);
                null, Display.DEFAULT_DISPLAY);

        verify(mWindowManager).addView(mViewCaptor.capture(), any());
        View view = mViewCaptor.getValue();
@@ -154,7 +156,7 @@ public class ToastUITest extends SysuiTestCase {
    @Test
    public void testShowToast_addsViewWithCorrectLayoutParamsToWindowManager() {
        mToastUI.showToast(UID_1, PACKAGE_NAME_1, TOKEN_1, TEXT, WINDOW_TOKEN_1, Toast.LENGTH_LONG,
                null);
                null, Display.DEFAULT_DISPLAY);

        verify(mWindowManager).addView(any(), mParamsCaptor.capture());
        ViewGroup.LayoutParams params = mParamsCaptor.getValue();
@@ -170,7 +172,7 @@ public class ToastUITest extends SysuiTestCase {
    @Test
    public void testShowToast_forAndroidPackage_addsAllUserFlag() throws Exception {
        mToastUI.showToast(ANDROID_UID, "android", TOKEN_1, TEXT, WINDOW_TOKEN_1, Toast.LENGTH_LONG,
                null);
                null, Display.DEFAULT_DISPLAY);

        verify(mWindowManager).addView(any(), mParamsCaptor.capture());
        ViewGroup.LayoutParams params = mParamsCaptor.getValue();
@@ -183,7 +185,7 @@ public class ToastUITest extends SysuiTestCase {
    @Test
    public void testShowToast_forSystemUiPackage_addsAllUserFlag() throws Exception {
        mToastUI.showToast(SYSTEMUI_UID, "com.android.systemui", TOKEN_1, TEXT, WINDOW_TOKEN_1,
                Toast.LENGTH_LONG, null);
                Toast.LENGTH_LONG, null, Display.DEFAULT_DISPLAY);

        verify(mWindowManager).addView(any(), mParamsCaptor.capture());
        ViewGroup.LayoutParams params = mParamsCaptor.getValue();
@@ -196,7 +198,7 @@ public class ToastUITest extends SysuiTestCase {
    @Test
    public void testShowToast_callsCallback() throws Exception {
        mToastUI.showToast(UID_1, PACKAGE_NAME_1, TOKEN_1, TEXT, WINDOW_TOKEN_1, Toast.LENGTH_LONG,
                mCallback);
                mCallback, Display.DEFAULT_DISPLAY);

        verify(mCallback).onToastShown();
    }
@@ -216,7 +218,7 @@ public class ToastUITest extends SysuiTestCase {
                mAccessibilityManager).sendAccessibilityEvent(any(), anyInt());

        mToastUI.showToast(UID_1, PACKAGE_NAME_1, TOKEN_1, TEXT, WINDOW_TOKEN_1, Toast.LENGTH_LONG,
                null);
                null, Display.DEFAULT_DISPLAY);

        eventParcel.setDataPosition(0);
        assertThat(eventParcel.dataSize()).isGreaterThan(0);
@@ -231,14 +233,14 @@ public class ToastUITest extends SysuiTestCase {
    public void testShowToast_accessibilityManagerClientIsRemoved() throws Exception {
        when(mContextSpy.getUserId()).thenReturn(USER_ID);
        mToastUI.showToast(UID_1, PACKAGE_NAME_1, TOKEN_1, TEXT, WINDOW_TOKEN_1, Toast.LENGTH_LONG,
                null);
                null, Display.DEFAULT_DISPLAY);
        verify(mAccessibilityManager).removeClient(any(), eq(USER_ID));
    }

    @Test
    public void testHideToast_removesView() throws Exception {
        mToastUI.showToast(UID_1, PACKAGE_NAME_1, TOKEN_1, TEXT, WINDOW_TOKEN_1, Toast.LENGTH_LONG,
                mCallback);
                mCallback, Display.DEFAULT_DISPLAY);
        final SystemUIToast toast = mToastUI.mToast;

        View view = verifyWmAddViewAndAttachToParent();
@@ -254,7 +256,7 @@ public class ToastUITest extends SysuiTestCase {
    @Test
    public void testHideToast_finishesToken() throws Exception {
        mToastUI.showToast(UID_1, PACKAGE_NAME_1, TOKEN_1, TEXT, WINDOW_TOKEN_1, Toast.LENGTH_LONG,
                mCallback);
                mCallback, Display.DEFAULT_DISPLAY);
        final SystemUIToast toast = mToastUI.mToast;

        verifyWmAddViewAndAttachToParent();
@@ -270,7 +272,7 @@ public class ToastUITest extends SysuiTestCase {
    @Test
    public void testHideToast_callsCallback() throws RemoteException {
        mToastUI.showToast(UID_1, PACKAGE_NAME_1, TOKEN_1, TEXT, WINDOW_TOKEN_1, Toast.LENGTH_LONG,
                mCallback);
                mCallback, Display.DEFAULT_DISPLAY);
        final SystemUIToast toast = mToastUI.mToast;

        verifyWmAddViewAndAttachToParent();
@@ -286,7 +288,7 @@ public class ToastUITest extends SysuiTestCase {
    @Test
    public void testHideToast_whenNotCurrentToastToken_doesNotHideToast() throws RemoteException {
        mToastUI.showToast(UID_1, PACKAGE_NAME_1, TOKEN_1, TEXT, WINDOW_TOKEN_1, Toast.LENGTH_LONG,
                mCallback);
                mCallback, Display.DEFAULT_DISPLAY);
        final SystemUIToast toast = mToastUI.mToast;

        verifyWmAddViewAndAttachToParent();
@@ -302,7 +304,7 @@ public class ToastUITest extends SysuiTestCase {
    @Test
    public void testHideToast_whenNotCurrentToastPackage_doesNotHideToast() throws RemoteException {
        mToastUI.showToast(UID_1, PACKAGE_NAME_1, TOKEN_1, TEXT, WINDOW_TOKEN_1, Toast.LENGTH_LONG,
                mCallback);
                mCallback, Display.DEFAULT_DISPLAY);
        final SystemUIToast toast = mToastUI.mToast;

        verifyWmAddViewAndAttachToParent();
@@ -318,12 +320,12 @@ public class ToastUITest extends SysuiTestCase {
    @Test
    public void testShowToast_afterShowToast_hidesCurrentToast() throws RemoteException {
        mToastUI.showToast(UID_1, PACKAGE_NAME_1, TOKEN_1, TEXT, WINDOW_TOKEN_1, Toast.LENGTH_LONG,
                mCallback);
                mCallback, Display.DEFAULT_DISPLAY);
        final SystemUIToast toast = mToastUI.mToast;

        View view = verifyWmAddViewAndAttachToParent();
        mToastUI.showToast(UID_2, PACKAGE_NAME_2, TOKEN_2, TEXT, WINDOW_TOKEN_2, Toast.LENGTH_LONG,
                null);
                null, Display.DEFAULT_DISPLAY);

        if (toast.getOutAnimation() != null) {
            assertThat(toast.getOutAnimation().isRunning()).isTrue();
@@ -338,7 +340,7 @@ public class ToastUITest extends SysuiTestCase {
    @Test
    public void testShowToast_logs() {
        mToastUI.showToast(UID_1, PACKAGE_NAME_1, TOKEN_1, TEXT, WINDOW_TOKEN_1, Toast.LENGTH_LONG,
                mCallback);
                mCallback, Display.DEFAULT_DISPLAY);

        verify(mToastLogger).logOnShowToast(UID_1, PACKAGE_NAME_1, TEXT, TOKEN_1.toString());
    }
@@ -354,7 +356,7 @@ public class ToastUITest extends SysuiTestCase {

        // WHEN the package posts a toast
        mToastUI.showToast(UID_1, PACKAGE_NAME_1, TOKEN_1, TEXT, WINDOW_TOKEN_1, Toast.LENGTH_LONG,
                mCallback);
                mCallback, Display.DEFAULT_DISPLAY);

        // THEN the view can have unlimited lines
        assertThat(((TextView) mToastUI.mToast.getView()
@@ -373,7 +375,7 @@ public class ToastUITest extends SysuiTestCase {

        // WHEN the package posts a toast
        mToastUI.showToast(UID_1, PACKAGE_NAME_1, TOKEN_1, TEXT, WINDOW_TOKEN_1, Toast.LENGTH_LONG,
                mCallback);
                mCallback, Display.DEFAULT_DISPLAY);

        // THEN the view is limited to 2 lines
        assertThat(((TextView) mToastUI.mToast.getView()
@@ -384,7 +386,7 @@ public class ToastUITest extends SysuiTestCase {
    @Test
    public void testHideToast_logs() {
        mToastUI.showToast(UID_1, PACKAGE_NAME_1, TOKEN_1, TEXT, WINDOW_TOKEN_1, Toast.LENGTH_LONG,
                mCallback);
                mCallback, Display.DEFAULT_DISPLAY);
        verifyWmAddViewAndAttachToParent();
        mToastUI.hideToast(PACKAGE_NAME_1, TOKEN_1);
        verify(mToastLogger).logOnHideToast(PACKAGE_NAME_1, TOKEN_1.toString());
+2 −1
Original line number Diff line number Diff line
@@ -62,7 +62,8 @@ public class TextToastRecord extends ToastRecord {
            Slog.w(TAG, "StatusBar not available to show text toast for package " + pkg);
            return false;
        }
        mStatusBar.showToast(uid, pkg, token, text, windowToken, getDuration(), mCallback);
        mStatusBar.showToast(uid, pkg, token, text, windowToken, getDuration(), mCallback,
                displayId);
        return true;
    }

Loading