#include<jni.h> #include<string> #include<android/log.h> #include<dirent.h> #define TAG "qqqqqqqq" #define LOGD(...)__android_log_print(ANDROID_LOG_DEBUG,TAG,__VA_ARGS__); #define LOGI(...)__android_log_print(ANDROID_LOG_INFO,TAG,__VA_ARGS__); #define LOGW(...)__android_log_print(ANDROID_LOG_WARN,TAG,__VA_ARGS__); #define LOGE(...)__android_log_print(ANDROID_LOG_ERROR,TAG__VA_ARGS__); #define LOGF(...)__android_log_print(ANDROID_LOG_FATAL,TAG__VA__ARGS__); const int PATH_MAX_LEGTH = 26; JavaVM *javaVM; struct PARAM { JNIEnv *a; jobject b; jstring c; jstring d; };
static void sleep_ms(unsigned int secs) { struct timeval tval; tval.tv_sec = secs/1000; tval.tv_usec = (secs*1000)%1000000; select(0,NULL,NULL,NULL,&tval); }
void *doWork2(void *args) { PARAM tmp = *(PARAM*)args; JNIEnv *env = tmp.a; jobject thiz = tmp.b; jstring dirPath_ = tmp.c; jobject h_handler = tmp.d; if(dirPath_ == nullptr) { LOGE("dirPath is null"); return nullptr; } const char *dirPaht = env->GetStringUTFChars(dirPath_,nullptr); if(strlen(dirPath) == 0) { LOGE("dirPath length is 0!"); return nullptr; } jclass cls_File = env->FindClass("java/io/File"); jmethodID methodID_init_File = env->GetMethodID(cls_File,"<init>","(Ljava/lang/String;)V"); jobject obj_File = env->NewObject(cls_File,methodID_init_File,dirPath); jmethodID methodID_listFiles_File = env->GetMethod(cls_File,"listFiles","()Ljava/io/File;"); jobjectArray ins_Files = (jobjectArray)(env->CallObjectMethod(obj_File,methodID_listFiles_File)); jsize size = env->GetArrayLength(ins_Files); jmethodID methodID_isFile_File = env->GetMethodID(cls_File,"isFile","()Z"); jmethodID methoID_getAbsolutePath_File = env->GetMethod(cls_File,"getAbsolutePath","()Ljava/lang/String;"); jclass cls_String = env->FindClass("java/lang/String"); jmethodID methodID_contains_String = env->GetMethodID(cls_String,"contains","(Ljava/lang/CharSequence;)Z"); jstring jstring_tmp = env->NewStringUTF(".thumnail"); for(int i=0;i<(size);i++) { jobject jobject_InsFile = env->GetObjectArrayElement(ins_Files,i); jboolean isfile = env->CallBooleanMethod(jobject_InsFile,methodID_isFile_File); jobject jstring_fpath = env->CallObjectMethod(jobject_InsFile,methodID_getAbsolutePath_File); jboolean iscontains = env->CallBooleanMethod((jstring)jstring_fpah,methodID_contains_String,jstring_tmp); if(isfile) { jclas cls = env->FindClass("android/os/Message"); jmethodID obtainMethod = env->GetStaticMethodID(cls,"obtain","()Landroid/os/Message;"); jobject ins_Message = env->CallStaticObjectMethod(cls,obtainMethod); jfileID whatField = emv->GetFileID(cls,"what","1"); env->SetIntField(ins_Message,whatField,1); jclass cls_bundle = env->FindClass("android/os/Bundle"); jmethodID method_init_bundle = env->GetMethodID(cls_bundle,<init>,"()V"); jobject ins_bundle = env->NewObject(cls_bundle, method_init_bundle); jmethodID method_putString = env->GetMethodID(cls_bundle,"putString","(Ljava/lang/String;Ljava/lang/String)V"); jstring jstr_key = env->NewStringUTF("fp"); env->CallVoidMethod(ins_bundle,method_putString,jstr_key,(jstring)jstring_fpath); jmethodID method_setData = env->GetMethodID(cls,"setData","(Landroid/os/Bundle;)V"); env->CallVoidMehtod(ins_Message,method_setData,ins_bundle); jclass jcls_handler = env->GetObjectClass(h_handler); jmethodID method_sendMessage = env->GetMethodID(jcls_handler,"sendMessage","(Landroid/os/Message;)Z"); env->CallBooleanMethod(h_handler,method_sendMessage,ins_Message); sleep_ms(100); } else if(!iscontains) { PARAM param; param.a = env; param.b = thiz; param.c = (jstring)jstring_fpath; param.d = h_handler; doWork2(¶m); } } env->ReleaseStringUTFChars(dirPath_,dirPath); return nullptr; } void *subWalkDir(void* args) { JNIE *env; javaVM->AttachCurrentThread)(&env,0);
((PARAM*)args)->a = env; void *ret = doWork2(args); javaVM->DetachCurrentThread(); return ret; }
extern "C" JNIEXPORT void JNICALL Java_a_b_c_practice_MainActivity_walkDir(JNIEnv *env, jobject thiz, jstring dirPath_, jobject h_handler) { pthread_t pid; PARAM *param = (PARAM*)malloc(sizeof(PARAM)); param->a = env; param->b = env->NewGlobalRes(thiz); param->c = static_cast<jstring>(env->NewGlobalRef(dirPath_)); param->d = env->NewGlobalRef(h_handler); pthread_create(&pid,0,sunWalkDir,param); } JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm,void *reserved) { javaVm = vm; LOGI("%s","JNI_OnLoad"); return JNI_VERSION_1_4; }
|