Commit e400a2c1 authored by Grigory Fedorov's avatar Grigory Fedorov

MessageManager: code formatting.

parent 663de4af
...@@ -66,9 +66,8 @@ import com.xabber.xmpp.delay.Delay; ...@@ -66,9 +66,8 @@ import com.xabber.xmpp.delay.Delay;
* *
* @author alexander.ivanov * @author alexander.ivanov
*/ */
public class MessageManager implements OnLoadListener, OnPacketListener, public class MessageManager implements OnLoadListener, OnPacketListener, OnDisconnectListener,
OnDisconnectListener, OnAccountRemovedListener, OnAccountRemovedListener, OnRosterReceivedListener, OnAccountArchiveModeChangedListener,
OnRosterReceivedListener, OnAccountArchiveModeChangedListener,
OnStatusChangeListener { OnStatusChangeListener {
/** /**
...@@ -95,7 +94,7 @@ public class MessageManager implements OnLoadListener, OnPacketListener, ...@@ -95,7 +94,7 @@ public class MessageManager implements OnLoadListener, OnPacketListener,
} }
private MessageManager() { private MessageManager() {
chats = new NestedMap<AbstractChat>(); chats = new NestedMap<>();
} }
@Override @Override
...@@ -106,8 +105,7 @@ public class MessageManager implements OnLoadListener, OnPacketListener, ...@@ -106,8 +105,7 @@ public class MessageManager implements OnLoadListener, OnPacketListener,
try { try {
if (cursor.moveToFirst()) { if (cursor.moveToFirst()) {
do { do {
loadChats.add(new BaseEntity(MessageTable loadChats.add(new BaseEntity(MessageTable.getAccount(cursor), MessageTable.getUser(cursor)));
.getAccount(cursor), MessageTable.getUser(cursor)));
} while (cursor.moveToNext()); } while (cursor.moveToNext());
} }
} finally { } finally {
...@@ -122,10 +120,11 @@ public class MessageManager implements OnLoadListener, OnPacketListener, ...@@ -122,10 +120,11 @@ public class MessageManager implements OnLoadListener, OnPacketListener,
} }
private void onLoaded(Set<BaseEntity> loadChats) { private void onLoaded(Set<BaseEntity> loadChats) {
for (BaseEntity baseEntity : loadChats) for (BaseEntity baseEntity : loadChats) {
if (getChat(baseEntity.getAccount(), if (getChat(baseEntity.getAccount(), Jid.getBareAddress(baseEntity.getUser())) == null) {
Jid.getBareAddress(baseEntity.getUser())) == null)
createChat(baseEntity.getAccount(), baseEntity.getUser()); createChat(baseEntity.getAccount(), baseEntity.getUser());
}
}
} }
/** /**
...@@ -160,8 +159,9 @@ public class MessageManager implements OnLoadListener, OnPacketListener, ...@@ -160,8 +159,9 @@ public class MessageManager implements OnLoadListener, OnPacketListener,
* @param chat * @param chat
*/ */
public void addChat(AbstractChat chat) { public void addChat(AbstractChat chat) {
if (getChat(chat.getAccount(), chat.getUser()) != null) if (getChat(chat.getAccount(), chat.getUser()) != null) {
throw new IllegalStateException(); throw new IllegalStateException();
}
chats.put(chat.getAccount(), chat.getUser(), chat); chats.put(chat.getAccount(), chat.getUser(), chat);
} }
...@@ -183,8 +183,9 @@ public class MessageManager implements OnLoadListener, OnPacketListener, ...@@ -183,8 +183,9 @@ public class MessageManager implements OnLoadListener, OnPacketListener,
*/ */
public void sendMessage(String account, String user, String text) { public void sendMessage(String account, String user, String text) {
AbstractChat chat = getChat(account, user); AbstractChat chat = getChat(account, user);
if (chat == null) if (chat == null) {
chat = createChat(account, user); chat = createChat(account, user);
}
MessageItem messageItem = chat.newMessage(text); MessageItem messageItem = chat.newMessage(text);
chat.sendQueue(messageItem); chat.sendQueue(messageItem);
} }
...@@ -196,19 +197,19 @@ public class MessageManager implements OnLoadListener, OnPacketListener, ...@@ -196,19 +197,19 @@ public class MessageManager implements OnLoadListener, OnPacketListener,
*/ */
public boolean hasActiveChat(String account, String user) { public boolean hasActiveChat(String account, String user) {
AbstractChat chat = getChat(account, user); AbstractChat chat = getChat(account, user);
if (chat == null) return chat != null && chat.isActive();
return false;
return chat.isActive();
} }
/** /**
* @return Collection with active chats. * @return Collection with active chats.
*/ */
public Collection<AbstractChat> getActiveChats() { public Collection<AbstractChat> getActiveChats() {
Collection<AbstractChat> collection = new ArrayList<AbstractChat>(); Collection<AbstractChat> collection = new ArrayList<>();
for (AbstractChat chat : chats.values()) for (AbstractChat chat : chats.values()) {
if (chat.isActive()) if (chat.isActive()) {
collection.add(chat); collection.add(chat);
}
}
return Collections.unmodifiableCollection(collection); return Collections.unmodifiableCollection(collection);
} }
...@@ -221,8 +222,9 @@ public class MessageManager implements OnLoadListener, OnPacketListener, ...@@ -221,8 +222,9 @@ public class MessageManager implements OnLoadListener, OnPacketListener,
*/ */
public AbstractChat getOrCreateChat(String account, String user) { public AbstractChat getOrCreateChat(String account, String user) {
AbstractChat chat = getChat(account, user); AbstractChat chat = getChat(account, user);
if (chat == null) if (chat == null) {
chat = createChat(account, user); chat = createChat(account, user);
}
return chat; return chat;
} }
...@@ -244,15 +246,17 @@ public class MessageManager implements OnLoadListener, OnPacketListener, ...@@ -244,15 +246,17 @@ public class MessageManager implements OnLoadListener, OnPacketListener,
*/ */
public void closeChat(String account, String user) { public void closeChat(String account, String user) {
AbstractChat chat = getChat(account, user); AbstractChat chat = getChat(account, user);
if (chat == null) if (chat == null) {
return; return;
}
chat.closeChat(); chat.closeChat();
} }
public void requestToLoadLocalHistory(String account, String user) { public void requestToLoadLocalHistory(String account, String user) {
AbstractChat chat = getChat(account, user); AbstractChat chat = getChat(account, user);
if (chat == null) if (chat == null) {
chat = createChat(account, user); chat = createChat(account, user);
}
chat.requestToLoadLocalHistory(); chat.requestToLoadLocalHistory();
} }
...@@ -264,8 +268,9 @@ public class MessageManager implements OnLoadListener, OnPacketListener, ...@@ -264,8 +268,9 @@ public class MessageManager implements OnLoadListener, OnPacketListener,
*/ */
public String getLastText(String account, String user) { public String getLastText(String account, String user) {
AbstractChat chat = getChat(account, user); AbstractChat chat = getChat(account, user);
if (chat == null) if (chat == null) {
return ""; return "";
}
return chat.getLastText(); return chat.getLastText();
} }
...@@ -276,8 +281,9 @@ public class MessageManager implements OnLoadListener, OnPacketListener, ...@@ -276,8 +281,9 @@ public class MessageManager implements OnLoadListener, OnPacketListener,
*/ */
public Date getLastTime(String account, String user) { public Date getLastTime(String account, String user) {
AbstractChat chat = getChat(account, user); AbstractChat chat = getChat(account, user);
if (chat == null) if (chat == null) {
return null; return null;
}
return chat.getLastTime(); return chat.getLastTime();
} }
...@@ -288,12 +294,11 @@ public class MessageManager implements OnLoadListener, OnPacketListener, ...@@ -288,12 +294,11 @@ public class MessageManager implements OnLoadListener, OnPacketListener,
* @param user * @param user
*/ */
public void setVisibleChat(String account, String user) { public void setVisibleChat(String account, String user) {
final boolean remove = !AccountManager.getInstance() final boolean remove = !AccountManager.getInstance().getArchiveMode(account).saveLocally();
.getArchiveMode(account).saveLocally();
AbstractChat chat = getChat(account, user); AbstractChat chat = getChat(account, user);
if (chat == null) if (chat == null) {
chat = createChat(account, user); chat = createChat(account, user);
else { } else {
// Mark messages as read and them delete from db if necessary. // Mark messages as read and them delete from db if necessary.
final ArrayList<MessageItem> messageItems = new ArrayList<MessageItem>(); final ArrayList<MessageItem> messageItems = new ArrayList<MessageItem>();
for (MessageItem messageItem : chat.getMessages()) { for (MessageItem messageItem : chat.getMessages()) {
...@@ -306,10 +311,11 @@ public class MessageManager implements OnLoadListener, OnPacketListener, ...@@ -306,10 +311,11 @@ public class MessageManager implements OnLoadListener, OnPacketListener,
@Override @Override
public void run() { public void run() {
Collection<Long> ids = getMessageIds(messageItems, remove); Collection<Long> ids = getMessageIds(messageItems, remove);
if (remove) if (remove) {
MessageTable.getInstance().removeMessages(ids); MessageTable.getInstance().removeMessages(ids);
else } else {
MessageTable.getInstance().markAsRead(ids); MessageTable.getInstance().markAsRead(ids);
}
} }
}); });
} }
...@@ -339,8 +345,9 @@ public class MessageManager implements OnLoadListener, OnPacketListener, ...@@ -339,8 +345,9 @@ public class MessageManager implements OnLoadListener, OnPacketListener,
*/ */
public void clearHistory(String account, String user) { public void clearHistory(String account, String user) {
AbstractChat chat = getChat(account, user); AbstractChat chat = getChat(account, user);
if (chat == null) if (chat == null) {
return; return;
}
chat.removeAllMessages(); chat.removeAllMessages();
onChatChanged(chat.getAccount(), chat.getUser(), false); onChatChanged(chat.getAccount(), chat.getUser(), false);
} }
...@@ -363,8 +370,9 @@ public class MessageManager implements OnLoadListener, OnPacketListener, ...@@ -363,8 +370,9 @@ public class MessageManager implements OnLoadListener, OnPacketListener,
*/ */
public Collection<MessageItem> getMessages(String account, String user) { public Collection<MessageItem> getMessages(String account, String user) {
AbstractChat chat = getChat(account, user); AbstractChat chat = getChat(account, user);
if (chat == null) if (chat == null) {
return Collections.emptyList(); return Collections.emptyList();
}
return chat.getMessages(); return chat.getMessages();
} }
...@@ -372,41 +380,46 @@ public class MessageManager implements OnLoadListener, OnPacketListener, ...@@ -372,41 +380,46 @@ public class MessageManager implements OnLoadListener, OnPacketListener,
* Called on action settings change. * Called on action settings change.
*/ */
public void onSettingsChanged() { public void onSettingsChanged() {
ChatsShowStatusChange showStatusChange = SettingsManager ChatsShowStatusChange showStatusChange = SettingsManager.chatsShowStatusChange();
.chatsShowStatusChange(); Collection<BaseEntity> changedEntities = new ArrayList<>();
Collection<BaseEntity> changedEntities = new ArrayList<BaseEntity>(); for (AbstractChat chat : chats.values()) {
for (AbstractChat chat : chats.values())
if ((chat instanceof RegularChat && showStatusChange != ChatsShowStatusChange.always) if ((chat instanceof RegularChat && showStatusChange != ChatsShowStatusChange.always)
|| (chat instanceof RoomChat && showStatusChange == ChatsShowStatusChange.never)) { || (chat instanceof RoomChat && showStatusChange == ChatsShowStatusChange.never)) {
// Remove actions with status change. // Remove actions with status change.
ArrayList<MessageItem> remove = new ArrayList<MessageItem>(); ArrayList<MessageItem> remove = new ArrayList<>();
for (MessageItem messageItem : chat.getMessages()) for (MessageItem messageItem : chat.getMessages()) {
if (messageItem.getAction() != null if (messageItem.getAction() != null && messageItem.getAction().isStatusChage()) {
&& messageItem.getAction().isStatusChage())
remove.add(messageItem); remove.add(messageItem);
if (remove.isEmpty()) }
}
if (remove.isEmpty()) {
continue; continue;
for (MessageItem messageItem : remove) }
for (MessageItem messageItem : remove) {
chat.removeMessage(messageItem); chat.removeMessage(messageItem);
}
changedEntities.add(chat); changedEntities.add(chat);
} }
}
RosterManager.getInstance().onContactsChanged(changedEntities); RosterManager.getInstance().onContactsChanged(changedEntities);
} }
@Override @Override
public void onAccountArchiveModeChanged(AccountItem accountItem) { public void onAccountArchiveModeChanged(AccountItem accountItem) {
final ArchiveMode archiveMode = AccountManager.getInstance() final ArchiveMode archiveMode = AccountManager.getInstance().getArchiveMode(accountItem.getAccount());
.getArchiveMode(accountItem.getAccount()); if (archiveMode.saveLocally()) {
if (archiveMode.saveLocally())
return; return;
}
final String account = accountItem.getAccount(); final String account = accountItem.getAccount();
final ArrayList<MessageItem> removeMessageItems = new ArrayList<MessageItem>(); final ArrayList<MessageItem> removeMessageItems = new ArrayList<MessageItem>();
for (AbstractChat chat : chats.getNested(account).values()) for (AbstractChat chat : chats.getNested(account).values()) {
for (MessageItem messageItem : chat.getMessages()) for (MessageItem messageItem : chat.getMessages()) {
if (archiveMode == ArchiveMode.dontStore if (archiveMode == ArchiveMode.dontStore || ((messageItem.isRead()
|| ((messageItem.isRead() || archiveMode != ArchiveMode.unreadOnly) && messageItem || archiveMode != ArchiveMode.unreadOnly) && messageItem.isSent())) {
.isSent()))
removeMessageItems.add(messageItem); removeMessageItems.add(messageItem);
}
}
}
Application.getInstance().runInBackground(new Runnable() { Application.getInstance().runInBackground(new Runnable() {
@Override @Override
public void run() { public void run() {
...@@ -415,51 +428,57 @@ public class MessageManager implements OnLoadListener, OnPacketListener, ...@@ -415,51 +428,57 @@ public class MessageManager implements OnLoadListener, OnPacketListener,
// such message will have no effect as if it was removed. // such message will have no effect as if it was removed.
// History ids becomes invalid and will be cleared on next // History ids becomes invalid and will be cleared on next
// history load. // history load.
MessageTable.getInstance().removeMessages( MessageTable.getInstance().removeMessages(getMessageIds(removeMessageItems, true));
getMessageIds(removeMessageItems, true)); if (archiveMode == ArchiveMode.dontStore) {
if (archiveMode == ArchiveMode.dontStore)
MessageTable.getInstance().removeAccount(account); MessageTable.getInstance().removeAccount(account);
else if (archiveMode == ArchiveMode.unreadOnly) } else if (archiveMode == ArchiveMode.unreadOnly) {
MessageTable.getInstance().removeReadAndSent(account); MessageTable.getInstance().removeReadAndSent(account);
else } else {
MessageTable.getInstance().removeSent(account); MessageTable.getInstance().removeSent(account);
}
} }
}); });
AccountManager.getInstance().onAccountChanged(accountItem.getAccount()); AccountManager.getInstance().onAccountChanged(accountItem.getAccount());
} }
@Override @Override
public void onPacket(ConnectionItem connection, String bareAddress, public void onPacket(ConnectionItem connection, String bareAddress, Packet packet) {
Packet packet) { if (!(connection instanceof AccountItem)) {
if (!(connection instanceof AccountItem))
return; return;
}
String account = ((AccountItem) connection).getAccount(); String account = ((AccountItem) connection).getAccount();
if (bareAddress == null) if (bareAddress == null) {
return; return;
}
if (packet instanceof Message if (packet instanceof Message
&& MessageArchiveManager.getInstance().isModificationsSucceed( && MessageArchiveManager.getInstance().isModificationsSucceed(account)
account) && Delay.isOfflineMessage(Jid.getServer(account), packet)) {
&& Delay.isOfflineMessage(Jid.getServer(account), packet))
// Ignore offline message if modification from server side message // Ignore offline message if modification from server side message
// archive have been received. // archive have been received.
return; return;
}
final String user = packet.getFrom(); final String user = packet.getFrom();
boolean processed = false; boolean processed = false;
for (AbstractChat chat : chats.getNested(account).values()) for (AbstractChat chat : chats.getNested(account).values()) {
if (chat.onPacket(bareAddress, packet)) { if (chat.onPacket(bareAddress, packet)) {
processed = true; processed = true;
break; break;
} }
if (getChat(account, user) != null) }
if (getChat(account, user) != null) {
return; return;
}
if (!processed && packet instanceof Message) { if (!processed && packet instanceof Message) {
final Message message = (Message) packet; final Message message = (Message) packet;
final String body = message.getBody(); final String body = message.getBody();
if (body == null) if (body == null) {
return; return;
for (PacketExtension packetExtension : message.getExtensions()) }
if (packetExtension instanceof MUCUser) for (PacketExtension packetExtension : message.getExtensions()) {
if (packetExtension instanceof MUCUser) {
return; return;
}
}
createChat(account, user).onPacket(bareAddress, packet); createChat(account, user).onPacket(bareAddress, packet);
} }
} }
...@@ -467,17 +486,20 @@ public class MessageManager implements OnLoadListener, OnPacketListener, ...@@ -467,17 +486,20 @@ public class MessageManager implements OnLoadListener, OnPacketListener,
@Override @Override
public void onRosterReceived(AccountItem accountItem) { public void onRosterReceived(AccountItem accountItem) {
String account = accountItem.getAccount(); String account = accountItem.getAccount();
for (AbstractChat chat : chats.getNested(account).values()) for (AbstractChat chat : chats.getNested(account).values()) {
chat.onComplete(); chat.onComplete();
}
} }
@Override @Override
public void onDisconnect(ConnectionItem connection) { public void onDisconnect(ConnectionItem connection) {
if (!(connection instanceof AccountItem)) if (!(connection instanceof AccountItem)) {
return; return;
}
String account = ((AccountItem) connection).getAccount(); String account = ((AccountItem) connection).getAccount();
for (AbstractChat chat : chats.getNested(account).values()) for (AbstractChat chat : chats.getNested(account).values()) {
chat.onDisconnect(); chat.onDisconnect();
}
} }
@Override @Override
...@@ -493,42 +515,37 @@ public class MessageManager implements OnLoadListener, OnPacketListener, ...@@ -493,42 +515,37 @@ public class MessageManager implements OnLoadListener, OnPacketListener,
* @param fileName * @param fileName
* @throws NetworkException * @throws NetworkException
*/ */
public File exportChat(String account, String user, String fileName) public File exportChat(String account, String user, String fileName) throws NetworkException {
throws NetworkException { final File file = new File(Environment.getExternalStorageDirectory(), fileName);
final File file = new File(Environment.getExternalStorageDirectory(),
fileName);
try { try {
BufferedWriter out = new BufferedWriter(new FileWriter(file)); BufferedWriter out = new BufferedWriter(new FileWriter(file));
final String titleName = RosterManager.getInstance().getName( final String titleName = RosterManager.getInstance().getName(account, user) + " (" + user + ")";
account, user)
+ " (" + user + ")";
out.write("<html><head><title>"); out.write("<html><head><title>");
out.write(StringUtils.escapeHtml(titleName)); out.write(StringUtils.escapeHtml(titleName));
out.write("</title></head><body>"); out.write("</title></head><body>");
final AbstractChat abstractChat = getChat(account, user); final AbstractChat abstractChat = getChat(account, user);
if (abstractChat != null) { if (abstractChat != null) {
final boolean isMUC = abstractChat instanceof RoomChat; final boolean isMUC = abstractChat instanceof RoomChat;
final String accountName = AccountManager.getInstance() final String accountName = AccountManager.getInstance().getNickName(account);
.getNickName(account); final String userName = RosterManager.getInstance().getName(account, user);
final String userName = RosterManager.getInstance().getName(
account, user);
for (MessageItem messageItem : abstractChat.getMessages()) { for (MessageItem messageItem : abstractChat.getMessages()) {
if (messageItem.getAction() != null) if (messageItem.getAction() != null) {
continue; continue;
}
final String name; final String name;
if (isMUC) { if (isMUC) {
name = messageItem.getResource(); name = messageItem.getResource();
} else { } else {
if (messageItem.isIncoming()) if (messageItem.isIncoming()) {
name = userName; name = userName;
else } else {
name = accountName; name = accountName;
}
} }
out.write("<b>"); out.write("<b>");
out.write(StringUtils.escapeHtml(name)); out.write(StringUtils.escapeHtml(name));
out.write("</b>&nbsp;("); out.write("</b>&nbsp;(");
out.write(StringUtils.getDateTimeText(messageItem out.write(StringUtils.getDateTimeText(messageItem.getTimestamp()));
.getTimestamp()));
out.write(")<br />\n<p>"); out.write(")<br />\n<p>");
out.write(StringUtils.escapeHtml(messageItem.getText())); out.write(StringUtils.escapeHtml(messageItem.getText()));
out.write("</p><hr />\n"); out.write("</p><hr />\n");
...@@ -554,11 +571,10 @@ public class MessageManager implements OnLoadListener, OnPacketListener, ...@@ -554,11 +571,10 @@ public class MessageManager implements OnLoadListener, OnPacketListener,
Application.getInstance().runOnUiThread(new Runnable() { Application.getInstance().runOnUiThread(new Runnable() {
@Override @Override
public void run() { public void run() {
for (OnChatChangedListener onChatChangedListener : Application for (OnChatChangedListener onChatChangedListener
.getInstance().getUIListeners( : Application.getInstance().getUIListeners(OnChatChangedListener.class)) {
OnChatChangedListener.class)) onChatChangedListener.onChatChanged(account, user, incoming);
onChatChangedListener }
.onChatChanged(account, user, incoming);
} }
}); });
} }
...@@ -568,42 +584,42 @@ public class MessageManager implements OnLoadListener, OnPacketListener, ...@@ -568,42 +584,42 @@ public class MessageManager implements OnLoadListener, OnPacketListener,
* @param clearId Whether message id must be set to the <code>null</code>. * @param clearId Whether message id must be set to the <code>null</code>.
* @return Collection with ids for specified messages. * @return Collection with ids for specified messages.
*/ */
static Collection<Long> getMessageIds(Collection<MessageItem> messageItems, static Collection<Long> getMessageIds(Collection<MessageItem> messageItems, boolean clearId) {
boolean clearId) { ArrayList<Long> ids = new ArrayList<>();
ArrayList<Long> ids = new ArrayList<Long>();
for (MessageItem messageItem : messageItems) { for (MessageItem messageItem : messageItems) {
Long id = messageItem.getId(); Long id = messageItem.getId();
if (id == null) if (id == null) {
continue; continue;
}
ids.add(id); ids.add(id);
if (clearId) if (clearId) {
messageItem.setId(null); messageItem.setId(null);
}
} }
return ids; return ids;
} }
private boolean isStatusTrackingEnabled(String account, String bareAddress) { private boolean isStatusTrackingEnabled(String account, String bareAddress) {
if (SettingsManager.chatsShowStatusChange() != ChatsShowStatusChange.always) if (SettingsManager.chatsShowStatusChange() != ChatsShowStatusChange.always) {
return false; return false;
}
AbstractChat abstractChat = getChat(account, bareAddress); AbstractChat abstractChat = getChat(account, bareAddress);
return abstractChat != null && abstractChat instanceof RegularChat return abstractChat != null && abstractChat instanceof RegularChat && abstractChat.isStatusTrackingEnabled();
&& abstractChat.isStatusTrackingEnabled();
} }
@Override @Override
public void onStatusChanged(String account, String bareAddress, public void onStatusChanged(String account, String bareAddress, String resource, String statusText) {
String resource, String statusText) { if (isStatusTrackingEnabled(account, bareAddress)) {
if (isStatusTrackingEnabled(account, bareAddress)) getChat(account, bareAddress).newAction(resource, statusText, ChatAction.status);
getChat(account, bareAddress).newAction(resource, statusText, }
ChatAction.status);
} }
@Override @Override
public void onStatusChanged(String account, String bareAddress, public void onStatusChanged(String account, String bareAddress, String resource,
String resource, StatusMode statusMode, String statusText) { StatusMode statusMode, String statusText) {
if (isStatusTrackingEnabled(account, bareAddress)) if (isStatusTrackingEnabled(account, bareAddress)) {
getChat(account, bareAddress).newAction(resource, statusText, getChat(account, bareAddress).newAction(resource, statusText, ChatAction.getChatAction(statusMode));
ChatAction.getChatAction(statusMode)); }
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment