From 3ad112cce7347aac989baf0318be538c1f36648d Mon Sep 17 00:00:00 2001 From: Graham Jones Date: Mon, 8 Apr 2024 20:34:30 +0100 Subject: [PATCH] Fixed notificaton problems on Android 13, tidied up start-up sequence. Added a 'restart' menu option. --- CHANGELOG.md | 4 +- app/src/main/AndroidManifest.xml | 2 +- .../org/openseizuredetector/MainActivity.java | 36 +++++++++------- .../openseizuredetector/MainActivity2.java | 41 ++++++++++--------- .../org/openseizuredetector/PrefActivity.java | 37 +++++++++-------- .../openseizuredetector/StartupActivity.java | 24 +++++++++++ .../main/res/menu/main_activity_actions.xml | 5 +++ app/src/main/res/values/strings.xml | 1 + 8 files changed, 97 insertions(+), 53 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5635f80..a856cb8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ OpenSeizureDetector Android App - Change Log ============================================ - + V4.2.6 - Fixed problem with notifications in Android 13 + - Improved start-up checks for permissions + - Improved system re-start after changing settings (but still not perfect!) V4.2.5 - Set BLE device time if the characteristic is available. V4.2.4 - Added checks and a FAULT condition for Bluetooth errors in Bluetooth Data Source V4.2.3 - Uses 3d accelerometer data to calculate magnitude if vector magnitude is not sent from data source. diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index f7528a1..1bf28fe 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -2,7 +2,7 @@ + android:versionName="4.2.6b"> diff --git a/app/src/main/java/uk/org/openseizuredetector/MainActivity.java b/app/src/main/java/uk/org/openseizuredetector/MainActivity.java index 2816289..71f9322 100644 --- a/app/src/main/java/uk/org/openseizuredetector/MainActivity.java +++ b/app/src/main/java/uk/org/openseizuredetector/MainActivity.java @@ -86,6 +86,7 @@ public class MainActivity extends AppCompatActivity { private Intent sdServerIntent; final Handler serverStatusHandler = new Handler(); + final Handler mHandler = new Handler(); Messenger messenger = new Messenger(new ResponseHandler()); Timer mUiTimer; private Context mContext; @@ -275,22 +276,27 @@ public class MainActivity extends AppCompatActivity { mConnection.mSdServer.acceptAlarm(); } return true; - case R.id.action_start_stop: + case R.id.action_exit: // Respond to the start/stop server menu item. - Log.i(TAG, "action_sart_stop"); - if (mConnection.mBound) { - Log.i(TAG, "Stopping Server"); - mUtil.unbindFromServer(getApplicationContext(), mConnection); - stopServer(); - // we exit the activity when the server is stopped to make it consistent with MainActivity2 - finish(); - } else { - Log.i(TAG, "Starting Server"); - startServer(); - // and bind to it so we can see its data - Log.i(TAG, "Binding to Server"); - mUtil.bindToServer(getApplicationContext(), mConnection); - } + Log.i(TAG, "action_exit: Stopping Server"); + mUtil.unbindFromServer(getApplicationContext(), mConnection); + stopServer(); + // We exit this activity as a crude way of forcing the fragments to disconnect from the server + // so that the server exits properly - otherwise we end up with multiple threads running. + // FIXME - tell the threads to unbind from the serer before calling stopServer as an alternative. + finish(); + return true; + case R.id.action_start_stop: + Log.i(TAG, "action_start_stop: restarting server"); + mUtil.showToast("Stopping Background Service...."); + mUtil.stopServer(); + // Wait 1 second to give the server chance to shutdown, then re-start it + mHandler.postDelayed(new Runnable() { + public void run() { + mUtil.showToast("Re-Starting Background Service..."); + mUtil.startServer(); + } + }, 1000); return true; /* fault beep test does not work with fault timer, so disable test option. case R.id.action_test_fault_beep: diff --git a/app/src/main/java/uk/org/openseizuredetector/MainActivity2.java b/app/src/main/java/uk/org/openseizuredetector/MainActivity2.java index fdaabb1..a38f523 100644 --- a/app/src/main/java/uk/org/openseizuredetector/MainActivity2.java +++ b/app/src/main/java/uk/org/openseizuredetector/MainActivity2.java @@ -46,6 +46,7 @@ public class MainActivity2 extends AppCompatActivity { private OsdUtil mUtil; private SdServiceConnection mConnection; final Handler serverStatusHandler = new Handler(); + private Handler mHandler = new Handler(); @Override protected void onCreate(Bundle savedInstanceState) { @@ -65,8 +66,7 @@ public class MainActivity2 extends AppCompatActivity { mConnection = new SdServiceConnection(getApplicationContext()); mUtil.writeToSysLogFile("MainActivity2.onCreate()"); mContext = this; - - + mHandler = new Handler(); } /** @@ -194,24 +194,27 @@ public class MainActivity2 extends AppCompatActivity { mConnection.mSdServer.acceptAlarm(); } return true; - case R.id.action_start_stop: + case R.id.action_exit: // Respond to the start/stop server menu item. - Log.i(TAG, "action_start_stop"); - if (mConnection.mBound) { - Log.i(TAG, "Stopping Server"); - mUtil.unbindFromServer(getApplicationContext(), mConnection); - stopServer(); - // We exit this activity as a crude way of forcing the fragments to disconnect from the server - // so that the server exits properly - otherwise we end up with multiple threads running. - // FIXME - tell the threads to unbind from the serer before calling stopServer as an alternative. - finish(); - } else { - Log.i(TAG, "Starting Server"); - startServer(); - // and bind to it so we can see its data - Log.i(TAG, "Binding to Server"); - mUtil.bindToServer(getApplicationContext(), mConnection); - } + Log.i(TAG, "action_exit: Stopping Server"); + mUtil.unbindFromServer(getApplicationContext(), mConnection); + stopServer(); + // We exit this activity as a crude way of forcing the fragments to disconnect from the server + // so that the server exits properly - otherwise we end up with multiple threads running. + // FIXME - tell the threads to unbind from the serer before calling stopServer as an alternative. + finish(); + return true; + case R.id.action_start_stop: + Log.i(TAG, "action_start_stop: restarting server"); + mUtil.showToast("Stopping Background Service...."); + mUtil.stopServer(); + // Wait 1 second to give the server chance to shutdown, then re-start it + mHandler.postDelayed(new Runnable() { + public void run() { + mUtil.showToast("Re-Starting Background Service..."); + mUtil.startServer(); + } + }, 1000); return true; case R.id.action_test_alarm_beep: Log.i(TAG, "action_test_alarm_beep"); diff --git a/app/src/main/java/uk/org/openseizuredetector/PrefActivity.java b/app/src/main/java/uk/org/openseizuredetector/PrefActivity.java index 72e31f5..8248876 100644 --- a/app/src/main/java/uk/org/openseizuredetector/PrefActivity.java +++ b/app/src/main/java/uk/org/openseizuredetector/PrefActivity.java @@ -160,6 +160,7 @@ public class PrefActivity extends PreferenceActivity implements SharedPreference Log.i(TAG, "OnSharedPreferenceChanged(): SMS Alarm disabled so do not need permissions"); } } + // If we have changed the data source, re-start the whole system if (s.equals("DataSource")) { Log.i(TAG, "onSharedPreferenceChanged(): Data Source Changed - Restarting start-up activity to check permissions"); mUtil.showToast("Restarting OpenSeizureDetector"); @@ -176,25 +177,27 @@ public class PrefActivity extends PreferenceActivity implements SharedPreference return; } }, 1000); - } - // For all other preference changes we just restart SdServer so it is not as alarming for the user! - //mUtil.showToast("Setting " + s + " Changed - restarting server"); - mUtil.showToast("Restarting OpenSeizureDetector"); - mPrefChanged = true; - mUtil.stopServer(); - // Wait 1 second to give the server chance to shutdown, then re-start it - mHandler.postDelayed(new Runnable() { - public void run() { - mUtil.startServer(); + return; + } else { + // For all other preference changes we just restart SdServer so it is not as alarming for the user! + //mUtil.showToast("Setting " + s + " Changed - restarting server"); + mUtil.showToast("Stopping Background Service..."); + mPrefChanged = true; + mUtil.stopServer(); + // Wait 1 second to give the server chance to shutdown, then re-start it + mHandler.postDelayed(new Runnable() { + public void run() { + mUtil.showToast("Re-Starting Background Service..."); + mUtil.startServer(); + } + }, 1000); + + if (s.equals("advancedMode")) { + Log.i(TAG, "Re-starting PrefActivity to refresh list"); + startActivity(getIntent()); + finish(); } - }, 1000); - - if (s.equals("DataSource") || s.equals("advancedMode")) { - Log.i(TAG, "Re-starting PrefActivity to refresh list"); - startActivity(getIntent()); - finish(); } - } @Override diff --git a/app/src/main/java/uk/org/openseizuredetector/StartupActivity.java b/app/src/main/java/uk/org/openseizuredetector/StartupActivity.java index d8cf51d..6ece997 100644 --- a/app/src/main/java/uk/org/openseizuredetector/StartupActivity.java +++ b/app/src/main/java/uk/org/openseizuredetector/StartupActivity.java @@ -124,6 +124,14 @@ public class StartupActivity extends AppCompatActivity { private String mBleDeviceAddr; private String mBleDeviceName; + private final int MODE_INIT = 0; + private final int MODE_SHUTDOWN_SERVER = 1; + private final int MODE_START_SERVER = 2; + private final int MODE_CONNECT_SERVER = 3; + private final int MODE_WATCH_RUNNING = 4; + private final int MODE_SD_DATA_OK = 5; + private int mMode = MODE_INIT; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -221,10 +229,12 @@ public class StartupActivity extends AppCompatActivity { if (mUtil.isServerRunning()) { + mMode = MODE_SHUTDOWN_SERVER; Log.i(TAG, "onStart() - server running - stopping it - isServerRunning=" + mUtil.isServerRunning()); mUtil.writeToSysLogFile("StartupActivity.onStart() - server already running - stopping it."); mUtil.stopServer(); } else { + mMode = MODE_START_SERVER; Log.i(TAG, "onStart() - server not running - isServerRunning=" + mUtil.isServerRunning()); } @@ -375,13 +385,27 @@ public class StartupActivity extends AppCompatActivity { } if (allOk) { + tv = (TextView) findViewById(R.id.textItem1); + pb = (ProgressBar) findViewById(R.id.progressBar1); + if (!mUtil.isServerRunning()) { mUtil.writeToSysLogFile("StartupActivity.onStart() - starting server - isServerRunning=" + mUtil.isServerRunning()); Log.i(TAG, "onStart() - starting server -isServerRunning=" + mUtil.isServerRunning()); mUtil.startServer(); mBindInProgress = false; allOk = false; + tv.setText("Starting Server"); + tv.setBackgroundColor(alarmColour); + tv.setTextColor(alarmTextColour); + pb.setIndeterminateDrawable(getResources().getDrawable(R.drawable.start_server)); + pb.setProgressDrawable(getResources().getDrawable(R.drawable.start_server)); + mMode = MODE_START_SERVER; } else { + tv.setText(getString(R.string.ServerRunningOK)); + tv.setBackgroundColor(okColour); + tv.setTextColor(okTextColour); + pb.setIndeterminateDrawable(getResources().getDrawable(R.drawable.start_server)); + pb.setProgressDrawable(getResources().getDrawable(R.drawable.start_server)); if (mBindInProgress) { Log.i(TAG,"Waiting to bind to server"); } else { diff --git a/app/src/main/res/menu/main_activity_actions.xml b/app/src/main/res/menu/main_activity_actions.xml index eb11bb2..3637529 100644 --- a/app/src/main/res/menu/main_activity_actions.xml +++ b/app/src/main/res/menu/main_activity_actions.xml @@ -12,6 +12,11 @@ android:id="@+id/action_start_stop" android:icon="@drawable/stop_server" app:showAsAction="never|withText" + android:title="@string/restart_server" /> + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 7da8239..6088ff6 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -574,4 +574,5 @@ Bluetooth Permissions Not Granted Bluetooth Permissions Required Bluetooth permissions are required to communicate with the bluetooth (BLE) data source + Restart Server