Use window context to create the touch overlay
Fixes: 160919984
Test: manual
Change-Id: I01ee4ea836664fdb9418018cc056a4f92afe02c5
diff --git a/src/com/android/car/rotary/RotaryService.java b/src/com/android/car/rotary/RotaryService.java
index 835ee32..1a7f63e 100644
--- a/src/com/android/car/rotary/RotaryService.java
+++ b/src/com/android/car/rotary/RotaryService.java
@@ -303,6 +303,8 @@
*/
private int mCenterButtonRepeatCount;
+ private Context mWindowContext;
+
private static final Map<Integer, Integer> TEST_TO_REAL_KEYCODE_MAP;
private static final Map<Integer, Integer> DIRECTION_TO_KEYCODE_MAP;
@@ -431,12 +433,7 @@
// Guarantee that we always return the same WindowManager instance.
if (WINDOW_SERVICE.equals(name)) {
if (mWindowManager == null) {
- // We need to set the display before creating the WindowContext.
- DisplayManager displayManager = getSystemService(DisplayManager.class);
- Display primaryDisplay = displayManager.getDisplay(DEFAULT_DISPLAY);
- updateDisplay(primaryDisplay.getDisplayId());
-
- Context windowContext = createWindowContext(TYPE_APPLICATION_OVERLAY, null);
+ Context windowContext = getWindowContext();
mWindowManager = (WindowManager) windowContext.getSystemService(WINDOW_SERVICE);
}
return mWindowManager;
@@ -592,6 +589,18 @@
// Do nothing.
}
+ private Context getWindowContext() {
+ if (mWindowContext == null) {
+ // We need to set the display before creating the WindowContext.
+ DisplayManager displayManager = getSystemService(DisplayManager.class);
+ Display primaryDisplay = displayManager.getDisplay(DEFAULT_DISPLAY);
+ updateDisplay(primaryDisplay.getDisplayId());
+
+ mWindowContext = createWindowContext(TYPE_APPLICATION_OVERLAY, null);
+ }
+ return mWindowContext;
+ }
+
/**
* Adds an overlay to capture touch events. The overlay has zero width and height so
* it doesn't prevent other windows from receiving touch events. It sets
@@ -601,7 +610,10 @@
* isn't considered a click.
*/
private void addTouchOverlay() {
- FrameLayout frameLayout = new FrameLayout(this);
+ // Only views with a visual context, such as a window context, can be added by
+ // WindowManager.
+ FrameLayout frameLayout = new FrameLayout(getWindowContext());
+
FrameLayout.LayoutParams frameLayoutParams =
new FrameLayout.LayoutParams(/* width= */ 0, /* height= */ 0);
frameLayout.setLayoutParams(frameLayoutParams);