Browse Source

enhance(android): use view intent for asset-links

Close #10976
Andelf 1 year ago
parent
commit
37241cc368

+ 18 - 0
android/app/src/main/java/com/logseq/app/FolderPicker.java

@@ -11,6 +11,7 @@ import android.provider.Settings;
 import android.util.Log;
 
 import androidx.activity.result.ActivityResult;
+import androidx.core.content.FileProvider;
 import androidx.documentfile.provider.DocumentFile;
 
 import com.getcapacitor.JSObject;
@@ -49,6 +50,23 @@ public class FolderPicker extends Plugin {
         }
     }
 
+    @PluginMethod()
+    public void openFile(PluginCall call) {
+        Uri uri = Uri.parse(call.getString("uri"));
+        File file = new File(uri.getPath());
+
+        // Get URI and MIME type of file
+        String appId = getAppId();
+        uri = FileProvider.getUriForFile(getActivity(), appId + ".fileprovider", file);
+        String mime = getContext().getContentResolver().getType(uri);
+
+        Intent intent = new Intent();
+        intent.setAction(Intent.ACTION_VIEW);
+        intent.setDataAndType(uri, mime);
+        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
+        getContext().startActivity(intent);
+    }
+
     @ActivityCallback
     private void folderPickerResult(PluginCall call, ActivityResult result) {
         if (call == null) {

+ 2 - 3
src/main/frontend/components/block.cljs

@@ -2,7 +2,6 @@
   (:refer-clojure :exclude [range])
   (:require-macros [hiccups.core])
   (:require ["/frontend/utils" :as utils]
-            ["@capacitor/share" :refer [^js Share]]
             [cljs-bean.core :as bean]
             [cljs.core.match :refer [match]]
             [cljs.reader :as reader]
@@ -48,6 +47,7 @@
             [frontend.handler.whiteboard :as whiteboard-handler]
             [frontend.handler.export.common :as export-common-handler]
             [frontend.mobile.util :as mobile-util]
+            [frontend.mobile.intent :as mobile-intent]
             [frontend.modules.outliner.tree :as tree]
             [frontend.security :as security]
             [frontend.shui :refer [get-shui-component-version make-shui-context]]
@@ -395,8 +395,7 @@
                          (let [[rel-dir basename] (util/get-dir-and-basename href)
                                rel-dir (string/replace rel-dir #"^/+" "")
                                asset-url (path/path-join repo-dir rel-dir basename)]
-                           (.share Share (clj->js {:url asset-url
-                                                   :title "Open file with your favorite app"})))))]
+                           (mobile-intent/share-file asset-url))))]
 
         (cond
           (contains? config/audio-formats ext)

+ 17 - 0
src/main/frontend/mobile/intent.cljs

@@ -1,5 +1,6 @@
 (ns frontend.mobile.intent
   (:require ["@capacitor/filesystem" :refer [Filesystem]]
+            ["@capacitor/share" :refer [^js Share]]
             ["path" :as node-path]
             ["send-intent" :refer [^js SendIntent]]
             [clojure.pprint :as pprint]
@@ -22,6 +23,22 @@
             [logseq.graph-parser.util.page-ref :as page-ref]
             [promesa.core :as p]))
 
+(defn share-file
+  "Share file to mobile platform"
+  [uri]
+  (cond
+    ;; using ACTION_VIEW to open file with system default app
+    (mobile-util/native-android?)
+    (.openFile mobile-util/folder-picker (clj->js {:uri uri}))
+
+    (mobile-util/native-ios?)
+    (.share Share (clj->js {:url uri
+                            :dialogTitle "Open file with your favorite app"
+                            :title "Open file with your favorite app"}))
+
+    :else
+    (js/window.open uri "_blank")))
+
 (defn- is-link
   [url]
   (when (not-empty url)