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

Commit 0b494663 authored by Daichi Hirono's avatar Daichi Hirono
Browse files

Add readEvent method to MtpDevice.

BUG=23368533

Change-Id: Ibefff559fa7dd0bee17e2812bd7cdd129108d804
parent 52b290f7
Loading
Loading
Loading
Loading
+6 −0
Original line number Original line Diff line number Diff line
@@ -18122,6 +18122,7 @@ package android.mtp {
    method public boolean importFile(int, java.lang.String);
    method public boolean importFile(int, java.lang.String);
    method public boolean importFile(int, android.os.ParcelFileDescriptor);
    method public boolean importFile(int, android.os.ParcelFileDescriptor);
    method public boolean open(android.hardware.usb.UsbDeviceConnection);
    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 boolean sendObject(int, int, android.os.ParcelFileDescriptor);
    method public android.mtp.MtpObjectInfo sendObjectInfo(android.mtp.MtpObjectInfo);
    method public android.mtp.MtpObjectInfo sendObjectInfo(android.mtp.MtpObjectInfo);
  }
  }
@@ -18133,6 +18134,11 @@ package android.mtp {
    method public final java.lang.String getVersion();
    method public final java.lang.String getVersion();
  }
  }
  public class MtpEvent {
    ctor public MtpEvent();
    method public int getEventCode();
  }
  public final class MtpObjectInfo {
  public final class MtpObjectInfo {
    method public final int getAssociationDesc();
    method public final int getAssociationDesc();
    method public final int getAssociationType();
    method public final int getAssociationType();
+6 −0
Original line number Original line Diff line number Diff line
@@ -19634,6 +19634,7 @@ package android.mtp {
    method public boolean importFile(int, java.lang.String);
    method public boolean importFile(int, java.lang.String);
    method public boolean importFile(int, android.os.ParcelFileDescriptor);
    method public boolean importFile(int, android.os.ParcelFileDescriptor);
    method public boolean open(android.hardware.usb.UsbDeviceConnection);
    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 boolean sendObject(int, int, android.os.ParcelFileDescriptor);
    method public android.mtp.MtpObjectInfo sendObjectInfo(android.mtp.MtpObjectInfo);
    method public android.mtp.MtpObjectInfo sendObjectInfo(android.mtp.MtpObjectInfo);
  }
  }
@@ -19645,6 +19646,11 @@ package android.mtp {
    method public final java.lang.String getVersion();
    method public final java.lang.String getVersion();
  }
  }
  public class MtpEvent {
    ctor public MtpEvent();
    method public int getEventCode();
  }
  public final class MtpObjectInfo {
  public final class MtpObjectInfo {
    method public final int getAssociationDesc();
    method public final int getAssociationDesc();
    method public final int getAssociationType();
    method public final int getAssociationType();
+38 −1
Original line number Original line Diff line number Diff line
@@ -18,6 +18,8 @@ package android.mtp;


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


/**
/**
@@ -278,6 +280,38 @@ public final class MtpDevice {
        return native_send_object_info(info);
        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
    // used by the JNI code
    private long mNativeContext;
    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_import_file(int objectHandle, int fd);
    private native boolean native_send_object(int objectHandle, int size, 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 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 Original line 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 Original line Diff line number Diff line
@@ -46,10 +46,14 @@ static jfieldID field_context;
jclass clazz_deviceInfo;
jclass clazz_deviceInfo;
jclass clazz_storageInfo;
jclass clazz_storageInfo;
jclass clazz_objectInfo;
jclass clazz_objectInfo;
jclass clazz_event;
jclass clazz_io_exception;
jclass clazz_operation_canceled_exception;


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


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


// MtpEvent fields
static jfieldID field_event_eventCode;

MtpDevice* get_device_from_object(JNIEnv* env, jobject javaDevice)
MtpDevice* get_device_from_object(JNIEnv* env, jobject javaDevice)
{
{
    return (MtpDevice*)env->GetLongField(javaDevice, field_context);
    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;
    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 JNINativeMethod gMethods[] = {
static JNINativeMethod gMethods[] = {
@@ -513,7 +556,11 @@ static JNINativeMethod gMethods[] = {
    {"native_import_file",      "(II)Z",(void *)android_mtp_MtpDevice_import_file_to_fd},
    {"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",      "(III)Z",(void *)android_mtp_MtpDevice_send_object},
    {"native_send_object_info", "(Landroid/mtp/MtpObjectInfo;)Landroid/mtp/MtpObjectInfo;",
    {"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)
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_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");
    clazz = env->FindClass("android/mtp/MtpDevice");
    if (clazz == NULL) {
    if (clazz == NULL) {
        ALOGE("Can't find android/mtp/MtpDevice");
        ALOGE("Can't find android/mtp/MtpDevice");
@@ -713,6 +777,18 @@ int register_android_mtp_MtpDevice(JNIEnv *env)
        ALOGE("Can't find MtpDevice.mNativeContext");
        ALOGE("Can't find MtpDevice.mNativeContext");
        return -1;
        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,
    return AndroidRuntime::registerNativeMethods(env,
                "android/mtp/MtpDevice", gMethods, NELEM(gMethods));
                "android/mtp/MtpDevice", gMethods, NELEM(gMethods));
Loading