Search in sources :

Example 11 with MessageItem

use of com.xabber.android.data.database.messagerealm.MessageItem in project xabber-android by redsolution.

the class MessageTable method createMessageItem.

public static MessageItem createMessageItem(Cursor cursor) throws XmppStringprepException, UserJid.UserJidCreateException {
    MessageItem messageItem = new MessageItem();
    messageItem.setAccount(AccountJid.from(getAccount(cursor)));
    messageItem.setUser(UserJid.from(getUser(cursor)));
    messageItem.setResource(Resourcepart.from(getResource(cursor)));
    messageItem.setText(getText(cursor));
    ChatAction action = getAction(cursor);
    if (action != null) {
        messageItem.setAction(action.name());
    }
    messageItem.setTimestamp(getTimeStamp(cursor));
    messageItem.setDelayTimestamp(getDelayTimeStamp(cursor));
    messageItem.setIncoming(isIncoming(cursor));
    messageItem.setRead(isRead(cursor));
    messageItem.setSent(isSent(cursor));
    messageItem.setError(hasError(cursor));
    messageItem.setStanzaId(getStanzaId(cursor));
    messageItem.setReceivedFromMessageArchive(getReceivedFromMessageArchive(cursor));
    return messageItem;
}
Also used : MessageItem(com.xabber.android.data.database.messagerealm.MessageItem) ChatAction(com.xabber.android.data.message.ChatAction)

Example 12 with MessageItem

use of com.xabber.android.data.database.messagerealm.MessageItem in project xabber-android by redsolution.

the class ChatMessageAdapter method setUpImage.

private void setUpImage(MessageItem messageItem, final Message messageHolder) {
    messageHolder.messageImage.setVisibility(View.GONE);
    if (!messageItem.isImage() || !SettingsManager.connectionLoadImages()) {
        return;
    }
    if (messageItem.getFilePath() != null) {
        boolean result = FileManager.loadImageFromFile(context, messageItem.getFilePath(), messageHolder.messageImage);
        if (result) {
            messageHolder.messageImage.setVisibility(View.VISIBLE);
            messageHolder.messageText.setVisibility(View.GONE);
        } else {
            final String uniqueId = messageItem.getUniqueId();
            final Realm realm = MessageDatabaseManager.getInstance().getRealmUiThread();
            realm.executeTransactionAsync(new Realm.Transaction() {

                @Override
                public void execute(Realm realm) {
                    MessageItem first = realm.where(MessageItem.class).equalTo(MessageItem.Fields.UNIQUE_ID, uniqueId).findFirst();
                    if (first != null) {
                        first.setFilePath(null);
                    }
                }
            });
        }
    } else {
        final ViewGroup.LayoutParams layoutParams = messageHolder.messageImage.getLayoutParams();
        Integer imageWidth = messageItem.getImageWidth();
        Integer imageHeight = messageItem.getImageHeight();
        if (imageWidth != null && imageHeight != null) {
            FileManager.scaleImage(layoutParams, imageHeight, imageWidth);
            Glide.with(context).load(messageItem.getText()).listener(new RequestListener<String, GlideDrawable>() {

                @Override
                public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                    messageHolder.messageImage.setVisibility(View.GONE);
                    messageHolder.messageText.setVisibility(View.VISIBLE);
                    return true;
                }

                @Override
                public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                    return false;
                }
            }).into(messageHolder.messageImage);
            messageHolder.messageImage.setVisibility(View.VISIBLE);
            messageHolder.messageText.setVisibility(View.GONE);
        } else {
            final String uniqueId = messageItem.getUniqueId();
            Glide.with(context).load(messageItem.getText()).asBitmap().into(new SimpleTarget<Bitmap>() {

                @Override
                public void onResourceReady(final Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                    final int width = resource.getWidth();
                    final int height = resource.getHeight();
                    if (width <= 0 || height <= 0) {
                        messageHolder.messageImage.setVisibility(View.GONE);
                        messageHolder.messageText.setVisibility(View.VISIBLE);
                        return;
                    }
                    final Realm realm = MessageDatabaseManager.getInstance().getRealmUiThread();
                    realm.executeTransactionAsync(new Realm.Transaction() {

                        @Override
                        public void execute(Realm realm) {
                            MessageItem first = realm.where(MessageItem.class).equalTo(MessageItem.Fields.UNIQUE_ID, uniqueId).findFirst();
                            if (first != null) {
                                first.setImageWidth(width);
                                first.setImageHeight(height);
                            }
                        }
                    });
                    FileManager.scaleImage(layoutParams, height, width);
                    messageHolder.messageImage.setImageBitmap(resource);
                    messageHolder.messageImage.setVisibility(View.VISIBLE);
                    messageHolder.messageText.setVisibility(View.GONE);
                }
            });
        }
    }
}
Also used : MessageItem(com.xabber.android.data.database.messagerealm.MessageItem) RequestListener(com.bumptech.glide.request.RequestListener) ViewGroup(android.view.ViewGroup) SimpleTarget(com.bumptech.glide.request.target.SimpleTarget) Target(com.bumptech.glide.request.target.Target) Bitmap(android.graphics.Bitmap) Realm(io.realm.Realm) GlideDrawable(com.bumptech.glide.load.resource.drawable.GlideDrawable)

Example 13 with MessageItem

use of com.xabber.android.data.database.messagerealm.MessageItem in project xabber-android by redsolution.

the class ChatFragment method onMessageImageClick.

@Override
public void onMessageImageClick(View caller, int position) {
    MessageItem messageItem = chatMessageAdapter.getMessageItem(position);
    if (messageItem == null) {
        LogManager.w(LOG_TAG, "onMessageImageClick: null message item. Position: " + position);
        return;
    }
    Intent i = new Intent(Intent.ACTION_VIEW);
    i.setData(Uri.parse(messageItem.getText()));
    try {
        startActivity(i);
    // possible if image was not sent and don't have URL yet.
    } catch (ActivityNotFoundException e) {
        LogManager.exception(LOG_TAG, e);
    }
}
Also used : MessageItem(com.xabber.android.data.database.messagerealm.MessageItem) ActivityNotFoundException(android.content.ActivityNotFoundException) Intent(android.content.Intent)

Example 14 with MessageItem

use of com.xabber.android.data.database.messagerealm.MessageItem in project xabber-android by redsolution.

the class ChatFragment method updateFirstRemoteSyncedItemPosition.

private void updateFirstRemoteSyncedItemPosition() {
    if (!syncInfoResults.isLoaded() || !messageItems.isLoaded() || syncInfoResults.isEmpty()) {
        return;
    }
    SyncInfo syncInfo = syncInfoResults.first();
    String firstMamMessageStanzaId = syncInfo.getFirstMamMessageStanzaId();
    if (firstMamMessageStanzaId == null) {
        return;
    }
    RealmResults<MessageItem> allSorted = messageItems.where().equalTo(MessageItem.Fields.STANZA_ID, firstMamMessageStanzaId).findAllSorted(MessageItem.Fields.TIMESTAMP, Sort.ASCENDING);
    if (allSorted.isEmpty()) {
        return;
    }
    String firstRemotelySyncedMessageUniqueId = allSorted.last().getUniqueId();
    firstRemoteSyncedItemPosition = chatMessageAdapter.findMessagePosition(firstRemotelySyncedMessageUniqueId);
}
Also used : MessageItem(com.xabber.android.data.database.messagerealm.MessageItem) SyncInfo(com.xabber.android.data.database.messagerealm.SyncInfo)

Example 15 with MessageItem

use of com.xabber.android.data.database.messagerealm.MessageItem in project xabber-android by redsolution.

the class MamManager method syncMessages.

private void syncMessages(Realm realm, AbstractChat chat, final Collection<MessageItem> messagesFromServer) {
    if (messagesFromServer == null || messagesFromServer.isEmpty()) {
        return;
    }
    LogManager.i(this, "syncMessages: " + messagesFromServer.size());
    RealmResults<MessageItem> localMessages = realm.where(MessageItem.class).equalTo(MessageItem.Fields.ACCOUNT, chat.getAccount().toString()).equalTo(MessageItem.Fields.USER, chat.getUser().toString()).findAll();
    Iterator<MessageItem> iterator = messagesFromServer.iterator();
    while (iterator.hasNext()) {
        MessageItem remoteMessage = iterator.next();
        // assume that Stanza ID could be not unique
        if (localMessages.where().equalTo(MessageItem.Fields.STANZA_ID, remoteMessage.getStanzaId()).equalTo(MessageItem.Fields.TEXT, remoteMessage.getText()).count() > 0) {
            LogManager.i(this, "Sync. Removing message with same Stanza ID and text. Remote message:" + " Text: " + remoteMessage.getText() + " Timestamp: " + remoteMessage.getTimestamp() + " Delay Timestamp: " + remoteMessage.getDelayTimestamp() + " StanzaId: " + remoteMessage.getStanzaId());
            iterator.remove();
            continue;
        }
        Long remoteMessageDelayTimestamp = remoteMessage.getDelayTimestamp();
        Long remoteMessageTimestamp = remoteMessage.getTimestamp();
        RealmResults<MessageItem> sameTextMessages = localMessages.where().equalTo(MessageItem.Fields.TEXT, remoteMessage.getText()).findAll();
        if (isTimeStampSimilar(sameTextMessages, remoteMessageTimestamp)) {
            LogManager.i(this, "Sync. Found messages with same text and similar remote timestamp. Removing. Remote message:" + " Text: " + remoteMessage.getText() + " Timestamp: " + remoteMessage.getTimestamp() + " Delay Timestamp: " + remoteMessage.getDelayTimestamp() + " StanzaId: " + remoteMessage.getStanzaId());
            iterator.remove();
            continue;
        }
        if (remoteMessageDelayTimestamp != null && isTimeStampSimilar(sameTextMessages, remoteMessageDelayTimestamp)) {
            LogManager.i(this, "Sync. Found messages with same text and similar remote delay timestamp. Removing. Remote message:" + " Text: " + remoteMessage.getText() + " Timestamp: " + remoteMessage.getTimestamp() + " Delay Timestamp: " + remoteMessage.getDelayTimestamp() + " StanzaId: " + remoteMessage.getStanzaId());
            iterator.remove();
            continue;
        }
    }
    realm.beginTransaction();
    realm.copyToRealm(messagesFromServer);
    realm.commitTransaction();
}
Also used : MessageItem(com.xabber.android.data.database.messagerealm.MessageItem)

Aggregations

MessageItem (com.xabber.android.data.database.messagerealm.MessageItem)24 Realm (io.realm.Realm)14 Date (java.util.Date)6 UserJid (com.xabber.android.data.entity.UserJid)4 AccountItem (com.xabber.android.data.account.AccountItem)3 ArrayList (java.util.ArrayList)3 Message (org.jivesoftware.smack.packet.Message)3 DelayInformation (org.jivesoftware.smackx.delay.packet.DelayInformation)3 NetworkException (com.xabber.android.data.NetworkException)2 SyncInfo (com.xabber.android.data.database.messagerealm.SyncInfo)2 AccountJid (com.xabber.android.data.entity.AccountJid)2 AbstractChat (com.xabber.android.data.message.AbstractChat)2 ChatAction (com.xabber.android.data.message.ChatAction)2 RealmResults (io.realm.RealmResults)2 File (java.io.File)2 IOException (java.io.IOException)2 ActivityNotFoundException (android.content.ActivityNotFoundException)1 Intent (android.content.Intent)1 Cursor (android.database.Cursor)1 Bitmap (android.graphics.Bitmap)1