Search in sources :

Example 1 with Conversational

use of eu.siacs.conversations.entities.Conversational in project Conversations by siacs.

the class MessageSearchTask method run.

@Override
public void run() {
    long startTimestamp = SystemClock.elapsedRealtime();
    Cursor cursor = null;
    try {
        final HashMap<String, Conversational> conversationCache = new HashMap<>();
        final List<Message> result = new ArrayList<>();
        cursor = xmppConnectionService.databaseBackend.getMessageSearchCursor(term, uuid);
        long dbTimer = SystemClock.elapsedRealtime();
        if (isCancelled) {
            Log.d(Config.LOGTAG, "canceled search task");
            return;
        }
        if (cursor != null && cursor.getCount() > 0) {
            cursor.moveToLast();
            final int indexBody = cursor.getColumnIndex(Message.BODY);
            final int indexOob = cursor.getColumnIndex(Message.OOB);
            final int indexConversation = cursor.getColumnIndex(Message.CONVERSATION);
            final int indexAccount = cursor.getColumnIndex(Conversation.ACCOUNT);
            final int indexContact = cursor.getColumnIndex(Conversation.CONTACTJID);
            final int indexMode = cursor.getColumnIndex(Conversation.MODE);
            do {
                if (isCancelled) {
                    Log.d(Config.LOGTAG, "canceled search task");
                    return;
                }
                final String body = cursor.getString(indexBody);
                final boolean oob = cursor.getInt(indexOob) > 0;
                if (MessageUtils.treatAsDownloadable(body, oob)) {
                    continue;
                }
                final String conversationUuid = cursor.getString(indexConversation);
                Conversational conversation = conversationCache.get(conversationUuid);
                if (conversation == null) {
                    String accountUuid = cursor.getString(indexAccount);
                    String contactJid = cursor.getString(indexContact);
                    int mode = cursor.getInt(indexMode);
                    conversation = findOrGenerateStub(conversationUuid, accountUuid, contactJid, mode);
                    conversationCache.put(conversationUuid, conversation);
                }
                Message message = IndividualMessage.fromCursor(cursor, conversation);
                result.add(message);
            } while (cursor.moveToPrevious());
        }
        long stopTimestamp = SystemClock.elapsedRealtime();
        Log.d(Config.LOGTAG, "found " + result.size() + " messages in " + (stopTimestamp - startTimestamp) + "ms" + " (db was " + (dbTimer - startTimestamp) + "ms)");
        onSearchResultsAvailable.onSearchResultsAvailable(term, result);
    } catch (Exception e) {
        Log.d(Config.LOGTAG, "exception while searching ", e);
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}
Also used : Conversational(eu.siacs.conversations.entities.Conversational) IndividualMessage(eu.siacs.conversations.entities.IndividualMessage) Message(eu.siacs.conversations.entities.Message) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Cursor(android.database.Cursor)

Example 2 with Conversational

use of eu.siacs.conversations.entities.Conversational in project Conversations by siacs.

the class MessageAdapter method getView.

@Override
public View getView(int position, View view, ViewGroup parent) {
    final Message message = getItem(position);
    final boolean omemoEncryption = message.getEncryption() == Message.ENCRYPTION_AXOLOTL;
    final boolean isInValidSession = message.isValidInSession() && (!omemoEncryption || message.isTrusted());
    final Conversational conversation = message.getConversation();
    final Account account = conversation.getAccount();
    final int type = getItemViewType(position);
    ViewHolder viewHolder;
    if (view == null) {
        viewHolder = new ViewHolder();
        switch(type) {
            case DATE_SEPARATOR:
                view = activity.getLayoutInflater().inflate(R.layout.message_date_bubble, parent, false);
                viewHolder.status_message = view.findViewById(R.id.message_body);
                viewHolder.message_box = view.findViewById(R.id.message_box);
                viewHolder.indicatorReceived = view.findViewById(R.id.indicator_received);
                break;
            case RTP_SESSION:
                view = activity.getLayoutInflater().inflate(R.layout.message_rtp_session, parent, false);
                viewHolder.status_message = view.findViewById(R.id.message_body);
                viewHolder.message_box = view.findViewById(R.id.message_box);
                viewHolder.indicatorReceived = view.findViewById(R.id.indicator_received);
                break;
            case SENT:
                view = activity.getLayoutInflater().inflate(R.layout.message_sent, parent, false);
                viewHolder.message_box = view.findViewById(R.id.message_box);
                viewHolder.contact_picture = view.findViewById(R.id.message_photo);
                viewHolder.download_button = view.findViewById(R.id.download_button);
                viewHolder.indicator = view.findViewById(R.id.security_indicator);
                viewHolder.edit_indicator = view.findViewById(R.id.edit_indicator);
                viewHolder.image = view.findViewById(R.id.message_image);
                viewHolder.messageBody = view.findViewById(R.id.message_body);
                viewHolder.time = view.findViewById(R.id.message_time);
                viewHolder.indicatorReceived = view.findViewById(R.id.indicator_received);
                viewHolder.audioPlayer = view.findViewById(R.id.audio_player);
                break;
            case RECEIVED:
                view = activity.getLayoutInflater().inflate(R.layout.message_received, parent, false);
                viewHolder.message_box = view.findViewById(R.id.message_box);
                viewHolder.contact_picture = view.findViewById(R.id.message_photo);
                viewHolder.download_button = view.findViewById(R.id.download_button);
                viewHolder.indicator = view.findViewById(R.id.security_indicator);
                viewHolder.edit_indicator = view.findViewById(R.id.edit_indicator);
                viewHolder.image = view.findViewById(R.id.message_image);
                viewHolder.messageBody = view.findViewById(R.id.message_body);
                viewHolder.time = view.findViewById(R.id.message_time);
                viewHolder.indicatorReceived = view.findViewById(R.id.indicator_received);
                viewHolder.encryption = view.findViewById(R.id.message_encryption);
                viewHolder.audioPlayer = view.findViewById(R.id.audio_player);
                break;
            case STATUS:
                view = activity.getLayoutInflater().inflate(R.layout.message_status, parent, false);
                viewHolder.contact_picture = view.findViewById(R.id.message_photo);
                viewHolder.status_message = view.findViewById(R.id.status_message);
                viewHolder.load_more_messages = view.findViewById(R.id.load_more_messages);
                break;
            default:
                throw new AssertionError("Unknown view type");
        }
        view.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) view.getTag();
        if (viewHolder == null) {
            return view;
        }
    }
    boolean darkBackground = type == RECEIVED && (!isInValidSession || mUseGreenBackground) || activity.isDarkTheme();
    if (type == DATE_SEPARATOR) {
        if (UIHelper.today(message.getTimeSent())) {
            viewHolder.status_message.setText(R.string.today);
        } else if (UIHelper.yesterday(message.getTimeSent())) {
            viewHolder.status_message.setText(R.string.yesterday);
        } else {
            viewHolder.status_message.setText(DateUtils.formatDateTime(activity, message.getTimeSent(), DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_YEAR));
        }
        viewHolder.message_box.setBackgroundResource(activity.isDarkTheme() ? R.drawable.date_bubble_grey : R.drawable.date_bubble_white);
        return view;
    } else if (type == RTP_SESSION) {
        final boolean isDarkTheme = activity.isDarkTheme();
        final boolean received = message.getStatus() <= Message.STATUS_RECEIVED;
        final RtpSessionStatus rtpSessionStatus = RtpSessionStatus.of(message.getBody());
        final long duration = rtpSessionStatus.duration;
        if (received) {
            if (duration > 0) {
                viewHolder.status_message.setText(activity.getString(R.string.incoming_call_duration, TimeFrameUtils.resolve(activity, duration)));
            } else if (rtpSessionStatus.successful) {
                viewHolder.status_message.setText(R.string.incoming_call);
            } else {
                viewHolder.status_message.setText(activity.getString(R.string.missed_call_timestamp, UIHelper.readableTimeDifferenceFull(activity, message.getTimeSent())));
            }
        } else {
            if (duration > 0) {
                viewHolder.status_message.setText(activity.getString(R.string.outgoing_call_duration, TimeFrameUtils.resolve(activity, duration)));
            } else {
                viewHolder.status_message.setText(R.string.outgoing_call);
            }
        }
        viewHolder.indicatorReceived.setImageResource(RtpSessionStatus.getDrawable(received, rtpSessionStatus.successful, isDarkTheme));
        viewHolder.indicatorReceived.setAlpha(isDarkTheme ? 0.7f : 0.57f);
        viewHolder.message_box.setBackgroundResource(isDarkTheme ? R.drawable.date_bubble_grey : R.drawable.date_bubble_white);
        return view;
    } else if (type == STATUS) {
        if ("LOAD_MORE".equals(message.getBody())) {
            viewHolder.status_message.setVisibility(View.GONE);
            viewHolder.contact_picture.setVisibility(View.GONE);
            viewHolder.load_more_messages.setVisibility(View.VISIBLE);
            viewHolder.load_more_messages.setOnClickListener(v -> loadMoreMessages((Conversation) message.getConversation()));
        } else {
            viewHolder.status_message.setVisibility(View.VISIBLE);
            viewHolder.load_more_messages.setVisibility(View.GONE);
            viewHolder.status_message.setText(message.getBody());
            boolean showAvatar;
            if (conversation.getMode() == Conversation.MODE_SINGLE) {
                showAvatar = true;
                AvatarWorkerTask.loadAvatar(message, viewHolder.contact_picture, R.dimen.avatar_on_status_message);
            } else if (message.getCounterpart() != null || message.getTrueCounterpart() != null || (message.getCounterparts() != null && message.getCounterparts().size() > 0)) {
                showAvatar = true;
                AvatarWorkerTask.loadAvatar(message, viewHolder.contact_picture, R.dimen.avatar_on_status_message);
            } else {
                showAvatar = false;
            }
            if (showAvatar) {
                viewHolder.contact_picture.setAlpha(0.5f);
                viewHolder.contact_picture.setVisibility(View.VISIBLE);
            } else {
                viewHolder.contact_picture.setVisibility(View.GONE);
            }
        }
        return view;
    } else {
        AvatarWorkerTask.loadAvatar(message, viewHolder.contact_picture, R.dimen.avatar);
    }
    resetClickListener(viewHolder.message_box, viewHolder.messageBody);
    viewHolder.contact_picture.setOnClickListener(v -> {
        if (MessageAdapter.this.mOnContactPictureClickedListener != null) {
            MessageAdapter.this.mOnContactPictureClickedListener.onContactPictureClicked(message);
        }
    });
    viewHolder.contact_picture.setOnLongClickListener(v -> {
        if (MessageAdapter.this.mOnContactPictureLongClickedListener != null) {
            MessageAdapter.this.mOnContactPictureLongClickedListener.onContactPictureLongClicked(v, message);
            return true;
        } else {
            return false;
        }
    });
    final Transferable transferable = message.getTransferable();
    final boolean unInitiatedButKnownSize = MessageUtils.unInitiatedButKnownSize(message);
    if (unInitiatedButKnownSize || message.isDeleted() || (transferable != null && transferable.getStatus() != Transferable.STATUS_UPLOADING)) {
        if (unInitiatedButKnownSize || transferable != null && transferable.getStatus() == Transferable.STATUS_OFFER) {
            displayDownloadableMessage(viewHolder, message, activity.getString(R.string.download_x_file, UIHelper.getFileDescriptionString(activity, message)), darkBackground);
        } else if (transferable != null && transferable.getStatus() == Transferable.STATUS_OFFER_CHECK_FILESIZE) {
            displayDownloadableMessage(viewHolder, message, activity.getString(R.string.check_x_filesize, UIHelper.getFileDescriptionString(activity, message)), darkBackground);
        } else {
            displayInfoMessage(viewHolder, UIHelper.getMessagePreview(activity, message).first, darkBackground);
        }
    } else if (message.isFileOrImage() && message.getEncryption() != Message.ENCRYPTION_PGP && message.getEncryption() != Message.ENCRYPTION_DECRYPTION_FAILED) {
        if (message.getFileParams().width > 0 && message.getFileParams().height > 0) {
            displayMediaPreviewMessage(viewHolder, message, darkBackground);
        } else if (message.getFileParams().runtime > 0) {
            displayAudioMessage(viewHolder, message, darkBackground);
        } else {
            displayOpenableMessage(viewHolder, message, darkBackground);
        }
    } else if (message.getEncryption() == Message.ENCRYPTION_PGP) {
        if (account.isPgpDecryptionServiceConnected()) {
            if (conversation instanceof Conversation && !account.hasPendingPgpIntent((Conversation) conversation)) {
                displayInfoMessage(viewHolder, activity.getString(R.string.message_decrypting), darkBackground);
            } else {
                displayInfoMessage(viewHolder, activity.getString(R.string.pgp_message), darkBackground);
            }
        } else {
            displayInfoMessage(viewHolder, activity.getString(R.string.install_openkeychain), darkBackground);
            viewHolder.message_box.setOnClickListener(this::promptOpenKeychainInstall);
            viewHolder.messageBody.setOnClickListener(this::promptOpenKeychainInstall);
        }
    } else if (message.getEncryption() == Message.ENCRYPTION_DECRYPTION_FAILED) {
        displayInfoMessage(viewHolder, activity.getString(R.string.decryption_failed), darkBackground);
    } else if (message.getEncryption() == Message.ENCRYPTION_AXOLOTL_NOT_FOR_THIS_DEVICE) {
        displayInfoMessage(viewHolder, activity.getString(R.string.not_encrypted_for_this_device), darkBackground);
    } else if (message.getEncryption() == Message.ENCRYPTION_AXOLOTL_FAILED) {
        displayInfoMessage(viewHolder, activity.getString(R.string.omemo_decryption_failed), darkBackground);
    } else {
        if (message.isGeoUri()) {
            displayLocationMessage(viewHolder, message, darkBackground);
        } else if (message.bodyIsOnlyEmojis() && message.getType() != Message.TYPE_PRIVATE) {
            displayEmojiMessage(viewHolder, message.getBody().trim(), darkBackground);
        } else if (message.treatAsDownloadable()) {
            try {
                final URI uri = new URI(message.getBody());
                displayDownloadableMessage(viewHolder, message, activity.getString(R.string.check_x_filesize_on_host, UIHelper.getFileDescriptionString(activity, message), uri.getHost()), darkBackground);
            } catch (Exception e) {
                displayDownloadableMessage(viewHolder, message, activity.getString(R.string.check_x_filesize, UIHelper.getFileDescriptionString(activity, message)), darkBackground);
            }
        } else {
            displayTextMessage(viewHolder, message, darkBackground, type);
        }
    }
    if (type == RECEIVED) {
        if (isInValidSession) {
            int bubble;
            if (!mUseGreenBackground) {
                bubble = activity.getThemeResource(R.attr.message_bubble_received_monochrome, R.drawable.message_bubble_received_white);
            } else {
                bubble = activity.getThemeResource(R.attr.message_bubble_received_green, R.drawable.message_bubble_received);
            }
            viewHolder.message_box.setBackgroundResource(bubble);
            viewHolder.encryption.setVisibility(View.GONE);
        } else {
            viewHolder.message_box.setBackgroundResource(R.drawable.message_bubble_received_warning);
            viewHolder.encryption.setVisibility(View.VISIBLE);
            if (omemoEncryption && !message.isTrusted()) {
                viewHolder.encryption.setText(R.string.not_trusted);
            } else {
                viewHolder.encryption.setText(CryptoHelper.encryptionTypeToText(message.getEncryption()));
            }
        }
    }
    displayStatus(viewHolder, message, type, darkBackground);
    return view;
}
Also used : Conversational(eu.siacs.conversations.entities.Conversational) Account(eu.siacs.conversations.entities.Account) Message(eu.siacs.conversations.entities.Message) RtpSessionStatus(eu.siacs.conversations.entities.RtpSessionStatus) Transferable(eu.siacs.conversations.entities.Transferable) Conversation(eu.siacs.conversations.entities.Conversation) URI(java.net.URI)

Example 3 with Conversational

use of eu.siacs.conversations.entities.Conversational in project Conversations by siacs.

the class JingleConnectionManager method writeMessage.

private void writeMessage(final Message message) {
    final Conversational conversational = message.getConversation();
    if (conversational instanceof Conversation) {
        ((Conversation) conversational).add(message);
        mXmppConnectionService.databaseBackend.createMessage(message);
        mXmppConnectionService.updateConversationUi();
    } else {
        throw new IllegalStateException("Somehow the conversation in a message was a stub");
    }
}
Also used : Conversational(eu.siacs.conversations.entities.Conversational) Conversation(eu.siacs.conversations.entities.Conversation)

Example 4 with Conversational

use of eu.siacs.conversations.entities.Conversational in project Conversations by siacs.

the class JingleRtpConnection method writeMessage.

private void writeMessage() {
    final Conversational conversational = message.getConversation();
    if (conversational instanceof Conversation) {
        ((Conversation) conversational).add(this.message);
        xmppConnectionService.createMessageAsync(message);
        xmppConnectionService.updateConversationUi();
    } else {
        throw new IllegalStateException("Somehow the conversation in a message was a stub");
    }
}
Also used : Conversational(eu.siacs.conversations.entities.Conversational) Conversation(eu.siacs.conversations.entities.Conversation)

Example 5 with Conversational

use of eu.siacs.conversations.entities.Conversational in project Conversations by siacs.

the class UIHelper method getMessageDisplayName.

public static String getMessageDisplayName(final Message message) {
    final Conversational conversation = message.getConversation();
    if (message.getStatus() == Message.STATUS_RECEIVED) {
        final Contact contact = message.getContact();
        if (conversation.getMode() == Conversation.MODE_MULTI) {
            if (contact != null) {
                return contact.getDisplayName();
            } else {
                return getDisplayedMucCounterpart(message.getCounterpart());
            }
        } else {
            return contact != null ? contact.getDisplayName() : "";
        }
    } else {
        if (conversation instanceof Conversation && conversation.getMode() == Conversation.MODE_MULTI) {
            return ((Conversation) conversation).getMucOptions().getSelf().getName();
        } else {
            final Account account = conversation.getAccount();
            final Jid jid = account.getJid();
            final String displayName = account.getDisplayName();
            if (Strings.isNullOrEmpty(displayName)) {
                return jid.getLocal() != null ? jid.getLocal() : jid.getDomain().toString();
            } else {
                return displayName;
            }
        }
    }
}
Also used : Conversational(eu.siacs.conversations.entities.Conversational) Account(eu.siacs.conversations.entities.Account) Jid(eu.siacs.conversations.xmpp.Jid) Conversation(eu.siacs.conversations.entities.Conversation) Contact(eu.siacs.conversations.entities.Contact)

Aggregations

Conversational (eu.siacs.conversations.entities.Conversational)8 Conversation (eu.siacs.conversations.entities.Conversation)5 Account (eu.siacs.conversations.entities.Account)3 Contact (eu.siacs.conversations.entities.Contact)3 Message (eu.siacs.conversations.entities.Message)2 Jid (eu.siacs.conversations.xmpp.Jid)2 ArrayList (java.util.ArrayList)2 Intent (android.content.Intent)1 Cursor (android.database.Cursor)1 IndividualMessage (eu.siacs.conversations.entities.IndividualMessage)1 MucOptions (eu.siacs.conversations.entities.MucOptions)1 RtpSessionStatus (eu.siacs.conversations.entities.RtpSessionStatus)1 Transferable (eu.siacs.conversations.entities.Transferable)1 URI (java.net.URI)1 HashMap (java.util.HashMap)1 GeoPoint (org.osmdroid.util.GeoPoint)1