Merge "Wire display context for text toasts" into tm-dev am: ca6ef9125d am: 7bb2e73638

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/18660549

Change-Id: I09157e65dc8fd173a1bcd37108e620c3054c1b08
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
Iris Yang
2022-06-03 03:40:32 +00:00
committed by Automerger Merge Worker
8 changed files with 53 additions and 37 deletions

View File

@@ -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}.

View File

@@ -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;
}

View File

@@ -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();

View File

@@ -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());

View File

@@ -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;
}

View File

@@ -144,11 +144,11 @@ public interface StatusBarManagerInternal {
/**
* @see com.android.internal.statusbar.IStatusBar#showToast(String, IBinder, CharSequence,
* IBinder, int, ITransientNotificationCallback)
* IBinder, int, ITransientNotificationCallback, int)
*/
void showToast(int uid, String packageName, IBinder token, CharSequence text,
IBinder windowToken, int duration,
@Nullable ITransientNotificationCallback textCallback);
@Nullable ITransientNotificationCallback textCallback, int displayId);
/** @see com.android.internal.statusbar.IStatusBar#hideToast(String, IBinder) */
void hideToast(String packageName, IBinder token);

View File

@@ -631,10 +631,11 @@ public class StatusBarManagerService extends IStatusBarService.Stub implements D
@Override
public void showToast(int uid, String packageName, IBinder token, CharSequence text,
IBinder windowToken, int duration,
@Nullable ITransientNotificationCallback callback) {
@Nullable ITransientNotificationCallback callback, int displayId) {
if (mBar != null) {
try {
mBar.showToast(uid, packageName, token, text, windowToken, duration, callback);
mBar.showToast(uid, packageName, token, text, windowToken, duration, callback,
displayId);
} catch (RemoteException ex) { }
}
}

View File

@@ -6227,13 +6227,13 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
// first time trying to show the toast, showToast gets called
nmService.enqueueTextToast(testPackage, token, "Text", 2000, 0, null);
verify(mStatusBar, times(1))
.showToast(anyInt(), any(), any(), any(), any(), anyInt(), any());
.showToast(anyInt(), any(), any(), any(), any(), anyInt(), any(), anyInt());
// second time trying to show the same toast, showToast isn't called again (total number of
// invocations stays at one)
nmService.enqueueTextToast(testPackage, token, "Text", 2000, 0, null);
verify(mStatusBar, times(1))
.showToast(anyInt(), any(), any(), any(), any(), anyInt(), any());
.showToast(anyInt(), any(), any(), any(), any(), anyInt(), any(), anyInt());
}
@Test
@@ -6255,7 +6255,7 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
nmService.enqueueTextToast(testPackage, token, "Text", 2000, 0, null);
verify(mStatusBar, times(0))
.showToast(anyInt(), any(), any(), any(), any(), anyInt(), any());
.showToast(anyInt(), any(), any(), any(), any(), anyInt(), any(), anyInt());
}
@Test
@@ -6277,7 +6277,7 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
nmService.enqueueTextToast(testPackage, token, "Text", 2000, 0, null);
verify(mStatusBar, times(1))
.showToast(anyInt(), any(), any(), any(), any(), anyInt(), any());
.showToast(anyInt(), any(), any(), any(), any(), anyInt(), any(), anyInt());
}
@Test
@@ -6297,7 +6297,8 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
INotificationManager nmService = (INotificationManager) mService.mService;
nmService.enqueueTextToast(testPackage, token, "Text", 2000, 0, null);
verify(mStatusBar).showToast(anyInt(), any(), any(), any(), any(), anyInt(), any());
verify(mStatusBar).showToast(anyInt(), any(), any(), any(), any(), anyInt(), any(),
anyInt());
}
@Test
@@ -6326,7 +6327,7 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
// but never shown
verify(mStatusBar, times(0))
.showToast(anyInt(), any(), any(), any(), any(), anyInt(), any());
.showToast(anyInt(), any(), any(), any(), any(), anyInt(), any(), anyInt());
// and removed when rate limited
verify(mWindowManagerInternal)
@@ -6417,7 +6418,8 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
// enqueue toast -> no toasts enqueued
((INotificationManager) mService.mService).enqueueTextToast(testPackage, new Binder(),
"Text", 2000, 0, null);
verify(mStatusBar).showToast(anyInt(), any(), any(), any(), any(), anyInt(), any());
verify(mStatusBar).showToast(anyInt(), any(), any(), any(), any(), anyInt(), any(),
anyInt());
}
@Test