From 5d9f920b2c8c4dfa68f8acf5c7d7a6f3aa5fa195 Mon Sep 17 00:00:00 2001 From: Graham Jones Date: Wed, 5 Jan 2022 19:49:23 +0000 Subject: [PATCH] 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. --- app/src/main/AndroidManifest.xml | 1 + .../AuthenticateActivity.java | 70 ------- .../LogManagerControlActivity.java | 13 ++ .../openseizuredetector/RemoteDbActivity.java | 173 ++++++++++++++++++ .../main/res/layout/activity_authenticate.xml | 3 +- .../layout/activity_log_manager_control.xml | 5 + .../main/res/layout/activity_remote_db.xml | 44 +++++ app/src/main/res/values/strings.xml | 1 + 8 files changed, 239 insertions(+), 71 deletions(-) create mode 100644 app/src/main/java/uk/org/openseizuredetector/RemoteDbActivity.java create mode 100644 app/src/main/res/layout/activity_remote_db.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 043ea1a..35ac3a2 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -65,6 +65,7 @@ android:exported="false" /> + getAuthHeaders() { + HashMap 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(); + } + + } + + +} \ No newline at end of file diff --git a/app/src/main/res/layout/activity_authenticate.xml b/app/src/main/res/layout/activity_authenticate.xml index d508fd7..2540c24 100644 --- a/app/src/main/res/layout/activity_authenticate.xml +++ b/app/src/main/res/layout/activity_authenticate.xml @@ -93,6 +93,7 @@ android:layout_weight="1" android:text="@string/logout" /> + \ No newline at end of file diff --git a/app/src/main/res/layout/activity_log_manager_control.xml b/app/src/main/res/layout/activity_log_manager_control.xml index 68d0e88..0068922 100644 --- a/app/src/main/res/layout/activity_log_manager_control.xml +++ b/app/src/main/res/layout/activity_log_manager_control.xml @@ -77,6 +77,11 @@ android:layout_height="wrap_content" android:text="@string/authenticate" /> +