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

Commit 51db10d9 authored by chaviw's avatar chaviw
Browse files

Add displayInfo to Java WindowInfosListener.

The displayInfo contains information about transform and display size.
This info was not being passed to the WindowInfosListener, making it
difficult to get coordinates on screen.

Send the displayInfo through JNI to the Java side listener

Test: WindowInfosListener gets displayInfo
Bug: 188792659
Change-Id: Ib4fdb6f0c27bd4276421c4a5235c5112514b32f3
parent 157d6623
Loading
Loading
Loading
Loading
+34 −1
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

package android.window;

import android.graphics.Matrix;
import android.util.Size;
import android.view.InputWindowHandle;

import libcore.util.NativeAllocationRegistry;
@@ -40,7 +42,8 @@ public abstract class WindowInfosListener {
     * @param windowHandles Reverse Z ordered array of window information that was on screen,
     *                      where the first value is the topmost window.
     */
    public abstract void onWindowInfosChanged(InputWindowHandle[] windowHandles);
    public abstract void onWindowInfosChanged(InputWindowHandle[] windowHandles,
            DisplayInfo[] displayInfos);

    /**
     * Register the WindowInfosListener.
@@ -60,4 +63,34 @@ public abstract class WindowInfosListener {
    private static native void nativeRegister(long ptr);
    private static native void nativeUnregister(long ptr);
    private static native long nativeGetFinalizer();

    /**
     * Describes information about a display that can have windows in it.
     */
    public static final class DisplayInfo {
        public final int mDisplayId;

        /**
         * Logical display dimensions.
         */
        public final Size mLogicalSize;

        /**
         * The display transform. This takes display coordinates to logical display coordinates.
         */
        public final Matrix mTransform;

        private DisplayInfo(int displayId, int logicalWidth, int logicalHeight, Matrix transform) {
            mDisplayId = displayId;
            mLogicalSize = new Size(logicalWidth, logicalHeight);
            mTransform = transform;
        }

        @Override
        public String toString() {
            return "displayId=" + mDisplayId
                    + ", mLogicalSize=" + mLogicalSize
                    + ", mTransform=" + mTransform;
        }
    }
}
+35 −3
Original line number Diff line number Diff line
@@ -16,8 +16,10 @@

#define LOG_TAG "WindowInfosListener"

#include <android/graphics/matrix.h>
#include <android_runtime/AndroidRuntime.h>
#include <android_runtime/Log.h>
#include <gui/DisplayInfo.h>
#include <gui/SurfaceComposerClient.h>
#include <nativehelper/JNIHelp.h>
#include <utils/Log.h>
@@ -37,14 +39,30 @@ static struct {
    jmethodID onWindowInfosChanged;
} gListenerClassInfo;

static struct {
    jclass clazz;
    jmethodID ctor;
} gDisplayInfoClassInfo;

static jclass gInputWindowHandleClass;

jobject fromDisplayInfo(JNIEnv* env, gui::DisplayInfo displayInfo) {
    float transformValues[9];
    for (int i = 0; i < 9; i++) {
        transformValues[i] = displayInfo.transform[i % 3][i / 3];
    }
    ScopedLocalRef<jobject> matrixObj(env, AMatrix_newInstance(env, transformValues));
    return env->NewObject(gDisplayInfoClassInfo.clazz, gDisplayInfoClassInfo.ctor,
                          displayInfo.displayId, displayInfo.logicalWidth,
                          displayInfo.logicalHeight, matrixObj.get());
}

struct WindowInfosListener : public gui::WindowInfosListener {
    WindowInfosListener(JNIEnv* env, jobject listener)
          : mListener(env->NewWeakGlobalRef(listener)) {}

    void onWindowInfosChanged(const std::vector<WindowInfo>& windowInfos,
                              const std::vector<DisplayInfo>& /*displayInfos*/) override {
                              const std::vector<DisplayInfo>& displayInfos) override {
        JNIEnv* env = AndroidRuntime::getJNIEnv();
        LOG_ALWAYS_FATAL_IF(env == nullptr, "Unable to retrieve JNIEnv in onWindowInfoChanged.");

@@ -64,7 +82,15 @@ struct WindowInfosListener : public gui::WindowInfosListener {
            env->SetObjectArrayElement(jWindowHandlesArray, i, jWindowHandle.get());
        }

        env->CallVoidMethod(listener, gListenerClassInfo.onWindowInfosChanged, jWindowHandlesArray);
        jobjectArray jDisplayInfoArray =
                env->NewObjectArray(displayInfos.size(), gDisplayInfoClassInfo.clazz, nullptr);
        for (int i = 0; i < displayInfos.size(); i++) {
            ScopedLocalRef<jobject> jDisplayInfo(env, fromDisplayInfo(env, displayInfos[i]));
            env->SetObjectArrayElement(jDisplayInfoArray, i, jDisplayInfo.get());
        }

        env->CallVoidMethod(listener, gListenerClassInfo.onWindowInfosChanged, jWindowHandlesArray,
                            jDisplayInfoArray);
        env->DeleteGlobalRef(listener);

        if (env->ExceptionCheck()) {
@@ -126,10 +152,16 @@ int register_android_window_WindowInfosListener(JNIEnv* env) {
    gListenerClassInfo.clazz = MakeGlobalRefOrDie(env, clazz);
    gListenerClassInfo.onWindowInfosChanged =
            env->GetMethodID(gListenerClassInfo.clazz, "onWindowInfosChanged",
                             "([Landroid/view/InputWindowHandle;)V");
                             "([Landroid/view/InputWindowHandle;[Landroid/window/"
                             "WindowInfosListener$DisplayInfo;)V");

    clazz = env->FindClass("android/view/InputWindowHandle");
    gInputWindowHandleClass = MakeGlobalRefOrDie(env, clazz);

    clazz = env->FindClass("android/window/WindowInfosListener$DisplayInfo");
    gDisplayInfoClassInfo.clazz = MakeGlobalRefOrDie(env, clazz);
    gDisplayInfoClassInfo.ctor = env->GetMethodID(gDisplayInfoClassInfo.clazz, "<init>",
                                                  "(IIILandroid/graphics/Matrix;)V");
    return 0;
}