Added activity to view the remote database data - needs to point to a (not yet written) ReactJS based page - just shows the API raw data at the moment.
This commit is contained in:
@@ -1,9 +1,7 @@
|
||||
package uk.org.openseizuredetector;
|
||||
|
||||
//import androidx.appcompat.app.AppCompatActivity;
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
|
||||
import android.os.Bundle;
|
||||
@@ -14,20 +12,12 @@ import android.widget.EditText;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.TextView;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
public class AuthenticateActivity extends AppCompatActivity
|
||||
implements AuthCallbackInterface, EventCallbackInterface, DatapointCallbackInterface {
|
||||
private String TAG = "AuthenticateActivity";
|
||||
private Context mContext;
|
||||
private EditText mUnameEt;
|
||||
private EditText mPasswdEt;
|
||||
private boolean mIsLoggedIn;
|
||||
private WebApiConnection mWac;
|
||||
private LogManager mLm;
|
||||
|
||||
@@ -47,14 +37,6 @@ public class AuthenticateActivity extends AppCompatActivity
|
||||
Button logoutBtn = (Button)findViewById(R.id.logoutBtn);
|
||||
logoutBtn.setOnClickListener(onLogout);
|
||||
|
||||
Button createEventBtn = (Button)findViewById(R.id.createEventBtn);
|
||||
createEventBtn.setOnClickListener(onCreateEvent);
|
||||
Button createDatapointBtn = (Button)findViewById(R.id.createDatapointBtn);
|
||||
createDatapointBtn.setOnClickListener(onCreateDatapoint);
|
||||
Button getLocalEventsBtn = (Button)findViewById(R.id.getLocalEventsBtn);
|
||||
getLocalEventsBtn.setOnClickListener(onGetLocalEvents);
|
||||
|
||||
|
||||
mUnameEt = (EditText) findViewById(R.id.username);
|
||||
mPasswdEt = (EditText) findViewById(R.id.password);
|
||||
mWac = new WebApiConnection(this, this, this, this);
|
||||
@@ -140,56 +122,4 @@ public class AuthenticateActivity extends AppCompatActivity
|
||||
updateUi();
|
||||
}
|
||||
};
|
||||
View.OnClickListener onCreateEvent =
|
||||
new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
Log.v(TAG, "onCreateEvent");
|
||||
mWac.createEvent(10,new Date(),"eventDescription....");
|
||||
}
|
||||
};
|
||||
|
||||
View.OnClickListener onCreateDatapoint =
|
||||
new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
Log.v(TAG, "onCreateDatapoint - Pruning database");
|
||||
mLm.pruneLocalDb();
|
||||
}
|
||||
};
|
||||
|
||||
View.OnClickListener onCreateDatapointOld =
|
||||
new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
Log.v(TAG, "onCreateDatapoint");
|
||||
String jsonStr = "";
|
||||
JSONObject dataObj;
|
||||
try {
|
||||
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss");
|
||||
jsonStr = "{HR:70}";
|
||||
dataObj = new JSONObject(jsonStr);
|
||||
dataObj.put("dataTime", dateFormat.format(new Date()));
|
||||
Log.v(TAG, "Creating Datapoint...SdData="+dataObj.toString());
|
||||
mWac.createDatapoint(dataObj,10);
|
||||
} catch (JSONException e) {
|
||||
Log.v(TAG,"Error Creating JSON Object from string "+jsonStr);
|
||||
dataObj = null;
|
||||
jsonStr = null;
|
||||
}
|
||||
//m_status=false;
|
||||
// mWac.logout();
|
||||
//updateUi();
|
||||
}
|
||||
};
|
||||
|
||||
View.OnClickListener onGetLocalEvents =
|
||||
new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
Log.v(TAG, "onGetLocalEvents");
|
||||
mLm.uploadSdData();
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
@@ -32,6 +32,9 @@ public class LogManagerControlActivity extends AppCompatActivity {
|
||||
Button pruneBtn =
|
||||
(Button) findViewById(R.id.pruneDatabaseBtn);
|
||||
pruneBtn.setOnClickListener(onPruneBtn);
|
||||
Button remoteDbBtn =
|
||||
(Button) findViewById(R.id.view_remote_db_button);
|
||||
remoteDbBtn.setOnClickListener(onRemoteDbBtn);
|
||||
|
||||
|
||||
mLm = new LogManager(this);
|
||||
@@ -96,6 +99,16 @@ public class LogManagerControlActivity extends AppCompatActivity {
|
||||
mLm.pruneLocalDb();
|
||||
}
|
||||
};
|
||||
View.OnClickListener onRemoteDbBtn =
|
||||
new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
Log.v(TAG, "onRemoteDbBtn");
|
||||
Intent i;
|
||||
i =new Intent(mContext, RemoteDbActivity.class);
|
||||
startActivity(i);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
|
||||
@@ -0,0 +1,173 @@
|
||||
package uk.org.openseizuredetector;
|
||||
|
||||
//import androidx.appcompat.app.AppCompatActivity;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.os.CountDownTimer;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.webkit.WebSettings;
|
||||
import android.webkit.WebView;
|
||||
import android.widget.Button;
|
||||
import android.widget.TextView;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public class RemoteDbActivity extends AppCompatActivity {
|
||||
private String TAG = "RemoteDbActivity";
|
||||
private Context mContext;
|
||||
private UiTimer mUiTimer;
|
||||
private LogManager mLm;
|
||||
private WebView mWebView;
|
||||
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
Log.v(TAG, "onCreate()");
|
||||
super.onCreate(savedInstanceState);
|
||||
mContext = this;
|
||||
setContentView(R.layout.activity_remote_db);
|
||||
mLm= new LogManager(mContext);
|
||||
|
||||
Button authBtn =
|
||||
(Button) findViewById(R.id.auth_button);
|
||||
authBtn.setOnClickListener(onAuth);
|
||||
//Button pruneBtn =
|
||||
// (Button) findViewById(R.id.pruneDatabaseBtn);
|
||||
//pruneBtn.setOnClickListener(onPruneBtn);
|
||||
|
||||
mWebView = (WebView) findViewById(R.id.remote_db_webview);
|
||||
WebSettings webSettings = mWebView.getSettings();
|
||||
webSettings.setJavaScriptEnabled(true);
|
||||
|
||||
updateUi();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStart() {
|
||||
super.onStart();
|
||||
//startUiTimer();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
super.onPause();
|
||||
stopUiTimer();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
startUiTimer();
|
||||
mWebView.loadUrl("https://osdapi.ddns.net/api/events/", getAuthHeaders());
|
||||
}
|
||||
|
||||
private HashMap<String, String> getAuthHeaders() {
|
||||
HashMap<String, String> headersMap = new HashMap<>();
|
||||
String authToken = mLm.mWac.getStoredToken();
|
||||
headersMap.put("Authorization", "Token "+authToken);
|
||||
return (headersMap);
|
||||
}
|
||||
|
||||
|
||||
private void updateUi() {
|
||||
Log.v(TAG,"updateUi()");
|
||||
TextView tv;
|
||||
Button btn;
|
||||
// Local Database Information
|
||||
//tv = (TextView)findViewById(R.id.num_local_events_tv);
|
||||
//int eventCount = 0;
|
||||
//tv.setText(String.format("%d",eventCount));
|
||||
//tv = (TextView)findViewById(R.id.num_local_datapoints_tv);
|
||||
//int datapointsCount = 0;
|
||||
//tv.setText(String.format("%d",datapointsCount));
|
||||
|
||||
|
||||
|
||||
// Remote Database Information
|
||||
tv = (TextView)findViewById(R.id.authStatusTv);
|
||||
btn = (Button)findViewById(R.id.auth_button);
|
||||
if (mLm.mWac.isLoggedIn()) {
|
||||
tv.setText("Authenticated");
|
||||
btn.setText("Log Out");
|
||||
} else {
|
||||
tv.setText("NOT AUTHENTICATED");
|
||||
btn.setText("Log In");
|
||||
}
|
||||
}
|
||||
|
||||
View.OnClickListener onAuth =
|
||||
new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
Log.v(TAG, "onAuth");
|
||||
Intent i;
|
||||
i =new Intent(mContext, AuthenticateActivity.class);
|
||||
startActivity(i);
|
||||
}
|
||||
};
|
||||
View.OnClickListener onPruneBtn =
|
||||
new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
Log.v(TAG, "onPruneBtn");
|
||||
mLm.pruneLocalDb();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
* Start the timer that will upload data to the remote server after a given period.
|
||||
*/
|
||||
private void startUiTimer() {
|
||||
if (mUiTimer != null) {
|
||||
Log.v(TAG, "startRemoteLogTimer -timer already running - cancelling it");
|
||||
mUiTimer.cancel();
|
||||
mUiTimer = null;
|
||||
}
|
||||
Log.v(TAG, "startRemoteLogTimer() - starting RemoteLogTimer");
|
||||
mUiTimer =
|
||||
new UiTimer(1000, 1000);
|
||||
mUiTimer.start();
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Cancel the remote logging timer to prevent attempts to upload to remote database.
|
||||
*/
|
||||
public void stopUiTimer() {
|
||||
if (mUiTimer != null) {
|
||||
Log.v(TAG, "stopRemoteLogTimer(): cancelling Remote Log timer");
|
||||
mUiTimer.cancel();
|
||||
mUiTimer = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Upload recorded data to the remote database periodically.
|
||||
*/
|
||||
private class UiTimer extends CountDownTimer {
|
||||
public UiTimer(long startTime, long interval) {
|
||||
super(startTime, interval);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTick(long l) {
|
||||
// Do Nothing
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFinish() {
|
||||
Log.v(TAG, "UiTimer - onFinish - Updating UI");
|
||||
updateUi();
|
||||
// Restart this timer.
|
||||
start();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user