Search in sources :

Example 41 with Message

use of de.pixart.messenger.entities.Message in project Pix-Art-Messenger by kriztan.

the class AudioPlayer method startStop.

private void startStop(ImageButton playPause) {
    if (ContextCompat.checkSelfPermission(messageAdapter.getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
        pendingOnClickView.push(new WeakReference<>(playPause));
        ActivityCompat.requestPermissions(messageAdapter.getActivity(), new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, ConversationActivity.REQUEST_PLAY_PAUSE);
        return;
    }
    final RelativeLayout audioPlayer = (RelativeLayout) playPause.getParent();
    final ViewHolder viewHolder = ViewHolder.get(audioPlayer);
    final Message message = (Message) audioPlayer.getTag();
    if (startStop(viewHolder, message)) {
        this.audioPlayerLayouts.clear();
        this.audioPlayerLayouts.addWeakReferenceTo(audioPlayer);
        stopRefresher(true);
    }
}
Also used : Message(de.pixart.messenger.entities.Message) RelativeLayout(android.widget.RelativeLayout)

Example 42 with Message

use of de.pixart.messenger.entities.Message in project Pix-Art-Messenger by kriztan.

the class XmppConnectionService method sendReadMarker.

public void sendReadMarker(final Conversation conversation) {
    final boolean isPrivateAndNonAnonymousMuc = conversation.getMode() == Conversation.MODE_MULTI && conversation.isPrivateAndNonAnonymous();
    final Message markable = conversation.getLatestMarkableMessage(isPrivateAndNonAnonymousMuc);
    if (this.markRead(conversation)) {
        updateConversationUi();
    }
    if (confirmMessages() && markable != null && (markable.trusted() || isPrivateAndNonAnonymousMuc) && markable.getRemoteMsgId() != null) {
        Log.d(Config.LOGTAG, conversation.getAccount().getJid().toBareJid() + ": sending read marker to " + markable.getCounterpart().toString());
        Account account = conversation.getAccount();
        final Jid to = markable.getCounterpart();
        final boolean groupChat = conversation.getMode() == Conversation.MODE_MULTI;
        MessagePacket packet = mMessageGenerator.confirm(account, to, markable.getRemoteMsgId(), markable.getCounterpart(), groupChat);
        this.sendMessagePacket(conversation.getAccount(), packet);
    }
}
Also used : MessagePacket(de.pixart.messenger.xmpp.stanzas.MessagePacket) Account(de.pixart.messenger.entities.Account) XmppAxolotlMessage(de.pixart.messenger.crypto.axolotl.XmppAxolotlMessage) Message(de.pixart.messenger.entities.Message) Jid(de.pixart.messenger.xmpp.jid.Jid)

Example 43 with Message

use of de.pixart.messenger.entities.Message in project Pix-Art-Messenger by kriztan.

the class XmppConnectionService method clearConversationHistory.

public void clearConversationHistory(final Conversation conversation) {
    final long clearDate;
    final String reference;
    if (conversation.countMessages() > 0) {
        Message latestMessage = conversation.getLatestMessage();
        clearDate = latestMessage.getTimeSent() + 1000;
        reference = latestMessage.getServerMsgId();
    } else {
        clearDate = System.currentTimeMillis();
        reference = null;
    }
    conversation.clearMessages();
    // avoid messages getting loaded through mam
    conversation.setHasMessagesLeftOnServer(false);
    conversation.setLastClearHistory(clearDate, reference);
    Runnable runnable = new Runnable() {

        @Override
        public void run() {
            databaseBackend.deleteMessagesInConversation(conversation);
            databaseBackend.updateConversation(conversation);
        }
    };
    mDatabaseWriterExecutor.execute(runnable);
}
Also used : XmppAxolotlMessage(de.pixart.messenger.crypto.axolotl.XmppAxolotlMessage) Message(de.pixart.messenger.entities.Message)

Example 44 with Message

use of de.pixart.messenger.entities.Message in project Pix-Art-Messenger by kriztan.

the class ConversationAdapter method getView.

@Override
public View getView(int position, View view, ViewGroup parent) {
    if (view == null) {
        LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.conversation_list_row, parent, false);
    }
    ViewHolder viewHolder = ViewHolder.get(view);
    Conversation conversation = getItem(position);
    if (this.activity instanceof XmppActivity) {
        int c = Color.get(activity, conversation == selectedConversation ? R.attr.color_background_secondary : R.attr.color_background_primary);
        viewHolder.swipeableItem.setBackgroundColor(c);
    }
    if (conversation.getMode() == Conversation.MODE_SINGLE || activity.useSubjectToIdentifyConference()) {
        viewHolder.name.setText(EmojiWrapper.transform(conversation.getName()));
    } else {
        viewHolder.name.setText(conversation.getJid().toBareJid().toString());
    }
    Message message = conversation.getLatestMessage();
    int unreadcount = conversation.unreadCount();
    int failedcount = conversation.failedCount();
    viewHolder.receivedStatus.setVisibility(View.GONE);
    viewHolder.readStatus.setVisibility(View.GONE);
    if (!conversation.isRead()) {
        viewHolder.name.setTypeface(null, Typeface.BOLD);
    } else {
        viewHolder.name.setTypeface(null, Typeface.NORMAL);
    }
    if (unreadcount > 0) {
        viewHolder.unreadCount.setVisibility(View.VISIBLE);
        viewHolder.unreadCount.setText(unreadcount > 99 ? "\u221E" : String.valueOf(unreadcount));
    } else {
        viewHolder.unreadCount.setVisibility(View.GONE);
    }
    if (failedcount > 0) {
        viewHolder.failedCount.setVisibility(View.VISIBLE);
        viewHolder.failedCount.setText(failedcount > 99 ? "\u221E" : String.valueOf(failedcount));
    } else {
        viewHolder.failedCount.setVisibility(View.GONE);
    }
    final boolean fileAvailable = message.getTransferable() == null || message.getTransferable().getStatus() != Transferable.STATUS_DELETED;
    final boolean showPreviewText;
    if (fileAvailable && (message.isFileOrImage() || message.treatAsDownloadable() || message.isGeoUri())) {
        final int imageResource;
        if (message.isGeoUri()) {
            imageResource = activity.getThemeResource(R.attr.ic_attach_location, R.drawable.ic_attach_location);
            showPreviewText = false;
        } else {
            final String mime = message.getMimeType();
            switch(mime == null ? "" : mime.split("/")[0]) {
                case "image":
                    imageResource = activity.getThemeResource(R.attr.ic_attach_photo, R.drawable.ic_attach_photo);
                    showPreviewText = false;
                    break;
                case "video":
                    imageResource = activity.getThemeResource(R.attr.ic_attach_video, R.drawable.ic_attach_video);
                    showPreviewText = false;
                    break;
                case "audio":
                    imageResource = activity.getThemeResource(R.attr.ic_attach_record, R.drawable.ic_attach_record);
                    showPreviewText = false;
                    break;
                default:
                    imageResource = activity.getThemeResource(R.attr.ic_attach_document, R.drawable.ic_attach_document);
                    showPreviewText = true;
                    break;
            }
        }
        viewHolder.lastMessageIcon.setImageResource(imageResource);
        viewHolder.lastMessageIcon.setVisibility(View.VISIBLE);
    } else {
        viewHolder.lastMessageIcon.setVisibility(View.GONE);
        showPreviewText = true;
    }
    final Pair<String, Boolean> preview = UIHelper.getMessagePreview(activity, message);
    if (showPreviewText) {
        viewHolder.lastMessage.setText(EmojiWrapper.transform(preview.first));
    } else {
        viewHolder.lastMessageIcon.setContentDescription(preview.first);
    }
    viewHolder.lastMessage.setVisibility(showPreviewText ? View.VISIBLE : View.GONE);
    if (preview.second) {
        if (conversation.isRead()) {
            viewHolder.lastMessage.setTypeface(null, Typeface.ITALIC);
            viewHolder.sender.setTypeface(null, Typeface.NORMAL);
        } else {
            viewHolder.lastMessage.setTypeface(null, Typeface.BOLD_ITALIC);
            viewHolder.sender.setTypeface(null, Typeface.BOLD);
        }
    } else {
        if (conversation.isRead()) {
            viewHolder.lastMessage.setTypeface(null, Typeface.NORMAL);
            viewHolder.sender.setTypeface(null, Typeface.NORMAL);
        } else {
            viewHolder.lastMessage.setTypeface(null, Typeface.BOLD);
            viewHolder.sender.setTypeface(null, Typeface.BOLD);
        }
    }
    if (message.getStatus() == Message.STATUS_RECEIVED) {
        if (conversation.getMode() == Conversation.MODE_MULTI) {
            viewHolder.sender.setVisibility(View.VISIBLE);
            viewHolder.sender.setText(UIHelper.getMessageDisplayName(message).split("\\s+")[0] + ':');
        } else {
            viewHolder.sender.setVisibility(View.GONE);
        }
    } else if (message.getType() != Message.TYPE_STATUS) {
        viewHolder.sender.setVisibility(View.VISIBLE);
        viewHolder.sender.setText(activity.getString(R.string.me) + ':');
    } else {
        viewHolder.sender.setVisibility(View.GONE);
    }
    long muted_till = conversation.getLongAttribute(Conversation.ATTRIBUTE_MUTED_TILL, 0);
    if (muted_till == Long.MAX_VALUE) {
        viewHolder.notificationIcon.setVisibility(View.VISIBLE);
        viewHolder.notificationIcon.setImageResource(R.drawable.ic_notifications_off_grey600_24dp);
    } else if (muted_till >= System.currentTimeMillis()) {
        viewHolder.notificationIcon.setVisibility(View.VISIBLE);
        viewHolder.notificationIcon.setImageResource(R.drawable.ic_notifications_paused_grey600_24dp);
    } else if (conversation.alwaysNotify()) {
        viewHolder.notificationIcon.setVisibility(View.GONE);
    } else {
        viewHolder.notificationIcon.setVisibility(View.VISIBLE);
        viewHolder.notificationIcon.setImageResource(R.drawable.ic_notifications_none_grey600_24dp);
    }
    viewHolder.timestamp.setText(UIHelper.readableTimeDifference(activity, conversation.getLatestMessage().getTimeSent()));
    loadAvatar(conversation, viewHolder.avatar);
    if (conversation.getMode() == Conversation.MODE_SINGLE && ShowPresenceColoredNames()) {
        switch(conversation.getContact().getPresences().getShownStatus()) {
            case CHAT:
            case ONLINE:
                viewHolder.name.setTextColor(ContextCompat.getColor(activity, R.color.online));
                break;
            case AWAY:
                viewHolder.name.setTextColor(ContextCompat.getColor(activity, R.color.away));
                break;
            case XA:
            case DND:
                viewHolder.name.setTextColor(ContextCompat.getColor(activity, R.color.notavailable));
                break;
            default:
                viewHolder.name.setTextColor(ContextCompat.getColor(activity, R.color.black87));
                break;
        }
    } else {
        viewHolder.name.setTextColor(ContextCompat.getColor(activity, R.color.black87));
    }
    if (activity.xmppConnectionService.indicateReceived()) {
        switch(message.getMergedStatus()) {
            case Message.STATUS_SEND_RECEIVED:
                viewHolder.receivedStatus.setVisibility(View.VISIBLE);
                break;
            case Message.STATUS_SEND_DISPLAYED:
                viewHolder.receivedStatus.setVisibility(View.VISIBLE);
                viewHolder.readStatus.setVisibility(View.VISIBLE);
                break;
        }
    }
    if (conversation.getMode() == Conversation.MODE_SINGLE) {
        if (conversation.getIncomingChatState().equals(ChatState.COMPOSING)) {
            viewHolder.lastMessage.setText(R.string.is_typing);
            viewHolder.lastMessage.setTypeface(null, Typeface.BOLD_ITALIC);
            viewHolder.sender.setVisibility(View.GONE);
        }
    } else {
        if (conversation.getParticipants() != null) {
            ChatState state = ChatState.COMPOSING;
            List<MucOptions.User> userWithChatStates = conversation.getMucOptions().getUsersWithChatState(state, 5);
            if (userWithChatStates.size() == 0) {
                state = ChatState.PAUSED;
                userWithChatStates = conversation.getMucOptions().getUsersWithChatState(state, 5);
            }
            if (state == ChatState.COMPOSING) {
                if (userWithChatStates.size() > 0) {
                    if (userWithChatStates.size() == 1) {
                        MucOptions.User user = userWithChatStates.get(0);
                        viewHolder.lastMessage.setText(activity.getString(R.string.contact_is_typing, UIHelper.getDisplayName(user)));
                        viewHolder.lastMessage.setTypeface(null, Typeface.BOLD_ITALIC);
                        viewHolder.sender.setVisibility(View.GONE);
                    } else {
                        StringBuilder builder = new StringBuilder();
                        for (MucOptions.User user : userWithChatStates) {
                            if (builder.length() != 0) {
                                builder.append(", ");
                            }
                            builder.append(UIHelper.getDisplayName(user));
                        }
                        viewHolder.lastMessage.setText(activity.getString(R.string.contacts_are_typing, builder.toString()));
                        viewHolder.lastMessage.setTypeface(null, Typeface.BOLD_ITALIC);
                        viewHolder.sender.setVisibility(View.GONE);
                    }
                }
            }
        }
    }
    return view;
}
Also used : ChatState(de.pixart.messenger.xmpp.chatstate.ChatState) Message(de.pixart.messenger.entities.Message) Conversation(de.pixart.messenger.entities.Conversation) XmppActivity(de.pixart.messenger.ui.XmppActivity) MucOptions(de.pixart.messenger.entities.MucOptions) LayoutInflater(android.view.LayoutInflater)

Example 45 with Message

use of de.pixart.messenger.entities.Message in project Pix-Art-Messenger by kriztan.

the class MessageAdapter method displayTextMessage.

private void displayTextMessage(final ViewHolder viewHolder, final Message message, boolean darkBackground, int type) {
    viewHolder.download_button.setVisibility(View.GONE);
    viewHolder.image.setVisibility(View.GONE);
    viewHolder.audioPlayer.setVisibility(View.GONE);
    viewHolder.messageBody.setVisibility(View.VISIBLE);
    if (darkBackground) {
        viewHolder.messageBody.setTextAppearance(getContext(), R.style.TextAppearance_Conversations_Body1_OnDark);
    } else {
        viewHolder.messageBody.setTextAppearance(getContext(), R.style.TextAppearance_Conversations_Body1);
    }
    viewHolder.messageBody.setHighlightColor(ContextCompat.getColor(activity, darkBackground ? R.color.grey800 : R.color.grey500));
    viewHolder.messageBody.setTypeface(null, Typeface.NORMAL);
    if (message.getBody() != null) {
        final String nick = UIHelper.getMessageDisplayName(message);
        SpannableStringBuilder body = message.getMergedBody();
        boolean hasMeCommand = message.hasMeCommand();
        if (mConversationFragment != null) {
            String searchQuery = mConversationFragment.binding.searchfieldInput.getText().toString().toLowerCase().trim();
            if (((!searchQuery.isEmpty() || !searchQuery.contains("")) && searchQuery.length() >= 3) && body.toString().toLowerCase().contains(searchQuery)) {
                int ofe = body.toString().toLowerCase().indexOf(searchQuery, 0);
                for (int ofs = 0; ofs < body.length() && ofe != -1; ofs = ofe + 1) {
                    ofe = body.toString().toLowerCase().indexOf(searchQuery, ofs);
                    if (ofe == -1) {
                        break;
                    } else {
                        body.setSpan(new StyleSpan(Typeface.BOLD), ofe, ofe + searchQuery.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                        body.setSpan(new BackgroundColorSpan(0xFFFFFF00), ofe, ofe + searchQuery.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    }
                }
            }
        }
        if (hasMeCommand) {
            body = body.replace(0, Message.ME_COMMAND.length(), nick);
        }
        if (body.length() > Config.MAX_DISPLAY_MESSAGE_CHARS) {
            body = new SpannableStringBuilder(body, 0, Config.MAX_DISPLAY_MESSAGE_CHARS);
            body.append("\u2026");
        }
        Message.MergeSeparator[] mergeSeparators = body.getSpans(0, body.length(), Message.MergeSeparator.class);
        for (Message.MergeSeparator mergeSeparator : mergeSeparators) {
            int start = body.getSpanStart(mergeSeparator);
            int end = body.getSpanEnd(mergeSeparator);
            body.setSpan(new DividerSpan(true), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        boolean startsWithQuote = handleTextQuotes(body, darkBackground);
        if (message.getType() != Message.TYPE_PRIVATE) {
            if (hasMeCommand) {
                body.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), 0, nick.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        } else {
            String privateMarker;
            if (message.getStatus() <= Message.STATUS_RECEIVED) {
                privateMarker = activity.getString(R.string.private_message);
            } else {
                final String to;
                if (message.getCounterpart() != null) {
                    to = message.getCounterpart().getResourcepart();
                } else {
                    to = "";
                }
                privateMarker = activity.getString(R.string.private_message_to, to);
            }
            body.insert(0, privateMarker);
            int privateMarkerIndex = privateMarker.length();
            if (startsWithQuote) {
                body.insert(privateMarkerIndex, "\n\n");
                body.setSpan(new DividerSpan(false), privateMarkerIndex, privateMarkerIndex + 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            } else {
                body.insert(privateMarkerIndex, " ");
            }
            body.setSpan(new ForegroundColorSpan(getMessageTextColor(darkBackground, false)), 0, privateMarkerIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            body.setSpan(new StyleSpan(Typeface.BOLD), 0, privateMarkerIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            if (hasMeCommand) {
                body.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), privateMarkerIndex + 1, privateMarkerIndex + 1 + nick.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
        if (message.getConversation().getMode() == Conversation.MODE_MULTI && message.getStatus() == Message.STATUS_RECEIVED) {
            Pattern pattern = NotificationService.generateNickHighlightPattern(message.getConversation().getMucOptions().getActualNick());
            Matcher matcher = pattern.matcher(body);
            while (matcher.find()) {
                body.setSpan(new StyleSpan(Typeface.BOLD), matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
        Matcher matcher = Emoticons.generatePattern(body).matcher(body);
        while (matcher.find()) {
            if (matcher.start() < matcher.end()) {
                body.setSpan(new RelativeSizeSpan(1.5f), matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
        StylingHelper.format(body, viewHolder.messageBody.getCurrentTextColor());
        Linkify.addLinks(body, XMPP_PATTERN, "xmpp", XMPPURI_MATCH_FILTER, null);
        Linkify.addLinks(body, Patterns.AUTOLINK_WEB_URL, "http", WEBURL_MATCH_FILTER, WEBURL_TRANSFORM_FILTER);
        Linkify.addLinks(body, GeoHelper.GEO_URI, "geo");
        FixedURLSpan.fix(body);
        viewHolder.messageBody.setAutoLinkMask(0);
        viewHolder.messageBody.setText(EmojiWrapper.transform(body));
        viewHolder.messageBody.setTextIsSelectable(true);
        viewHolder.messageBody.setMovementMethod(ClickableMovementMethod.getInstance());
        listSelectionManager.onUpdate(viewHolder.messageBody, message);
    } else {
        viewHolder.messageBody.setText("");
        viewHolder.messageBody.setTextIsSelectable(false);
    }
}
Also used : Pattern(java.util.regex.Pattern) Message(de.pixart.messenger.entities.Message) ForegroundColorSpan(android.text.style.ForegroundColorSpan) Matcher(java.util.regex.Matcher) SpannableString(android.text.SpannableString) RelativeSizeSpan(android.text.style.RelativeSizeSpan) StyleSpan(android.text.style.StyleSpan) DividerSpan(de.pixart.messenger.ui.text.DividerSpan) SpannableStringBuilder(android.text.SpannableStringBuilder) BackgroundColorSpan(android.text.style.BackgroundColorSpan)

Aggregations

Message (de.pixart.messenger.entities.Message)47 XmppAxolotlMessage (de.pixart.messenger.crypto.axolotl.XmppAxolotlMessage)16 Conversation (de.pixart.messenger.entities.Conversation)13 EditMessage (de.pixart.messenger.ui.widget.EditMessage)12 SuppressLint (android.annotation.SuppressLint)11 PendingIntent (android.app.PendingIntent)7 SpannableString (android.text.SpannableString)7 Account (de.pixart.messenger.entities.Account)7 Jid (de.pixart.messenger.xmpp.jid.Jid)7 ArrayList (java.util.ArrayList)5 Uri (android.net.Uri)4 Toast (android.widget.Toast)4 MessagePacket (de.pixart.messenger.xmpp.stanzas.MessagePacket)4 StyleSpan (android.text.style.StyleSpan)3 RelativeLayout (android.widget.RelativeLayout)3 Element (de.pixart.messenger.xml.Element)3 InvalidJidException (de.pixart.messenger.xmpp.jid.InvalidJidException)3 Date (java.util.Date)3 SharedPreferences (android.content.SharedPreferences)2 PackageManager (android.content.pm.PackageManager)2