diff --git a/plugin.xml b/plugin.xml
index 47b4505..f4507ce 100644
--- a/plugin.xml
+++ b/plugin.xml
@@ -30,22 +30,25 @@
-
+
+
-
+
+
+
diff --git a/src/android/CommunicationHelper.java b/src/android/CommunicationHelper.java
index 66597ce..029ca79 100644
--- a/src/android/CommunicationHelper.java
+++ b/src/android/CommunicationHelper.java
@@ -69,6 +69,56 @@ public static String readResults(Context ctxt, String cacheControlProperty)
return rawHTML;
}
+ /*
+ * The other methods here take in a fullURL and return a string,
+ * respectively. The plugin interface passes in a relative URL and returns
+ * a JSONObject. For now, let us have this be consistent with the other
+ * calls here, and glue things together in the plugin.
+ */
+ public static String pushGetJSON(Context ctxt, String fullURL,
+ JSONObject filledJsonObject)
+ throws IOException, JSONException {
+
+ // Initialize the message
+ String result = "";
+ HttpPost msg = new HttpPost(fullURL);
+ System.out.println("Posting data to " + msg.getURI());
+ msg.setHeader("Content-Type", "application/json");
+
+ // Fill in the object
+ final String userName = UserProfile.getInstance(ctxt).getUserEmail();
+ final String userToken = GoogleAccountManagerAuth.getServerToken(ctxt, userName);
+ filledJsonObject.put("user", userToken);
+ msg.setEntity(new StringEntity(filledJsonObject.toString()));
+
+ // Perform the operation
+ AndroidHttpClient connection = AndroidHttpClient.newInstance(ctxt.getString(R.string.app_name));
+ HttpResponse response = connection.execute(msg);
+ StatusLine statusLine = response.getStatusLine();
+ Log.i(ctxt, TAG, "Got response "+response+" with status "+statusLine);
+ int statusCode = statusLine.getStatusCode();
+
+ if(statusCode == 200){
+ BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
+ StringBuilder builder = new StringBuilder();
+ String currLine = null;
+ while ((currLine = in.readLine()) != null) {
+ builder.append(currLine+"\n");
+ }
+ result = builder.toString();
+ System.out.println("Result Summary JSON = "+
+ result.substring(0, Math.min(200, result.length())) + " length "+result.length());
+ Log.i(ctxt, TAG, "Result Summary JSON = "+
+ result.substring(0, Math.min(200, result.length())) + " length "+result.length());
+ in.close();
+ } else {
+ Log.e(ctxt, R.class.toString(),"Failed to get JSON object");
+ throw new IOException();
+ }
+ connection.close();
+ return result;
+ }
+
public static void pushJSON(Context ctxt, String fullURL, String userToken,
String objectLabel, Object jsonObjectOrArray)
throws IOException, JSONException {
diff --git a/src/android/CommunicationHelperPlugin.java b/src/android/CommunicationHelperPlugin.java
new file mode 100644
index 0000000..8496da6
--- /dev/null
+++ b/src/android/CommunicationHelperPlugin.java
@@ -0,0 +1,35 @@
+package edu.berkeley.eecs.emission.cordova.comm;
+
+import org.apache.cordova.*;
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
+
+import android.content.Context;
+import edu.berkeley.eecs.emission.cordova.comm.CommunicationHelper;
+import edu.berkeley.eecs.emission.cordova.connectionsettings.ConnectionSettings;
+
+public class CommunicationHelperPlugin extends CordovaPlugin {
+ @Override
+ public boolean execute(String action, JSONArray data, CallbackContext callbackContext) throws JSONException {
+ if (action.equals("pushGetJSON")) {
+ try {
+ Context ctxt = cordova.getActivity();
+ String relativeURL = data.getString(0);
+ JSONObject filledMessage = data.getJSONObject(1);
+
+ String commuteTrackerHost = ConnectionSettings.getConnectURL(ctxt);
+ String fullURL = commuteTrackerHost + relativeURL;
+
+ String resultString = CommunicationHelper.pushGetJSON(ctxt, fullURL, filledMessage);
+ callbackContext.success(new JSONObject(resultString));
+ } catch (Exception e) {
+ callbackContext.error("While pushing/getting from server "+e.getMessage());
+ }
+ return true;
+ } else {
+ return false;
+ }
+ }
+}
+
diff --git a/src/ios/BEMCommunicationHelper.h b/src/ios/BEMCommunicationHelper.h
index af0eb68..193eba6 100644
--- a/src/ios/BEMCommunicationHelper.h
+++ b/src/ios/BEMCommunicationHelper.h
@@ -22,6 +22,7 @@
+(void)phone_to_server:(NSArray*) entriesToPush completionHandler:(void (^)(NSData *data, NSURLResponse *response, NSError *error))completionHandler;;
// Generic GET and POST methods
++(void)pushGetJSON:(NSDictionary*)toSend toURL:(NSString*)relativeURL completionHandler:(void (^)(NSData *data, NSURLResponse *response, NSError *error))completionHandler;
+(void)getData:(NSURL *)url completionHandler:(void (^)(NSData *data, NSURLResponse *response, NSError *error))completionHandler;
-(id)initPost:(NSURL *)url data:(NSMutableDictionary*)jsonDict completionHandler:(void (^)(NSData *data, NSURLResponse *response, NSError *error))completionHandler;
diff --git a/src/ios/BEMCommunicationHelper.m b/src/ios/BEMCommunicationHelper.m
index c3d0c73..0214162 100644
--- a/src/ios/BEMCommunicationHelper.m
+++ b/src/ios/BEMCommunicationHelper.m
@@ -114,6 +114,17 @@ +(void)phone_to_server:(NSArray *)entriesToPush completionHandler:(void (^)(NSDa
[executor execute];
}
++(void)pushGetJSON:(NSDictionary*)toSend toURL:(NSString*)relativeURL completionHandler:(void (^)(NSData *data, NSURLResponse *response, NSError *error))completionHandler {
+ NSMutableDictionary *toPush = [NSMutableDictionary dictionaryWithDictionary:toSend];
+
+ NSURL* kBaseURL = [[ConnectionSettings sharedInstance] getConnectUrl];
+ NSURL* absoluteURL = [NSURL URLWithString:relativeURL
+ relativeToURL:kBaseURL];
+
+ CommunicationHelper *executor = [[CommunicationHelper alloc] initPost:absoluteURL data:toPush completionHandler:completionHandler];
+ [executor execute];
+}
+
+(void)getData:(NSURL*)url completionHandler:(void (^)(NSData *data, NSURLResponse *response, NSError *error))completionHandler {
NSURLSession *sharedSession = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [sharedSession dataTaskWithURL:url completionHandler:completionHandler];
@@ -149,7 +160,10 @@ -(void)execute {
GTMOAuth2Authentication* currAuth = [AuthCompletionHandler sharedInstance].currAuth;
if (currAuth == NULL) {
[self tryToAuthenticate:jsonData];
- } else {
+ currAuth = [AuthCompletionHandler sharedInstance].currAuth;
+ }
+
+ if (currAuth != NULL) {
BOOL expired = ([currAuth.expirationDate compare:[NSDate date]] == NSOrderedAscending);
// The access token may not have expired, but the id token may not be available because the app has been restarted,
// so it is not in memory, and the ID token is not stored in the keychain. It is a real pain to store the ID token
@@ -215,7 +229,7 @@ -(void)execute {
}
}
-- (void)tryToAuthenticate:(NSData*)jsonData {
+- (BOOL)tryToAuthenticate:(NSData*)jsonData {
[LocalNotificationManager addNotification:[NSString stringWithFormat:
@"tryToAuthenticate called"] showUI:FALSE];
[[AuthCompletionHandler sharedInstance] registerFinishDelegate:self];
@@ -234,9 +248,8 @@ - (void)tryToAuthenticate:(NSData*)jsonData {
self.mCompletionHandler(jsonData, NULL, authError);
} else {
[LocalNotificationManager addNotification:[NSString stringWithFormat:
- @"callback should be called, we will deal with it there"]
+ @"ready to authenticate, checking expiration"]
showUI:FALSE];
- NSLog(@"callback should be called, we will deal with it there");
// So far, callback has not taken a long time...
// But callback may take a long time. In that case, we may want to return early.
// Also, callback will invoke mCompletionHandler in a separate thread, which won't
@@ -246,6 +259,7 @@ - (void)tryToAuthenticate:(NSData*)jsonData {
// So we will be called again, and won't have to invoke this call then
// mCompletionHandler(NULL, NULL, NULL);
}
+ return silentAuthResult;
}
- (void)postToHost {
diff --git a/src/ios/BEMCommunicationHelperPlugin.h b/src/ios/BEMCommunicationHelperPlugin.h
new file mode 100644
index 0000000..3351134
--- /dev/null
+++ b/src/ios/BEMCommunicationHelperPlugin.h
@@ -0,0 +1,7 @@
+#import
+
+@interface BEMCommunicationHelperPlugin: CDVPlugin
+
+- (void) pushGetJSON:(CDVInvokedUrlCommand*)command;
+
+@end
diff --git a/src/ios/BEMCommunicationHelperPlugin.m b/src/ios/BEMCommunicationHelperPlugin.m
new file mode 100644
index 0000000..4089e03
--- /dev/null
+++ b/src/ios/BEMCommunicationHelperPlugin.m
@@ -0,0 +1,44 @@
+#import "BEMCommunicationHelperPlugin.h"
+#import "BEMCommunicationHelper.h"
+
+@implementation BEMCommunicationHelperPlugin
+
+- (void)pushGetJSON:(CDVInvokedUrlCommand *)command
+{
+ NSString* callbackId = [command callbackId];
+
+ @try {
+ NSString* relativeURL = [[command arguments] objectAtIndex:0];
+ NSDictionary* filledMessage = [[command arguments] objectAtIndex:1];
+
+ [CommunicationHelper pushGetJSON:filledMessage toURL:relativeURL completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
+ if (error != NULL) {
+ [self sendError:error callBackID:callbackId];
+ }
+ NSError *parseError;
+ NSDictionary *parsedResult = [NSJSONSerialization JSONObjectWithData:data
+ options:kNilOptions
+ error: &parseError];
+ if (parseError != NULL) {
+ [self sendError:parseError callBackID:callbackId];
+ }
+ CDVPluginResult* result = [CDVPluginResult
+ resultWithStatus:CDVCommandStatus_OK
+ messageAsDictionary:parsedResult];
+ [self.commandDelegate sendPluginResult:result callbackId:callbackId];
+ }];
+ }
+ @catch (NSException *exception) {
+ [self sendError:exception callBackID:callbackId];
+ }
+}
+
+- (void) sendError:(id) error callBackID:(NSString*)callbackID {
+ NSString* msg = [NSString stringWithFormat: @"During server call, error %@", error];
+ CDVPluginResult* result = [CDVPluginResult
+ resultWithStatus:CDVCommandStatus_ERROR
+ messageAsString:msg];
+ [self.commandDelegate sendPluginResult:result callbackId:callbackID];
+}
+
+@end
diff --git a/www/servercomm.js b/www/servercomm.js
index ad9df33..44620cb 100644
--- a/www/servercomm.js
+++ b/www/servercomm.js
@@ -21,35 +21,9 @@ var ServerCommunication = {
* libraries from our javascript framework.
*/
pushGetJSON: function(relativeURL, messageFiller, successCallback, errorCallback) {
- var request = new XMLHttpRequest();
-
- request.onreadystatechange = function() {
- if(request.readyState == request.DONE) {
- if (request.status == 200) {
- var resultObj = JSON.parse(request.responseText);
- successCallback(resultObj);
- } else {
- errorCallback(request.statusText);
- }
- } else {
- console.log("during HTTP post, state "+request.readyState);
- }
- };
- window.cordova.plugins.BEMConnectionSettings.getSettings(function(settings) {
- var fullURL = settings.connectURL + relativeURL;
- window.cordova.plugins.BEMJWTAuth.getJWT(function(token) {
- message = {};
- message.user = token;
- messageFiller(message);
- request.open("POST", fullURL, true);
- request.setRequestHeader("Content-Type", "application/json");
- request.send(JSON.stringify(message));
- }, function(error) {
- errorCallback(error);
- })
- }, function(error) {
- errorCallback(error);
- });
+ filledMessage = {};
+ messageFiller(filledMessage);
+ exec(successCallback, errorCallback, "ServerComm", "pushGetJSON", [relativeURL, filledMessage]);
},
pushJSON: function(relativeUrl, objectLabel, objectJSON, successCallback, errorCallback) {
var msgFiller = function(message) {