From 701039e67356eed837dc4d501ec2a9debf1aeeb4 Mon Sep 17 00:00:00 2001 From: TaaviE Date: Tue, 28 Jul 2020 02:09:59 +0300 Subject: [PATCH] Instead of crashing, display a notification that the service couldn't be started --- .../gadgetbridge/GBApplication.java | 27 ++++++++++++++++--- app/src/main/res/values/strings.xml | 3 +++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java index d24d8d532..a505354ef 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java @@ -43,6 +43,7 @@ import android.provider.ContactsContract.PhoneLookup; import android.util.Log; import android.util.TypedValue; +import androidx.core.app.NotificationCompat; import androidx.localbroadcastmanager.content.LocalBroadcastManager; import java.io.File; @@ -86,8 +87,9 @@ import static nodomain.freeyourgadget.gadgetbridge.model.DeviceType.MIBAND; import static nodomain.freeyourgadget.gadgetbridge.model.DeviceType.MIBAND2; import static nodomain.freeyourgadget.gadgetbridge.model.DeviceType.MIBAND3; import static nodomain.freeyourgadget.gadgetbridge.model.DeviceType.fromKey; -import static nodomain.freeyourgadget.gadgetbridge.util.GB.NOTIFICATION_CHANNEL_ID; import static nodomain.freeyourgadget.gadgetbridge.util.GB.NOTIFICATION_CHANNEL_HIGH_PRIORITY_ID; +import static nodomain.freeyourgadget.gadgetbridge.util.GB.NOTIFICATION_CHANNEL_ID; + /** * Main Application class that initializes and provides access to certain things like * logging and DB access. @@ -104,6 +106,9 @@ public class GBApplication extends Application { private static final String PREFS_VERSION = "shared_preferences_version"; //if preferences have to be migrated, increment the following and add the migration logic in migratePrefs below; see http://stackoverflow.com/questions/16397848/how-can-i-migrate-android-preferences-with-a-new-version private static final int CURRENT_PREFS_VERSION = 7; + + private static final int ERROR_IN_GADGETBRIDGE_NOTIFICATION = 42; + private static LimitedQueue mIDSenderLookup = new LimitedQueue(16); private static Prefs prefs; private static GBPrefs gbPrefs; @@ -208,7 +213,7 @@ public class GBApplication extends Application { notificationManager.createNotificationChannel(channel); } - NotificationChannel channelHighPr = notificationManager.getNotificationChannel(NOTIFICATION_CHANNEL_HIGH_PRIORITY_ID ); + NotificationChannel channelHighPr = notificationManager.getNotificationChannel(NOTIFICATION_CHANNEL_HIGH_PRIORITY_ID); if (channelHighPr == null) { channelHighPr = new NotificationChannel(NOTIFICATION_CHANNEL_HIGH_PRIORITY_ID, getString(R.string.notification_channel_high_priority_name), @@ -219,7 +224,23 @@ public class GBApplication extends Application { bluetoothStateChangeReceiver = new BluetoothStateChangeReceiver(); registerReceiver(bluetoothStateChangeReceiver, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED)); } - startService(new Intent(this, NotificationCollectorMonitorService.class)); + try { + startService(new Intent(this, NotificationCollectorMonitorService.class)); + } catch (IllegalStateException e) { + String message = e.toString(); + if (message == null) { + message = getString(R.string._unknown_); + } + notificationManager.notify(ERROR_IN_GADGETBRIDGE_NOTIFICATION, + new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_HIGH_PRIORITY_ID) + .setSmallIcon(R.drawable.gadgetbridge_img) + .setContentTitle(getString(R.string.error_background_service)) + .setContentText(getString(R.string.error_background_service_reason_truncated)) + .setStyle(new NotificationCompat.BigTextStyle() + .bigText(getString(R.string.error_background_service_reason) + "\"" + message + "\"")) + .setPriority(NotificationCompat.PRIORITY_DEFAULT) + .build()); + } } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 6f0ea8cb8..93ae3b496 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -880,6 +880,9 @@ Many thanks to all unlisted contributors for contributing code, support, ideas, motivation, bug reports, money… ✊ All these permissions are required and instability might occur if not granted CAUTION: Error when checking version information! You should not continue! Saw version name \"%s\" + Failed to start background service + Starting the background service failed because… + Starting the background service failed because of an exception. Error: %d hour %d hours