Search in sources :

Example 26 with Message

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

the class NotificationService method modifyForTextOnly.

private void modifyForTextOnly(final Builder builder, final ArrayList<Message> messages) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        NotificationCompat.MessagingStyle messagingStyle = new NotificationCompat.MessagingStyle(mXmppConnectionService.getString(R.string.me));
        Conversation conversation = messages.get(0).getConversation();
        if (conversation.getMode() == Conversation.MODE_MULTI) {
            messagingStyle.setConversationTitle(conversation.getName());
        }
        for (Message message : messages) {
            String sender = message.getStatus() == Message.STATUS_RECEIVED ? UIHelper.getMessageDisplayName(message) : null;
            messagingStyle.addMessage(UIHelper.getMessagePreview(mXmppConnectionService, message).first, message.getTimeSent(), sender);
        }
        builder.setStyle(messagingStyle);
    } else {
        if (messages.get(0).getConversation().getMode() == Conversation.MODE_SINGLE) {
            builder.setStyle(new NotificationCompat.BigTextStyle().bigText(getMergedBodies(messages)));
            builder.setContentText(UIHelper.getMessagePreview(mXmppConnectionService, messages.get((messages.size() - 1))).first);
        } else {
            final NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle();
            SpannableString styledString;
            for (Message message : messages) {
                final String name = UIHelper.getMessageDisplayName(message);
                styledString = new SpannableString(name + ": " + message.getBody());
                styledString.setSpan(new StyleSpan(Typeface.BOLD), 0, name.length(), 0);
                style.addLine(styledString);
            }
            builder.setStyle(style);
            int count = messages.size();
            if (count == 1) {
                final String name = UIHelper.getMessageDisplayName(messages.get(0));
                styledString = new SpannableString(name + ": " + messages.get(0).getBody());
                styledString.setSpan(new StyleSpan(Typeface.BOLD), 0, name.length(), 0);
                builder.setContentText(styledString);
            } else {
                builder.setContentText(mXmppConnectionService.getResources().getQuantityString(R.plurals.x_messages, count, count));
            }
        }
    }
}
Also used : SpannableString(android.text.SpannableString) Message(eu.siacs.conversations.entities.Message) StyleSpan(android.text.style.StyleSpan) NotificationCompat(android.support.v4.app.NotificationCompat) Conversation(eu.siacs.conversations.entities.Conversation) SpannableString(android.text.SpannableString)

Example 27 with Message

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

the class XmppConnectionService method attachImageToConversation.

public void attachImageToConversation(final Conversation conversation, final Uri uri, final UiCallback<Message> callback) {
    if (FileBackend.weOwnFile(this, uri)) {
        Log.d(Config.LOGTAG, "trying to attach file that belonged to us");
        callback.error(R.string.security_error_invalid_file_access, null);
        return;
    }
    final String mimeType = MimeUtils.guessMimeTypeFromUri(this, uri);
    final String compressPictures = getCompressPicturesPreference();
    if ("never".equals(compressPictures) || ("auto".equals(compressPictures) && getFileBackend().useImageAsIs(uri)) || (mimeType != null && mimeType.endsWith("/gif"))) {
        Log.d(Config.LOGTAG, conversation.getAccount().getJid().toBareJid() + ": not compressing picture. sending as file");
        attachFileToConversation(conversation, uri, callback);
        return;
    }
    final Message message;
    if (conversation.getNextEncryption() == Message.ENCRYPTION_PGP) {
        message = new Message(conversation, "", Message.ENCRYPTION_DECRYPTED);
    } else {
        message = new Message(conversation, "", conversation.getNextEncryption());
    }
    message.setCounterpart(conversation.getNextCounterpart());
    message.setType(Message.TYPE_IMAGE);
    mFileAddingExecutor.execute(new Runnable() {

        @Override
        public void run() {
            try {
                getFileBackend().copyImageToPrivateStorage(message, uri);
                if (conversation.getNextEncryption() == Message.ENCRYPTION_PGP) {
                    final PgpEngine pgpEngine = getPgpEngine();
                    if (pgpEngine != null) {
                        pgpEngine.encrypt(message, callback);
                    } else if (callback != null) {
                        callback.error(R.string.unable_to_connect_to_keychain, null);
                    }
                } else {
                    callback.success(message);
                }
            } catch (final FileBackend.FileCopyException e) {
                callback.error(e.getResId(), message);
            }
        }
    });
}
Also used : XmppAxolotlMessage(eu.siacs.conversations.crypto.axolotl.XmppAxolotlMessage) Message(eu.siacs.conversations.entities.Message) PgpEngine(eu.siacs.conversations.crypto.PgpEngine)

Example 28 with Message

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

the class ConversationFragment method onCreateView.

@Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final View view = inflater.inflate(R.layout.fragment_conversation, container, false);
    view.setOnClickListener(null);
    String[] allImagesMimeType = { "image/*" };
    mEditMessage = (EditMessage) view.findViewById(R.id.textinput);
    mEditMessage.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (activity != null) {
                activity.hideConversationsOverview();
            }
        }
    });
    mEditMessage.setOnEditorActionListener(mEditorActionListener);
    mEditMessage.setRichContentListener(allImagesMimeType, mEditorContentListener);
    mSendButton = (ImageButton) view.findViewById(R.id.textSendButton);
    mSendButton.setOnClickListener(this.mSendButtonListener);
    snackbar = (RelativeLayout) view.findViewById(R.id.snackbar);
    snackbarMessage = (TextView) view.findViewById(R.id.snackbar_message);
    snackbarAction = (TextView) view.findViewById(R.id.snackbar_action);
    messagesView = (ListView) view.findViewById(R.id.messages_view);
    messagesView.setOnScrollListener(mOnScrollListener);
    messagesView.setTranscriptMode(ListView.TRANSCRIPT_MODE_NORMAL);
    messageListAdapter = new MessageAdapter((ConversationActivity) getActivity(), this.messageList);
    messageListAdapter.setOnContactPictureClicked(new OnContactPictureClicked() {

        @Override
        public void onContactPictureClicked(Message message) {
            if (message.getStatus() <= Message.STATUS_RECEIVED) {
                if (message.getConversation().getMode() == Conversation.MODE_MULTI) {
                    Jid user = message.getCounterpart();
                    if (user != null && !user.isBareJid()) {
                        if (!message.getConversation().getMucOptions().isUserInRoom(user)) {
                            Toast.makeText(activity, activity.getString(R.string.user_has_left_conference, user.getResourcepart()), Toast.LENGTH_SHORT).show();
                        }
                        highlightInConference(user.getResourcepart());
                    }
                } else {
                    if (!message.getContact().isSelf()) {
                        String fingerprint;
                        if (message.getEncryption() == Message.ENCRYPTION_PGP || message.getEncryption() == Message.ENCRYPTION_DECRYPTED) {
                            fingerprint = "pgp";
                        } else {
                            fingerprint = message.getFingerprint();
                        }
                        activity.switchToContactDetails(message.getContact(), fingerprint);
                    }
                }
            } else {
                Account account = message.getConversation().getAccount();
                Intent intent;
                if (activity.manuallyChangePresence()) {
                    intent = new Intent(activity, SetPresenceActivity.class);
                    intent.putExtra(SetPresenceActivity.EXTRA_ACCOUNT, account.getJid().toBareJid().toString());
                } else {
                    intent = new Intent(activity, EditAccountActivity.class);
                    intent.putExtra("jid", account.getJid().toBareJid().toString());
                    String fingerprint;
                    if (message.getEncryption() == Message.ENCRYPTION_PGP || message.getEncryption() == Message.ENCRYPTION_DECRYPTED) {
                        fingerprint = "pgp";
                    } else {
                        fingerprint = message.getFingerprint();
                    }
                    intent.putExtra("fingerprint", fingerprint);
                }
                startActivity(intent);
            }
        }
    });
    messageListAdapter.setOnContactPictureLongClicked(new OnContactPictureLongClicked() {

        @Override
        public void onContactPictureLongClicked(Message message) {
            if (message.getStatus() <= Message.STATUS_RECEIVED) {
                if (message.getConversation().getMode() == Conversation.MODE_MULTI) {
                    Jid user = message.getCounterpart();
                    if (user != null && !user.isBareJid()) {
                        if (message.getConversation().getMucOptions().isUserInRoom(user)) {
                            privateMessageWith(user);
                        } else {
                            Toast.makeText(activity, activity.getString(R.string.user_has_left_conference, user.getResourcepart()), Toast.LENGTH_SHORT).show();
                        }
                    }
                }
            } else {
                activity.showQrCode();
            }
        }
    });
    messageListAdapter.setOnQuoteListener(new MessageAdapter.OnQuoteListener() {

        @Override
        public void onQuote(String text) {
            if (mEditMessage.isEnabled()) {
                text = text.replaceAll("(\n *){2,}", "\n").replaceAll("(^|\n)", "$1> ").replaceAll("\n$", "");
                Editable editable = mEditMessage.getEditableText();
                int position = mEditMessage.getSelectionEnd();
                if (position == -1)
                    position = editable.length();
                if (position > 0 && editable.charAt(position - 1) != '\n') {
                    editable.insert(position++, "\n");
                }
                editable.insert(position, text);
                position += text.length();
                editable.insert(position++, "\n");
                if (position < editable.length() && editable.charAt(position) != '\n') {
                    editable.insert(position, "\n");
                }
                mEditMessage.setSelection(position);
                mEditMessage.requestFocus();
                InputMethodManager inputMethodManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                if (inputMethodManager != null) {
                    inputMethodManager.showSoftInput(mEditMessage, InputMethodManager.SHOW_IMPLICIT);
                }
            }
        }
    });
    messagesView.setAdapter(messageListAdapter);
    registerForContextMenu(messagesView);
    return view;
}
Also used : MessageAdapter(eu.siacs.conversations.ui.adapter.MessageAdapter) Account(eu.siacs.conversations.entities.Account) Message(eu.siacs.conversations.entities.Message) Jid(eu.siacs.conversations.xmpp.jid.Jid) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent) InputMethodManager(android.view.inputmethod.InputMethodManager) View(android.view.View) AdapterView(android.widget.AdapterView) TextView(android.widget.TextView) ListView(android.widget.ListView) AbsListView(android.widget.AbsListView) OnContactPictureLongClicked(eu.siacs.conversations.ui.adapter.MessageAdapter.OnContactPictureLongClicked) OnContactPictureClicked(eu.siacs.conversations.ui.adapter.MessageAdapter.OnContactPictureClicked) OnClickListener(android.view.View.OnClickListener) Editable(android.text.Editable)

Example 29 with Message

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

the class ConversationFragment method reInit.

public boolean reInit(Conversation conversation) {
    if (conversation == null) {
        return false;
    }
    this.activity = (ConversationActivity) getActivity();
    setupIme();
    if (this.conversation != null) {
        final String msg = mEditMessage.getText().toString();
        this.conversation.setNextMessage(msg);
        if (this.conversation != conversation) {
            updateChatState(this.conversation, msg);
        }
        this.conversation.trim();
    }
    this.conversation = conversation;
    boolean canWrite = this.conversation.getMode() == Conversation.MODE_SINGLE || this.conversation.getMucOptions().participating();
    this.mEditMessage.setEnabled(canWrite);
    this.mSendButton.setEnabled(canWrite);
    this.mEditMessage.setKeyboardListener(null);
    this.mEditMessage.setText("");
    this.mEditMessage.append(this.conversation.getNextMessage());
    this.mEditMessage.setKeyboardListener(this);
    messageListAdapter.updatePreferences();
    this.messagesView.setAdapter(messageListAdapter);
    updateMessages();
    this.conversation.messagesLoaded.set(true);
    synchronized (this.messageList) {
        final Message first = conversation.getFirstUnreadMessage();
        final int bottom = Math.max(0, this.messageList.size() - 1);
        final int pos;
        if (first == null) {
            pos = bottom;
        } else {
            int i = getIndexOf(first.getUuid(), this.messageList);
            pos = i < 0 ? bottom : i;
        }
        messagesView.setSelection(pos);
        return pos == bottom;
    }
}
Also used : Message(eu.siacs.conversations.entities.Message)

Example 30 with Message

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

the class ConversationActivity method handleViewConversationIntent.

private void handleViewConversationIntent(final Intent intent) {
    final String uuid = intent.getStringExtra(CONVERSATION);
    final String downloadUuid = intent.getStringExtra(EXTRA_DOWNLOAD_UUID);
    final String text = intent.getStringExtra(TEXT);
    final String nick = intent.getStringExtra(NICK);
    final boolean pm = intent.getBooleanExtra(PRIVATE_MESSAGE, false);
    if (selectConversationByUuid(uuid)) {
        this.mConversationFragment.reInit(getSelectedConversation());
        if (nick != null) {
            if (pm) {
                Jid jid = getSelectedConversation().getJid();
                try {
                    Jid next = Jid.fromParts(jid.getLocalpart(), jid.getDomainpart(), nick);
                    this.mConversationFragment.privateMessageWith(next);
                } catch (final InvalidJidException ignored) {
                //do nothing
                }
            } else {
                this.mConversationFragment.highlightInConference(nick);
            }
        } else {
            this.mConversationFragment.appendText(text);
        }
        hideConversationsOverview();
        mUnprocessedNewIntent = false;
        openConversation();
        if (mContentView instanceof SlidingPaneLayout) {
            //fixes bug where slp isn't properly closed yet
            updateActionBarTitle(true);
        }
        if (downloadUuid != null) {
            final Message message = mSelectedConversation.findMessageWithFileAndUuid(downloadUuid);
            if (message != null) {
                startDownloadable(message);
            }
        }
    } else {
        mUnprocessedNewIntent = false;
    }
}
Also used : Jid(eu.siacs.conversations.xmpp.jid.Jid) Message(eu.siacs.conversations.entities.Message) InvalidJidException(eu.siacs.conversations.xmpp.jid.InvalidJidException) SlidingPaneLayout(android.support.v4.widget.SlidingPaneLayout)

Aggregations

Message (eu.siacs.conversations.entities.Message)39 XmppAxolotlMessage (eu.siacs.conversations.crypto.axolotl.XmppAxolotlMessage)16 Conversation (eu.siacs.conversations.entities.Conversation)12 Account (eu.siacs.conversations.entities.Account)9 Jid (eu.siacs.conversations.xmpp.jid.Jid)7 PendingIntent (android.app.PendingIntent)5 InvalidJidException (eu.siacs.conversations.xmpp.jid.InvalidJidException)5 MessagePacket (eu.siacs.conversations.xmpp.stanzas.MessagePacket)5 ArrayList (java.util.ArrayList)5 SuppressLint (android.annotation.SuppressLint)4 SpannableString (android.text.SpannableString)4 NotificationCompat (android.support.v4.app.NotificationCompat)3 StyleSpan (android.text.style.StyleSpan)3 View (android.view.View)3 TextView (android.widget.TextView)3 PgpEngine (eu.siacs.conversations.crypto.PgpEngine)3 Contact (eu.siacs.conversations.entities.Contact)3 Element (eu.siacs.conversations.xml.Element)3 Cursor (android.database.Cursor)2 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)2