Game registers at main API
This commit is contained in:
parent
a80425b547
commit
5bdda26f30
6 changed files with 200 additions and 4 deletions
|
@ -29,5 +29,6 @@ dependencies {
|
||||||
compile 'com.affectiva.android:affdexsdk:3.1'
|
compile 'com.affectiva.android:affdexsdk:3.1'
|
||||||
compile 'com.google.code.gson:gson:2.4'
|
compile 'com.google.code.gson:gson:2.4'
|
||||||
compile 'com.android.support:design:23.4.0'
|
compile 'com.android.support:design:23.4.0'
|
||||||
|
compile 'com.loopj.android:android-async-http:1.4.9'
|
||||||
testCompile 'junit:junit:4.12'
|
testCompile 'junit:junit:4.12'
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
package="com.rubenvandeven.emotionhero">
|
package="com.rubenvandeven.emotionhero">
|
||||||
|
|
||||||
<uses-permission android:name="android.permission.CAMERA" />
|
<uses-permission android:name="android.permission.CAMERA" />
|
||||||
|
<uses-permission android:name="android.permission.INTERNET" />
|
||||||
|
|
||||||
<application
|
<application
|
||||||
android:allowBackup="true"
|
android:allowBackup="true"
|
||||||
|
|
|
@ -0,0 +1,149 @@
|
||||||
|
package com.rubenvandeven.emotionhero;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
|
import com.loopj.android.http.AsyncHttpClient;
|
||||||
|
import com.loopj.android.http.AsyncHttpResponseHandler;
|
||||||
|
import com.loopj.android.http.JsonHttpResponseHandler;
|
||||||
|
import com.loopj.android.http.RequestParams;
|
||||||
|
import com.loopj.android.http.SyncHttpClient;
|
||||||
|
|
||||||
|
import org.json.JSONException;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
|
||||||
|
import cz.msebera.android.httpclient.Header;
|
||||||
|
import cz.msebera.android.httpclient.HeaderElement;
|
||||||
|
import cz.msebera.android.httpclient.ParseException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by ruben on 01/09/16.
|
||||||
|
*
|
||||||
|
* A way to interact with api.emotionhero.com
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class ApiRestClient {
|
||||||
|
private static final String BASE_URL = "http://api.emotionhero.com"; // TODO: https!
|
||||||
|
|
||||||
|
private static AsyncHttpClient client = new AsyncHttpClient();
|
||||||
|
private static SyncHttpClient syncClient = new SyncHttpClient();
|
||||||
|
|
||||||
|
private String jwt;
|
||||||
|
|
||||||
|
private Player player;
|
||||||
|
|
||||||
|
public ApiRestClient(Player player) {
|
||||||
|
this.player = player;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void registerIfNeeded() {
|
||||||
|
if(player.getJWT() == null) {
|
||||||
|
requestWithJWT(null, null, null, null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* For now call register endpoint. Given JWT should have a long enough lifetime (for now)
|
||||||
|
* @todo However, custom token endpoint should be used eventually
|
||||||
|
* @param method
|
||||||
|
* @param url
|
||||||
|
* @param params
|
||||||
|
* @param responseHandler
|
||||||
|
*/
|
||||||
|
public void requestWithJWT(final String method, final String url, final RequestParams params, final AsyncHttpResponseHandler responseHandler) {
|
||||||
|
// sync call, so we can return a value!
|
||||||
|
client.post(BASE_URL + "/api/register", null, new JsonHttpResponseHandler() {
|
||||||
|
@Override
|
||||||
|
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
|
||||||
|
// If the response is JSONObject instead of expected JSONArray
|
||||||
|
try {
|
||||||
|
String token = response.getString("jwt");
|
||||||
|
String remoteId = response.getString("id");
|
||||||
|
Log.d("API", "Token " + token);
|
||||||
|
Log.d("API", "RemoteId" + remoteId);
|
||||||
|
getPlayer().setJWT(token);
|
||||||
|
getPlayer().setRemoteId(remoteId);
|
||||||
|
|
||||||
|
if(method != null) {
|
||||||
|
ApiRestClient.this.request( method, url, params, responseHandler);
|
||||||
|
}
|
||||||
|
} catch (JSONException e) {
|
||||||
|
// responseHandler.sendFailureMessage(500, null, null);
|
||||||
|
// failure!!
|
||||||
|
// retrying later probably....
|
||||||
|
Log.e("API", "Failed request");
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public Player getPlayer() {
|
||||||
|
return player;
|
||||||
|
}
|
||||||
|
|
||||||
|
class TokenHeader implements Header {
|
||||||
|
/**
|
||||||
|
* Get the name of the Header.
|
||||||
|
*
|
||||||
|
* @return the name of the Header, never {@code null}
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public String getName() {
|
||||||
|
return "X-Access-Token";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the value of the Header.
|
||||||
|
*
|
||||||
|
* @return the value of the Header, may be {@code null}
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public String getValue() {
|
||||||
|
return "Bearer " + player.getJWT();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parses the value.
|
||||||
|
*
|
||||||
|
* @return an array of {@link HeaderElement} entries, may be empty, but is never {@code null}
|
||||||
|
* @throws ParseException
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public HeaderElement[] getElements() throws ParseException {
|
||||||
|
return new HeaderElement[]{};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void request(String method, String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
|
||||||
|
Header[] headers = new Header[]{new TokenHeader()};
|
||||||
|
Log.d("API", "Do request to: " + url);
|
||||||
|
if(method == "post") {
|
||||||
|
client.post(player.getContext(), url, headers,params,"application/json", responseHandler);
|
||||||
|
} else {
|
||||||
|
client.get(player.getContext(), url, headers, params, responseHandler);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
|
||||||
|
String jwt = player.getJWT();
|
||||||
|
if(jwt != null) {
|
||||||
|
request("get", getAbsoluteUrl(url), params, responseHandler);
|
||||||
|
} else {
|
||||||
|
requestWithJWT("get", getAbsoluteUrl(url), params, responseHandler);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
|
||||||
|
String jwt = player.getJWT();
|
||||||
|
if(jwt != null) {
|
||||||
|
request("post", getAbsoluteUrl(url), params, responseHandler);
|
||||||
|
} else {
|
||||||
|
requestWithJWT("post", getAbsoluteUrl(url), params, responseHandler);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String getAbsoluteUrl(String relativeUrl) {
|
||||||
|
return BASE_URL + relativeUrl;
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,7 +2,6 @@ package com.rubenvandeven.emotionhero;
|
||||||
|
|
||||||
import android.Manifest;
|
import android.Manifest;
|
||||||
import android.annotation.TargetApi;
|
import android.annotation.TargetApi;
|
||||||
import android.content.Context;
|
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
import android.media.AudioAttributes;
|
import android.media.AudioAttributes;
|
||||||
|
@ -27,12 +26,10 @@ import com.affectiva.android.affdex.sdk.detector.CameraDetector;
|
||||||
import com.affectiva.android.affdex.sdk.detector.Detector;
|
import com.affectiva.android.affdex.sdk.detector.Detector;
|
||||||
import com.affectiva.android.affdex.sdk.detector.Face;
|
import com.affectiva.android.affdex.sdk.detector.Face;
|
||||||
|
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.FileOutputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An example full-screen activity that shows and hides the system UI (i.e.
|
* An example full-screen activity that shows and hides the system UI (i.e.
|
||||||
* status bar and navigation/system bar) with user interaction.
|
* status bar and navigation/system bar) with user interaction.
|
||||||
|
|
|
@ -85,6 +85,8 @@ public class IntroActivity extends AppCompatActivity {
|
||||||
|
|
||||||
continueHandler = new Handler();
|
continueHandler = new Handler();
|
||||||
continueHandler.postDelayed(continueRunnable, 7000 + (3*extraLogoDelay));
|
continueHandler.postDelayed(continueRunnable, 7000 + (3*extraLogoDelay));
|
||||||
|
|
||||||
|
player.api.registerIfNeeded();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void continueToMenu() {
|
public void continueToMenu() {
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package com.rubenvandeven.emotionhero;
|
package com.rubenvandeven.emotionhero;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.content.SharedPreferences;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
|
@ -21,6 +22,13 @@ public class Player {
|
||||||
|
|
||||||
PlayerInfo info;
|
PlayerInfo info;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Api calls are on player basis, so call them from here
|
||||||
|
*/
|
||||||
|
public ApiRestClient api;
|
||||||
|
|
||||||
|
public static final String PREFS_NAME = "PlayerPrefs";
|
||||||
|
|
||||||
public static Player getInstance(Context c) {
|
public static Player getInstance(Context c) {
|
||||||
if(ourInstance == null) {
|
if(ourInstance == null) {
|
||||||
ourInstance = new Player(c);
|
ourInstance = new Player(c);
|
||||||
|
@ -31,6 +39,44 @@ public class Player {
|
||||||
private Player(Context c) {
|
private Player(Context c) {
|
||||||
this.c = c;
|
this.c = c;
|
||||||
this.info = loadPlayerInfo();
|
this.info = loadPlayerInfo();
|
||||||
|
|
||||||
|
api = new ApiRestClient(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Context getContext() {
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the Id as it is used on the API
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public String getRemoteId() {
|
||||||
|
SharedPreferences settings = c.getSharedPreferences(PREFS_NAME, 0);
|
||||||
|
return settings.getString("remoteId", null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRemoteId(String remoteId) {
|
||||||
|
SharedPreferences settings = c.getSharedPreferences(PREFS_NAME, 0);
|
||||||
|
SharedPreferences.Editor editor = settings.edit();
|
||||||
|
editor.putString("remoteId", remoteId);
|
||||||
|
editor.commit();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the JWT as it is used on the API
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public String getJWT() {
|
||||||
|
SharedPreferences settings = c.getSharedPreferences(PREFS_NAME, 0);
|
||||||
|
return settings.getString("jwt", null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setJWT(String jwt) {
|
||||||
|
SharedPreferences settings = c.getSharedPreferences(PREFS_NAME, 0);
|
||||||
|
SharedPreferences.Editor editor = settings.edit();
|
||||||
|
editor.putString("jwt", jwt);
|
||||||
|
editor.commit();
|
||||||
}
|
}
|
||||||
|
|
||||||
public PlayerInfo getPlayerInfo() {
|
public PlayerInfo getPlayerInfo() {
|
||||||
|
|
Loading…
Reference in a new issue