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
|
package nl.xservices.plugins;
import android.content.Intent;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaActivity;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Locale;
public class LaunchMyApp extends CordovaPlugin {
private static final String ACTION_CHECKINTENT = "checkIntent";
private static final String ACTION_CLEARINTENT = "clearIntent";
private static final String ACTION_GETLASTINTENT = "getLastIntent";
private String lastIntentString = null;
/**
* We don't want to interfere with other plugins requiring the intent data,
* but in case of a multi-page app your app may receive the same intent data
* multiple times, that's why you'll get an option to reset it (null it).
*
* Add this to config.xml to enable that behaviour (default false):
* <preference name="CustomURLSchemePluginClearsAndroidIntent" value="true"/>
*/
private boolean resetIntent;
@Override
public void initialize(final CordovaInterface cordova, CordovaWebView webView){
this.resetIntent = preferences.getBoolean("resetIntent", false) ||
preferences.getBoolean("CustomURLSchemePluginClearsAndroidIntent", false);
}
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
if (ACTION_CLEARINTENT.equalsIgnoreCase(action)) {
final Intent intent = ((CordovaActivity) this.webView.getContext()).getIntent();
if (resetIntent){
intent.setData(null);
}
return true;
} else if (ACTION_CHECKINTENT.equalsIgnoreCase(action)) {
final Intent intent = ((CordovaActivity) this.webView.getContext()).getIntent();
final String intentString = intent.getDataString();
if (intentString != null && intent.getScheme() != null) {
lastIntentString = intentString;
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, intent.getDataString()));
} else {
callbackContext.error("App was not started via the launchmyapp URL scheme. Ignoring this errorcallback is the best approach.");
}
return true;
} else if (ACTION_GETLASTINTENT.equalsIgnoreCase(action)) {
if(lastIntentString != null) {
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, lastIntentString));
} else {
callbackContext.error("No intent received so far.");
}
return true;
} else {
callbackContext.error("This plugin only responds to the " + ACTION_CHECKINTENT + " action.");
return false;
}
}
@Override
public void onNewIntent(Intent intent) {
final String intentString = intent.getDataString();
if (intentString != null && intent.getScheme() != null) {
if (resetIntent){
intent.setData(null);
}
try {
StringWriter writer = new StringWriter(intentString.length() * 2);
escapeJavaStyleString(writer, intentString, true, false);
webView.loadUrl("javascript:handleOpenURL('" + writer.toString() + "');");
} catch (IOException ignore) {
}
}
}
// Taken from commons StringEscapeUtils
private static void escapeJavaStyleString(Writer out, String str, boolean escapeSingleQuote,
boolean escapeForwardSlash) throws IOException {
if (out == null) {
throw new IllegalArgumentException("The Writer must not be null");
}
if (str == null) {
return;
}
int sz;
sz = str.length();
for (int i = 0; i < sz; i++) {
char ch = str.charAt(i);
// handle unicode
if (ch > 0xfff) {
out.write("\\u" + hex(ch));
} else if (ch > 0xff) {
out.write("\\u0" + hex(ch));
} else if (ch > 0x7f) {
out.write("\\u00" + hex(ch));
} else if (ch < 32) {
switch (ch) {
case '\b':
out.write('\\');
out.write('b');
break;
case '\n':
out.write('\\');
out.write('n');
break;
case '\t':
out.write('\\');
out.write('t');
break;
case '\f':
out.write('\\');
out.write('f');
break;
case '\r':
out.write('\\');
out.write('r');
break;
default:
if (ch > 0xf) {
out.write("\\u00" + hex(ch));
} else {
out.write("\\u000" + hex(ch));
}
break;
}
} else {
switch (ch) {
case '\'':
if (escapeSingleQuote) {
out.write('\\');
}
out.write('\'');
break;
case '"':
out.write('\\');
out.write('"');
break;
case '\\':
out.write('\\');
out.write('\\');
break;
case '/':
if (escapeForwardSlash) {
out.write('\\');
}
out.write('/');
break;
default:
out.write(ch);
break;
}
}
}
}
private static String hex(char ch) {
return Integer.toHexString(ch).toUpperCase(Locale.ENGLISH);
}
}
|