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;
}
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);
}
});
}
}
}
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);
}
}
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);
}
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();
}
Aggregations