*/}}
浏览代码

PWD and permission stuff

YimingWu 4 天之前
父节点
当前提交
45e1b100c6
共有 4 个文件被更改,包括 54 次插入2 次删除
  1. 7 1
      la_data.c
  2. 5 0
      la_interface.h
  3. 37 1
      la_kernel.c
  4. 5 0
      resources/la_operators.c

+ 7 - 1
la_data.c

@@ -2227,7 +2227,13 @@ void la_GetWorkingDirectoryInternal(){
 #ifdef LAGUI_ANDROID
     strSafeSet(&MAIN.WorkingDirectory, ""); return;
 #endif
-    char mbuf[2048]; getcwd(mbuf,2048);
+    char mbuf[2048];
+#ifdef LA_LINUX
+    readlink("/proc/self/exe", mbuf, 2048);
+#endif
+#ifdef _WIN32
+    GetModuleFileName(NULL, mbuf, 2048);
+#endif
     int len=strlen(mbuf);if(mbuf[len]!=LA_PATH_SEP){ mbuf[len]=LA_PATH_SEP; mbuf[len+1]=0; }
     strSafeSet(&MAIN.WorkingDirectory, mbuf);
 }

+ 5 - 0
la_interface.h

@@ -2686,6 +2686,11 @@ void laui_PropOperatorUiDefine(laUiList *uil, laPropPack *This, laPropPack *Unus
 
 int laCopyToClipboard(unsigned char * text);
 
+#ifdef LAGUI_ANDROID
+bool la_check_permission(const char* permission);
+void la_request_permission(const char* permission);
+#endif
+
 extern laUiType *_LA_UI_FIXED_GROUP;
 extern laUiType *_LA_UI_TAB;
 extern laUiType _LA_UI_CONDITION;      //NO PTR

+ 37 - 1
la_kernel.c

@@ -1523,7 +1523,13 @@ void laShutoff(int SavePrefereces){
 int laRestoreFactorySettings(){
     char path[1024];
 #ifdef LAGUI_ANDROID
-    sprintf(path,"%s","preferences.udf");
+    sprintf(path,"%s/%s",MAIN.InternalDataPath,"preferences.udf");
+    logPrintNew("%s", path);
+    if(!remove(path)){ return 1; }
+    sprintf(path,"%s/%s",MAIN.ExternalDataPath,"preferences.udf");
+    logPrintNew("%s", path);
+    if(!remove(path)){ return 1; }
+    return 0;
 #else
     sprintf(path,"%s%s",SSTR(MAIN.WorkingDirectory),"preferences.udf");
 #endif
@@ -8906,6 +8912,36 @@ void la_DisplayKeyboard(bool pShow) {
     (*lJavaVM)->DetachCurrentThread(lJavaVM);
 }
 
+bool la_check_permission(const char* permission) {
+    JavaVM* lJavaVM = MAIN.app->activity->vm;
+    JNIEnv* lJNIEnv = MAIN.app->activity->env;
+    (*lJavaVM)->AttachCurrentThread(lJavaVM, &lJNIEnv,0);
+    jobject lNativeActivity = MAIN.app->activity->clazz;
+    jclass ClassNativeActivity = (*lJNIEnv)->GetObjectClass(lJNIEnv,lNativeActivity);
+    jmethodID activity_checkSelfPermission = (*lJNIEnv)->GetMethodID(lJNIEnv, ClassNativeActivity, "checkSelfPermission", "(Ljava/lang/String;)I");
+    jstring jobj_permission = (*lJNIEnv)->NewStringUTF(lJNIEnv,permission);
+	jint    result          = (*lJNIEnv)->CallIntMethod(lJNIEnv,ClassNativeActivity, activity_checkSelfPermission, jobj_permission);
+	(*lJNIEnv)->DeleteLocalRef(lJNIEnv,jobj_permission);
+    (*lJavaVM)->DetachCurrentThread(lJavaVM);
+	return result == 0;
+}
+void la_request_permission(const char* permission) {
+    JavaVM* lJavaVM = MAIN.app->activity->vm;
+    JNIEnv* lJNIEnv = MAIN.app->activity->env;
+    (*lJavaVM)->AttachCurrentThread(lJavaVM, &lJNIEnv,0);
+    jobject lNativeActivity = MAIN.app->activity->clazz;
+    jclass ClassNativeActivity = (*lJNIEnv)->GetObjectClass(lJNIEnv,lNativeActivity);
+	jmethodID contextCompat_checkSelfPermission = (*lJNIEnv)->GetMethodID   (lJNIEnv, ClassNativeActivity, "checkSelfPermission", "(Ljava/lang/String;)I");
+	jmethodID activity_requestPermissions       = (*lJNIEnv)->GetMethodID   (lJNIEnv, ClassNativeActivity, "requestPermissions",  "([Ljava/lang/String;I)V");
+	jstring      jobj_permission      = (*lJNIEnv)->NewStringUTF  (lJNIEnv,permission);
+	jobjectArray jobj_permission_list = (*lJNIEnv)->NewObjectArray(lJNIEnv,1, (*lJNIEnv)->FindClass(lJNIEnv,"java/lang/String"), NULL);
+	(*lJNIEnv)->SetObjectArrayElement(lJNIEnv,jobj_permission_list, 0, jobj_permission);
+	(*lJNIEnv)->CallVoidMethod       (lJNIEnv,lNativeActivity, activity_requestPermissions, jobj_permission_list, 0);
+	(*lJNIEnv)->DeleteLocalRef(lJNIEnv,jobj_permission);
+	(*lJNIEnv)->DeleteLocalRef(lJNIEnv,jobj_permission_list);
+    (*lJavaVM)->DetachCurrentThread(lJavaVM);
+}
+
 #endif //android
 
 #ifndef LAGUI_ANDROID

+ 5 - 0
resources/la_operators.c

@@ -380,6 +380,11 @@ laFileBrowser *la_FileBrowserInit(laOperator *a){
     strcpy(bf->Path,fb->Path); strcpy(bf->Name,strGetLastSegment(fb->Path,"External")); lstAppendItem(&fb->Bookmarks,bf);
     bf=memAcquireSimple(sizeof(laBookmarkedFolder));
     strcpy(bf->Path,MAIN.InternalDataPath); strcpy(bf->Name,"Internal"); lstAppendItem(&fb->Bookmarks,bf);
+
+    if(!la_check_permission("android.permission.WRITE_EXTERNAL_STORAGE")){
+        la_request_permission("android.permission.WRITE_EXTERNAL_STORAGE");
+    }
+
 #else
     strcpy(fb->Path, SSTR(MAIN.WorkingDirectory));
 #endif