浏览代码

feat(sync): capacitor glue code

Tienson Qin 3 年之前
父节点
当前提交
00c9a5b299

+ 1 - 0
android/app/build.gradle

@@ -38,6 +38,7 @@ dependencies {
     androidTestImplementation "androidx.test.ext:junit:$androidxJunitVersion"
     androidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion"
     implementation project(':capacitor-cordova-android-plugins')
+    implementation project(':file-sync')
 }
 
 apply from: 'capacitor.build.gradle'

+ 57 - 0
android/app/src/main/java/com/logseq/app/GraphFileSync.java

@@ -0,0 +1,57 @@
+package com.logseq.app;
+
+import com.getcapacitor.JSObject;
+import com.getcapacitor.Plugin;
+import com.getcapacitor.annotation.CapacitorPlugin;
+import com.getcapacitor.PluginCall;
+import com.getcapacitor.PluginMethod;
+import com.logseq.file_sync.FileSync;
+
+import java.util.List;
+
+@CapacitorPlugin(name = "GraphFileSync")
+public class GraphFileSync extends Plugin {
+
+    //@PluginMethod(returnType = PluginMethod.RETURN_CALLBACK)
+    @PluginMethod()
+    public void watch(PluginCall call) {
+        String path = call.getString("path");
+        android.util.Log.i("FileSync", "path = " + path);
+
+        FileSync.ping();
+        String watched = FileSync.watch(this, path);
+        android.util.Log.i("FileSync", "started");
+        JSObject ret = new JSObject();
+        ret.put("path", watched);
+
+        // call.setKeepAlive(true);
+        call.resolve(ret);
+    }
+
+    public void notifyChange(String event, List<String> paths) {
+        android.util.Log.i("FileSync", "Event:" + event + " path: " + paths);
+        for (String p : paths) {
+            android.util.Log.i("FileSync", "Got path:" + p);
+        }
+        JSObject ret = new JSObject();
+        ret.put("event", event);
+        ret.put("path", paths);
+        notifyListeners(event, ret);
+    }
+
+    @PluginMethod()
+    public void close(PluginCall call) {
+        FileSync.close();
+        JSObject ret = new JSObject();
+        ret.put("value", "closed watcher");
+        call.resolve(ret);
+    }
+
+    @PluginMethod()
+    public void ping(PluginCall call) {
+        String res = FileSync.ping();
+        JSObject ret = new JSObject();
+        ret.put("value", res);
+        call.resolve(ret);
+    }
+}

+ 1 - 0
android/file-sync/.gitignore

@@ -0,0 +1 @@
+/build

+ 36 - 0
android/file-sync/build.gradle

@@ -0,0 +1,36 @@
+plugins {
+    id 'com.android.library'
+}
+
+android {
+    compileSdkVersion 30
+
+    defaultConfig {
+        minSdkVersion 21
+        targetSdkVersion 30
+        versionCode 1
+        versionName "1.0"
+
+        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
+        consumerProguardFiles "consumer-rules.pro"
+    }
+
+    buildTypes {
+        release {
+            minifyEnabled false
+            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
+        }
+    }
+    compileOptions {
+        sourceCompatibility JavaVersion.VERSION_1_8
+        targetCompatibility JavaVersion.VERSION_1_8
+    }
+}
+
+dependencies {
+
+    implementation 'com.android.support:appcompat-v7:28.0.0'
+    testImplementation 'junit:junit:4.+'
+    androidTestImplementation 'com.android.support.test:runner:1.0.2'
+    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
+}

+ 0 - 0
android/file-sync/consumer-rules.pro


+ 21 - 0
android/file-sync/proguard-rules.pro

@@ -0,0 +1,21 @@
+# Add project specific ProGuard rules here.
+# You can control the set of applied configuration files using the
+# proguardFiles setting in build.gradle.
+#
+# For more details, see
+#   http://developer.android.com/guide/developing/tools/proguard.html
+
+# If your project uses WebView with JS, uncomment the following
+# and specify the fully qualified class name to the JavaScript interface
+# class:
+#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
+#   public *;
+#}
+
+# Uncomment this to preserve the line number information for
+# debugging stack traces.
+#-keepattributes SourceFile,LineNumberTable
+
+# If you keep the line number information, uncomment this to
+# hide the original source file name.
+#-renamesourcefileattribute SourceFile

+ 25 - 0
android/file-sync/src/androidTest/java/com/logseq/file_sync/ExampleInstrumentedTest.java

@@ -0,0 +1,25 @@
+package com.logseq.file_sync;
+
+import android.content.Context;
+import android.support.test.InstrumentationRegistry;
+import android.support.test.runner.AndroidJUnit4;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import static org.junit.Assert.*;
+
+/**
+ * Instrumented test, which will execute on an Android device.
+ *
+ * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
+ */
+@RunWith(AndroidJUnit4.class)
+public class ExampleInstrumentedTest {
+    @Test
+    public void useAppContext() {
+        // Context of the app under test.
+        Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
+        assertEquals("com.logseq.file_sync.test", appContext.getPackageName());
+    }
+}

+ 5 - 0
android/file-sync/src/main/AndroidManifest.xml

@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="utf-8"?>
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+    package="com.logseq.file_sync">
+
+</manifest>

+ 12 - 0
android/file-sync/src/main/java/com/logseq/file_sync/FileSync.java

@@ -0,0 +1,12 @@
+package com.logseq.file_sync;
+
+public class FileSync {
+    static {
+        System.loadLibrary("filesync");
+    }
+
+    public static native String watch(final Object plugin, final String path);
+
+    public static native void close();
+    public static native String ping();
+}

二进制
android/file-sync/src/main/jniLibs/arm64-v8a/libfilesync.so


二进制
android/file-sync/src/main/jniLibs/armeabi-v7a/libfilesync.so


二进制
android/file-sync/src/main/jniLibs/x86/libfilesync.so


二进制
android/file-sync/src/main/jniLibs/x86_64/libfilesync.so


+ 17 - 0
android/file-sync/src/test/java/com/logseq/file_sync/ExampleUnitTest.java

@@ -0,0 +1,17 @@
+package com.logseq.file_sync;
+
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+/**
+ * Example local unit test, which will execute on the development machine (host).
+ *
+ * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
+ */
+public class ExampleUnitTest {
+    @Test
+    public void addition_isCorrect() {
+        assertEquals(4, 2 + 2);
+    }
+}

+ 2 - 1
android/settings.gradle

@@ -2,4 +2,5 @@ include ':app'
 include ':capacitor-cordova-android-plugins'
 project(':capacitor-cordova-android-plugins').projectDir = new File('./capacitor-cordova-android-plugins/')
 
-apply from: 'capacitor.settings.gradle'
+apply from: 'capacitor.settings.gradle'
+include ':file-sync'

+ 2 - 0
src/main/frontend/mobile/util.cljs

@@ -21,6 +21,8 @@
   (.convertFileSrc Capacitor path-str))
 
 (defonce folder-picker (registerPlugin "FolderPicker"))
+(when (native-android?)
+  (defonce file-sync (registerPlugin "GraphFileSync")))
 (when (native-ios?)
   (defonce download-icloud-files (registerPlugin "DownloadiCloudFiles"))
   (defonce ios-file-container (registerPlugin "FileContainer")))