Skip to content

Commit f0ac86b

Browse files
Merge pull request #21 from JoaoCaixinha/master
Run plugin actions on secondary thread, added hash table for commands…
2 parents 7443b61 + f1dcf3e commit f0ac86b

File tree

3 files changed

+69
-33
lines changed

3 files changed

+69
-33
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cordovapush",
3-
"version": "0.1.24",
3+
"version": "0.1.25",
44
"description": "This Cordova plugin should be used with the iOS/Android platforms together with the Realtime Messaging library (ORTC) for Push Notifications support.",
55
"main": "index.js",
66
"scripts": {

plugin.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0"
33
id="co.realtime.plugins.CordovaPush"
4-
version="0.1.24">
4+
version="0.1.25">
55

66
<name>cordovapush</name>
77
<author>Reatime</author>

src/android/OrtcPushPlugin.java

Lines changed: 67 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package co.realtime.plugins.android.cordovapush;
22

3-
import android.app.Activity;
43
import android.app.NotificationManager;
54
import android.os.Bundle;
6-
import android.os.Looper;
75
import android.util.Log;
86

97
import org.apache.cordova.CallbackContext;
@@ -14,6 +12,8 @@
1412
import org.json.JSONException;
1513
import org.json.JSONObject;
1614

15+
import java.util.HashMap;
16+
1717
import ibt.ortc.api.Ortc;
1818
import ibt.ortc.extensibility.OnConnected;
1919
import ibt.ortc.extensibility.OnDisconnected;
@@ -40,11 +40,11 @@ public class OrtcPushPlugin extends CordovaPlugin {
4040
public static final String ACTION_CANCEL_NOTIFICATIONS = "cancelAllLocalNotifications";
4141
public static final String ACTION_SEND_MESSAGE = "send";
4242
private OrtcClient client;
43-
private CallbackContext callback = null;
4443
private static CordovaWebView gWebView;
4544
private static Bundle gCachedExtras = null;
4645
private static boolean gForeground = false;
4746
private static CordovaInterface sCordova;
47+
private static HashMap commands = new HashMap();
4848
@Override
4949
public void initialize(final CordovaInterface cordova, CordovaWebView webView) {
5050
super.initialize(cordova, webView);
@@ -65,7 +65,9 @@ public void initialize(final CordovaInterface cordova, CordovaWebView webView) {
6565
client.onConnected = new OnConnected() {
6666
@Override
6767
public void run(OrtcClient ortcClient) {
68-
callback.success();
68+
CallbackContext call = (CallbackContext)commands.get(ACTION_CONNECT);
69+
if (call != null)
70+
call.success();
6971
}
7072
};
7173

@@ -81,14 +83,18 @@ public void run(OrtcClient ortcClient) {
8183
@Override
8284
public void run(OrtcClient ortcClient, String channel) {
8385
Log.i(TAG,"Unsubscribed from:" + channel);
84-
callback.success();
86+
CallbackContext call = (CallbackContext)commands.get(ACTION_UNSUBSCRIBE);
87+
if (call != null)
88+
call.success();
8589
}
8690
};
8791

8892
client.onSubscribed = new OnSubscribed() {
8993
@Override
9094
public void run(OrtcClient ortcClient, String s) {
91-
callback.success();
95+
CallbackContext call = (CallbackContext)commands.get(ACTION_SUBSCRIBE);
96+
if (call != null)
97+
call.success();
9298
}
9399
};
94100

@@ -141,54 +147,84 @@ public void onDestroy() {
141147
@Override
142148
public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {
143149

144-
callback = callbackContext;
145150
gWebView = this.webView;
146-
151+
final JSONArray argss = args;
147152
try {
148153
if (ACTION_LOG.equals(action)) {
149154
Log.i(TAG,args.get(0).toString());
150155
callbackContext.success();
151156
return true;
152157
}
153158
else if(ACTION_CONNECT.equals(action)){
154-
JSONObject arg_object = args.getJSONObject(0);
155-
client.setClusterUrl(arg_object.getString("url"));
156-
client.setConnectionMetadata(arg_object.getString("metadata"));
157-
client.setGoogleProjectId(arg_object.getString("projectId"));
158-
client.setApplicationContext(this.cordova.getActivity().getApplicationContext());
159-
client.connect(arg_object.getString("appkey"),arg_object.getString("token"));
160-
159+
commands.put(ACTION_CONNECT, callbackContext);
160+
cordova.getThreadPool().execute(new Runnable() {
161+
@Override
162+
public void run() {
163+
JSONObject arg_object = null;
164+
try {
165+
arg_object = argss.getJSONObject(0);
166+
client.setClusterUrl(arg_object.getString("url"));
167+
client.setConnectionMetadata(arg_object.getString("metadata"));
168+
client.setGoogleProjectId(arg_object.getString("projectId"));
169+
client.setApplicationContext(cordova.getActivity().getApplicationContext());
170+
client.connect(arg_object.getString("appkey"),arg_object.getString("token"));
171+
} catch (JSONException e) {
172+
e.printStackTrace();
173+
}
174+
}
175+
});
161176
return true;
162177
}
163178
else if(ACTION_DISCONNECT.equals(action)){
164179
client.disconnect();
165180
return true;
166181
}
167-
else if(ACTION_SUBSCRIBE.equals(action)){
168-
JSONObject arg_object = args.getJSONObject(0);
169-
String channel = arg_object.getString("channel");
170-
client.subscribeWithNotifications(channel, true, new OnMessage() {
182+
else if(ACTION_SUBSCRIBE.equals(action)) {
183+
commands.put(ACTION_SUBSCRIBE, callbackContext);
184+
cordova.getThreadPool().execute(new Runnable() {
171185
@Override
172-
public void run(OrtcClient ortcClient, final String channel, final String message) {
173-
cordova.getActivity().runOnUiThread(new Runnable() {
174-
@Override
175-
public void run() {
176-
Log.i(TAG, "Received message:" + message + " from channel: " + channel);
177-
178-
}
179-
});
180-
186+
public void run() {
187+
JSONObject arg_object = null;
188+
try {
189+
arg_object = argss.getJSONObject(0);
190+
String channel = arg_object.getString("channel");
191+
client.subscribeWithNotifications(channel, true, new OnMessage() {
192+
@Override
193+
public void run(OrtcClient ortcClient, final String channel, final String message) {
194+
cordova.getActivity().runOnUiThread(new Runnable() {
195+
@Override
196+
public void run() {
197+
Log.i(TAG, "Received message:" + message + " from channel: " + channel);
198+
}
199+
});
200+
}
201+
});
202+
} catch (JSONException e) {
203+
e.printStackTrace();
204+
}
181205
}
182206
});
183207
return true;
184208
}
185209
else if(ACTION_UNSUBSCRIBE.equals(action)){
186-
JSONObject arg_object = args.getJSONObject(0);
187-
String channel = arg_object.getString("channel");
188-
client.unsubscribe(channel);
210+
commands.put(ACTION_SUBSCRIBE, callbackContext);
211+
cordova.getThreadPool().execute(new Runnable() {
212+
@Override
213+
public void run() {
214+
JSONObject arg_object = null;
215+
try {
216+
arg_object = argss.getJSONObject(0);
217+
String channel = arg_object.getString("channel");
218+
client.unsubscribe(channel);
219+
} catch (JSONException e) {
220+
e.printStackTrace();
221+
}
222+
}
223+
});
189224
return true;
190225
}
191226
else if(ACTION_CANCEL_NOTIFICATIONS.equals(action)){
227+
commands.put(ACTION_SUBSCRIBE, callbackContext);
192228
final NotificationManager notificationManager = (NotificationManager) this.cordova.getActivity().getSystemService(this.cordova.getActivity().NOTIFICATION_SERVICE);
193229
notificationManager.cancelAll();
194230
callbackContext.success();

0 commit comments

Comments
 (0)