Skip to content

Commit

Permalink
Avoid crash when foreground service cannot be started
Browse files Browse the repository at this point in the history
  • Loading branch information
Martchus committed Jan 16, 2025
1 parent 08c9679 commit 8555b13
Showing 1 changed file with 17 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.martchus.syncthingtray;

import android.app.ForegroundServiceStartNotAllowedException;
import android.app.Service;
import android.app.PendingIntent;
import android.app.Notification;
Expand Down Expand Up @@ -131,7 +132,7 @@ public static void updateExtraNotification(String title, String text, String sub
intent.putExtra("notification", true);
intent.putExtra("page", page);
s_instance.m_extraNotificationBuilder.setContentTitle(title).setContentText(text).setSubText(subText);
s_instance.m_extraNotificationBuilder.setSmallIcon(Icon.createWithBitmap(bitmapIcon));
s_instance.m_extraNotificationBuilder.setSmallIcon(bitmapIcon != null ? Icon.createWithBitmap(bitmapIcon) : null);
s_instance.m_extraNotificationBuilder.setContentIntent(PendingIntent.getActivity(s_instance, s_activityIntentRequestCode + id, intent, PendingIntent.FLAG_IMMUTABLE));
s_instance.m_notificationManager.notify(id, s_instance.m_extraNotificationBuilder.build());
}
Expand Down Expand Up @@ -188,10 +189,21 @@ public void showForegroundNotification() {
// would not update the initial notification and instead again lead to duplicate notifications.
// We also have to use stopForeground() to remove the notification because invoking notificationManager.cancel() doesn't
// have any effect.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
startForeground(s_notificationID, m_notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_SPECIAL_USE);
} else {
startForeground(s_notificationID, m_notification);
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
startForeground(s_notificationID, m_notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_SPECIAL_USE);
} else {
startForeground(s_notificationID, m_notification);
}
cancelExtraNotification(s_notificationID, -1);
} catch (ForegroundServiceStartNotAllowedException e) {
updateExtraNotification(
s_notificationTitle,
"Unable restart foreground service to run Syncthing while app is in background. Click to start Syncthing app again.",
"",
"",
null,
s_notificationID);
}
}
}

0 comments on commit 8555b13

Please sign in to comment.