diff --git a/app/build.gradle b/app/build.gradle index 44522d9..061d7e7 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -7,7 +7,7 @@ android { defaultConfig { applicationId "uk.org.openseizuredetector" minSdkVersion 23 // Android 6 - targetSdkVersion 33 // Android 13 + targetSdkVersion 33 // Android 13 = 33 multiDexEnabled true } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index af13d8d..663a33b 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -2,8 +2,9 @@ + android:versionName="4.2.6"> + diff --git a/app/src/main/java/uk/org/openseizuredetector/BLEScanActivity.java b/app/src/main/java/uk/org/openseizuredetector/BLEScanActivity.java index 7381736..7915df6 100644 --- a/app/src/main/java/uk/org/openseizuredetector/BLEScanActivity.java +++ b/app/src/main/java/uk/org/openseizuredetector/BLEScanActivity.java @@ -275,6 +275,10 @@ public class BLEScanActivity extends ListActivity { SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences((this)); Log.v(TAG, "Check of saved values - Name=" + SP.getString("BLE_Device_Name", "NOT SET") + ", Addr=" + SP.getString("BLE_Device_Addr", "NOT SET")); + Log.i(TAG, "Restarting start-up activity so change takes effect"); + Intent i; + i = new Intent(this, StartupActivity.class); + startActivity(i); finish(); } diff --git a/app/src/main/java/uk/org/openseizuredetector/PrefActivity.java b/app/src/main/java/uk/org/openseizuredetector/PrefActivity.java index a99ff18..72e31f5 100644 --- a/app/src/main/java/uk/org/openseizuredetector/PrefActivity.java +++ b/app/src/main/java/uk/org/openseizuredetector/PrefActivity.java @@ -146,8 +146,9 @@ public class PrefActivity extends PreferenceActivity implements SharedPreference // if we have enabled the SMS alarm, we may need extra permissions approving. This is handled in // StartUpActivity, so we exit this activity and start start-up activity. - if (s.equals("SMSAlarm")) { + if (s.equals("SMSAlarm")) { if (sharedPreferences.getBoolean("SMSAlarm", false) == true) { + mUtil.showToast("Restarting OpenSeizureDetector"); Log.i(TAG, "onSharedPreferenceChanged(): SMS Alarm Enabled - Restarting start-up activity to check permissions"); Intent i; i = new Intent(this, StartupActivity.class); @@ -159,21 +160,39 @@ public class PrefActivity extends PreferenceActivity implements SharedPreference Log.i(TAG, "OnSharedPreferenceChanged(): SMS Alarm disabled so do not need permissions"); } } + if (s.equals("DataSource")) { + Log.i(TAG, "onSharedPreferenceChanged(): Data Source Changed - Restarting start-up activity to check permissions"); + mUtil.showToast("Restarting OpenSeizureDetector"); + mUtil.stopServer(); + // Wait 1 second to give the server chance to shutdown, then re-start it + mHandler.postDelayed(new Runnable() { + public void run() { + Intent i; + Log.i(TAG, "onSharedPreferenceChanged(): Data Source Changed - Restarting start-up activity to check permissions"); + i = new Intent(getApplicationContext(), StartupActivity.class); + startActivity(i); + Log.i(TAG, "onSharedPreferenceChanged() - finishing PrefActivity"); + finish(); + 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 0.1 second to give the server chance to shutdown, then re-start it + // Wait 1 second to give the server chance to shutdown, then re-start it mHandler.postDelayed(new Runnable() { public void run() { mUtil.startServer(); } - }, 100); + }, 1000); if (s.equals("DataSource") || s.equals("advancedMode")) { Log.i(TAG, "Re-starting PrefActivity to refresh list"); - finish(); startActivity(getIntent()); + finish(); } } diff --git a/app/src/main/java/uk/org/openseizuredetector/StartupActivity.java b/app/src/main/java/uk/org/openseizuredetector/StartupActivity.java index 262dd82..d8cf51d 100644 --- a/app/src/main/java/uk/org/openseizuredetector/StartupActivity.java +++ b/app/src/main/java/uk/org/openseizuredetector/StartupActivity.java @@ -34,7 +34,6 @@ import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.graphics.Color; import android.net.Uri; -import android.os.Build; import android.os.Bundle; import android.os.Handler; import android.os.PowerManager; @@ -53,7 +52,6 @@ import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; -import androidx.core.text.HtmlCompat; import com.rohitss.uceh.UCEHandler; @@ -89,10 +87,12 @@ public class StartupActivity extends AppCompatActivity { private boolean mLocationPermissions2Requested; private boolean mSmsPermissionsRequested; private boolean mPermissionsRequested; + private boolean mBindInProgress = false; public final String[] REQUIRED_PERMISSIONS = { //Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.WAKE_LOCK, + Manifest.permission.POST_NOTIFICATIONS, }; public final String[] SMS_PERMISSIONS_1 = { @@ -112,6 +112,17 @@ public class StartupActivity extends AppCompatActivity { Manifest.permission.ACCESS_BACKGROUND_LOCATION, }; + public final String[] BT_PERMISSIONS = { + Manifest.permission.ACCESS_FINE_LOCATION, + Manifest.permission.BLUETOOTH, + Manifest.permission.BLUETOOTH_SCAN, + Manifest.permission.BLUETOOTH_ADMIN, + Manifest.permission.BLUETOOTH_CONNECT, + }; + private boolean mBTPermissionsRequested = false; + private String mSdDataSourceName; + private String mBleDeviceAddr; + private String mBleDeviceName; @Override protected void onCreate(Bundle savedInstanceState) { @@ -196,10 +207,17 @@ public class StartupActivity extends AppCompatActivity { // Display the DataSource name SharedPreferences SP = PreferenceManager .getDefaultSharedPreferences(getBaseContext()); - ; - String dataSourceName = SP.getString("DataSource", "Pebble"); + + mSdDataSourceName = SP.getString("DataSource", "Pebble"); + mBleDeviceAddr = SP.getString("BLE_Device_Addr", ""); + mBleDeviceName = SP.getString("BLE_Device_Name", ""); tv = (TextView) findViewById(R.id.dataSourceTextView); - tv.setText(String.format("%s = %s", getString(R.string.DataSource), dataSourceName)); + + if (mSdDataSourceName.equals("BLE")) { + tv.setText(String.format("%s = %s (%s - %s)", getString(R.string.DataSource), mSdDataSourceName, mBleDeviceName, mBleDeviceAddr)); + } else { + tv.setText(String.format("%s = %s", getString(R.string.DataSource), mSdDataSourceName)); + } if (mUtil.isServerRunning()) { @@ -209,18 +227,6 @@ public class StartupActivity extends AppCompatActivity { } else { Log.i(TAG, "onStart() - server not running - isServerRunning=" + mUtil.isServerRunning()); } - // Wait 0.1 second to give the server chance to shutdown in case we have just shut it down below, then start it - mHandler.postDelayed(new Runnable() { - public void run() { - mUtil.writeToSysLogFile("StartupActivity.onStart() - starting server after delay - isServerRunning=" + mUtil.isServerRunning()); - Log.i(TAG, "onStart() - starting server after delay -isServerRunning=" + mUtil.isServerRunning()); - mUtil.startServer(); - // Bind to the service. - Log.i(TAG, "onStart() - binding to server"); - mUtil.writeToSysLogFile("StartupActivity.onStart() - binding to server"); - mUtil.bindToServer(getApplicationContext(), mConnection); - } - }, 100); // Check power management settings PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE); @@ -299,7 +305,33 @@ public class StartupActivity extends AppCompatActivity { tv = (TextView) findViewById(R.id.textItem1); pb = (ProgressBar) findViewById(R.id.progressBar1); if (arePermissionsOK()) { - if (smsAlarmsActive && !areSMSPermissions1OK()) { + Log.i(TAG,"arePermissionsOK=true"); + Log.i(TAG,"mSdDataSourceName = "+ mSdDataSourceName); + tv.setText(getString(R.string.AppPermissionsOk)); + tv.setBackgroundColor(okColour); + tv.setTextColor(okTextColour); + pb.setIndeterminateDrawable(getResources().getDrawable(R.drawable.start_server)); + pb.setProgressDrawable(getResources().getDrawable(R.drawable.start_server)); + + if (mSdDataSourceName.equals("BLE")) { + if (!areBTPermissionsOK()) { + Log.i(TAG, "Bluetooth permissions NOT OK"); + tv.setText(getString(R.string.BTPermissionWarning)); + tv.setBackgroundColor(alarmColour); + tv.setTextColor(alarmTextColour); + //pb.setIndeterminateDrawable(getResources().getDrawable(R.drawable.start_server)); + //pb.setProgressDrawable(getResources().getDrawable(R.drawable.start_server)); + requestBTPermissions(); + allOk = false; + } else if (mBleDeviceAddr.equals("")) { + Log.i(TAG,"BLE daa source selected, but no device address specified"); + Intent i; + i = new Intent(getApplicationContext(), BLEScanActivity.class); + startActivity(i); + finish(); + return; + } + } else if (smsAlarmsActive && !areSMSPermissions1OK()) { Log.i(TAG, "SMS permissions NOT OK"); tv.setText(getString(R.string.SmsPermissionWarning)); tv.setBackgroundColor(alarmColour); @@ -322,12 +354,6 @@ public class StartupActivity extends AppCompatActivity { tv.setTextColor(alarmTextColour); requestLocationPermissions2(); allOk = false; - } else { - tv.setText(getString(R.string.AppPermissionsOk)); - tv.setBackgroundColor(okColour); - tv.setTextColor(okTextColour); - pb.setIndeterminateDrawable(getResources().getDrawable(R.drawable.start_server)); - pb.setProgressDrawable(getResources().getDrawable(R.drawable.start_server)); } } else { tv.setText(getString(R.string.AppPermissionsWarning)); @@ -348,6 +374,27 @@ public class StartupActivity extends AppCompatActivity { allOk = false; } + if (allOk) { + 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; + } else { + if (mBindInProgress) { + Log.i(TAG,"Waiting to bind to server"); + } else { + Log.i(TAG, "ServerStatusRunnable() - not starting server - allOk=" + allOk + ", isServerRunning()=" + mUtil.isServerRunning()); + // Bind to the service. + Log.i(TAG, "ServerStatusRunnable() - binding to server"); + mUtil.writeToSysLogFile("StartupActivity.onStart() - binding to server"); + mUtil.bindToServer(getApplicationContext(), mConnection); + mBindInProgress = true; + } + } + } + // Are we Bound to the Service tv = (TextView) findViewById(R.id.textItem2); pb = (ProgressBar) findViewById(R.id.progressBar2); @@ -442,6 +489,7 @@ public class StartupActivity extends AppCompatActivity { startActivity(intent); mStartedMainActivity = true; finish(); + return; } catch (Exception ex) { mStartedMainActivity = false; Log.e(TAG, "exception starting main activity " + ex.toString()); @@ -677,6 +725,21 @@ public class StartupActivity extends AppCompatActivity { return allOk; } + public boolean areBTPermissionsOK() { + boolean allOk = true; + Log.d(TAG, "areBTPermissions OK()"); + for (int i = 0; i < BT_PERMISSIONS.length; i++) { + if (ContextCompat.checkSelfPermission(this, BT_PERMISSIONS[i]) + != PackageManager.PERMISSION_GRANTED) { + Log.d(TAG, BT_PERMISSIONS[i] + " Permission Not Granted"); + allOk = false; + } + } + return allOk; + } + + + public void requestPermissions(AppCompatActivity activity) { if (mPermissionsRequested) { Log.i(TAG, "requestPermissions() - request already sent - not doing anything"); @@ -785,15 +848,47 @@ public class StartupActivity extends AppCompatActivity { } } + public void requestBTPermissions() { + if (mBTPermissionsRequested) { + Log.i(TAG, "requestBTPermissions() - request already sent - not doing anything"); + } else { + Log.i(TAG, "requestBTPermissions() - requesting permissions"); + mBTPermissionsRequested = true; + AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder( + this); + alertDialogBuilder + .setTitle(R.string.BTpermissions_required) + .setMessage(R.string.BT_permissions_rationale) + .setCancelable(false) + .setPositiveButton(getString(R.string.okBtnTxt), new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + dialog.cancel(); + Log.i(TAG, "requestBTPermissions(): Launching ActivityCompat.requestPermissions()"); + ActivityCompat.requestPermissions(StartupActivity.this, + BT_PERMISSIONS, + 46); + } + }) + //.setNegativeButton(getString(R.string.cancelBtnTxt), new DialogInterface.OnClickListener() { + // public void onClick(DialogInterface dialog, int id) { + // dialog.cancel(); + // } + //} + //) + .create().show(); + } + } @Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { - Log.i(TAG, "onRequestPermissionsResult - Permission" + permissions + " = " + grantResults); - super.onRequestPermissionsResult(requestCode, permissions, grantResults); + Log.i(TAG, "onRequestPermissionsResult - requestCode="+requestCode+" nPermissions="+permissions.length); + Log.i(TAG, "onRequestPermissionsResult: "+permissions[0]+": "+grantResults[0]); for (int i = 0; i < permissions.length; i++) { - Log.i(TAG, "Permission " + permissions[i] + " = " + grantResults[i]); + Log.i(TAG, String.format("onRequestPermissionsResult: i="+i+", Permission " + permissions[i].toString() + " = " + grantResults[i])); + //Log.i(TAG,"i="+i); } + super.onRequestPermissionsResult(requestCode, permissions, grantResults); } diff --git a/app/src/main/res/layout/ble_scan_activity.xml b/app/src/main/res/layout/ble_scan_activity.xml index a212a6e..2a7cd2e 100644 --- a/app/src/main/res/layout/ble_scan_activity.xml +++ b/app/src/main/res/layout/ble_scan_activity.xml @@ -73,14 +73,13 @@ android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" - android:background="#00FF00" android:drawSelectorOnTop="false" /> diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 23b3930..7da8239 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -571,4 +571,7 @@ Battery History Watch Battery History (%) NOTE: Devices will not appear on this list if they are already connected - disconnect device from GadgetBridge etc. to allow it to be selected here + Bluetooth Permissions Not Granted + Bluetooth Permissions Required + Bluetooth permissions are required to communicate with the bluetooth (BLE) data source