Generate ACTION_CANCEL event when screen turned off by proximity sensor.

When using PROXIMITY_SCREEN_OFF_WAKE_LOCK and screen was turned off by proximity sensor,
input service should generate ACTION_CANCEL, same as display turned off by pressing power button.
Add power state in display view port and notify input service for
display power state change.

Bug: 154074380
Test: atest libgui_test
Change-Id: Icb65aead4b5544182c46101f4ba535f5955ebd6c
Merged-In: Icb65aead4b5544182c46101f4ba535f5955ebd6c
diff --git a/core/java/android/hardware/display/DisplayViewport.java b/core/java/android/hardware/display/DisplayViewport.java
index 5adf948..f2d4c3d 100644
--- a/core/java/android/hardware/display/DisplayViewport.java
+++ b/core/java/android/hardware/display/DisplayViewport.java
@@ -49,6 +49,9 @@
     // True if this viewport is valid.
     public boolean valid;
 
+    // True if this viewport is active.
+    public boolean isActive;
+
     // The logical display id.
     public int displayId;
 
@@ -79,6 +82,7 @@
 
     public void copyFrom(DisplayViewport viewport) {
         valid = viewport.valid;
+        isActive = viewport.isActive;
         displayId = viewport.displayId;
         orientation = viewport.orientation;
         logicalFrame.set(viewport.logicalFrame);
@@ -111,6 +115,7 @@
 
         DisplayViewport other = (DisplayViewport) o;
         return valid == other.valid
+              && isActive == other.isActive
               && displayId == other.displayId
               && orientation == other.orientation
               && logicalFrame.equals(other.logicalFrame)
@@ -127,6 +132,7 @@
         final int prime = 31;
         int result = 1;
         result += prime * result + (valid ? 1 : 0);
+        result += prime * result + (isActive ? 1 : 0);
         result += prime * result + displayId;
         result += prime * result + orientation;
         result += prime * result + logicalFrame.hashCode();
@@ -147,6 +153,7 @@
         final Integer port = physicalPort == null ? null : Byte.toUnsignedInt(physicalPort);
         return "DisplayViewport{type=" + typeToString(type)
                 + ", valid=" + valid
+                + ", isActive=" + isActive
                 + ", displayId=" + displayId
                 + ", uniqueId='" + uniqueId + "'"
                 + ", physicalPort=" + port
diff --git a/core/jni/android_hardware_display_DisplayViewport.cpp b/core/jni/android_hardware_display_DisplayViewport.cpp
index e74aafe..c25da0f 100644
--- a/core/jni/android_hardware_display_DisplayViewport.cpp
+++ b/core/jni/android_hardware_display_DisplayViewport.cpp
@@ -34,6 +34,7 @@
     jclass clazz;
 
     jfieldID displayId;
+    jfieldID isActive;
     jfieldID orientation;
     jfieldID logicalFrame;
     jfieldID physicalFrame;
@@ -59,6 +60,7 @@
     static const jmethodID byteValue = env->GetMethodID(byteClass, "byteValue", "()B");
 
     viewport->displayId = env->GetIntField(viewportObj, gDisplayViewportClassInfo.displayId);
+    viewport->isActive = env->GetBooleanField(viewportObj, gDisplayViewportClassInfo.isActive);
     viewport->orientation = env->GetIntField(viewportObj, gDisplayViewportClassInfo.orientation);
     viewport->deviceWidth = env->GetIntField(viewportObj, gDisplayViewportClassInfo.deviceWidth);
     viewport->deviceHeight = env->GetIntField(viewportObj, gDisplayViewportClassInfo.deviceHeight);
@@ -104,6 +106,9 @@
     gDisplayViewportClassInfo.displayId = GetFieldIDOrDie(env,
             gDisplayViewportClassInfo.clazz, "displayId", "I");
 
+    gDisplayViewportClassInfo.isActive =
+            GetFieldIDOrDie(env, gDisplayViewportClassInfo.clazz, "isActive", "Z");
+
     gDisplayViewportClassInfo.orientation = GetFieldIDOrDie(env,
             gDisplayViewportClassInfo.clazz, "orientation", "I");
 
diff --git a/services/core/java/com/android/server/display/DisplayManagerService.java b/services/core/java/com/android/server/display/DisplayManagerService.java
index 02d499f..dee6cd0 100644
--- a/services/core/java/com/android/server/display/DisplayManagerService.java
+++ b/services/core/java/com/android/server/display/DisplayManagerService.java
@@ -111,6 +111,7 @@
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
+import java.util.Optional;
 import java.util.concurrent.CopyOnWriteArrayList;
 
 /**
@@ -971,6 +972,18 @@
             if (diff == DisplayDeviceInfo.DIFF_STATE) {
                 Slog.i(TAG, "Display device changed state: \"" + info.name
                         + "\", " + Display.stateToString(info.state));
+                final Optional<Integer> viewportType = getViewportType(info);
+                if (viewportType.isPresent()) {
+                    for (DisplayViewport d : mViewports) {
+                        if (d.type == viewportType.get() && info.uniqueId.equals(d.uniqueId)) {
+                            // Update display view port power state
+                            d.isActive = Display.isActiveState(info.state);
+                        }
+                    }
+                    if (mInputManagerInternal != null) {
+                        mHandler.sendEmptyMessage(MSG_UPDATE_VIEWPORT);
+                    }
+                }
             } else if (diff != 0) {
                 Slog.i(TAG, "Display device changed: " + info);
             }
@@ -1507,6 +1520,23 @@
         mViewports.clear();
     }
 
+    private Optional<Integer> getViewportType(DisplayDeviceInfo info) {
+        // Get the corresponding viewport type.
+        if ((info.flags & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0) {
+            return Optional.of(VIEWPORT_INTERNAL);
+        } else if (info.touch == DisplayDeviceInfo.TOUCH_EXTERNAL) {
+            return Optional.of(VIEWPORT_EXTERNAL);
+        } else if (info.touch == DisplayDeviceInfo.TOUCH_VIRTUAL
+                && !TextUtils.isEmpty(info.uniqueId)) {
+            return Optional.of(VIEWPORT_VIRTUAL);
+        } else {
+            if (DEBUG) {
+                Slog.i(TAG, "Display " + info + " does not support input device matching.");
+            }
+        }
+        return Optional.empty();
+    }
+
     private void configureDisplayLocked(SurfaceControl.Transaction t, DisplayDevice device) {
         final DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
         final boolean ownContent = (info.flags & DisplayDeviceInfo.FLAG_OWN_CONTENT_ONLY) != 0;
@@ -1533,21 +1563,10 @@
             return;
         }
         display.configureDisplayLocked(t, device, info.state == Display.STATE_OFF);
-        final int viewportType;
-        // Update the corresponding viewport.
-        if ((info.flags & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0) {
-            viewportType = VIEWPORT_INTERNAL;
-        } else if (info.touch == DisplayDeviceInfo.TOUCH_EXTERNAL) {
-            viewportType = VIEWPORT_EXTERNAL;
-        } else if (info.touch == DisplayDeviceInfo.TOUCH_VIRTUAL
-                && !TextUtils.isEmpty(info.uniqueId)) {
-            viewportType = VIEWPORT_VIRTUAL;
-        } else {
-            Slog.i(TAG, "Display " + info + " does not support input device matching.");
-            return;
+        final Optional<Integer> viewportType = getViewportType(info);
+        if (viewportType.isPresent()) {
+            populateViewportLocked(viewportType.get(), display.getDisplayIdLocked(), device, info);
         }
-
-        populateViewportLocked(viewportType, display.getDisplayIdLocked(), device, info.uniqueId);
     }
 
     /**
@@ -1587,12 +1606,13 @@
         return viewport;
     }
 
-    private void populateViewportLocked(int viewportType,
-            int displayId, DisplayDevice device, String uniqueId) {
-        final DisplayViewport viewport = getViewportLocked(viewportType, uniqueId);
+    private void populateViewportLocked(int viewportType, int displayId, DisplayDevice device,
+            DisplayDeviceInfo info) {
+        final DisplayViewport viewport = getViewportLocked(viewportType, info.uniqueId);
         device.populateViewportLocked(viewport);
         viewport.valid = true;
         viewport.displayId = displayId;
+        viewport.isActive = Display.isActiveState(info.state);
     }
 
     private LogicalDisplay findLogicalDisplayForDeviceLocked(DisplayDevice device) {
diff --git a/services/core/jni/com_android_server_input_InputManagerService.cpp b/services/core/jni/com_android_server_input_InputManagerService.cpp
index 2013945..75ec224 100644
--- a/services/core/jni/com_android_server_input_InputManagerService.cpp
+++ b/services/core/jni/com_android_server_input_InputManagerService.cpp
@@ -403,7 +403,8 @@
 
             DisplayViewport viewport;
             android_hardware_display_DisplayViewport_toNative(env, viewportObj, &viewport);
-            ALOGI("Viewport [%d] to add: %s", (int) i, viewport.uniqueId.c_str());
+            ALOGI("Viewport [%d] to add: %s, isActive: %s", (int)i, viewport.uniqueId.c_str(),
+                  toString(viewport.isActive));
             viewports.push_back(viewport);
 
             env->DeleteLocalRef(viewportObj);