wrap all calls to notification manager in exception handler to catch dead object exception

This commit is contained in:
Daniel Gultsch 2018-04-15 17:47:51 +02:00
parent 064e5a4f0d
commit ffc35f5bc5
1 changed files with 75 additions and 43 deletions

View File

@ -225,8 +225,7 @@ public class NotificationService {
synchronized (notifications) { synchronized (notifications) {
markAsReadIfHasDirectReply(conversation); markAsReadIfHasDirectReply(conversation);
if (notifications.remove(conversation.getUuid()) != null) { if (notifications.remove(conversation.getUuid()) != null) {
final NotificationManagerCompat notificationManager = NotificationManagerCompat.from(mXmppConnectionService); cancel(conversation.getUuid(), NOTIFICATION_ID);
notificationManager.cancel(conversation.getUuid(), NOTIFICATION_ID);
updateNotification(false, true); updateNotification(false, true);
} }
} }
@ -256,11 +255,10 @@ public class NotificationService {
} }
public void updateNotification(final boolean notify, boolean summaryOnly) { public void updateNotification(final boolean notify, boolean summaryOnly) {
final NotificationManagerCompat notificationManager = NotificationManagerCompat.from(mXmppConnectionService);
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(mXmppConnectionService); final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(mXmppConnectionService);
if (notifications.size() == 0) { if (notifications.size() == 0) {
notificationManager.cancel(NOTIFICATION_ID); cancel(NOTIFICATION_ID);
} else { } else {
if (notify) { if (notify) {
this.markLastNotification(); this.markLastNotification();
@ -269,7 +267,7 @@ public class NotificationService {
if (notifications.size() == 1 && Build.VERSION.SDK_INT < Build.VERSION_CODES.N) { if (notifications.size() == 1 && Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
mBuilder = buildSingleConversations(notifications.values().iterator().next()); mBuilder = buildSingleConversations(notifications.values().iterator().next());
modifyForSoundVibrationAndLight(mBuilder, notify, preferences); modifyForSoundVibrationAndLight(mBuilder, notify, preferences);
notificationManager.notify(NOTIFICATION_ID, mBuilder.build()); notify(NOTIFICATION_ID, mBuilder.build());
} else { } else {
mBuilder = buildMultipleConversation(); mBuilder = buildMultipleConversation();
modifyForSoundVibrationAndLight(mBuilder, notify, preferences); modifyForSoundVibrationAndLight(mBuilder, notify, preferences);
@ -278,10 +276,10 @@ public class NotificationService {
Builder singleBuilder = buildSingleConversations(entry.getValue()); Builder singleBuilder = buildSingleConversations(entry.getValue());
singleBuilder.setGroup(CONVERSATIONS_GROUP); singleBuilder.setGroup(CONVERSATIONS_GROUP);
setNotificationColor(singleBuilder); setNotificationColor(singleBuilder);
notificationManager.notify(entry.getKey(), NOTIFICATION_ID, singleBuilder.build()); notify(entry.getKey(), NOTIFICATION_ID, singleBuilder.build());
} }
} }
notificationManager.notify(NOTIFICATION_ID, mBuilder.build()); notify(NOTIFICATION_ID, mBuilder.build());
} }
} }
} }
@ -771,7 +769,6 @@ public class NotificationService {
} }
public void updateErrorNotification() { public void updateErrorNotification() {
final NotificationManagerCompat notificationManager = NotificationManagerCompat.from(mXmppConnectionService);
final List<Account> errors = new ArrayList<>(); final List<Account> errors = new ArrayList<>();
for (final Account account : mXmppConnectionService.getAccounts()) { for (final Account account : mXmppConnectionService.getAccounts()) {
if (account.hasErrorStatus() && account.showErrorNotification()) { if (account.hasErrorStatus() && account.showErrorNotification()) {
@ -779,11 +776,11 @@ public class NotificationService {
} }
} }
if (mXmppConnectionService.keepForegroundService()) { if (mXmppConnectionService.keepForegroundService()) {
notificationManager.notify(FOREGROUND_NOTIFICATION_ID, createForegroundNotification()); notify(FOREGROUND_NOTIFICATION_ID, createForegroundNotification());
} }
final NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mXmppConnectionService); final NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mXmppConnectionService);
if (errors.size() == 0) { if (errors.size() == 0) {
notificationManager.cancel(ERROR_NOTIFICATION_ID); cancel(ERROR_NOTIFICATION_ID);
return; return;
} else if (errors.size() == 1) { } else if (errors.size() == 1) {
mBuilder.setContentTitle(mXmppConnectionService.getString(R.string.problem_connecting_to_account)); mBuilder.setContentTitle(mXmppConnectionService.getString(R.string.problem_connecting_to_account));
@ -808,18 +805,53 @@ public class NotificationService {
145, 145,
new Intent(mXmppConnectionService, ManageAccountActivity.class), new Intent(mXmppConnectionService, ManageAccountActivity.class),
PendingIntent.FLAG_UPDATE_CURRENT)); PendingIntent.FLAG_UPDATE_CURRENT));
notificationManager.notify(ERROR_NOTIFICATION_ID, mBuilder.build()); notify(ERROR_NOTIFICATION_ID, mBuilder.build());
} }
public Notification updateFileAddingNotification(int current, Message message) { public Notification updateFileAddingNotification(int current, Message message) {
final NotificationManagerCompat notificationManager = NotificationManagerCompat.from(mXmppConnectionService);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mXmppConnectionService); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mXmppConnectionService);
mBuilder.setContentTitle(mXmppConnectionService.getString(R.string.transcoding_video)); mBuilder.setContentTitle(mXmppConnectionService.getString(R.string.transcoding_video));
mBuilder.setProgress(100, current, false); mBuilder.setProgress(100, current, false);
mBuilder.setSmallIcon(R.drawable.ic_hourglass_empty_white_24dp); mBuilder.setSmallIcon(R.drawable.ic_hourglass_empty_white_24dp);
mBuilder.setContentIntent(createContentIntent(message.getConversation())); mBuilder.setContentIntent(createContentIntent(message.getConversation()));
Notification notification = mBuilder.build(); Notification notification = mBuilder.build();
notificationManager.notify(FOREGROUND_NOTIFICATION_ID, notification); notify(FOREGROUND_NOTIFICATION_ID, notification);
return notification; return notification;
} }
private void notify(String tag, int id, Notification notification) {
final NotificationManagerCompat notificationManager = NotificationManagerCompat.from(mXmppConnectionService);
try {
notificationManager.notify(tag, id, notification);
} catch (RuntimeException e) {
Log.d(Config.LOGTAG, "unable to make notification", e);
}
}
private void notify(int id, Notification notification) {
final NotificationManagerCompat notificationManager = NotificationManagerCompat.from(mXmppConnectionService);
try {
notificationManager.notify(id, notification);
} catch (RuntimeException e) {
Log.d(Config.LOGTAG, "unable to make notification", e);
}
}
private void cancel(int id) {
final NotificationManagerCompat notificationManager = NotificationManagerCompat.from(mXmppConnectionService);
try {
notificationManager.cancel(id);
} catch (RuntimeException e) {
Log.d(Config.LOGTAG, "unable to cancel notification", e);
}
}
private void cancel(String tag, int id) {
final NotificationManagerCompat notificationManager = NotificationManagerCompat.from(mXmppConnectionService);
try {
notificationManager.cancel(tag, id);
} catch (RuntimeException e) {
Log.d(Config.LOGTAG, "unable to cancel notification", e);
}
}
} }