Skip to content

Commit

Permalink
fix(android): handle exteral storage
Browse files Browse the repository at this point in the history
  • Loading branch information
andelf committed Aug 11, 2022
1 parent a887810 commit 3e78139
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
25 changes: 23 additions & 2 deletions android/app/src/main/java/com/logseq/app/FileUtil.java
Expand Up @@ -10,6 +10,9 @@
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.util.Log;

import java.io.File;

// https://stackoverflow.com/questions/29713587/how-to-get-the-real-path-with-action-open-document-tree-intent
// https://gist.github.com/asifmujteba/d89ba9074bc941de1eaa#file-asfurihelper
Expand All @@ -31,7 +34,26 @@ public static String getPath(final Context context, final Uri uri) {
return Environment.getExternalStorageDirectory() + "/" + split[1];
}

// TODO handle non-primary volumes
// NOTE: It's not a good idea to use storage root as Graph root.
String relPath = "";
if (split.length == 2) {
relPath = split[1];
}

String storageState = Environment.getExternalStorageState();
if (storageState.equals(Environment.MEDIA_MOUNTED)) {
// attempt 1
File dir = new File("/storage/" + type + "/" + relPath);
if (dir.exists()) {
return dir.getPath();
}
// attempt 2
dir = new File("/mnt/" + type + "/" + relPath);
if (dir.exists()) {
return dir.getPath();
}
}
// TODO: other cases
} else if (isDownloadsDocument(uri)) {
final String id = DocumentsContract.getDocumentId(uri);
if (!TextUtils.isEmpty(id)) {
Expand Down Expand Up @@ -67,7 +89,6 @@ public static String getPath(final Context context, final Uri uri) {
return getDataColumn(context, contentUri, selection, selectionArgs);
}
} else if ("content".equalsIgnoreCase(uri.getScheme())) {

// Return the remote address
if (isGooglePhotosUri(uri))
return uri.getLastPathSegment();
Expand Down
10 changes: 8 additions & 2 deletions android/app/src/main/java/com/logseq/app/FolderPicker.java
Expand Up @@ -58,7 +58,13 @@ private void folderPickerResult(PluginCall call, ActivityResult result) {
Uri docUri = DocumentsContract.buildDocumentUriUsingTree(treeUri,
DocumentsContract.getTreeDocumentId(treeUri));
Log.i("Logseq/FolderPicker", "Got uri " + docUri);
ret.put("path", FileUtil.getPath(context, docUri));
call.resolve(ret);
String path = FileUtil.getPath(context, docUri);
Log.i("Logseq/FolderPicker", "Convert to path " + FileUtil.getPath(context, docUri));
if (path == null || path.isEmpty()) {
call.reject("Cannot support this directory type: " + docUri);
} else {
ret.put("path", path);
call.resolve(ret);
}
}
}
7 changes: 5 additions & 2 deletions src/main/frontend/fs/capacitor_fs.cljs
Expand Up @@ -308,9 +308,12 @@
(open-dir [_this _ok-handler]
(p/let [_ (when (= (mobile-util/platform) "android") (check-permission-android))
{:keys [path localDocumentsPath]} (p/chain
(.pickFolder mobile-util/folder-picker)
(p/catch (.pickFolder mobile-util/folder-picker)
(fn [e]
(js/alert (str e))
nil)) ;; NOTE: Can not pick folder, let it crash
#(js->clj % :keywordize-keys true))
_ (when (and (mobile-util/native-ios?)
_ (when (and (mobile-util/native-ios?)
(not (or (local-container-path? path localDocumentsPath)
(mobile-util/iCloud-container-path? path))))
(state/pub-event! [:modal/show-instruction]))
Expand Down

0 comments on commit 3e78139

Please sign in to comment.