use of com.meisolsson.githubsdk.model.NotificationThread in project gh4a by slapperwan.
the class NotificationsJob method onRunJob.
@NonNull
@Override
protected Result onRunJob(Params params) {
List<List<NotificationThread>> notifsGroupedByRepo = new ArrayList<>();
try {
NotificationListLoadResult result = SingleFactory.getNotifications(false, false, false).blockingGet();
for (NotificationHolder holder : result.notifications) {
if (holder.notification == null) {
notifsGroupedByRepo.add(new ArrayList<>());
} else {
List<NotificationThread> list = notifsGroupedByRepo.get(notifsGroupedByRepo.size() - 1);
list.add(holder.notification);
}
}
} catch (Exception e) {
return Result.FAILURE;
}
synchronized (sPrefsLock) {
SharedPreferences prefs = getContext().getSharedPreferences(SettingsFragment.PREF_NAME, Context.MODE_PRIVATE);
long lastCheck = prefs.getLong(KEY_LAST_NOTIFICATION_CHECK, 0);
long lastSeen = prefs.getLong(KEY_LAST_NOTIFICATION_SEEN, 0);
Set<String> lastShownRepoIds = prefs.getStringSet(KEY_LAST_SHOWN_REPO_IDS, null);
Set<String> newShownRepoIds = new HashSet<>();
boolean hasUnseenNotification = false, hasNewNotification = false;
for (List<NotificationThread> list : notifsGroupedByRepo) {
for (NotificationThread n : list) {
long timestamp = n.updatedAt().getTime();
hasNewNotification |= timestamp > lastCheck;
hasUnseenNotification |= timestamp > lastSeen;
}
}
if (!hasUnseenNotification) {
// so everything is canceled at this point and we have nothing to notify of
return Result.SUCCESS;
}
NotificationManagerCompat nm = NotificationManagerCompat.from(getContext());
showSummaryNotification(nm, notifsGroupedByRepo, hasNewNotification);
for (List<NotificationThread> list : notifsGroupedByRepo) {
showRepoNotification(nm, list, lastCheck);
String repoId = String.valueOf(list.get(0).repository().id());
if (lastShownRepoIds != null) {
lastShownRepoIds.remove(repoId);
}
newShownRepoIds.add(repoId);
}
// cancel sub-notifications for repos that no longer have notifications
if (lastShownRepoIds != null) {
for (String repoId : lastShownRepoIds) {
nm.cancel(Integer.parseInt(repoId));
}
}
prefs.edit().putLong(KEY_LAST_NOTIFICATION_CHECK, System.currentTimeMillis()).putStringSet(KEY_LAST_SHOWN_REPO_IDS, newShownRepoIds).apply();
}
return Result.SUCCESS;
}
use of com.meisolsson.githubsdk.model.NotificationThread in project gh4a by slapperwan.
the class NotificationsJob method showRepoNotification.
private void showRepoNotification(NotificationManagerCompat nm, List<NotificationThread> notifications, long lastCheck) {
Repository repository = notifications.get(0).repository();
final int id = repository.id().intValue();
String title = repository.owner().login() + "/" + repository.name();
// notifications are sorted by time descending
long when = notifications.get(0).updatedAt().getTime();
String text = getContext().getResources().getQuantityString(R.plurals.unread_notifications_summary_text, notifications.size(), notifications.size());
Intent intent = NotificationHandlingService.makeOpenNotificationActionIntent(getContext(), repository.owner().login(), repository.name());
PendingIntent contentIntent = PendingIntent.getService(getContext(), id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent deleteIntent = PendingIntent.getService(getContext(), id, NotificationHandlingService.makeHandleDismissIntent(getContext(), id), PendingIntent.FLAG_UPDATE_CURRENT);
Intent markReadIntent = NotificationHandlingService.makeMarkReposNotificationsAsReadActionIntent(getContext(), id, repository.owner().login(), repository.name());
PendingIntent markReadPendingIntent = PendingIntent.getService(getContext(), id, markReadIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Action markReadAction = new NotificationCompat.Action(R.drawable.mark_read, getContext().getString(R.string.mark_as_read), markReadPendingIntent);
android.app.Notification publicVersion = makeBaseBuilder().setContentTitle(getContext().getString(R.string.unread_notifications_summary_title)).setContentText(text).setNumber(notifications.size()).setVisibility(NotificationCompat.VISIBILITY_PUBLIC).build();
NotificationCompat.Builder builder = makeBaseBuilder().setLargeIcon(loadRoundUserAvatar(repository.owner())).setGroup(GROUP_ID_GITHUB).setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_SUMMARY).setWhen(when).setShowWhen(true).setNumber(notifications.size()).setPublicVersion(publicVersion).setVisibility(NotificationCompat.VISIBILITY_PRIVATE).setContentTitle(title).setContentIntent(contentIntent).setDeleteIntent(deleteIntent).setAutoCancel(true).addAction(markReadAction).setContentText(text);
boolean hasNewNotification = false;
NotificationCompat.MessagingStyle style = new NotificationCompat.MessagingStyle("").setConversationTitle(title);
for (int i = notifications.size() - 1; i >= 0; i--) {
NotificationThread n = notifications.get(i);
style.addMessage(n.subject().title(), n.updatedAt().getTime(), determineNotificationTypeLabel(n));
hasNewNotification = hasNewNotification || n.updatedAt().getTime() > lastCheck;
}
builder.setStyle(style);
if (!hasNewNotification) {
builder.setOnlyAlertOnce(true);
}
nm.notify(id, builder.build());
}
Aggregations