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:
Graham Jones
2022-01-05 19:49:23 +00:00
parent 58a62d1469
commit 5d9f920b2c
8 changed files with 239 additions and 71 deletions

View File

@@ -65,6 +65,7 @@
android:exported="false" /> android:exported="false" />
<activity android:name=".LogManagerControlActivity" /> <activity android:name=".LogManagerControlActivity" />
<activity android:name=".RemoteDbActivity" />
<receiver <receiver
android:name=".BootBroadcastReceiver" android:name=".BootBroadcastReceiver"

View File

@@ -1,9 +1,7 @@
package uk.org.openseizuredetector; package uk.org.openseizuredetector;
//import androidx.appcompat.app.AppCompatActivity;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.os.Bundle; import android.os.Bundle;
@@ -14,20 +12,12 @@ import android.widget.EditText;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.TextView; 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 public class AuthenticateActivity extends AppCompatActivity
implements AuthCallbackInterface, EventCallbackInterface, DatapointCallbackInterface { implements AuthCallbackInterface, EventCallbackInterface, DatapointCallbackInterface {
private String TAG = "AuthenticateActivity"; private String TAG = "AuthenticateActivity";
private Context mContext; private Context mContext;
private EditText mUnameEt; private EditText mUnameEt;
private EditText mPasswdEt; private EditText mPasswdEt;
private boolean mIsLoggedIn;
private WebApiConnection mWac; private WebApiConnection mWac;
private LogManager mLm; private LogManager mLm;
@@ -47,14 +37,6 @@ public class AuthenticateActivity extends AppCompatActivity
Button logoutBtn = (Button)findViewById(R.id.logoutBtn); Button logoutBtn = (Button)findViewById(R.id.logoutBtn);
logoutBtn.setOnClickListener(onLogout); 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); mUnameEt = (EditText) findViewById(R.id.username);
mPasswdEt = (EditText) findViewById(R.id.password); mPasswdEt = (EditText) findViewById(R.id.password);
mWac = new WebApiConnection(this, this, this, this); mWac = new WebApiConnection(this, this, this, this);
@@ -140,56 +122,4 @@ public class AuthenticateActivity extends AppCompatActivity
updateUi(); 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();
}
};
} }

View File

@@ -32,6 +32,9 @@ public class LogManagerControlActivity extends AppCompatActivity {
Button pruneBtn = Button pruneBtn =
(Button) findViewById(R.id.pruneDatabaseBtn); (Button) findViewById(R.id.pruneDatabaseBtn);
pruneBtn.setOnClickListener(onPruneBtn); pruneBtn.setOnClickListener(onPruneBtn);
Button remoteDbBtn =
(Button) findViewById(R.id.view_remote_db_button);
remoteDbBtn.setOnClickListener(onRemoteDbBtn);
mLm = new LogManager(this); mLm = new LogManager(this);
@@ -96,6 +99,16 @@ public class LogManagerControlActivity extends AppCompatActivity {
mLm.pruneLocalDb(); 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);
}
};
/* /*

View File

@@ -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();
}
}
}

View File

@@ -93,6 +93,7 @@
android:layout_weight="1" android:layout_weight="1"
android:text="@string/logout" /> android:text="@string/logout" />
</LinearLayout> </LinearLayout>
<!--
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
@@ -125,7 +126,7 @@
android:text="getLocalEvents" /> android:text="getLocalEvents" />
</LinearLayout> </LinearLayout>
-->
</LinearLayout> </LinearLayout>
</LinearLayout> </LinearLayout>

View File

@@ -77,6 +77,11 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/authenticate" /> android:text="@string/authenticate" />
</LinearLayout> </LinearLayout>
<Button
android:id="@+id/view_remote_db_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/view_remote_db_btn" />
<!-- <!--
<ListView <ListView

View File

@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="uk.org.openseizuredetector.LogManager">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/remote_database"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/not_authenticated"
android:id="@+id/authStatusTv"
/>
<Button
android:id="@+id/auth_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/authenticate" />
</LinearLayout>
<WebView
android:id="@+id/remote_db_webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>

View File

@@ -298,4 +298,5 @@
<string name="remote_database">Remote Database</string> <string name="remote_database">Remote Database</string>
<string name="num_local_events">Number of Stored Events: </string> <string name="num_local_events">Number of Stored Events: </string>
<string name="num_local_datapoints">"Number of Stored Datapoints: "</string> <string name="num_local_datapoints">"Number of Stored Datapoints: "</string>
<string name="view_remote_db_btn">View Remote DB Data</string>
</resources> </resources>