Edded function to create new event in remote database
This commit is contained in:
@@ -14,6 +14,8 @@ import android.widget.EditText;
|
|||||||
import android.widget.LinearLayout;
|
import android.widget.LinearLayout;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
public class AuthenticateActivity extends AppCompatActivity implements AuthCallbackInterface {
|
public class AuthenticateActivity extends AppCompatActivity implements AuthCallbackInterface {
|
||||||
private String TAG = "AuthenticateActivity";
|
private String TAG = "AuthenticateActivity";
|
||||||
private Context mContext;
|
private Context mContext;
|
||||||
@@ -52,6 +54,7 @@ public class AuthenticateActivity extends AppCompatActivity implements AuthCallb
|
|||||||
public void authCallback(boolean authSuccess, String tokenStr) {
|
public void authCallback(boolean authSuccess, String tokenStr) {
|
||||||
Log.v(TAG,"authCallback");
|
Log.v(TAG,"authCallback");
|
||||||
updateUi();
|
updateUi();
|
||||||
|
mWac.createEvent(10,new Date(),"eventDescription....");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateUi() {
|
private void updateUi() {
|
||||||
@@ -60,8 +63,9 @@ public class AuthenticateActivity extends AppCompatActivity implements AuthCallb
|
|||||||
LinearLayout loginLl = (LinearLayout)findViewById(R.id.login_ui);
|
LinearLayout loginLl = (LinearLayout)findViewById(R.id.login_ui);
|
||||||
LinearLayout logoutLl = (LinearLayout)findViewById(R.id.logout_ui);
|
LinearLayout logoutLl = (LinearLayout)findViewById(R.id.logout_ui);
|
||||||
Log.i(TAG, "switchUi()");
|
Log.i(TAG, "switchUi()");
|
||||||
prefs = PreferenceManager.getDefaultSharedPreferences(this);
|
storedAuthToken = mWac.getStoredToken();
|
||||||
storedAuthToken = (prefs.getString("webApiAuthToken", null));
|
//prefs = PreferenceManager.getDefaultSharedPreferences(this);
|
||||||
|
//storedAuthToken = (prefs.getString("webApiAuthToken", null));
|
||||||
Log.v(TAG, "storedAuthToken=" + storedAuthToken);
|
Log.v(TAG, "storedAuthToken=" + storedAuthToken);
|
||||||
|
|
||||||
// Check if we are already logged in
|
// Check if we are already logged in
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package uk.org.openseizuredetector;
|
||||||
|
|
||||||
|
public interface EventCallbackInterface {
|
||||||
|
// Interface is called when a new event is created in the database.
|
||||||
|
void eventCallback(boolean success, String eventStr);
|
||||||
|
}
|
||||||
@@ -7,10 +7,12 @@ import android.preference.PreferenceManager;
|
|||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
|
|
||||||
|
import com.android.volley.AuthFailureError;
|
||||||
import com.android.volley.Request;
|
import com.android.volley.Request;
|
||||||
import com.android.volley.RequestQueue;
|
import com.android.volley.RequestQueue;
|
||||||
import com.android.volley.Response;
|
import com.android.volley.Response;
|
||||||
import com.android.volley.VolleyError;
|
import com.android.volley.VolleyError;
|
||||||
|
import com.android.volley.VolleyLog;
|
||||||
import com.android.volley.toolbox.JsonObjectRequest;
|
import com.android.volley.toolbox.JsonObjectRequest;
|
||||||
import com.android.volley.toolbox.StringRequest;
|
import com.android.volley.toolbox.StringRequest;
|
||||||
import com.android.volley.toolbox.Volley;
|
import com.android.volley.toolbox.Volley;
|
||||||
@@ -18,6 +20,10 @@ import com.android.volley.toolbox.Volley;
|
|||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
|
import java.io.UnsupportedEncodingException;
|
||||||
|
import java.text.DateFormat;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.Date;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@@ -29,12 +35,15 @@ public class WebApiConnection {
|
|||||||
private String mUrlBase = "https://osdApi.ddns.net";
|
private String mUrlBase = "https://osdApi.ddns.net";
|
||||||
private String TAG = "WebApiConnection";
|
private String TAG = "WebApiConnection";
|
||||||
private AuthCallbackInterface mAuthCallback;
|
private AuthCallbackInterface mAuthCallback;
|
||||||
|
private EventCallbackInterface mEventCallback;
|
||||||
private Context mContext;
|
private Context mContext;
|
||||||
|
private String TOKEN_ID = "webApiAuthToken";
|
||||||
RequestQueue mQueue;
|
RequestQueue mQueue;
|
||||||
|
|
||||||
public WebApiConnection(Context context, AuthCallbackInterface authCallback) {
|
public WebApiConnection(Context context, AuthCallbackInterface authCallback, EventCallbackInterface eventCallback) {
|
||||||
mContext = context;
|
mContext = context;
|
||||||
mAuthCallback = authCallback;
|
mAuthCallback = authCallback;
|
||||||
|
mEventCallback = eventCallback;
|
||||||
mQueue = Volley.newRequestQueue(context);
|
mQueue = Volley.newRequestQueue(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -59,8 +68,7 @@ public class WebApiConnection {
|
|||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
tokenStr = "Error Parsing Rsponse";
|
tokenStr = "Error Parsing Rsponse";
|
||||||
}
|
}
|
||||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
|
saveStoredToken(tokenStr);
|
||||||
prefs.edit().putString("webApiAuthToken", tokenStr).commit();
|
|
||||||
mAuthCallback.authCallback(true, response);
|
mAuthCallback.authCallback(true, response);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -69,8 +77,7 @@ public class WebApiConnection {
|
|||||||
public void onErrorResponse(VolleyError error) {
|
public void onErrorResponse(VolleyError error) {
|
||||||
String responseBody = new String(error.networkResponse.data);
|
String responseBody = new String(error.networkResponse.data);
|
||||||
Log.v(TAG, "Login Error: " + error.toString() + ", message:" + error.getMessage()+", Response Code:" + error.networkResponse.statusCode + ", Response: " + responseBody);
|
Log.v(TAG, "Login Error: " + error.toString() + ", message:" + error.getMessage()+", Response Code:" + error.networkResponse.statusCode + ", Response: " + responseBody);
|
||||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
|
saveStoredToken(null);
|
||||||
prefs.edit().putString("webApiAuthToken", null).commit();
|
|
||||||
mAuthCallback.authCallback(false,new String(error.networkResponse.data));
|
mAuthCallback.authCallback(false,new String(error.networkResponse.data));
|
||||||
}
|
}
|
||||||
}) {
|
}) {
|
||||||
@@ -92,20 +99,120 @@ public class WebApiConnection {
|
|||||||
// Remove the stored token so future calls are not authemticated.
|
// Remove the stored token so future calls are not authemticated.
|
||||||
public void logout() {
|
public void logout() {
|
||||||
Log.v(TAG, "logout()");
|
Log.v(TAG, "logout()");
|
||||||
|
saveStoredToken(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void saveStoredToken(String tokenStr) {
|
||||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
|
||||||
prefs.edit().putString("webApiAuthToken", null).commit();
|
prefs.edit().putString(TOKEN_ID, tokenStr).commit();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getStoredToken() {
|
||||||
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
|
||||||
|
String authToken = prefs.getString(TOKEN_ID, null);
|
||||||
|
return authToken;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isLoggedIn() {
|
||||||
|
String authToken = getStoredToken();
|
||||||
|
Log.v(TAG,"isLoggedIn(): token="+authToken);
|
||||||
|
if (authToken == null || authToken.length() == 0) {
|
||||||
|
Log.v(TAG,"isLogged in - not logged in");
|
||||||
|
return(false);
|
||||||
|
} else {
|
||||||
|
return(true);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Create a new event in the remote database, based on the provided parameters.
|
// Create a new event in the remote database, based on the provided parameters.
|
||||||
public boolean createEvent() {
|
public boolean createEvent(final int eventType, final Date eventDate, final String eventDesc) {
|
||||||
Log.v(TAG,"createEvent() - FIXME - This does not do anything!");
|
Log.v(TAG,"createEvent() - FIXME - This does not do anything!");
|
||||||
|
String urlStr = mUrlBase + "/api/events/";
|
||||||
|
Log.v(TAG, "urlStr=" + urlStr);
|
||||||
|
final String authtoken = getStoredToken();
|
||||||
|
|
||||||
|
if (!isLoggedIn()) {
|
||||||
|
Log.v(TAG,"not logged in - doing nothing");
|
||||||
|
return(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss");
|
||||||
|
JSONObject jsonObject = new JSONObject();
|
||||||
|
try {
|
||||||
|
jsonObject.put("eventType", String.valueOf(eventType));
|
||||||
|
jsonObject.put("dataTime", dateFormat.format(eventDate));
|
||||||
|
jsonObject.put("desc", eventDesc);
|
||||||
|
} catch (JSONException e) {
|
||||||
|
Log.e(TAG,"Error generating event JSON string");
|
||||||
|
}
|
||||||
|
final String dataStr = jsonObject.toString();
|
||||||
|
Log.v(TAG,"createEvent - data="+dataStr);
|
||||||
|
|
||||||
|
|
||||||
|
StringRequest req = new StringRequest(Request.Method.POST, urlStr,
|
||||||
|
new Response.Listener<String>() {
|
||||||
|
@Override
|
||||||
|
public void onResponse(String response) {
|
||||||
|
Log.v(TAG, "Response is: " + response);
|
||||||
|
mEventCallback.eventCallback(true, response);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
new Response.ErrorListener() {
|
||||||
|
@Override
|
||||||
|
public void onErrorResponse(VolleyError error) {
|
||||||
|
String responseBody = new String(error.networkResponse.data);
|
||||||
|
Log.v(TAG, "Create Event Error: " + error.toString() + ", message:" + error.getMessage()+", Response Code:" + error.networkResponse.statusCode + ", Response: " + responseBody);
|
||||||
|
mEventCallback.eventCallback(false,new String(error.networkResponse.data));
|
||||||
|
}
|
||||||
|
}) {
|
||||||
|
// Note, this is overriding part of StringRequest, not one of the sub-classes above!
|
||||||
|
@Override
|
||||||
|
protected Map<String, String> getParams() {
|
||||||
|
Map<String, String> params = new HashMap<>();
|
||||||
|
// params.put("name",sname); // passing parameters to server
|
||||||
|
String authToken = getStoredToken();
|
||||||
|
params.put("Authorization: Token "+authToken, authToken);
|
||||||
|
Log.v(TAG,"getParams: params="+params.toString());
|
||||||
|
//params.put("eventType", String.valueOf(eventType));
|
||||||
|
//params.put("dataTime", dateFormat.format(eventDate));
|
||||||
|
//params.put("desc", eventDesc);
|
||||||
|
return params;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public Map<String, String> getHeaders() throws AuthFailureError {
|
||||||
|
Map<String, String> params = new HashMap<String, String>();
|
||||||
|
params.put("Content-Type", "application/json; charset=UTF-8");
|
||||||
|
params.put("Authorization", "Token "+getStoredToken());
|
||||||
|
return params;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public byte[] getBody() throws AuthFailureError {
|
||||||
|
try {
|
||||||
|
return dataStr == null ? null : dataStr.getBytes("utf-8");
|
||||||
|
} catch (UnsupportedEncodingException uee) {
|
||||||
|
VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", dataStr, "utf-8");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
mQueue.add(req);
|
||||||
|
return (true);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean createDatapoint() {
|
public boolean createDatapoint() {
|
||||||
Log.v(TAG,"createDatapoint() - FIXME - This does not do anything!");
|
Log.v(TAG,"createDatapoint() - FIXME - This does not do anything!");
|
||||||
|
return(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user