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

Commit aeaa12c3 authored by Daichi Hirono's avatar Daichi Hirono Committed by Android (Google) Code Review
Browse files

Merge "Add readEvent method to MtpDevice."

parents f4a09907 0b494663
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -18133,6 +18133,7 @@ package android.mtp {
    method public boolean importFile(int, java.lang.String);
    method public boolean importFile(int, android.os.ParcelFileDescriptor);
    method public boolean open(android.hardware.usb.UsbDeviceConnection);
    method public android.mtp.MtpEvent readEvent(android.os.CancellationSignal);
    method public boolean sendObject(int, int, android.os.ParcelFileDescriptor);
    method public android.mtp.MtpObjectInfo sendObjectInfo(android.mtp.MtpObjectInfo);
  }
@@ -18144,6 +18145,11 @@ package android.mtp {
    method public final java.lang.String getVersion();
  }
  public class MtpEvent {
    ctor public MtpEvent();
    method public int getEventCode();
  }
  public final class MtpObjectInfo {
    method public final int getAssociationDesc();
    method public final int getAssociationType();
+6 −0
Original line number Diff line number Diff line
@@ -19645,6 +19645,7 @@ package android.mtp {
    method public boolean importFile(int, java.lang.String);
    method public boolean importFile(int, android.os.ParcelFileDescriptor);
    method public boolean open(android.hardware.usb.UsbDeviceConnection);
    method public android.mtp.MtpEvent readEvent(android.os.CancellationSignal);
    method public boolean sendObject(int, int, android.os.ParcelFileDescriptor);
    method public android.mtp.MtpObjectInfo sendObjectInfo(android.mtp.MtpObjectInfo);
  }
@@ -19656,6 +19657,11 @@ package android.mtp {
    method public final java.lang.String getVersion();
  }
  public class MtpEvent {
    ctor public MtpEvent();
    method public int getEventCode();
  }
  public final class MtpObjectInfo {
    method public final int getAssociationDesc();
    method public final int getAssociationType();
+38 −1
Original line number Diff line number Diff line
@@ -18,6 +18,8 @@ package android.mtp;

import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import android.os.CancellationSignal;
import android.os.OperationCanceledException;
import android.os.ParcelFileDescriptor;

/**
@@ -278,6 +280,38 @@ public final class MtpDevice {
        return native_send_object_info(info);
    }

    /**
     * Reads an event from the device. It blocks the current thread until it gets an event.
     * It throws OperationCanceledException if it is cancelled by signal.
     *
     * @param signal signal for cancellation
     * @return obtained event
     */
    public MtpEvent readEvent(CancellationSignal signal) {
        final int handle = native_submit_event_request();

        if (handle < 0) {
            throw new IllegalStateException("Other thread is reading an event.");
        }

        if (signal != null) {
            signal.setOnCancelListener(new CancellationSignal.OnCancelListener() {
                @Override
                public void onCancel() {
                    native_discard_event_request(handle);
                }
            });
        }

        try {
            return native_reap_event_request(handle);
        } finally {
            if (signal != null) {
                signal.setOnCancelListener(null);
            }
        }
    }

    // used by the JNI code
    private long mNativeContext;

@@ -297,4 +331,7 @@ public final class MtpDevice {
    private native boolean native_import_file(int objectHandle, int fd);
    private native boolean native_send_object(int objectHandle, int size, int fd);
    private native MtpObjectInfo native_send_object_info(MtpObjectInfo info);
    private native int native_submit_event_request();
    private native MtpEvent native_reap_event_request(int handle);
    private native void native_discard_event_request(int handle);
}
+31 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.mtp;

/**
 * This class encapsulates information about a MTP event.
 */
public class MtpEvent {
    private int mEventCode;

    /**
     * Returns event code of MTP event.
     *
     * @return event code
     */
    public int getEventCode() { return mEventCode; }
}
+77 −1
Original line number Diff line number Diff line
@@ -46,10 +46,14 @@ static jfieldID field_context;
jclass clazz_deviceInfo;
jclass clazz_storageInfo;
jclass clazz_objectInfo;
jclass clazz_event;
jclass clazz_io_exception;
jclass clazz_operation_canceled_exception;

jmethodID constructor_deviceInfo;
jmethodID constructor_storageInfo;
jmethodID constructor_objectInfo;
jmethodID constructor_event;

// MtpDeviceInfo fields
static jfieldID field_deviceInfo_manufacturer;
@@ -86,6 +90,9 @@ static jfieldID field_objectInfo_dateCreated;
static jfieldID field_objectInfo_dateModified;
static jfieldID field_objectInfo_keywords;

// MtpEvent fields
static jfieldID field_event_eventCode;

MtpDevice* get_device_from_object(JNIEnv* env, jobject javaDevice)
{
    return (MtpDevice*)env->GetLongField(javaDevice, field_context);
@@ -488,6 +495,42 @@ android_mtp_MtpDevice_send_object_info(JNIEnv *env, jobject thiz, jobject info)
    return result;
}

static jint android_mtp_MtpDevice_submit_event_request(JNIEnv *env, jobject thiz)
{
    MtpDevice* const device = get_device_from_object(env, thiz);
    if (!device) {
        env->ThrowNew(clazz_io_exception, "");
        return -1;
    }
    return device->submitEventRequest();
}

static jobject android_mtp_MtpDevice_reap_event_request(JNIEnv *env, jobject thiz, jint seq)
{
    MtpDevice* const device = get_device_from_object(env, thiz);
    if (!device) {
        env->ThrowNew(clazz_io_exception, "");
        return NULL;
    }
    const int eventCode = device->reapEventRequest(seq);
    if (eventCode <= 0) {
        env->ThrowNew(clazz_operation_canceled_exception, "");
        return NULL;
    }
    jobject result = env->NewObject(clazz_event, constructor_event);
    env->SetIntField(result, field_event_eventCode, eventCode);
    return result;
}

static void android_mtp_MtpDevice_discard_event_request(JNIEnv *env, jobject thiz, jint seq)
{
    MtpDevice* const device = get_device_from_object(env, thiz);
    if (!device) {
        return;
    }
    device->discardEventRequest(seq);
}

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

static const JNINativeMethod gMethods[] = {
@@ -513,7 +556,11 @@ static const JNINativeMethod gMethods[] = {
    {"native_import_file",      "(II)Z",(void *)android_mtp_MtpDevice_import_file_to_fd},
    {"native_send_object",      "(III)Z",(void *)android_mtp_MtpDevice_send_object},
    {"native_send_object_info", "(Landroid/mtp/MtpObjectInfo;)Landroid/mtp/MtpObjectInfo;",
                                        (void *)android_mtp_MtpDevice_send_object_info}
                                        (void *)android_mtp_MtpDevice_send_object_info},
    {"native_submit_event_request",  "()I", (void *)android_mtp_MtpDevice_submit_event_request},
    {"native_reap_event_request",   "(I)Landroid/mtp/MtpEvent;",
                                            (void *)android_mtp_MtpDevice_reap_event_request},
    {"native_discard_event_request", "(I)V", (void *)android_mtp_MtpDevice_discard_event_request},
};

int register_android_mtp_MtpDevice(JNIEnv *env)
@@ -703,6 +750,23 @@ int register_android_mtp_MtpDevice(JNIEnv *env)
    }
    clazz_objectInfo = (jclass)env->NewGlobalRef(clazz);

    clazz = env->FindClass("android/mtp/MtpEvent");
    if (clazz == NULL) {
        ALOGE("Can't find android/mtp/MtpEvent");
        return -1;
    }
    constructor_event = env->GetMethodID(clazz, "<init>", "()V");
    if (constructor_event == NULL) {
        ALOGE("Can't find android/mtp/MtpEvent constructor");
        return -1;
    }
    field_event_eventCode = env->GetFieldID(clazz, "mEventCode", "I");
    if (field_event_eventCode == NULL) {
        ALOGE("Can't find MtpObjectInfo.mEventCode");
        return -1;
    }
    clazz_event = (jclass)env->NewGlobalRef(clazz);

    clazz = env->FindClass("android/mtp/MtpDevice");
    if (clazz == NULL) {
        ALOGE("Can't find android/mtp/MtpDevice");
@@ -713,6 +777,18 @@ int register_android_mtp_MtpDevice(JNIEnv *env)
        ALOGE("Can't find MtpDevice.mNativeContext");
        return -1;
    }
    clazz = env->FindClass("java/io/IOException");
    if (clazz == NULL) {
        ALOGE("Can't find java.io.IOException");
        return -1;
    }
    clazz_io_exception = (jclass)env->NewGlobalRef(clazz);
    clazz = env->FindClass("android/os/OperationCanceledException");
    if (clazz == NULL) {
        ALOGE("Can't find android.os.OperationCanceledException");
        return -1;
    }
    clazz_operation_canceled_exception = (jclass)env->NewGlobalRef(clazz);

    return AndroidRuntime::registerNativeMethods(env,
                "android/mtp/MtpDevice", gMethods, NELEM(gMethods));
Loading