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

Commit 9643b85a authored by Patrick Williams's avatar Patrick Williams
Browse files

Update ScreenCaptureListener.onError to call through JNI

Bug: 370855681
Flag: com.android.graphics.surfaceflinger.flags.readback_screenshot
Test: atest CtsWindowManagerDeviceOther:android.server.wm.other.ScreenCaptureTest
Change-Id: Ic624224bc2e041fe46f6f9f9c338c18ba51fe6ac
parent a41df855
Loading
Loading
Loading
Loading
+3 −7
Original line number Diff line number Diff line
@@ -67,6 +67,8 @@ public class ScreenCaptureInternal {

    private static native long getNativeListenerFinalizer();

    private static native void nativeListenerOnError(long nativeObject, int errorCode);

    /**
     * @param captureArgs     Arguments about how to take the screenshot
     * @param captureListener A listener to receive the screenshot callback
@@ -785,8 +787,6 @@ public class ScreenCaptureInternal {
     * This listener can only be used for a single call to capture content call.
     */
    public static class ScreenCaptureListener implements Parcelable {
        // Transient. Not recoverable from Parcel.
        private final ObjIntConsumer<ScreenshotHardwareBuffer> mConsumer;
        final long mNativeObject;
        private static final NativeAllocationRegistry sRegistry =
                NativeAllocationRegistry.createMalloced(
@@ -796,13 +796,11 @@ public class ScreenCaptureInternal {
         * @param consumer The callback invoked when the screen capture is complete.
         */
        public ScreenCaptureListener(ObjIntConsumer<ScreenshotHardwareBuffer> consumer) {
            this.mConsumer = consumer;
            mNativeObject = nativeCreateScreenCaptureListener(consumer);
            sRegistry.registerNativeAllocation(this, mNativeObject);
        }

        private ScreenCaptureListener(Parcel in) {
            mConsumer = null;
            if (in.readBoolean()) {
                mNativeObject = nativeReadListenerFromParcel(in);
                sRegistry.registerNativeAllocation(this, mNativeObject);
@@ -830,9 +828,7 @@ public class ScreenCaptureInternal {
         * Call when the screen capture fails.
         */
        public void onError(@ScreenCapture.ScreenCaptureErrorCode int errorCode) {
            if (mConsumer != null) {
                mConsumer.accept(null, errorCode);
            }
            nativeListenerOnError(mNativeObject, errorCode);
        }

        public static final Parcelable.Creator<ScreenCaptureListener> CREATOR =
+16 −0
Original line number Diff line number Diff line
@@ -141,6 +141,15 @@ public:
        return binder::Status::ok();
    }

    void onError(JNIEnv* env, int errorCode) {
        ScopedLocalRef<jobject> consumer{env, env->NewLocalRef(mConsumerWeak)};
        if (consumer == nullptr) {
            ALOGE("ScreenCaptureListenerWrapper::onError - consumer not alive");
            return;
        }
        env->CallVoidMethod(consumer.get(), gConsumerClassInfo.accept, nullptr, errorCode);
    }

private:
    jweak mConsumerWeak;
    JavaVM* mVm;
@@ -302,6 +311,12 @@ static jlong getNativeListenerFinalizer(JNIEnv* env, jclass clazz) {
    return static_cast<jlong>(reinterpret_cast<uintptr_t>(&destroyNativeListener));
}

static void nativeListenerOnError(JNIEnv* env, jclass clazz, jlong listenerPtr, jint errorCode) {
    ScreenCaptureListenerWrapper* listener =
            reinterpret_cast<ScreenCaptureListenerWrapper*>(listenerPtr);
    listener->onError(env, errorCode);
}

// ----------------------------------------------------------------------------

static const JNINativeMethod sScreenCaptureMethods[] = {
@@ -316,6 +331,7 @@ static const JNINativeMethod sScreenCaptureMethods[] = {
    {"nativeReadListenerFromParcel", "(Landroid/os/Parcel;)J",
            (void*)nativeReadListenerFromParcel },
    {"getNativeListenerFinalizer", "()J", (void*)getNativeListenerFinalizer },
    {"nativeListenerOnError", "(JI)V", (void*)nativeListenerOnError },
        // clang-format on
};