From 08ac99b4c15c7b3e84fc5768d9a6fe7d8ea33a0a Mon Sep 17 00:00:00 2001 From: Greyson Parrelli Date: Wed, 27 Apr 2022 10:34:04 -0400 Subject: [PATCH] Fix crash around unbinding GenericForegroundService. --- .../service/NotificationController.java | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/service/NotificationController.java b/app/src/main/java/org/thoughtcrime/securesms/service/NotificationController.java index 566964b8b7..4e989dcc21 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/service/NotificationController.java +++ b/app/src/main/java/org/thoughtcrime/securesms/service/NotificationController.java @@ -24,6 +24,7 @@ public final class NotificationController implements AutoCloseable, private int progressMax; private boolean indeterminate; private long percent = -1; + private boolean isBound; private final AtomicReference service = new AtomicReference<>(); @@ -31,11 +32,11 @@ public final class NotificationController implements AutoCloseable, this.context = context; this.id = id; - bindToService(); + isBound = bindToService(); } - private void bindToService() { - context.bindService(new Intent(context, GenericForegroundService.class), this, Context.BIND_AUTO_CREATE); + private boolean bindToService() { + return context.bindService(new Intent(context, GenericForegroundService.class), this, Context.BIND_AUTO_CREATE); } public int getId() { @@ -44,8 +45,18 @@ public final class NotificationController implements AutoCloseable, @Override public void close() { - context.unbindService(this); - GenericForegroundService.stopForegroundTask(context, id); + try { + if (isBound) { + context.unbindService(this); + isBound = false; + } else { + Log.w(TAG, "Service was not bound at the time of close()..."); + } + + GenericForegroundService.stopForegroundTask(context, id); + } catch (IllegalArgumentException e) { + Log.w(TAG, "Failed to unbind service...", e); + } } public void setIndeterminateProgress() {