Search in sources :

Example 1 with Builder

use of androidx.core.app.NotificationCompat.Builder in project Conversations by siacs.

the class NotificationService method getPerson.

private Person getPerson(Message message) {
    final Contact contact = message.getContact();
    final Person.Builder builder = new Person.Builder();
    if (contact != null) {
        builder.setName(contact.getDisplayName());
        final Uri uri = contact.getSystemAccount();
        if (uri != null) {
            builder.setUri(uri.toString());
        }
    } else {
        builder.setName(UIHelper.getMessageDisplayName(message));
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
        builder.setIcon(IconCompat.createWithBitmap(mXmppConnectionService.getAvatarService().get(message, AvatarService.getSystemUiAvatarSize(mXmppConnectionService), false)));
    }
    return builder.build();
}
Also used : Builder(androidx.core.app.NotificationCompat.Builder) Person(androidx.core.app.Person) Uri(android.net.Uri) Contact(eu.siacs.conversations.entities.Contact)

Example 2 with Builder

use of androidx.core.app.NotificationCompat.Builder in project Conversations by siacs.

the class NotificationService method buildMultipleConversation.

private Builder buildMultipleConversation(final boolean notify, final boolean quietHours) {
    final Builder mBuilder = new NotificationCompat.Builder(mXmppConnectionService, quietHours ? "quiet_hours" : (notify ? "messages" : "silent_messages"));
    final NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle();
    style.setBigContentTitle(mXmppConnectionService.getResources().getQuantityString(R.plurals.x_unread_conversations, notifications.size(), notifications.size()));
    final StringBuilder names = new StringBuilder();
    Conversation conversation = null;
    for (final ArrayList<Message> messages : notifications.values()) {
        if (messages.size() > 0) {
            conversation = (Conversation) messages.get(0).getConversation();
            final String name = conversation.getName().toString();
            SpannableString styledString;
            if (Config.HIDE_MESSAGE_TEXT_IN_NOTIFICATION) {
                int count = messages.size();
                styledString = new SpannableString(name + ": " + mXmppConnectionService.getResources().getQuantityString(R.plurals.x_messages, count, count));
                styledString.setSpan(new StyleSpan(Typeface.BOLD), 0, name.length(), 0);
                style.addLine(styledString);
            } else {
                styledString = new SpannableString(name + ": " + UIHelper.getMessagePreview(mXmppConnectionService, messages.get(0)).first);
                styledString.setSpan(new StyleSpan(Typeface.BOLD), 0, name.length(), 0);
                style.addLine(styledString);
            }
            names.append(name);
            names.append(", ");
        }
    }
    if (names.length() >= 2) {
        names.delete(names.length() - 2, names.length());
    }
    final String contentTitle = mXmppConnectionService.getResources().getQuantityString(R.plurals.x_unread_conversations, notifications.size(), notifications.size());
    mBuilder.setContentTitle(contentTitle);
    mBuilder.setTicker(contentTitle);
    mBuilder.setContentText(names.toString());
    mBuilder.setStyle(style);
    if (conversation != null) {
        mBuilder.setContentIntent(createContentIntent(conversation));
    }
    mBuilder.setGroupSummary(true);
    mBuilder.setGroup(CONVERSATIONS_GROUP);
    mBuilder.setDeleteIntent(createDeleteIntent(null));
    mBuilder.setSmallIcon(R.drawable.ic_notification);
    return mBuilder;
}
Also used : SpannableString(android.text.SpannableString) Message(eu.siacs.conversations.entities.Message) Builder(androidx.core.app.NotificationCompat.Builder) StyleSpan(android.text.style.StyleSpan) NotificationCompat(androidx.core.app.NotificationCompat) Conversation(eu.siacs.conversations.entities.Conversation) SpannableString(android.text.SpannableString)

Example 3 with Builder

use of androidx.core.app.NotificationCompat.Builder in project Conversations by siacs.

the class NotificationService method pushFailedDelivery.

public void pushFailedDelivery(final Message message) {
    final Conversation conversation = (Conversation) message.getConversation();
    final boolean isScreenLocked = !mXmppConnectionService.isScreenLocked();
    if (this.mIsInForeground && isScreenLocked && this.mOpenConversation == message.getConversation()) {
        Log.d(Config.LOGTAG, message.getConversation().getAccount().getJid().asBareJid() + ": suppressing failed delivery notification because conversation is open");
        return;
    }
    final PendingIntent pendingIntent = createContentIntent(conversation);
    final int notificationId = generateRequestCode(conversation, 0) + DELIVERY_FAILED_NOTIFICATION_ID;
    final int failedDeliveries = conversation.countFailedDeliveries();
    final Notification notification = new Builder(mXmppConnectionService, "delivery_failed").setContentTitle(conversation.getName()).setAutoCancel(true).setSmallIcon(R.drawable.ic_error_white_24dp).setContentText(mXmppConnectionService.getResources().getQuantityText(R.plurals.some_messages_could_not_be_delivered, failedDeliveries)).setGroup("delivery_failed").setContentIntent(pendingIntent).build();
    final Notification summaryNotification = new Builder(mXmppConnectionService, "delivery_failed").setContentTitle(mXmppConnectionService.getString(R.string.failed_deliveries)).setContentText(mXmppConnectionService.getResources().getQuantityText(R.plurals.some_messages_could_not_be_delivered, 1024)).setSmallIcon(R.drawable.ic_error_white_24dp).setGroup("delivery_failed").setGroupSummary(true).setAutoCancel(true).build();
    notify(notificationId, notification);
    notify(DELIVERY_FAILED_NOTIFICATION_ID, summaryNotification);
}
Also used : Builder(androidx.core.app.NotificationCompat.Builder) Conversation(eu.siacs.conversations.entities.Conversation) PendingIntent(android.app.PendingIntent) Notification(android.app.Notification)

Example 4 with Builder

use of androidx.core.app.NotificationCompat.Builder in project Conversations by siacs.

the class NotificationService method showIncomingCallNotification.

private void showIncomingCallNotification(final AbstractJingleConnection.Id id, final Set<Media> media) {
    final Intent fullScreenIntent = new Intent(mXmppConnectionService, RtpSessionActivity.class);
    fullScreenIntent.putExtra(RtpSessionActivity.EXTRA_ACCOUNT, id.account.getJid().asBareJid().toEscapedString());
    fullScreenIntent.putExtra(RtpSessionActivity.EXTRA_WITH, id.with.toEscapedString());
    fullScreenIntent.putExtra(RtpSessionActivity.EXTRA_SESSION_ID, id.sessionId);
    fullScreenIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    fullScreenIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    final NotificationCompat.Builder builder = new NotificationCompat.Builder(mXmppConnectionService, INCOMING_CALLS_NOTIFICATION_CHANNEL);
    if (media.contains(Media.VIDEO)) {
        builder.setSmallIcon(R.drawable.ic_videocam_white_24dp);
        builder.setContentTitle(mXmppConnectionService.getString(R.string.rtp_state_incoming_video_call));
    } else {
        builder.setSmallIcon(R.drawable.ic_call_white_24dp);
        builder.setContentTitle(mXmppConnectionService.getString(R.string.rtp_state_incoming_call));
    }
    final Contact contact = id.getContact();
    builder.setLargeIcon(mXmppConnectionService.getAvatarService().get(contact, AvatarService.getSystemUiAvatarSize(mXmppConnectionService)));
    final Uri systemAccount = contact.getSystemAccount();
    if (systemAccount != null) {
        builder.addPerson(systemAccount.toString());
    }
    builder.setContentText(id.account.getRoster().getContact(id.with).getDisplayName());
    builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
    builder.setPriority(NotificationCompat.PRIORITY_HIGH);
    builder.setCategory(NotificationCompat.CATEGORY_CALL);
    PendingIntent pendingIntent = createPendingRtpSession(id, Intent.ACTION_VIEW, 101);
    builder.setFullScreenIntent(pendingIntent, true);
    // old androids need this?
    builder.setContentIntent(pendingIntent);
    builder.setOngoing(true);
    builder.addAction(new NotificationCompat.Action.Builder(R.drawable.ic_call_end_white_48dp, mXmppConnectionService.getString(R.string.dismiss_call), createCallAction(id.sessionId, XmppConnectionService.ACTION_DISMISS_CALL, 102)).build());
    builder.addAction(new NotificationCompat.Action.Builder(R.drawable.ic_call_white_24dp, mXmppConnectionService.getString(R.string.answer_call), createPendingRtpSession(id, RtpSessionActivity.ACTION_ACCEPT_CALL, 103)).build());
    modifyIncomingCall(builder);
    final Notification notification = builder.build();
    notification.flags = notification.flags | Notification.FLAG_INSISTENT;
    notify(INCOMING_CALL_NOTIFICATION_ID, notification);
}
Also used : Builder(androidx.core.app.NotificationCompat.Builder) Builder(androidx.core.app.NotificationCompat.Builder) NotificationCompat(androidx.core.app.NotificationCompat) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) Uri(android.net.Uri) Notification(android.app.Notification) Contact(eu.siacs.conversations.entities.Contact)

Example 5 with Builder

use of androidx.core.app.NotificationCompat.Builder in project Conversations by siacs.

the class NotificationService method buildSingleConversations.

private Builder buildSingleConversations(final ArrayList<Message> messages, final boolean notify, final boolean quietHours) {
    final Builder mBuilder = new NotificationCompat.Builder(mXmppConnectionService, quietHours ? "quiet_hours" : (notify ? "messages" : "silent_messages"));
    if (messages.size() >= 1) {
        final Conversation conversation = (Conversation) messages.get(0).getConversation();
        mBuilder.setLargeIcon(mXmppConnectionService.getAvatarService().get(conversation, AvatarService.getSystemUiAvatarSize(mXmppConnectionService)));
        mBuilder.setContentTitle(conversation.getName());
        if (Config.HIDE_MESSAGE_TEXT_IN_NOTIFICATION) {
            int count = messages.size();
            mBuilder.setContentText(mXmppConnectionService.getResources().getQuantityString(R.plurals.x_messages, count, count));
        } else {
            Message message;
            // TODO starting with Android 9 we might want to put images in MessageStyle
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P && (message = getImage(messages)) != null) {
                modifyForImage(mBuilder, message, messages);
            } else {
                modifyForTextOnly(mBuilder, messages);
            }
            RemoteInput remoteInput = new RemoteInput.Builder("text_reply").setLabel(UIHelper.getMessageHint(mXmppConnectionService, conversation)).build();
            PendingIntent markAsReadPendingIntent = createReadPendingIntent(conversation);
            NotificationCompat.Action markReadAction = new NotificationCompat.Action.Builder(R.drawable.ic_drafts_white_24dp, mXmppConnectionService.getString(R.string.mark_as_read), markAsReadPendingIntent).setSemanticAction(NotificationCompat.Action.SEMANTIC_ACTION_MARK_AS_READ).setShowsUserInterface(false).build();
            final String replyLabel = mXmppConnectionService.getString(R.string.reply);
            final String lastMessageUuid = Iterables.getLast(messages).getUuid();
            final NotificationCompat.Action replyAction = new NotificationCompat.Action.Builder(R.drawable.ic_send_text_offline, replyLabel, createReplyIntent(conversation, lastMessageUuid, false)).setSemanticAction(NotificationCompat.Action.SEMANTIC_ACTION_REPLY).setShowsUserInterface(false).addRemoteInput(remoteInput).build();
            final NotificationCompat.Action wearReplyAction = new NotificationCompat.Action.Builder(R.drawable.ic_wear_reply, replyLabel, createReplyIntent(conversation, lastMessageUuid, true)).addRemoteInput(remoteInput).build();
            mBuilder.extend(new NotificationCompat.WearableExtender().addAction(wearReplyAction));
            int addedActionsCount = 1;
            mBuilder.addAction(markReadAction);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                mBuilder.addAction(replyAction);
                ++addedActionsCount;
            }
            if (displaySnoozeAction(messages)) {
                String label = mXmppConnectionService.getString(R.string.snooze);
                PendingIntent pendingSnoozeIntent = createSnoozeIntent(conversation);
                NotificationCompat.Action snoozeAction = new NotificationCompat.Action.Builder(R.drawable.ic_notifications_paused_white_24dp, label, pendingSnoozeIntent).build();
                mBuilder.addAction(snoozeAction);
                ++addedActionsCount;
            }
            if (addedActionsCount < 3) {
                final Message firstLocationMessage = getFirstLocationMessage(messages);
                if (firstLocationMessage != null) {
                    final PendingIntent pendingShowLocationIntent = createShowLocationIntent(firstLocationMessage);
                    if (pendingShowLocationIntent != null) {
                        final String label = mXmppConnectionService.getResources().getString(R.string.show_location);
                        NotificationCompat.Action locationAction = new NotificationCompat.Action.Builder(R.drawable.ic_room_white_24dp, label, pendingShowLocationIntent).build();
                        mBuilder.addAction(locationAction);
                        ++addedActionsCount;
                    }
                }
            }
            if (addedActionsCount < 3) {
                Message firstDownloadableMessage = getFirstDownloadableMessage(messages);
                if (firstDownloadableMessage != null) {
                    String label = mXmppConnectionService.getResources().getString(R.string.download_x_file, UIHelper.getFileDescriptionString(mXmppConnectionService, firstDownloadableMessage));
                    PendingIntent pendingDownloadIntent = createDownloadIntent(firstDownloadableMessage);
                    NotificationCompat.Action downloadAction = new NotificationCompat.Action.Builder(R.drawable.ic_file_download_white_24dp, label, pendingDownloadIntent).build();
                    mBuilder.addAction(downloadAction);
                    ++addedActionsCount;
                }
            }
        }
        if (conversation.getMode() == Conversation.MODE_SINGLE) {
            Contact contact = conversation.getContact();
            Uri systemAccount = contact.getSystemAccount();
            if (systemAccount != null) {
                mBuilder.addPerson(systemAccount.toString());
            }
        }
        mBuilder.setWhen(conversation.getLatestMessage().getTimeSent());
        mBuilder.setSmallIcon(R.drawable.ic_notification);
        mBuilder.setDeleteIntent(createDeleteIntent(conversation));
        mBuilder.setContentIntent(createContentIntent(conversation));
    }
    return mBuilder;
}
Also used : RemoteInput(androidx.core.app.RemoteInput) Message(eu.siacs.conversations.entities.Message) Builder(androidx.core.app.NotificationCompat.Builder) Conversation(eu.siacs.conversations.entities.Conversation) SpannableString(android.text.SpannableString) Uri(android.net.Uri) Contact(eu.siacs.conversations.entities.Contact) NotificationCompat(androidx.core.app.NotificationCompat) PendingIntent(android.app.PendingIntent)

Aggregations

Builder (androidx.core.app.NotificationCompat.Builder)7 NotificationCompat (androidx.core.app.NotificationCompat)4 PendingIntent (android.app.PendingIntent)3 Uri (android.net.Uri)3 SpannableString (android.text.SpannableString)3 Contact (eu.siacs.conversations.entities.Contact)3 Conversation (eu.siacs.conversations.entities.Conversation)3 Notification (android.app.Notification)2 Message (eu.siacs.conversations.entities.Message)2 Intent (android.content.Intent)1 SharedPreferences (android.content.SharedPreferences)1 StyleSpan (android.text.style.StyleSpan)1 Person (androidx.core.app.Person)1 RemoteInput (androidx.core.app.RemoteInput)1 AbstractJingleConnection (eu.siacs.conversations.xmpp.jingle.AbstractJingleConnection)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1