Skip to content

Commit

Permalink
Stabilize the app when permissions have been revoked. Moved the start…
Browse files Browse the repository at this point in the history
… service to only after the permission check is complete.

In the stop service, only stop the service if notifications are enabled, due to a weird scenario where the service might start and immediately end due to lack of notifications, resulting in an app crash.
How did we end up like this.
Issue #1138
Issue #1053
  • Loading branch information
mendhak committed May 26, 2024
1 parent da36634 commit 95fa61a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
2 changes: 2 additions & 0 deletions gpslogger/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
<!-- allow this to run as a foreground service -->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />


<application
android:allowBackup="true"
Expand Down
26 changes: 23 additions & 3 deletions gpslogger/src/main/java/com/mendhak/gpslogger/GpsMainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
import androidx.appcompat.view.menu.ActionMenuItemView;
import androidx.appcompat.widget.ActionMenuView;
import androidx.appcompat.widget.Toolbar;
import androidx.core.app.NotificationManagerCompat;
import androidx.core.content.ContextCompat;
import androidx.core.content.FileProvider;
import androidx.drawerlayout.widget.DrawerLayout;
Expand Down Expand Up @@ -158,9 +159,7 @@ protected void onCreate(Bundle savedInstanceState) {
setUpNavigationDrawer(savedInstanceState);

loadDefaultFragmentView();
startAndBindService();
registerEventBus();
registerConscryptProvider();


if(!Systems.hasUserGrantedAllNecessaryPermissions(this)){
LOG.debug("Permission check - missing permissions");
Expand All @@ -170,6 +169,10 @@ protected void onCreate(Bundle savedInstanceState) {
else {
LOG.debug("Permission check - OK");

startAndBindService();
registerEventBus();
registerConscryptProvider();

if(preferenceHelper.shouldStartLoggingOnAppLaunch()){
LOG.debug("Start logging on app launch");
EventBus.getDefault().postSticky(new CommandEvents.RequestStartStop(true));
Expand Down Expand Up @@ -269,6 +272,9 @@ public boolean onResult(@NonNull String dialogTag, int which, @NonNull Bundle ex
permissions.add(Manifest.permission.ACCESS_FINE_LOCATION);
permissions.add(Manifest.permission.READ_EXTERNAL_STORAGE);
permissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
permissions.add(Manifest.permission.POST_NOTIFICATIONS);
}

if (Build.VERSION.SDK_INT == Build.VERSION_CODES.Q) {
// Only on Android 10 (Q), the permission dialog can include an 'Allow all the time'
Expand Down Expand Up @@ -1501,6 +1507,16 @@ private void startAndBindService() {
* Stops the service if it isn't logging. Also unbinds.
*/
private void stopAndUnbindServiceIfRequired() {
if(!NotificationManagerCompat.from(this).areNotificationsEnabled()) {
// Alright. Why is this needed.
// If the notification permission has been revoked or not granted for whatever reason.
// When the application opens, the service starts, then stops right away.
// Android requires a notification to be shown for a foreground service within 5 seconds.
// So the application crashes and comes back repeatedly. Very weird.
// The answer - if notifications are disabled, don't unbind the service. It will stop on its own.
// Might be related: https://stackoverflow.com/questions/73067939/start-foreground-service-after-notification-permission-was-disabled-causes-crash
return;
}
if (session.isBoundToService()) {

try {
Expand All @@ -1514,6 +1530,10 @@ private void stopAndUnbindServiceIfRequired() {
if (!session.isStarted()) {
LOG.debug("Stopping the service");
try {
// Stop service crashes if the intent is null. lol
if(serviceIntent == null){
serviceIntent = new Intent(this, GpsLoggingService.class);
}
stopService(serviceIntent);
} catch (Exception e) {
LOG.error("Could not stop the service", e);
Expand Down

0 comments on commit 95fa61a

Please sign in to comment.