forked from carsten-klaffke/send-intent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SendIntent.java
116 lines (103 loc) · 4.58 KB
/
SendIntent.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package de.mindlib.sendIntent;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import com.getcapacitor.JSObject;
import com.getcapacitor.Plugin;
import com.getcapacitor.PluginCall;
import com.getcapacitor.PluginMethod;
import com.getcapacitor.annotation.CapacitorPlugin;
import org.apache.commons.io.IOUtils;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
@CapacitorPlugin()
public class SendIntent extends Plugin {
@PluginMethod
public void checkSendIntentReceived(PluginCall call) {
Intent intent = bridge.getActivity().getIntent();
if (!"intent handled".equals(intent.getAction())) {
String action = intent.getAction();
String type = intent.getType();
intent.setAction("intent handled");
if (Intent.ACTION_SEND.equals(action) && type != null) {
if ("text/plain".equals(type)) {
JSObject ret = new JSObject();
String stringExtra = intent.getStringExtra(Intent.EXTRA_TEXT);
if (stringExtra == null) {
if (
intent.getClipData() != null &&
intent.getClipData().getItemAt(0) != null &&
intent.getClipData().getItemAt(0).getUri() != null
) {
try {
ret.put("file", getStringFromFile(getContext().getContentResolver().openInputStream(intent.getClipData().getItemAt(0).getUri())));
call.resolve(ret);
} catch (Exception e) {
e.printStackTrace();
}
}
} else {
ret.put("text", stringExtra);
}
call.resolve(ret);
} else if (type.startsWith("image/")) {
JSObject ret = new JSObject();
ret.put("image", encoder((Uri) intent.getExtras().get(Intent.EXTRA_STREAM)));
ret.put("text", intent.getStringExtra(Intent.EXTRA_TEXT));
call.resolve(ret);
} else if (type.equals("application/octet-stream")) {
JSObject ret = new JSObject();
if (intent.getClipData() != null &&
intent.getClipData().getItemAt(0) != null &&
intent.getClipData().getItemAt(0).getUri() != null)
try {
ret.put("file", getStringFromFile(getContext().getContentResolver().openInputStream(intent.getClipData().getItemAt(0).getUri())));
} catch (Exception e) {
e.printStackTrace();
}
call.resolve(ret);
}
} else if (Intent.ACTION_SEND_MULTIPLE.equals(action) && type != null) {
if (type.startsWith("image/")) {
}
} else {
// Handle other intents, such as being started from the home screen
}
}
call.reject("No processing needed");
}
private String encoder(Uri imagePath) {
String base64Image = "";
try (InputStream inputStream = getContext().getContentResolver().openInputStream(imagePath);) {
byte[] bytes = IOUtils.toByteArray(inputStream);
base64Image = java.util.Base64.getEncoder().encodeToString(bytes);
base64Image = "data:image/jpg;base64," + base64Image;
} catch (FileNotFoundException e) {
System.out.println("Image not found" + e);
} catch (IOException ioe) {
System.out.println("Exception while reading the Image " + ioe);
}
return base64Image;
}
private static String convertStreamToString(InputStream is) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line).append("\n");
}
reader.close();
return sb.toString();
}
private static String getStringFromFile(InputStream io) throws Exception {
String ret = convertStreamToString(io);
io.close();
return ret;
}
}