Skip to content

Commit fc33265

Browse files
authored
fix(android) : App can crash on clipboard.read if empty (#2815)
1 parent 3638a89 commit fc33265

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

android/capacitor/src/main/java/com/getcapacitor/plugin/Clipboard.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,23 +48,28 @@ public void read(PluginCall call) {
4848
ClipboardManager clipboard = (ClipboardManager)
4949
c.getSystemService(Context.CLIPBOARD_SERVICE);
5050

51-
CharSequence value = "";
52-
if(clipboard.getPrimaryClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) {
53-
Log.d(getLogTag(), "Got plaintxt");
54-
ClipData.Item item = clipboard.getPrimaryClip().getItemAt(0);
55-
value = item.getText();
56-
} else {
57-
Log.d(getLogTag(), "Not plaintext!");
58-
ClipData.Item item = clipboard.getPrimaryClip().getItemAt(0);
59-
value = item.coerceToText(this.getContext()).toString();
51+
CharSequence value = null;
52+
53+
if (clipboard.hasPrimaryClip()) {
54+
if(clipboard.getPrimaryClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) {
55+
Log.d(getLogTag(), "Got plaintxt");
56+
ClipData.Item item = clipboard.getPrimaryClip().getItemAt(0);
57+
value = item.getText();
58+
} else {
59+
Log.d(getLogTag(), "Not plaintext!");
60+
ClipData.Item item = clipboard.getPrimaryClip().getItemAt(0);
61+
value = item.coerceToText(this.getContext()).toString();
62+
}
6063
}
64+
6165
JSObject ret = new JSObject();
6266
String type = "text/plain";
6367
ret.put("value", value != null ? value : "");
6468
if (value != null && value.toString().startsWith("data:")) {
6569
type = value.toString().split(";")[0].split(":")[1];
6670
}
6771
ret.put("type", type);
72+
6873
call.success(ret);
6974
}
7075
}

0 commit comments

Comments
 (0)