Search in sources :

Example 1 with NotificationWrapper

use of com.android.calendar.alerts.AlertService.NotificationWrapper in project Etar-Calendar by Etar-Group.

the class AlertReceiver method makeDigestNotification.

/**
     * Creates an expanding digest notification for expired events.
     */
public static NotificationWrapper makeDigestNotification(Context context, ArrayList<AlertService.NotificationInfo> notificationInfos, String digestTitle, boolean expandable) {
    if (notificationInfos == null || notificationInfos.size() < 1) {
        return null;
    }
    Resources res = context.getResources();
    int numEvents = notificationInfos.size();
    long[] eventIds = new long[notificationInfos.size()];
    long[] startMillis = new long[notificationInfos.size()];
    for (int i = 0; i < notificationInfos.size(); i++) {
        eventIds[i] = notificationInfos.get(i).eventId;
        startMillis[i] = notificationInfos.get(i).startMillis;
    }
    // Create an intent triggered by clicking on the status icon that shows the alerts list.
    PendingIntent pendingClickIntent = createAlertActivityIntent(context);
    // Create an intent triggered by dismissing the digest notification that clears all
    // expired events.
    Intent deleteIntent = new Intent();
    deleteIntent.setClass(context, DismissAlarmsService.class);
    deleteIntent.setAction(DELETE_ALL_ACTION);
    deleteIntent.putExtra(AlertUtils.EVENT_IDS_KEY, eventIds);
    deleteIntent.putExtra(AlertUtils.EVENT_STARTS_KEY, startMillis);
    PendingIntent pendingDeleteIntent = PendingIntent.getService(context, 0, deleteIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    if (digestTitle == null || digestTitle.length() == 0) {
        digestTitle = res.getString(R.string.no_title_label);
    }
    Notification.Builder notificationBuilder = new Notification.Builder(context);
    notificationBuilder.setContentText(digestTitle);
    notificationBuilder.setSmallIcon(R.drawable.stat_notify_calendar_multiple);
    notificationBuilder.setContentIntent(pendingClickIntent);
    notificationBuilder.setDeleteIntent(pendingDeleteIntent);
    String nEventsStr = res.getQuantityString(R.plurals.Nevents, numEvents, numEvents);
    notificationBuilder.setContentTitle(nEventsStr);
    Notification n;
    if (Utils.isJellybeanOrLater()) {
        // New-style notification...
        // Set to min priority to encourage the notification manager to collapse it.
        notificationBuilder.setPriority(Notification.PRIORITY_MIN);
        if (expandable) {
            // Multiple reminders.  Combine into an expanded digest notification.
            Notification.InboxStyle expandedBuilder = new Notification.InboxStyle(notificationBuilder);
            int i = 0;
            for (AlertService.NotificationInfo info : notificationInfos) {
                if (i < NOTIFICATION_DIGEST_MAX_LENGTH) {
                    String name = info.eventName;
                    if (TextUtils.isEmpty(name)) {
                        name = context.getResources().getString(R.string.no_title_label);
                    }
                    String timeLocation = AlertUtils.formatTimeLocation(context, info.startMillis, info.allDay, info.location);
                    TextAppearanceSpan primaryTextSpan = new TextAppearanceSpan(context, R.style.NotificationPrimaryText);
                    TextAppearanceSpan secondaryTextSpan = new TextAppearanceSpan(context, R.style.NotificationSecondaryText);
                    // Event title in bold.
                    SpannableStringBuilder stringBuilder = new SpannableStringBuilder();
                    stringBuilder.append(name);
                    stringBuilder.setSpan(primaryTextSpan, 0, stringBuilder.length(), 0);
                    stringBuilder.append("  ");
                    // Followed by time and location.
                    int secondaryIndex = stringBuilder.length();
                    stringBuilder.append(timeLocation);
                    stringBuilder.setSpan(secondaryTextSpan, secondaryIndex, stringBuilder.length(), 0);
                    expandedBuilder.addLine(stringBuilder);
                    i++;
                } else {
                    break;
                }
            }
            // If there are too many to display, add "+X missed events" for the last line.
            int remaining = numEvents - i;
            if (remaining > 0) {
                String nMoreEventsStr = res.getQuantityString(R.plurals.N_remaining_events, remaining, remaining);
                // TODO: Add highlighting and icon to this last entry once framework allows it.
                expandedBuilder.setSummaryText(nMoreEventsStr);
            }
            // Remove the title in the expanded form (redundant with the listed items).
            expandedBuilder.setBigContentTitle("");
            n = expandedBuilder.build();
        } else {
            n = notificationBuilder.build();
        }
    } else {
        // Old-style notification (pre-JB).  We only need a standard notification (no
        // buttons) but use a custom view so it is consistent with the others.
        n = notificationBuilder.getNotification();
        // Use custom view with buttons to provide JB-like functionality (snooze/email).
        RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.notification);
        contentView.setImageViewResource(R.id.image, R.drawable.stat_notify_calendar_multiple);
        contentView.setTextViewText(R.id.title, nEventsStr);
        contentView.setTextViewText(R.id.text, digestTitle);
        contentView.setViewVisibility(R.id.time, View.VISIBLE);
        contentView.setViewVisibility(R.id.map_button, View.GONE);
        contentView.setViewVisibility(R.id.call_button, View.GONE);
        contentView.setViewVisibility(R.id.email_button, View.GONE);
        contentView.setViewVisibility(R.id.snooze_button, View.GONE);
        contentView.setViewVisibility(R.id.end_padding, View.VISIBLE);
        n.contentView = contentView;
        // Use timestamp to force expired digest notification to the bottom (there is no
        // priority setting before JB release).  This is hidden by the custom view.
        n.when = 1;
    }
    NotificationWrapper nw = new NotificationWrapper(n);
    if (AlertService.DEBUG) {
        for (AlertService.NotificationInfo info : notificationInfos) {
            nw.add(new NotificationWrapper(null, 0, info.eventId, info.startMillis, info.endMillis, false));
        }
    }
    return nw;
}
Also used : TextAppearanceSpan(android.text.style.TextAppearanceSpan) SpannableStringBuilder(android.text.SpannableStringBuilder) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) Notification(android.app.Notification) RemoteViews(android.widget.RemoteViews) Resources(android.content.res.Resources) PendingIntent(android.app.PendingIntent) SpannableStringBuilder(android.text.SpannableStringBuilder) NotificationWrapper(com.android.calendar.alerts.AlertService.NotificationWrapper)

Example 2 with NotificationWrapper

use of com.android.calendar.alerts.AlertService.NotificationWrapper in project Etar-Calendar by Etar-Group.

the class AlertReceiver method makeExpandingNotification.

/**
     * Creates an expanding notification.  The initial expanded state is decided by
     * the notification manager based on the priority.
     */
public static NotificationWrapper makeExpandingNotification(Context context, String title, String summaryText, String description, long startMillis, long endMillis, long eventId, int notificationId, boolean doPopup, int priority) {
    Notification.Builder basicBuilder = new Notification.Builder(context);
    Notification notification = buildBasicNotification(basicBuilder, context, title, summaryText, startMillis, endMillis, eventId, notificationId, doPopup, priority, true);
    if (Utils.isJellybeanOrLater()) {
        // Create a new-style expanded notification
        Notification.BigTextStyle expandedBuilder = new Notification.BigTextStyle(basicBuilder);
        if (description != null) {
            description = mBlankLinePattern.matcher(description).replaceAll("");
            description = description.trim();
        }
        CharSequence text;
        if (TextUtils.isEmpty(description)) {
            text = summaryText;
        } else {
            SpannableStringBuilder stringBuilder = new SpannableStringBuilder();
            stringBuilder.append(summaryText);
            stringBuilder.append("\n\n");
            stringBuilder.setSpan(new RelativeSizeSpan(0.5f), summaryText.length(), stringBuilder.length(), 0);
            stringBuilder.append(description);
            text = stringBuilder;
        }
        expandedBuilder.bigText(text);
        notification = expandedBuilder.build();
    }
    return new NotificationWrapper(notification, notificationId, eventId, startMillis, endMillis, doPopup);
}
Also used : SpannableStringBuilder(android.text.SpannableStringBuilder) RelativeSizeSpan(android.text.style.RelativeSizeSpan) Notification(android.app.Notification) SpannableStringBuilder(android.text.SpannableStringBuilder) NotificationWrapper(com.android.calendar.alerts.AlertService.NotificationWrapper)

Aggregations

Notification (android.app.Notification)2 SpannableStringBuilder (android.text.SpannableStringBuilder)2 NotificationWrapper (com.android.calendar.alerts.AlertService.NotificationWrapper)2 PendingIntent (android.app.PendingIntent)1 Intent (android.content.Intent)1 Resources (android.content.res.Resources)1 RelativeSizeSpan (android.text.style.RelativeSizeSpan)1 TextAppearanceSpan (android.text.style.TextAppearanceSpan)1 RemoteViews (android.widget.RemoteViews)1