-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathWebIntent.java
More file actions
235 lines (198 loc) · 9.14 KB
/
Copy pathWebIntent.java
File metadata and controls
235 lines (198 loc) · 9.14 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package net.tunts.webintent;
import java.util.HashMap;
import java.util.Map;
import org.apache.cordova.CordovaActivity;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.text.Html;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.PluginResult;
import org.apache.cordova.CallbackContext;
/**
* WebIntent is a PhoneGap plugin that bridges Android intents and web applications:
*
* 1. web apps can spawn intents that call native Android applications. 2. (after setting up correct intent filters for
* PhoneGap applications), Android intents can be handled by PhoneGap web applications.
*
* @author boris@borismus.com
*
*/
public class WebIntent extends CordovaPlugin {
private CallbackContext onNewIntentCallback = null;
/**
* Executes the request and returns PluginResult.
*
* @param action The action to execute.
* @param args JSONArray of arguments for the plugin.
* @param callbackContext The callbackContext used when calling back into JavaScript.
* @return boolean
*/
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
try {
if (action.equals("startActivity")) {
if (args.length() != 1) {
PluginResult res = new PluginResult(PluginResult.Status.INVALID_ACTION);
callbackContext.sendPluginResult(res);
return false;
}
// Parse the arguments
JSONObject obj = args.getJSONObject(0);
String type = obj.has("type") ? obj.getString("type") : null;
Uri uri = obj.has("url") ? Uri.parse(obj.getString("url")) : null;
JSONObject extras = obj.has("extras") ? obj.getJSONObject("extras") : null;
JSONObject handler = obj.has("handler") ? obj.getJSONObject("handler") : null;
Map<String, String> extrasMap = new HashMap<String, String>();
Map<String, String> handlerMap = null;
// Populate the extras if any exist
if (extras != null) {
JSONArray extraNames = extras.names();
for (int i = 0; i < extraNames.length(); i++) {
String key = extraNames.getString(i);
String value = extras.getString(key);
extrasMap.put(key, value);
}
}
if (handler != null) {
handlerMap = new HashMap<String, String>();
handlerMap.put("packageName", handler.getString("packageName"));
handlerMap.put("className", handler.getString("className"));
}
try{
startActivity(obj.getString("action"), uri, type, extrasMap, handlerMap);
} catch (ActivityNotFoundException e){
callbackContext.error(e.getMessage());
return false;
}
callbackContext.success();
return true;
} else if (action.equals("hasExtra")) {
if (args.length() != 1) {
PluginResult res = new PluginResult(PluginResult.Status.INVALID_ACTION);
callbackContext.sendPluginResult(res);
return false;
}
Intent i = ((CordovaActivity) this.cordova.getActivity()).getIntent();
String extraName = args.getString(0);
PluginResult res = new PluginResult(PluginResult.Status.OK, i.hasExtra(extraName));
callbackContext.sendPluginResult(res);
return true;
} else if (action.equals("getExtra")) {
if (args.length() != 1) {
PluginResult res = new PluginResult(PluginResult.Status.INVALID_ACTION);
callbackContext.sendPluginResult(res);
return false;
}
Intent i = ((CordovaActivity) this.cordova.getActivity()).getIntent();
String extraName = args.getString(0);
if (i.hasExtra(extraName)) {
PluginResult res = new PluginResult(PluginResult.Status.OK, i.hasExtra(extraName));
callbackContext.sendPluginResult(res);
return true;
} else {
PluginResult res = new PluginResult(PluginResult.Status.ERROR);
callbackContext.sendPluginResult(res);
return false;
}
} else if (action.equals("getUri")) {
if (args.length() != 0) {
PluginResult res = new PluginResult(PluginResult.Status.INVALID_ACTION);
callbackContext.sendPluginResult(res);
return false;
}
Intent i = ((CordovaActivity) this.cordova.getActivity()).getIntent();
String uri = i.getDataString();
callbackContext.success(uri);
return true;
} else if (action.equals("onNewIntent")) {
if (args.length() != 0) {
PluginResult res = new PluginResult(PluginResult.Status.INVALID_ACTION);
callbackContext.sendPluginResult(res);
return false;
}
this.onNewIntentCallback = callbackContext;
PluginResult res = new PluginResult(PluginResult.Status.NO_RESULT);
res.setKeepCallback(true);
callbackContext.sendPluginResult(res);
return true;
} else if (action.equals("sendBroadcast")) {
if (args.length() != 1) {
PluginResult res = new PluginResult(PluginResult.Status.INVALID_ACTION);
callbackContext.sendPluginResult(res);
return false;
}
// Parse the arguments
JSONObject obj = args.getJSONObject(0);
JSONObject extras = obj.has("extras") ? obj.getJSONObject("extras") : null;
Map<String, String> extrasMap = new HashMap<String, String>();
// Populate the extras if any exist
if (extras != null) {
JSONArray extraNames = extras.names();
for (int i = 0; i < extraNames.length(); i++) {
String key = extraNames.getString(i);
String value = extras.getString(key);
extrasMap.put(key, value);
}
}
sendBroadcast(obj.getString("action"), extrasMap);
callbackContext.success();
return true;
}
PluginResult res = new PluginResult(PluginResult.Status.INVALID_ACTION);
callbackContext.sendPluginResult(res);
return false;
}
catch (JSONException e) {
callbackContext.error(e.getMessage());
return false;
}
}
@Override
public void onNewIntent(Intent intent) {
if (this.onNewIntentCallback != null) {
this.onNewIntentCallback.success(intent.getDataString());
}
}
void startActivity(String action, Uri uri, String type, Map<String, String> extras, Map<String, String> handlerMap) {
Intent i = (uri != null ? new Intent(action, uri) : new Intent(action));
if (handlerMap != null) {
i.setClassName(handlerMap.get("packageName"), handlerMap.get("className"));
}
if (type != null && uri != null) {
i.setDataAndType(uri, type); // Fix the crash problem with android 2.3.6
} else {
if (type != null) {
i.setType(type);
}
}
for (String key : extras.keySet()) {
String value = extras.get(key);
// If type is text html, the extra text must sent as HTML
if (key.equals(Intent.EXTRA_TEXT) && type.equals("text/html")) {
i.putExtra(key, Html.fromHtml(value));
} else if (key.equals(Intent.EXTRA_STREAM)) {
// allowes sharing of images as attachments.
// value in this case should be a URI of a file
i.putExtra(key, Uri.parse(value));
} else if (key.equals(Intent.EXTRA_EMAIL)) {
// allows to add the email address of the receiver
i.putExtra(Intent.EXTRA_EMAIL, new String[] { value });
} else {
i.putExtra(key, value);
}
}
((CordovaActivity) this.cordova.getActivity()).startActivity(i);
}
void sendBroadcast(String action, Map<String, String> extras) {
Intent intent = new Intent();
intent.setAction(action);
for (String key : extras.keySet()) {
String value = extras.get(key);
intent.putExtra(key, value);
}
((CordovaActivity) this.cordova.getActivity()).sendBroadcast(intent);
}
}