Skip to content

Commit 3414030

Browse files
committed
Implemented writeTextFile on Android.
1 parent 4dfbcb7 commit 3414030

File tree

14 files changed

+303
-10
lines changed

14 files changed

+303
-10
lines changed

plugins/dialog/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,6 @@ impl<R: Runtime> FileDialogBuilder<R> {
576576
pub fn blocking_save_file(self) -> Option<PathBuf> {
577577
blocking_fn!(self, save_file)
578578
}
579-
580579
}
581580

582581
// taken from deno source code: https://github.com/denoland/deno/blob/ffffa2f7c44bd26aec5ae1957e0534487d099f48/runtime/ops/fs.rs#L913

plugins/fs/android/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/build
2+
/.tauri
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
plugins {
2+
id("com.android.library")
3+
id("org.jetbrains.kotlin.android")
4+
}
5+
6+
android {
7+
namespace = "com.plugin.fs"
8+
compileSdk = 34
9+
10+
defaultConfig {
11+
minSdk = 21
12+
targetSdk = 34
13+
14+
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
15+
consumerProguardFiles("consumer-rules.pro")
16+
}
17+
18+
buildTypes {
19+
release {
20+
isMinifyEnabled = false
21+
proguardFiles(
22+
getDefaultProguardFile("proguard-android-optimize.txt"),
23+
"proguard-rules.pro"
24+
)
25+
}
26+
}
27+
compileOptions {
28+
sourceCompatibility = JavaVersion.VERSION_1_8
29+
targetCompatibility = JavaVersion.VERSION_1_8
30+
}
31+
kotlinOptions {
32+
jvmTarget = "1.8"
33+
}
34+
}
35+
36+
dependencies {
37+
38+
implementation("androidx.core:core-ktx:1.9.0")
39+
implementation("androidx.appcompat:appcompat:1.6.0")
40+
implementation("com.google.android.material:material:1.7.0")
41+
testImplementation("junit:junit:4.13.2")
42+
androidTestImplementation("androidx.test.ext:junit:1.1.5")
43+
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
44+
implementation(project(":tauri-android"))
45+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile

plugins/fs/android/settings.gradle

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
pluginManagement {
2+
repositories {
3+
mavenCentral()
4+
gradlePluginPortal()
5+
google()
6+
}
7+
resolutionStrategy {
8+
eachPlugin {
9+
switch (requested.id.id) {
10+
case "com.android.library":
11+
useVersion("8.0.2")
12+
break
13+
case "org.jetbrains.kotlin.android":
14+
useVersion("1.8.20")
15+
break
16+
}
17+
}
18+
}
19+
}
20+
21+
dependencyResolutionManagement {
22+
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
23+
repositories {
24+
mavenCentral()
25+
google()
26+
27+
}
28+
}
29+
30+
include ':tauri-android'
31+
project(':tauri-android').projectDir = new File('./.tauri/tauri-api')
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.plugin.fs
2+
3+
import androidx.test.platform.app.InstrumentationRegistry
4+
import androidx.test.ext.junit.runners.AndroidJUnit4
5+
6+
import org.junit.Test
7+
import org.junit.runner.RunWith
8+
9+
import org.junit.Assert.*
10+
11+
/**
12+
* Instrumented test, which will execute on an Android device.
13+
*
14+
* See [testing documentation](http://d.android.com/tools/testing).
15+
*/
16+
@RunWith(AndroidJUnit4::class)
17+
class ExampleInstrumentedTest {
18+
@Test
19+
fun useAppContext() {
20+
// Context of the app under test.
21+
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
22+
assertEquals("com.plugin.fs", appContext.packageName)
23+
}
24+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
3+
</manifest>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.plugin.fs
2+
3+
import android.app.Activity
4+
import android.net.Uri
5+
import android.util.Log
6+
import app.tauri.annotation.Command
7+
import app.tauri.annotation.InvokeArg
8+
import app.tauri.annotation.TauriPlugin
9+
import app.tauri.plugin.JSObject
10+
import app.tauri.plugin.Plugin
11+
import app.tauri.plugin.Invoke
12+
13+
@InvokeArg
14+
class WriteTextFileArgs {
15+
val uri: String = ""
16+
val content: String = ""
17+
}
18+
19+
@TauriPlugin
20+
class FsPlugin(private val activity: Activity): Plugin(activity) {
21+
@Command
22+
fun writeTextFile(invoke: Invoke) {
23+
val args = invoke.parseArgs(WriteTextFileArgs::class.java)
24+
val uri = Uri.parse(args.uri)
25+
val content = args.content
26+
27+
if(uri != null){
28+
activity.getContentResolver().openOutputStream(uri).use { ost ->
29+
if(ost != null && content != null){
30+
ost.write(content.toByteArray());
31+
}
32+
}
33+
}
34+
35+
val ret = JSObject()
36+
invoke.resolve(ret)
37+
}
38+
}
39+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.plugin.fs
2+
3+
import org.junit.Test
4+
5+
import org.junit.Assert.*
6+
7+
/**
8+
* Example local unit test, which will execute on the development machine (host).
9+
*
10+
* See [testing documentation](http://d.android.com/tools/testing).
11+
*/
12+
class ExampleUnitTest {
13+
@Test
14+
fun addition_isCorrect() {
15+
assertEquals(4, 2 + 2)
16+
}
17+
}

plugins/fs/build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,5 +190,6 @@ permissions = [
190190
tauri_plugin::Builder::new(COMMANDS)
191191
.global_api_script_path("./api-iife.js")
192192
.global_scope_schema(schemars::schema_for!(FsScopeEntry))
193+
.android_path("android")
193194
.build();
194195
}

0 commit comments

Comments
 (0)