Skip to content

Commit 341a532

Browse files
authored
fix(clipboard): Android warnings and build on SDK under 28 (#1771)
1 parent cc03ccf commit 341a532

File tree

4 files changed

+48
-31
lines changed

4 files changed

+48
-31
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"clipboard-manager": patch
3+
"clipboard-manager-js": patch
4+
---
5+
6+
Fix warnings and clear implementation on Android below SDK 28.
Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,59 @@
11
<script>
2-
import * as clipboard from "@tauri-apps/plugin-clipboard-manager";
3-
import { open } from "@tauri-apps/plugin-dialog";
4-
import { arrayBufferToBase64 } from "../lib/utils";
5-
import { readFile } from "@tauri-apps/plugin-fs";
2+
import * as clipboard from '@tauri-apps/plugin-clipboard-manager'
3+
import { open } from '@tauri-apps/plugin-dialog'
4+
import { arrayBufferToBase64 } from '../lib/utils'
5+
import { readFile } from '@tauri-apps/plugin-fs'
66
7-
export let onMessage;
8-
export let insecureRenderHtml;
9-
let text = "clipboard message";
7+
export let onMessage
8+
export let insecureRenderHtml
9+
let text = 'clipboard message'
1010
1111
function writeText() {
1212
clipboard
1313
.writeText(text)
1414
.then(() => {
15-
onMessage("Wrote to the clipboard");
15+
onMessage('Wrote to the clipboard')
1616
})
17-
.catch(onMessage);
17+
.catch(onMessage)
1818
}
1919
2020
async function writeImage() {
2121
try {
22-
const res = await open({
23-
title: "Image to write to clipboard",
22+
const path = await open({
23+
title: 'Image to write to clipboard',
2424
filters: [
2525
{
26-
name: "Clipboard IMG",
27-
extensions: ["png", "jpg", "jpeg"],
28-
},
29-
],
30-
});
31-
const bytes = await readFile(res.path);
32-
await clipboard.writeImage(bytes);
33-
onMessage("wrote image");
26+
name: 'Clipboard IMG',
27+
extensions: ['png', 'jpg', 'jpeg']
28+
}
29+
]
30+
})
31+
const bytes = await readFile(path)
32+
await clipboard.writeImage(bytes)
33+
onMessage('wrote image')
3434
} catch (e) {
35-
onMessage(e);
35+
onMessage(e)
3636
}
3737
}
3838
3939
async function read() {
4040
try {
41-
const image = await clipboard.readImage();
41+
const image = await clipboard.readImage()
4242
arrayBufferToBase64(await image.rgba(), function (base64) {
43-
const src = "data:image/png;base64," + base64;
44-
insecureRenderHtml('<img src="' + src + '"></img>');
45-
});
46-
return;
43+
const src = 'data:image/png;base64,' + base64
44+
insecureRenderHtml('<img src="' + src + '"></img>')
45+
})
46+
return
4747
} catch (_) {}
4848
4949
clipboard
5050
.readText()
5151
.then((contents) => {
52-
onMessage(`Clipboard contents: ${contents}`);
52+
onMessage(`Clipboard contents: ${contents}`)
5353
})
5454
.catch((e) => {
55-
onMessage(e);
56-
});
55+
onMessage(e)
56+
})
5757
}
5858
</script>
5959

@@ -65,6 +65,5 @@
6565
/>
6666
<button class="btn" type="button" on:click={writeText}>Write</button>
6767
<button class="btn" type="button" on:click={writeImage}>Pick Image</button>
68-
6968
<button class="btn" type="button" on:click={read}>Read</button>
7069
</div>

plugins/clipboard-manager/android/src/main/java/ClipboardPlugin.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
package app.tauri.clipboard
66

7-
import android.R.attr.value
87
import android.app.Activity
98
import android.content.ClipData
109
import android.content.ClipDescription
1110
import android.content.ClipboardManager
1211
import android.content.Context
12+
import android.os.Build
1313
import app.tauri.annotation.Command
1414
import app.tauri.annotation.InvokeArg
1515
import app.tauri.annotation.TauriPlugin
@@ -59,6 +59,9 @@ internal class ReadClipDataSerializer @JvmOverloads constructor(t: Class<ReadCli
5959

6060
jgen.writeEndObject()
6161
}
62+
else -> {
63+
throw Exception("unimplemented ReadClipData")
64+
}
6265
}
6366

6467
jgen.writeEndObject()
@@ -93,7 +96,7 @@ class ClipboardPlugin(private val activity: Activity) : Plugin(activity) {
9396
is WriteOptions.PlainText -> {
9497
ClipData.newPlainText(args.label, args.text)
9598
} else -> {
96-
invoke.reject("Invalid write options provided")
99+
invoke.reject("unimplemented WriteOptions")
97100
return
98101
}
99102

@@ -128,7 +131,11 @@ class ClipboardPlugin(private val activity: Activity) : Plugin(activity) {
128131
@Command
129132
fun clear(invoke: Invoke) {
130133
if (manager.hasPrimaryClip()) {
134+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
131135
manager.clearPrimaryClip()
136+
} else {
137+
manager.setPrimaryClip(ClipData.newPlainText("", ""))
138+
}
132139
}
133140
invoke.resolve()
134141
}

plugins/clipboard-manager/guest-js/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ async function writeHtml(html: string, altHtml?: string): Promise<void> {
114114

115115
/**
116116
* Clears the clipboard.
117+
*
118+
* #### Platform-specific
119+
*
120+
* - **Android:** Only supported on SDK 28+. For older releases we write an empty string to the clipboard instead.
121+
*
117122
* @example
118123
* ```typescript
119124
* import { clear } from '@tauri-apps/plugin-clipboard-manager';

0 commit comments

Comments
 (0)