From 8a2bbd1cfb0173c5ac6d149d4da25b92e2e18b6c Mon Sep 17 00:00:00 2001 From: Graham Jones Date: Wed, 5 Jan 2022 18:03:46 +0000 Subject: [PATCH] Added LogManagerControl activity to view and control the status of the LogManager --- app/src/main/AndroidManifest.xml | 2 +- .../org/openseizuredetector/LogManager.java | 71 ++++++-- .../LogManagerControlActivity.java | 152 ++++++++++++++++++ .../org/openseizuredetector/MainActivity.java | 2 +- .../openseizuredetector/WebApiConnection.java | 4 +- .../layout/activity_log_manager_control.xml | 98 +++++++++++ .../main/res/menu/main_activity_actions.xml | 2 +- app/src/main/res/values/strings.xml | 4 + 8 files changed, 321 insertions(+), 14 deletions(-) create mode 100644 app/src/main/java/uk/org/openseizuredetector/LogManagerControlActivity.java create mode 100644 app/src/main/res/layout/activity_log_manager_control.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 6fe8fcc..043ea1a 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -64,7 +64,7 @@ android:name=".SdServer" android:exported="false" /> - + mDatapointsToUploadList; @@ -89,12 +88,17 @@ public class LogManager implements AuthCallbackInterface, EventCallbackInterface public LogManager(Context context) { Log.d(TAG,"LogManger Constructor"); - mLogRemote = false; - mLogRemoteMobile = false; - //mOSDUrl = null; mContext = context; - Handler handler = new Handler(); + + SharedPreferences prefs; + prefs = PreferenceManager.getDefaultSharedPreferences(mContext); + mLogRemote = (prefs.getBoolean("LogDataRemote", false)); + Log.v(TAG,"mLogRemote="+mLogRemote); + mLogRemoteMobile = (prefs.getBoolean("LogDataRemoteMobile", false)); + Log.v(TAG,"mLogRemoteMobile="+mLogRemoteMobile); + + mUtil = new OsdUtil(mContext, handler); openDb(); mWac = new WebApiConnection(mContext, this, this, this); @@ -346,6 +350,51 @@ public class LogManager implements AuthCallbackInterface, EventCallbackInterface return (recordId); } + /** + * Return the number of events stored in the local database + */ + public int getLocalEventsCount(boolean includeWarnings) { + Log.v(TAG, "getLocalEventsCount()"); + String SQLStr = "SQLStr"; + String statusListStr; + + if (includeWarnings) { + statusListStr ="1,2,3,5"; // Warning, Alarm, Fall, Manual Alarm + } else { + statusListStr = "2,3,5"; // Alarm, Fall, Manual Alarm + } + try { + SQLStr = "SELECT * from "+ mDbTableName + " where Status in ("+statusListStr+");"; + Cursor resultSet = mOSDDb.getWritableDatabase().rawQuery(SQLStr,null); + resultSet.moveToFirst(); + return (resultSet.getCount()); + } catch (SQLException e) { + Log.e(TAG,"getLocalEventsCount(): Error selecting Data: " + e.toString()); + Log.e(TAG,"SQLStr was "+SQLStr); + return(0); + } + } + + /** + * Return the number of datapoints stored in the local database + */ + public int getLocalDatapointsCount() { + Log.v(TAG, "getLocalDatapointsCount()"); + String SQLStr = "SQLStr"; + String statusListStr; + + try { + SQLStr = "SELECT * from "+ mDbTableName + ";"; + Cursor resultSet = mOSDDb.getWritableDatabase().rawQuery(SQLStr,null); + resultSet.moveToFirst(); + return (resultSet.getCount()); + } catch (SQLException e) { + Log.e(TAG,"getLocalDatapointsCount(): Error selecting Data: " + e.toString()); + Log.e(TAG,"SQLStr was "+SQLStr); + return(0); + } + } + public void writeToRemoteServer() { Log.v(TAG,"writeToRemoteServer()"); @@ -522,6 +571,8 @@ public class LogManager implements AuthCallbackInterface, EventCallbackInterface uploadNextDatapoint(); } + // takes the next datapoint of the list mDatapointsToUploadList and uploads it to the remote server. + // datapointCallback is called when the upload is complete. public void uploadNextDatapoint() { Log.v(TAG,"uploadDatapoint()"); if (mDatapointsToUploadList.size() > 0) { @@ -556,7 +607,9 @@ public class LogManager implements AuthCallbackInterface, EventCallbackInterface } - + /** + * close() - shut down the logging system + */ public void close() { mOSDDb.close(); stopRemoteLogTimer(); diff --git a/app/src/main/java/uk/org/openseizuredetector/LogManagerControlActivity.java b/app/src/main/java/uk/org/openseizuredetector/LogManagerControlActivity.java new file mode 100644 index 0000000..8d3e38e --- /dev/null +++ b/app/src/main/java/uk/org/openseizuredetector/LogManagerControlActivity.java @@ -0,0 +1,152 @@ +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.widget.Button; +import android.widget.TextView; + +public class LogManagerControlActivity extends AppCompatActivity { + private String TAG = "LogManagerControlActivity"; + private LogManager mLm; + private Context mContext; + private UiTimer mUiTimer; + + + @Override + protected void onCreate(Bundle savedInstanceState) { + Log.v(TAG, "onCreate()"); + super.onCreate(savedInstanceState); + mContext = this; + setContentView(R.layout.activity_log_manager_control); + + Button authBtn = + (Button) findViewById(R.id.auth_button); + authBtn.setOnClickListener(onAuth); + Button pruneBtn = + (Button) findViewById(R.id.pruneDatabaseBtn); + pruneBtn.setOnClickListener(onPruneBtn); + + + mLm = new LogManager(this); + updateUi(); + } + + @Override + protected void onStart() { + super.onStart(); + startUiTimer(); + } + + @Override + protected void onPause() { + super.onPause(); + stopUiTimer(); + } + + + + 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 = mLm.getLocalEventsCount(true); + tv.setText(String.format("%d",eventCount)); + tv = (TextView)findViewById(R.id.num_local_datapoints_tv); + int datapointsCount = mLm.getLocalDatapointsCount(); + 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(); + } + + } + + +} \ No newline at end of file diff --git a/app/src/main/java/uk/org/openseizuredetector/MainActivity.java b/app/src/main/java/uk/org/openseizuredetector/MainActivity.java index f21bb87..a23f8e4 100644 --- a/app/src/main/java/uk/org/openseizuredetector/MainActivity.java +++ b/app/src/main/java/uk/org/openseizuredetector/MainActivity.java @@ -288,7 +288,7 @@ public class MainActivity extends AppCompatActivity { try { Intent intent = new Intent( MainActivity.this, - LogManagerActivity.class); + LogManagerControlActivity.class); this.startActivity(intent); } catch (Exception ex) { Log.i(TAG, "exception starting log manager activity " + ex.toString()); diff --git a/app/src/main/java/uk/org/openseizuredetector/WebApiConnection.java b/app/src/main/java/uk/org/openseizuredetector/WebApiConnection.java index 005cbd0..2ce534b 100644 --- a/app/src/main/java/uk/org/openseizuredetector/WebApiConnection.java +++ b/app/src/main/java/uk/org/openseizuredetector/WebApiConnection.java @@ -119,7 +119,7 @@ public class WebApiConnection { return authToken; } - private boolean isLoggedIn() { + public boolean isLoggedIn() { String authToken = getStoredToken(); Log.v(TAG, "isLoggedIn(): token=" + authToken); if (authToken == null || authToken.length() == 0) { @@ -142,7 +142,7 @@ public class WebApiConnection { // 7: Other Seizure // 9: Other Medical Issue 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()"); String urlStr = mUrlBase + "/api/events/"; Log.v(TAG, "urlStr=" + urlStr); final String authtoken = getStoredToken(); diff --git a/app/src/main/res/layout/activity_log_manager_control.xml b/app/src/main/res/layout/activity_log_manager_control.xml new file mode 100644 index 0000000..68d0e88 --- /dev/null +++ b/app/src/main/res/layout/activity_log_manager_control.xml @@ -0,0 +1,98 @@ + + + + + + + + + + + + +