Skip to content

Commit

Permalink
feat(sync): capacitor glue code
Browse files Browse the repository at this point in the history
  • Loading branch information
tiensonqin authored and RCmerci committed Apr 4, 2022
1 parent 56047e6 commit 00c9a5b
Show file tree
Hide file tree
Showing 16 changed files with 179 additions and 1 deletion.
1 change: 1 addition & 0 deletions android/app/build.gradle
Expand Up @@ -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'
Expand Down
57 changes: 57 additions & 0 deletions 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 change: 1 addition & 0 deletions android/file-sync/.gitignore
@@ -0,0 +1 @@
/build
36 changes: 36 additions & 0 deletions 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'
}
Empty file.
21 changes: 21 additions & 0 deletions 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
@@ -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 changes: 5 additions & 0 deletions 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 changes: 12 additions & 0 deletions 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();
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -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);
}
}
3 changes: 2 additions & 1 deletion android/settings.gradle
Expand Up @@ -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 changes: 2 additions & 0 deletions src/main/frontend/mobile/util.cljs
Expand Up @@ -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")))
Expand Down

0 comments on commit 00c9a5b

Please sign in to comment.