Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix problems discovered while using the code in the emission app #1

Merged
merged 10 commits into from
Feb 5, 2016
5 changes: 3 additions & 2 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
<clobbers target="cordova.plugins.BEMServerComm" />
</js-module>

<!--
<dependency id="edu.berkeley.eecs.emission.cordova.unifiedlogger"
url="https://github.com/e-mission/cordova-unified-logger.git"/>
<dependency id="edu.berkeley.eecs.emission.cordova.settings"
url="https://github.com/e-mission/cordova-connection-settings.git"/>

-->
<framework src="src/android/httplib.gradle" custom="true" type="gradleReference" />
<platform name="android">

<config-file target="res/xml/config.xml" parent="/*">
Expand All @@ -33,7 +35,6 @@
</config-file>

<source-file src="src/android/CommunicationHelper.java" target-dir="src/edu/berkeley/eecs/emission/cordova/comm"/>
<source-file src="src/android/ConnectionSettings.java" target-dir="src/edu/berkeley/eecs/emission/cordova/comm"/>
</platform>

<platform name="ios">
Expand Down
11 changes: 8 additions & 3 deletions src/android/CommunicationHelper.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package edu.berkeley.eecs.cordova.comm;
package edu.berkeley.eecs.emission.cordova.comm;

import android.content.Context;
import android.net.http.AndroidHttpClient;
Expand All @@ -19,8 +19,13 @@
import java.net.MalformedURLException;
import java.net.URL;

import edu.berkeley.eecs.emission.cordova.connectionsettings.ConnectionSettings;
import edu.berkeley.eecs.emission.cordova.jwtauth.GoogleAccountManagerAuth;
import edu.berkeley.eecs.emission.cordova.jwtauth.UserProfile;
import edu.berkeley.eecs.emission.cordova.unifiedlogger.Log;

import edu.berkeley.eecs.emission.R;

public class CommunicationHelper {
public static final String TAG = "CommunicationHelper";

Expand Down Expand Up @@ -115,9 +120,9 @@ public static String getUserPersonalData(Context ctxt, String fullURL, String us
}
result = builder.toString();
System.out.println("Result Summary JSON = "+
result.substring(0, Math.min(200, result.length()) + " length "+result.length());
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());
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");
Expand Down
3 changes: 3 additions & 0 deletions src/android/httplib.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
android {
useLibrary 'org.apache.http.legacy'
}
11 changes: 3 additions & 8 deletions src/ios/BEMCommunicationHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@
// Copyright (c) 2014 Kalyanaraman Shankari. All rights reserved.
//

#import "CommunicationHelper.h"
#import "BEMCommunicationHelper.h"
#import "AuthCompletionHandler.h"
#import "Constants.h"
#import "ConnectionSettings.h"
#import "BEMConnectionSettings.h"

#import <GoogleOpenSource/GoogleOpenSource.h>

Expand All @@ -30,10 +29,6 @@
static NSString* kCustomSettingsPath = @"/profile/settings";
static NSString* kRegisterPath = @"/profile/create";

static inline NSString* NSStringFromBOOL(BOOL aBool) {
return aBool? @"YES" : @"NO";
}

@interface CommunicationHelper() <AuthCompletionDelegate> {
}
@end
Expand Down Expand Up @@ -141,7 +136,7 @@ -(void)execute {
return;
}

[[AuthCompletionHandler sharedInstance] getValidAuth:^((GTMOAuth2Authentication *)auth error:(NSError*)error) {
[[AuthCompletionHandler sharedInstance] getValidAuth:^(GTMOAuth2Authentication *auth,NSError* error) {
if (error != NULL) {
self.mCompletionHandler(jsonData, NULL, error);
} else {
Expand Down
8 changes: 0 additions & 8 deletions www/server_communication.js

This file was deleted.

68 changes: 68 additions & 0 deletions www/servercomm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*global cordova, module*/

var exec = require("cordova/exec")

/*
* While this is the javascript implementation, it does not actually call any
* native code! That is because it is basically a wrapper around HTTP get and
* post, just like the corresponding native implementations are. Since the
* javascript has perfectly good support for HTTP, we don't really need the
* overhead of going to native. In fact, you could even argue that we don't
* really need a wrapper here since the default implementation is easy enough
* to use, but abstracting out the implementation makes it consistent with
* native code, and simultaneously makes it easier for us to change the library
* if we switch to a newer and cooler library (websockets? pub/sub? capnproto?).
*/

var ServerCommunication = {
/*
* This is only used for communication with our own server. For
* communication with other services, we can use the standard HTTP
* 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);
});
},
pushJSON: function(relativeUrl, objectLabel, objectJSON, successCallback, errorCallback) {
var msgFiller = function(message) {
message[objectLabel] = objectJSON;
};
ServerCommuniation.pushGetJSON(relativeUrl, msgFiller, successCallback, errorCallback);
},
getUserPersonalData: function(relativeUrl, successCallback, errorCallback) {
var msgFiller = function(message) {
// nop. we don't really send any data for what are effectively get calls
};
ServerCommunication.pushGetJSON(relativeUrl, msgFiller, successCallback, errorCallback);
}
}

module.exports = ServerCommunication;