Search in sources :

Example 6 with NotificationThread

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;
}
Also used : NotificationThread(com.meisolsson.githubsdk.model.NotificationThread) SharedPreferences(android.content.SharedPreferences) ArrayList(java.util.ArrayList) NotificationManagerCompat(android.support.v4.app.NotificationManagerCompat) NotificationHolder(com.gh4a.model.NotificationHolder) ApiRequestException(com.gh4a.ApiRequestException) NotificationListLoadResult(com.gh4a.model.NotificationListLoadResult) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet) NonNull(android.support.annotation.NonNull)

Example 7 with NotificationThread

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());
}
Also used : NotificationThread(com.meisolsson.githubsdk.model.NotificationThread) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) Paint(android.graphics.Paint) Repository(com.meisolsson.githubsdk.model.Repository) NotificationCompat(android.support.v4.app.NotificationCompat) Notification(android.app.Notification) PendingIntent(android.app.PendingIntent)

Aggregations

NotificationThread (com.meisolsson.githubsdk.model.NotificationThread)7 Repository (com.meisolsson.githubsdk.model.Repository)4 Notification (android.app.Notification)2 PendingIntent (android.app.PendingIntent)2 Intent (android.content.Intent)2 Paint (android.graphics.Paint)2 NotificationCompat (android.support.v4.app.NotificationCompat)2 NotificationHolder (com.gh4a.model.NotificationHolder)2 NotificationListLoadResult (com.gh4a.model.NotificationListLoadResult)2 NotificationItem (com.github.pockethub.android.ui.item.notification.NotificationItem)2 ArrayList (java.util.ArrayList)2 SharedPreferences (android.content.SharedPreferences)1 NonNull (android.support.annotation.NonNull)1 NotificationManagerCompat (android.support.v4.app.NotificationManagerCompat)1 SpannableStringBuilder (android.text.SpannableStringBuilder)1 TextAppearanceSpan (android.text.style.TextAppearanceSpan)1 ApiRequestException (com.gh4a.ApiRequestException)1 ApiHelpers (com.gh4a.utils.ApiHelpers)1 NotificationHeaderItem (com.github.pockethub.android.ui.item.notification.NotificationHeaderItem)1 Issue (com.meisolsson.githubsdk.model.Issue)1