Fixed notifications and Bluetooth permissions issues on Android 13 Go. Re-start of the app improved to reduce the chance of leaving part of background service running, but this may make startup activity seem less stable.

This commit is contained in:
Graham Jones
2024-04-06 20:41:04 +01:00
parent 6633eee60d
commit dee2bcc3b8
7 changed files with 156 additions and 35 deletions

View File

@@ -7,7 +7,7 @@ android {
defaultConfig { defaultConfig {
applicationId "uk.org.openseizuredetector" applicationId "uk.org.openseizuredetector"
minSdkVersion 23 // Android 6 minSdkVersion 23 // Android 6
targetSdkVersion 33 // Android 13 targetSdkVersion 33 // Android 13 = 33
multiDexEnabled true multiDexEnabled true
} }

View File

@@ -2,8 +2,9 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:versionCode="139" android:versionCode="139"
android:versionName="4.2.5"> android:versionName="4.2.6">
<!-- android:allowBackup="false" --> <!-- android:allowBackup="false" -->
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
<uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" /> <uses-permission android:name="android.permission.BLUETOOTH_SCAN" />

View File

@@ -275,6 +275,10 @@ public class BLEScanActivity extends ListActivity {
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences((this)); 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.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(); finish();
} }

View File

@@ -148,6 +148,7 @@ public class PrefActivity extends PreferenceActivity implements SharedPreference
// StartUpActivity, so we exit this activity and start start-up activity. // 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) { if (sharedPreferences.getBoolean("SMSAlarm", false) == true) {
mUtil.showToast("Restarting OpenSeizureDetector");
Log.i(TAG, "onSharedPreferenceChanged(): SMS Alarm Enabled - Restarting start-up activity to check permissions"); Log.i(TAG, "onSharedPreferenceChanged(): SMS Alarm Enabled - Restarting start-up activity to check permissions");
Intent i; Intent i;
i = new Intent(this, StartupActivity.class); 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"); 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! // 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("Setting " + s + " Changed - restarting server");
mUtil.showToast("Restarting OpenSeizureDetector");
mPrefChanged = true; mPrefChanged = true;
mUtil.stopServer(); 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() { mHandler.postDelayed(new Runnable() {
public void run() { public void run() {
mUtil.startServer(); mUtil.startServer();
} }
}, 100); }, 1000);
if (s.equals("DataSource") || s.equals("advancedMode")) { if (s.equals("DataSource") || s.equals("advancedMode")) {
Log.i(TAG, "Re-starting PrefActivity to refresh list"); Log.i(TAG, "Re-starting PrefActivity to refresh list");
finish();
startActivity(getIntent()); startActivity(getIntent());
finish();
} }
} }

View File

@@ -34,7 +34,6 @@ import android.content.pm.PackageInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.graphics.Color; import android.graphics.Color;
import android.net.Uri; import android.net.Uri;
import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.os.PowerManager; import android.os.PowerManager;
@@ -53,7 +52,6 @@ import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat; import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat; import androidx.core.content.ContextCompat;
import androidx.core.text.HtmlCompat;
import com.rohitss.uceh.UCEHandler; import com.rohitss.uceh.UCEHandler;
@@ -89,10 +87,12 @@ public class StartupActivity extends AppCompatActivity {
private boolean mLocationPermissions2Requested; private boolean mLocationPermissions2Requested;
private boolean mSmsPermissionsRequested; private boolean mSmsPermissionsRequested;
private boolean mPermissionsRequested; private boolean mPermissionsRequested;
private boolean mBindInProgress = false;
public final String[] REQUIRED_PERMISSIONS = { public final String[] REQUIRED_PERMISSIONS = {
//Manifest.permission.WRITE_EXTERNAL_STORAGE, //Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.WAKE_LOCK, Manifest.permission.WAKE_LOCK,
Manifest.permission.POST_NOTIFICATIONS,
}; };
public final String[] SMS_PERMISSIONS_1 = { public final String[] SMS_PERMISSIONS_1 = {
@@ -112,6 +112,17 @@ public class StartupActivity extends AppCompatActivity {
Manifest.permission.ACCESS_BACKGROUND_LOCATION, 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 @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@@ -196,10 +207,17 @@ public class StartupActivity extends AppCompatActivity {
// Display the DataSource name // Display the DataSource name
SharedPreferences SP = PreferenceManager SharedPreferences SP = PreferenceManager
.getDefaultSharedPreferences(getBaseContext()); .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 = (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()) { if (mUtil.isServerRunning()) {
@@ -209,18 +227,6 @@ public class StartupActivity extends AppCompatActivity {
} else { } else {
Log.i(TAG, "onStart() - server not running - isServerRunning=" + mUtil.isServerRunning()); 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 // Check power management settings
PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE); PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
@@ -299,7 +305,33 @@ public class StartupActivity extends AppCompatActivity {
tv = (TextView) findViewById(R.id.textItem1); tv = (TextView) findViewById(R.id.textItem1);
pb = (ProgressBar) findViewById(R.id.progressBar1); pb = (ProgressBar) findViewById(R.id.progressBar1);
if (arePermissionsOK()) { 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"); Log.i(TAG, "SMS permissions NOT OK");
tv.setText(getString(R.string.SmsPermissionWarning)); tv.setText(getString(R.string.SmsPermissionWarning));
tv.setBackgroundColor(alarmColour); tv.setBackgroundColor(alarmColour);
@@ -322,12 +354,6 @@ public class StartupActivity extends AppCompatActivity {
tv.setTextColor(alarmTextColour); tv.setTextColor(alarmTextColour);
requestLocationPermissions2(); requestLocationPermissions2();
allOk = false; 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 { } else {
tv.setText(getString(R.string.AppPermissionsWarning)); tv.setText(getString(R.string.AppPermissionsWarning));
@@ -348,6 +374,27 @@ public class StartupActivity extends AppCompatActivity {
allOk = false; 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 // Are we Bound to the Service
tv = (TextView) findViewById(R.id.textItem2); tv = (TextView) findViewById(R.id.textItem2);
pb = (ProgressBar) findViewById(R.id.progressBar2); pb = (ProgressBar) findViewById(R.id.progressBar2);
@@ -442,6 +489,7 @@ public class StartupActivity extends AppCompatActivity {
startActivity(intent); startActivity(intent);
mStartedMainActivity = true; mStartedMainActivity = true;
finish(); finish();
return;
} catch (Exception ex) { } catch (Exception ex) {
mStartedMainActivity = false; mStartedMainActivity = false;
Log.e(TAG, "exception starting main activity " + ex.toString()); Log.e(TAG, "exception starting main activity " + ex.toString());
@@ -677,6 +725,21 @@ public class StartupActivity extends AppCompatActivity {
return allOk; 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) { public void requestPermissions(AppCompatActivity activity) {
if (mPermissionsRequested) { if (mPermissionsRequested) {
Log.i(TAG, "requestPermissions() - request already sent - not doing anything"); 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 @Override
public void onRequestPermissionsResult(int requestCode, public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) { String permissions[], int[] grantResults) {
Log.i(TAG, "onRequestPermissionsResult - Permission" + permissions + " = " + grantResults); Log.i(TAG, "onRequestPermissionsResult - requestCode="+requestCode+" nPermissions="+permissions.length);
super.onRequestPermissionsResult(requestCode, permissions, grantResults); Log.i(TAG, "onRequestPermissionsResult: "+permissions[0]+": "+grantResults[0]);
for (int i = 0; i < permissions.length; i++) { 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);
} }

View File

@@ -73,14 +73,13 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_weight="1" android:layout_weight="1"
android:background="#00FF00"
android:drawSelectorOnTop="false" /> android:drawSelectorOnTop="false" />
<TextView <TextView
android:id="@android:id/empty" android:id="@android:id/empty"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:background="#FF0000" android:background="#101010"
android:text="No data" /> android:text="No data" />
</LinearLayout> </LinearLayout>
</LinearLayout> </LinearLayout>

View File

@@ -571,4 +571,7 @@
<string name="battery_history">Battery History</string> <string name="battery_history">Battery History</string>
<string name="watch_batt_hist">Watch Battery History (%)</string> <string name="watch_batt_hist">Watch Battery History (%)</string>
<string name="ble_connected_warning_text">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</string> <string name="ble_connected_warning_text">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</string>
<string name="BTPermissionWarning">Bluetooth Permissions Not Granted</string>
<string name="BTpermissions_required">Bluetooth Permissions Required</string>
<string name="BT_permissions_rationale">Bluetooth permissions are required to communicate with the bluetooth (BLE) data source</string>
</resources> </resources>