Adjust toast window parameters to support FLAG_SHOW_WHEN_LOCKED

Toast can not show on display when screen is locked.Adjust rule to support
it with conditions.The calling activity initiate toast and is visible with
FLAG_SHOW_WHEN_LOCKED.

Bug: 129434562
Test: 1. atest DisplayPolicyTests
      2. Launch an activity with the flag. Make sure toast be shown when
         screen is locked.

Change-Id: I7092f2d1c35cb6b8afb85576b54540f59e4f1c2f
This commit is contained in:
Jeff Chang
2019-04-09 17:28:30 +08:00
parent 715c495a41
commit b10fac728e
2 changed files with 55 additions and 0 deletions

View File

@@ -822,6 +822,10 @@ public class DisplayPolicy {
(int) attrs.hideTimeoutMilliseconds,
AccessibilityManager.FLAG_CONTENT_TEXT);
attrs.windowAnimations = com.android.internal.R.style.Animation_Toast;
// Toast can show with below conditions when the screen is locked.
if (canToastShowWhenLocked(callingPid)) {
attrs.flags |= WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
}
break;
}
@@ -831,6 +835,16 @@ public class DisplayPolicy {
}
}
/**
* @return {@code true} if the calling activity initiate toast and is visible with
* {@link WindowManager.LayoutParams#FLAG_SHOW_WHEN_LOCKED} flag.
*/
boolean canToastShowWhenLocked(int callingPid) {
return mDisplayContent.forAllWindows(w -> {
return callingPid == w.mSession.mPid && w.isVisible() && w.canShowWhenLocked();
}, true /* traverseTopToBottom */);
}
/**
* Preflight adding a window to the system.
*

View File

@@ -22,13 +22,17 @@ import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
import static android.view.WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
import static android.view.WindowManager.LayoutParams.FLAG_DIM_BEHIND;
import static android.view.WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS;
import static android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
import static android.view.WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR;
import static android.view.WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
import static android.view.WindowManager.LayoutParams.ROTATION_ANIMATION_SEAMLESS;
import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION;
import static android.view.WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
import static android.view.WindowManager.LayoutParams.TYPE_TOAST;
import static com.android.dx.mockito.inline.extended.ExtendedMockito.doReturn;
import static com.android.dx.mockito.inline.extended.ExtendedMockito.mock;
@@ -38,6 +42,7 @@ import static com.android.server.policy.WindowManagerPolicy.NAV_BAR_RIGHT;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.spy;
@@ -232,4 +237,40 @@ public class DisplayPolicyTests extends WindowTestsBase {
displayRotation, Surface.ROTATION_0, Surface.ROTATION_90));
}
}
@Test
public void testShouldShowToastWhenScreenLocked() {
final DisplayPolicy policy = mDisplayContent.getDisplayPolicy();
final WindowState activity = createApplicationWindow();
final WindowState toast = createToastWindow();
synchronized (mWm.mGlobalLock) {
policy.adjustWindowParamsLw(
toast, toast.mAttrs, 0 /* callingPid */, 0 /* callingUid */);
assertTrue(policy.canToastShowWhenLocked(0 /* callingUid */));
assertNotEquals(0, toast.getAttrs().flags & FLAG_SHOW_WHEN_LOCKED);
}
}
private WindowState createToastWindow() {
final WindowState win = createWindow(null, TYPE_TOAST, "Toast");
final WindowManager.LayoutParams attrs = win.mAttrs;
attrs.width = WRAP_CONTENT;
attrs.height = WRAP_CONTENT;
attrs.flags = FLAG_KEEP_SCREEN_ON | FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCHABLE;
attrs.format = PixelFormat.TRANSLUCENT;
return win;
}
private WindowState createApplicationWindow() {
final WindowState win = createWindow(null, TYPE_APPLICATION, "Application");
final WindowManager.LayoutParams attrs = win.mAttrs;
attrs.width = MATCH_PARENT;
attrs.height = MATCH_PARENT;
attrs.flags = FLAG_SHOW_WHEN_LOCKED | FLAG_LAYOUT_IN_SCREEN | FLAG_LAYOUT_INSET_DECOR;
attrs.format = PixelFormat.OPAQUE;
win.mHasSurface = true;
return win;
}
}