blob: 9f961a2b238e16acbc7d41f95f2ae84134b267a3 [file] [log] [blame]
Mike Lockwood98ef64e2010-06-29 16:42:13 -04001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "MtpServerJNI"
18#include "utils/Log.h"
19
20#include <stdio.h>
21#include <assert.h>
22#include <limits.h>
23#include <unistd.h>
24#include <fcntl.h>
25#include <utils/threads.h>
26
27#include "jni.h"
28#include "JNIHelp.h"
29#include "android_runtime/AndroidRuntime.h"
30
31#include "MtpServer.h"
32
33namespace android {
34
35// ----------------------------------------------------------------------------
36
37static jfieldID field_context;
38
39
40// ----------------------------------------------------------------------------
41
42static bool ExceptionCheck(void* env)
43{
44 return ((JNIEnv *)env)->ExceptionCheck();
45}
46
47class MtpThread : public Thread {
48private:
49 String8 mStoragePath;
50 String8 mDatabasePath;
51 bool mDone;
52 bool mScannedOnce;
53
54public:
55 MtpThread(const char* storagePath, const char* databasePath)
56 : mStoragePath(storagePath), mDatabasePath(databasePath), mDone(false), mScannedOnce(false)
57 {
58 }
59
60 virtual bool threadLoop() {
61 int fd = open("/dev/mtp_usb", O_RDWR);
62 printf("open returned %d\n", fd);
63 if (fd < 0) {
64 LOGE("could not open MTP driver\n");
65 return false;
66 }
67
68 MtpServer* server = new MtpServer(fd, mDatabasePath);
69 server->addStorage(mStoragePath);
70
71 // temporary
72 LOGD("MtpThread server->scanStorage");
73 server->scanStorage();
74 LOGD("MtpThread server->run");
75 server->run();
76 close(fd);
77 delete server;
78
79 bool done = mDone;
80 if (done)
81 delete this;
82 LOGD("threadLoop returning %s", (done ? "false" : "true"));
83 return !done;
84 }
85
86 void setDone() { mDone = true; }
87};
88
89static void
90android_media_MtpServer_setup(JNIEnv *env, jobject thiz, jstring storagePath, jstring databasePath)
91{
92 LOGD("setup\n");
93
94 const char *storagePathStr = env->GetStringUTFChars(storagePath, NULL);
95 const char *databasePathStr = env->GetStringUTFChars(databasePath, NULL);
96
97 MtpThread* thread = new MtpThread(storagePathStr, databasePathStr);
98 env->SetIntField(thiz, field_context, (int)thread);
99
100 env->ReleaseStringUTFChars(storagePath, storagePathStr);
101 env->ReleaseStringUTFChars(databasePath, databasePathStr);
102}
103
104static void
105android_media_MtpServer_finalize(JNIEnv *env, jobject thiz)
106{
107 LOGD("finalize\n");
108}
109
110
111static void
112android_media_MtpServer_start(JNIEnv *env, jobject thiz)
113{
114 LOGD("start\n");
115 MtpThread *thread = (MtpThread *)env->GetIntField(thiz, field_context);
116 thread->run("MtpThread");
117}
118
119static void
120android_media_MtpServer_stop(JNIEnv *env, jobject thiz)
121{
122 LOGD("stop\n");
123 MtpThread *thread = (MtpThread *)env->GetIntField(thiz, field_context);
124 if (thread) {
125 thread->setDone();
126 env->SetIntField(thiz, field_context, 0);
127 }
128}
129
130// ----------------------------------------------------------------------------
131
132static JNINativeMethod gMethods[] = {
133 {"native_setup", "(Ljava/lang/String;Ljava/lang/String;)V", (void *)android_media_MtpServer_setup},
134 {"native_finalize", "()V", (void *)android_media_MtpServer_finalize},
135 {"native_start", "()V", (void *)android_media_MtpServer_start},
136 {"native_stop", "()V", (void *)android_media_MtpServer_stop},
137};
138
139static const char* const kClassPathName = "android/media/MtpServer";
140
141int register_android_media_MtpServer(JNIEnv *env)
142{
143 jclass clazz;
144
145 LOGD("register_android_media_MtpServer\n");
146
147 clazz = env->FindClass("android/media/MtpServer");
148 if (clazz == NULL) {
149 LOGE("Can't find android/media/MtpServer");
150 return -1;
151 }
152 field_context = env->GetFieldID(clazz, "mNativeContext", "I");
153 if (field_context == NULL) {
154 LOGE("Can't find MtpServer.mNativeContext");
155 return -1;
156 }
157
158 return AndroidRuntime::registerNativeMethods(env,
159 "android/media/MtpServer", gMethods, NELEM(gMethods));
160}
161
162} // namespace android