Mike Lockwood | 98ef64e | 2010-06-29 16:42:13 -0400 | [diff] [blame^] | 1 | /* |
| 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 | |
| 33 | namespace android { |
| 34 | |
| 35 | // ---------------------------------------------------------------------------- |
| 36 | |
| 37 | static jfieldID field_context; |
| 38 | |
| 39 | |
| 40 | // ---------------------------------------------------------------------------- |
| 41 | |
| 42 | static bool ExceptionCheck(void* env) |
| 43 | { |
| 44 | return ((JNIEnv *)env)->ExceptionCheck(); |
| 45 | } |
| 46 | |
| 47 | class MtpThread : public Thread { |
| 48 | private: |
| 49 | String8 mStoragePath; |
| 50 | String8 mDatabasePath; |
| 51 | bool mDone; |
| 52 | bool mScannedOnce; |
| 53 | |
| 54 | public: |
| 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 | |
| 89 | static void |
| 90 | android_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 | |
| 104 | static void |
| 105 | android_media_MtpServer_finalize(JNIEnv *env, jobject thiz) |
| 106 | { |
| 107 | LOGD("finalize\n"); |
| 108 | } |
| 109 | |
| 110 | |
| 111 | static void |
| 112 | android_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 | |
| 119 | static void |
| 120 | android_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 | |
| 132 | static 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 | |
| 139 | static const char* const kClassPathName = "android/media/MtpServer"; |
| 140 | |
| 141 | int 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 |