1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
|
/*
* Copyright (C) 2010 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.
*/
#define LOG_TAG "MtpClientJNI"
#include "utils/Log.h"
#include <stdio.h>
#include <assert.h>
#include <limits.h>
#include <unistd.h>
#include <fcntl.h>
#include "jni.h"
#include "JNIHelp.h"
#include "android_runtime/AndroidRuntime.h"
#include "MtpClient.h"
#include "MtpDevice.h"
#include "MtpObjectInfo.h"
using namespace android;
// ----------------------------------------------------------------------------
static jmethodID method_deviceAdded;
static jmethodID method_deviceRemoved;
static jfieldID field_context;
static struct file_descriptor_offsets_t
{
jclass mClass;
jmethodID mConstructor;
jfieldID mDescriptor;
} gFileDescriptorOffsets;
static struct parcel_file_descriptor_offsets_t
{
jclass mClass;
jmethodID mConstructor;
} gParcelFileDescriptorOffsets;
#ifdef HAVE_ANDROID_OS
static void checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName) {
if (env->ExceptionCheck()) {
LOGE("An exception was thrown by callback '%s'.", methodName);
LOGE_EX(env);
env->ExceptionClear();
}
}
class MyClient : public MtpClient {
private:
virtual void deviceAdded(MtpDevice *device);
virtual void deviceRemoved(MtpDevice *device);
jobject mClient;
MtpDevice* mEventDevice;
public:
MyClient(JNIEnv *env, jobject client);
void cleanup(JNIEnv *env);
};
MtpClient* get_client_from_object(JNIEnv* env, jobject javaClient)
{
return (MtpClient*)env->GetIntField(javaClient, field_context);
}
MyClient::MyClient(JNIEnv *env, jobject client)
: mClient(env->NewGlobalRef(client))
{
}
void MyClient::cleanup(JNIEnv *env) {
env->DeleteGlobalRef(mClient);
}
void MyClient::deviceAdded(MtpDevice *device) {
JNIEnv* env = AndroidRuntime::getJNIEnv();
const char* name = device->getDeviceName();
LOGD("MyClient::deviceAdded %s\n", name);
env->CallVoidMethod(mClient, method_deviceAdded, device->getID());
checkAndClearExceptionFromCallback(env, __FUNCTION__);
}
void MyClient::deviceRemoved(MtpDevice *device) {
JNIEnv* env = AndroidRuntime::getJNIEnv();
const char* name = device->getDeviceName();
LOGD("MyClient::deviceRemoved %s\n", name);
env->CallVoidMethod(mClient, method_deviceRemoved, device->getID());
checkAndClearExceptionFromCallback(env, __FUNCTION__);
}
#endif // HAVE_ANDROID_OS
// ----------------------------------------------------------------------------
static void
android_media_MtpClient_setup(JNIEnv *env, jobject thiz)
{
#ifdef HAVE_ANDROID_OS
LOGD("setup\n");
MyClient* client = new MyClient(env, thiz);
client->start();
env->SetIntField(thiz, field_context, (int)client);
#endif
}
static void
android_media_MtpClient_finalize(JNIEnv *env, jobject thiz)
{
#ifdef HAVE_ANDROID_OS
LOGD("finalize\n");
MyClient *client = (MyClient *)env->GetIntField(thiz, field_context);
client->cleanup(env);
delete client;
env->SetIntField(thiz, field_context, 0);
#endif
}
static jboolean
android_media_MtpClient_start(JNIEnv *env, jobject thiz)
{
#ifdef HAVE_ANDROID_OS
LOGD("start\n");
MyClient *client = (MyClient *)env->GetIntField(thiz, field_context);
return client->start();
#else
return false;
#endif
}
static void
android_media_MtpClient_stop(JNIEnv *env, jobject thiz)
{
#ifdef HAVE_ANDROID_OS
LOGD("stop\n");
MyClient *client = (MyClient *)env->GetIntField(thiz, field_context);
client->stop();
#endif
}
static jboolean
android_media_MtpClient_delete_object(JNIEnv *env, jobject thiz,
jint device_id, jint object_id)
{
#ifdef HAVE_ANDROID_OS
MyClient *client = (MyClient *)env->GetIntField(thiz, field_context);
MtpDevice* device = client->getDevice(device_id);
if (device)
return device->deleteObject(object_id);
else
#endif
return NULL;
}
static jint
android_media_MtpClient_get_parent(JNIEnv *env, jobject thiz,
jint device_id, jint object_id)
{
#ifdef HAVE_ANDROID_OS
MyClient *client = (MyClient *)env->GetIntField(thiz, field_context);
MtpDevice* device = client->getDevice(device_id);
if (device)
return device->getParent(object_id);
else
#endif
return -1;
}
static jint
android_media_MtpClient_get_storage_id(JNIEnv *env, jobject thiz,
jint device_id, jint object_id)
{
#ifdef HAVE_ANDROID_OS
MyClient *client = (MyClient *)env->GetIntField(thiz, field_context);
MtpDevice* device = client->getDevice(device_id);
if (device)
return device->getStorageID(object_id);
else
#endif
return -1;
}
static jobject
android_media_MtpClient_open_file(JNIEnv *env, jobject thiz,
jint device_id, jint object_id)
{
#ifdef HAVE_ANDROID_OS
MyClient *client = (MyClient *)env->GetIntField(thiz, field_context);
MtpDevice* device = client->getDevice(device_id);
if (!device)
return NULL;
MtpObjectInfo* info = device->getObjectInfo(object_id);
if (!info)
return NULL;
int object_size = info->mCompressedSize;
delete info;
int fd = device->readObject(object_id, object_size);
if (fd < 0)
return NULL;
jobject fileDescriptor = env->NewObject(gFileDescriptorOffsets.mClass,
gFileDescriptorOffsets.mConstructor);
if (fileDescriptor != NULL) {
env->SetIntField(fileDescriptor, gFileDescriptorOffsets.mDescriptor, fd);
} else {
return NULL;
}
return env->NewObject(gParcelFileDescriptorOffsets.mClass,
gParcelFileDescriptorOffsets.mConstructor, fileDescriptor);
#endif
return NULL;
}
// ----------------------------------------------------------------------------
static JNINativeMethod gMethods[] = {
{"native_setup", "()V", (void *)android_media_MtpClient_setup},
{"native_finalize", "()V", (void *)android_media_MtpClient_finalize},
{"native_start", "()Z", (void *)android_media_MtpClient_start},
{"native_stop", "()V", (void *)android_media_MtpClient_stop},
{"native_delete_object", "(II)Z", (void *)android_media_MtpClient_delete_object},
{"native_get_parent", "(II)I", (void *)android_media_MtpClient_get_parent},
{"native_get_storage_id", "(II)I", (void *)android_media_MtpClient_get_storage_id},
{"native_open_file", "(II)Landroid/os/ParcelFileDescriptor;",
(void *)android_media_MtpClient_open_file},
};
static const char* const kClassPathName = "android/media/MtpClient";
int register_android_media_MtpClient(JNIEnv *env)
{
jclass clazz;
LOGD("register_android_media_MtpClient\n");
clazz = env->FindClass("android/media/MtpClient");
if (clazz == NULL) {
LOGE("Can't find android/media/MtpClient");
return -1;
}
method_deviceAdded = env->GetMethodID(clazz, "deviceAdded", "(I)V");
if (method_deviceAdded == NULL) {
LOGE("Can't find deviceAdded");
return -1;
}
method_deviceRemoved = env->GetMethodID(clazz, "deviceRemoved", "(I)V");
if (method_deviceRemoved == NULL) {
LOGE("Can't find deviceRemoved");
return -1;
}
field_context = env->GetFieldID(clazz, "mNativeContext", "I");
if (field_context == NULL) {
LOGE("Can't find MtpClient.mNativeContext");
return -1;
}
clazz = env->FindClass("java/io/FileDescriptor");
LOG_FATAL_IF(clazz == NULL, "Unable to find class java.io.FileDescriptor");
gFileDescriptorOffsets.mClass = (jclass) env->NewGlobalRef(clazz);
gFileDescriptorOffsets.mConstructor = env->GetMethodID(clazz, "<init>", "()V");
gFileDescriptorOffsets.mDescriptor = env->GetFieldID(clazz, "descriptor", "I");
LOG_FATAL_IF(gFileDescriptorOffsets.mDescriptor == NULL,
"Unable to find descriptor field in java.io.FileDescriptor");
clazz = env->FindClass("android/os/ParcelFileDescriptor");
LOG_FATAL_IF(clazz == NULL, "Unable to find class android.os.ParcelFileDescriptor");
gParcelFileDescriptorOffsets.mClass = (jclass) env->NewGlobalRef(clazz);
gParcelFileDescriptorOffsets.mConstructor = env->GetMethodID(clazz, "<init>", "(Ljava/io/FileDescriptor;)V");
LOG_FATAL_IF(gParcelFileDescriptorOffsets.mConstructor == NULL,
"Unable to find constructor for android.os.ParcelFileDescriptor");
return AndroidRuntime::registerNativeMethods(env,
"android/media/MtpClient", gMethods, NELEM(gMethods));
}
|