use of com.xabber.android.data.database.messagerealm.MessageItem in project xabber-android by redsolution.
the class ContactListPresenter method getChatsGroup.
/**
* @param chats which must be filtered
* @param state for which you want to filter
* @return GroupConfiguration that may contains recent, unread or archived chats.
*/
private GroupConfiguration getChatsGroup(Collection<AbstractChat> chats, ChatListState state) {
GroupConfiguration chatsGroup = new GroupConfiguration(GroupManager.NO_ACCOUNT, GroupVO.RECENT_CHATS_TITLE, GroupManager.getInstance());
List<AbstractChat> newChats = new ArrayList<>();
int unreadMessageCount = 0;
for (AbstractChat abstractChat : chats) {
MessageItem lastMessage = abstractChat.getLastMessage();
if (lastMessage != null && !TextUtils.isEmpty(lastMessage.getText())) {
AccountItem accountItem = AccountManager.getInstance().getAccount(abstractChat.getAccount());
if (accountItem != null && accountItem.isEnabled()) {
int unread = abstractChat.getUnreadMessageCount();
if (abstractChat.notifyAboutMessage())
unreadMessageCount = unreadMessageCount + unread;
switch(state) {
case unread:
if (!abstractChat.isArchived() && unread > 0)
newChats.add(abstractChat);
break;
case archived:
if (abstractChat.isArchived())
newChats.add(abstractChat);
break;
default:
// recent
if (!abstractChat.isArchived())
newChats.add(abstractChat);
break;
}
}
}
}
EventBus.getDefault().post(new UpdateUnreadCountEvent(unreadMessageCount));
Collections.sort(newChats, ChatComparator.CHAT_COMPARATOR);
chatsGroup.setNotEmpty();
int itemsCount = 0;
for (AbstractChat chat : newChats) {
if (itemsCount < MAX_RECENT_ITEMS || state != ChatListState.recent) {
chatsGroup.addAbstractContact(RosterManager.getInstance().getBestContact(chat.getAccount(), chat.getUser()));
chatsGroup.increment(true);
itemsCount++;
} else
break;
}
return chatsGroup;
}
use of com.xabber.android.data.database.messagerealm.MessageItem in project xabber-android by redsolution.
the class ContactVO method convert.
public static ContactVO convert(AbstractContact contact, ContactClickListener listener) {
boolean showOfflineShadow;
int accountColorIndicator;
int accountColorIndicatorBack;
Drawable avatar;
int statusLevel;
int mucIndicatorLevel;
boolean isOutgoing = false;
Date time = null;
int messageStatus = 0;
int unreadCount = 0;
String messageOwner = null;
AccountItem accountItem = AccountManager.getInstance().getAccount(contact.getAccount());
if (accountItem != null && accountItem.getState() == ConnectionState.connected) {
showOfflineShadow = false;
} else {
showOfflineShadow = true;
}
accountColorIndicator = ColorManager.getInstance().getAccountPainter().getAccountMainColor(contact.getAccount());
accountColorIndicatorBack = ColorManager.getInstance().getAccountPainter().getAccountIndicatorBackColor(contact.getAccount());
avatar = contact.getAvatarForContactList();
String name = contact.getName();
if (MUCManager.getInstance().hasRoom(contact.getAccount(), contact.getUser())) {
mucIndicatorLevel = 1;
} else if (MUCManager.getInstance().isMucPrivateChat(contact.getAccount(), contact.getUser())) {
mucIndicatorLevel = 2;
} else {
mucIndicatorLevel = 0;
}
statusLevel = contact.getStatusMode().getStatusLevel();
String messageText;
String statusText = contact.getStatusText().trim();
int statusId = contact.getStatusMode().getStringID();
MessageManager messageManager = MessageManager.getInstance();
AbstractChat chat = messageManager.getOrCreateChat(contact.getAccount(), contact.getUser());
MessageItem lastMessage = chat.getLastMessage();
if (lastMessage == null) {
messageText = statusText;
} else {
if (lastMessage.getFilePath() != null) {
messageText = new File(lastMessage.getFilePath()).getName();
} else {
messageText = lastMessage.getText().trim();
}
time = new Date(lastMessage.getTimestamp());
isOutgoing = !lastMessage.isIncoming();
if ((mucIndicatorLevel == 1 || mucIndicatorLevel == 2) && lastMessage.isIncoming() && lastMessage.getText() != null && !lastMessage.getText().trim().isEmpty())
messageOwner = lastMessage.getResource().toString();
// message status
if (lastMessage.isForwarded()) {
messageStatus = 1;
} else if (lastMessage.isReceivedFromMessageArchive()) {
messageStatus = 2;
} else if (lastMessage.isError()) {
messageStatus = 3;
} else if (!lastMessage.isDelivered()) {
if (lastMessage.isAcknowledged()) {
messageStatus = 4;
} else {
messageStatus = 5;
}
}
}
if (!isOutgoing)
unreadCount = chat.getUnreadMessageCount();
// notification icon
NotificationState.NotificationMode mode = NotificationState.NotificationMode.bydefault;
boolean defaultValue = mucIndicatorLevel == 0 ? SettingsManager.eventsOnChat() : SettingsManager.eventsOnMuc();
if (chat.getNotificationState().getMode() == NotificationState.NotificationMode.enabled && !defaultValue)
mode = NotificationState.NotificationMode.enabled;
if (chat.getNotificationState().getMode() == NotificationState.NotificationMode.disabled && defaultValue)
mode = NotificationState.NotificationMode.disabled;
return new ContactVO(accountColorIndicator, accountColorIndicatorBack, showOfflineShadow, name, statusText, statusId, statusLevel, avatar, mucIndicatorLevel, contact.getUser(), contact.getAccount(), unreadCount, !chat.notifyAboutMessage(), mode, messageText, isOutgoing, time, messageStatus, messageOwner, chat.isArchived(), listener);
}
use of com.xabber.android.data.database.messagerealm.MessageItem in project xabber-android by redsolution.
the class MamManager method requestFullChatHistory.
/**
* Only for debugging
* Call only from background thread
* @param chat
*/
public void requestFullChatHistory(final AbstractChat chat) {
if (chat == null || chat.isRemotePreviousHistoryCompletelyLoaded()) {
return;
}
final AccountItem accountItem = AccountManager.getInstance().getAccount(chat.getAccount());
if (accountItem == null || !accountItem.getFactualStatusMode().isOnline()) {
return;
}
if (!checkSupport(accountItem)) {
return;
}
String firstMamMessageMamId;
boolean remoteHistoryCompletelyLoaded;
{
Realm realm = MessageDatabaseManager.getInstance().getNewBackgroundRealm();
SyncInfo syncInfo = getSyncInfo(realm, chat.getAccount(), chat.getUser());
firstMamMessageMamId = syncInfo.getFirstMamMessageMamId();
remoteHistoryCompletelyLoaded = syncInfo.isRemoteHistoryCompletelyLoaded();
realm.close();
}
if (remoteHistoryCompletelyLoaded) {
chat.setRemotePreviousHistoryCompletelyLoaded(true);
}
if (firstMamMessageMamId == null || remoteHistoryCompletelyLoaded) {
return;
}
org.jivesoftware.smackx.mam.MamManager mamManager = org.jivesoftware.smackx.mam.MamManager.getInstanceFor(accountItem.getConnection());
final org.jivesoftware.smackx.mam.MamManager.MamQueryResult mamQueryResult;
try {
LogManager.i("MAM", "Loading previous history");
mamQueryResult = mamManager.queryArchive(chat.getUser().getJid());
} catch (SmackException.NotLoggedInException | SmackException.NoResponseException | XMPPException.XMPPErrorException | InterruptedException | SmackException.NotConnectedException e) {
LogManager.exception(this, e);
return;
}
LogManager.i("MAM", "queryArchive finished. fin count expected: " + mamQueryResult.mamFin.getRSMSet().getCount() + " real: " + mamQueryResult.forwardedMessages.size());
Realm realm = MessageDatabaseManager.getInstance().getNewBackgroundRealm();
List<MessageItem> messageItems = getMessageItems(mamQueryResult, chat);
syncMessages(realm, chat, messageItems);
updatePreviousHistorySyncInfo(realm, chat, mamQueryResult, messageItems);
realm.close();
}
use of com.xabber.android.data.database.messagerealm.MessageItem in project xabber-android by redsolution.
the class MamManager method getMessageItems.
private List<MessageItem> getMessageItems(org.jivesoftware.smackx.mam.MamManager.MamQueryResult mamQueryResult, AbstractChat chat) {
List<MessageItem> messageItems = new ArrayList<>();
for (Forwarded forwarded : mamQueryResult.forwardedMessages) {
if (!(forwarded.getForwardedStanza() instanceof Message)) {
continue;
}
Message message = (Message) forwarded.getForwardedStanza();
DelayInformation delayInformation = forwarded.getDelayInformation();
DelayInformation messageDelay = DelayInformation.from(message);
String body = message.getBody();
net.java.otr4j.io.messages.AbstractMessage otrMessage;
try {
otrMessage = SerializationUtils.toMessage(body);
} catch (IOException e) {
continue;
}
boolean encrypted = false;
if (otrMessage != null) {
if (otrMessage.messageType != net.java.otr4j.io.messages.AbstractMessage.MESSAGE_PLAINTEXT) {
encrypted = true;
try {
// this transforming just decrypt message if have keys. No action as injectMessage or something else
body = OTRManager.getInstance().transformReceivingIfSessionExist(chat.getAccount(), chat.getUser(), body);
if (OTRManager.getInstance().isEncrypted(body)) {
continue;
}
} catch (Exception e) {
continue;
}
} else
body = ((PlainTextMessage) otrMessage).cleanText;
}
boolean incoming = message.getFrom().asBareJid().equals(chat.getUser().getJid().asBareJid());
MessageItem messageItem = new MessageItem();
messageItem.setAccount(chat.getAccount());
messageItem.setUser(chat.getUser());
messageItem.setResource(chat.getUser().getJid().getResourceOrNull());
messageItem.setText(body);
messageItem.setTimestamp(delayInformation.getStamp().getTime());
if (messageDelay != null) {
messageItem.setDelayTimestamp(messageDelay.getStamp().getTime());
}
messageItem.setIncoming(incoming);
messageItem.setStanzaId(message.getStanzaId());
messageItem.setReceivedFromMessageArchive(true);
messageItem.setRead(true);
messageItem.setSent(true);
messageItem.setEncrypted(encrypted);
FileManager.processFileMessage(messageItem);
messageItems.add(messageItem);
}
return messageItems;
}
use of com.xabber.android.data.database.messagerealm.MessageItem in project xabber-android by redsolution.
the class ChatMessageAdapter method onBindViewHolder.
@Override
public void onBindViewHolder(BasicMessage holder, int position) {
final int viewType = getItemViewType(position);
MessageItem messageItem = getMessageItem(position);
if (messageItem == null) {
LogManager.w(LOG_TAG, "onBindViewHolder Null message item. Position: " + position);
return;
}
switch(viewType) {
case VIEW_TYPE_HINT:
// holder.messageText.setText(hint);
break;
case VIEW_TYPE_ACTION_MESSAGE:
ChatAction action = MessageItem.getChatAction(messageItem);
String time = StringUtils.getSmartTimeText(context, new Date(messageItem.getTimestamp()));
String name;
if (isMUC) {
name = messageItem.getResource().toString();
} else {
name = RosterManager.getInstance().getBestContact(account, messageItem.getUser()).getName();
}
holder.messageText.setText(time + ": " + action.getText(context, name, MessageItem.getSpannable(messageItem).toString()));
break;
case VIEW_TYPE_INCOMING_MESSAGE:
setUpIncomingMessage((IncomingMessage) holder, messageItem);
break;
case VIEW_TYPE_OUTGOING_MESSAGE:
setUpOutgoingMessage((Message) holder, messageItem);
break;
}
}
Aggregations