diff options
author | Christian Schneppe <christian@pix-art.de> | 2018-02-10 20:26:51 +0100 |
---|---|---|
committer | Christian Schneppe <christian@pix-art.de> | 2018-02-10 20:26:51 +0100 |
commit | 41c1665f67b5ee12e864f9bab8daae526cb42f11 (patch) | |
tree | e74a6f48732663662fa1fdd2cb5c6d50f22b452d /src/main/java/de/pixart/messenger | |
parent | 0325594075c2844580cd3d92943aa06f1832e4d4 (diff) |
do not cross reference bookmarks and conversations
Diffstat (limited to 'src/main/java/de/pixart/messenger')
8 files changed, 37 insertions, 48 deletions
diff --git a/src/main/java/de/pixart/messenger/entities/Account.java b/src/main/java/de/pixart/messenger/entities/Account.java index bc2fe371a..c641902a3 100644 --- a/src/main/java/de/pixart/messenger/entities/Account.java +++ b/src/main/java/de/pixart/messenger/entities/Account.java @@ -612,18 +612,21 @@ public class Account extends AbstractEntity { return this.bookmarks; } - public void setBookmarks(final List<Bookmark> bookmarks) { + public void setBookmarks(final CopyOnWriteArrayList<Bookmark> bookmarks) { this.bookmarks = bookmarks; } public boolean hasBookmarkFor(final Jid conferenceJid) { + return getBookmark(conferenceJid) != null; + } + + public Bookmark getBookmark(final Jid jid) { for (final Bookmark bookmark : this.bookmarks) { - final Jid jid = bookmark.getJid(); - if (jid != null && jid.equals(conferenceJid.toBareJid())) { - return true; + if (bookmark.getJid() != null && jid.toBareJid().equals(bookmark.getJid().toBareJid())) { + return bookmark; } } - return false; + return null; } public boolean setAvatar(final String filename) { diff --git a/src/main/java/de/pixart/messenger/entities/Bookmark.java b/src/main/java/de/pixart/messenger/entities/Bookmark.java index 34dddd6d9..8074c9430 100644 --- a/src/main/java/de/pixart/messenger/entities/Bookmark.java +++ b/src/main/java/de/pixart/messenger/entities/Bookmark.java @@ -2,6 +2,7 @@ package de.pixart.messenger.entities; import android.content.Context; +import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.List; import java.util.Locale; @@ -13,7 +14,7 @@ import de.pixart.messenger.xmpp.jid.Jid; public class Bookmark extends Element implements ListItem { private Account account; - private Conversation mJoinedConversation; + private WeakReference<Conversation> conversation; public Bookmark(final Account account, final Jid jid) { super("conference"); @@ -49,8 +50,9 @@ public class Bookmark extends Element implements ListItem { @Override public String getDisplayName() { - if (this.mJoinedConversation != null) { - return this.mJoinedConversation.getName(); + final Conversation c = getConversation(); + if (c != null) { + return c.getName(); } else if (getBookmarkName() != null && !getBookmarkName().trim().isEmpty()) { return getBookmarkName().trim(); @@ -146,12 +148,15 @@ public class Bookmark extends Element implements ListItem { return this.account; } - public Conversation getConversation() { - return this.mJoinedConversation; + public synchronized Conversation getConversation() { + return this.conversation != null ? this.conversation.get() : null; } - public void setConversation(Conversation conversation) { - this.mJoinedConversation = conversation; + public synchronized void setConversation(Conversation conversation) { + if (this.conversation != null) { + this.conversation.clear(); + } + this.conversation = new WeakReference<>(conversation); } public String getBookmarkName() { @@ -167,11 +172,4 @@ public class Bookmark extends Element implements ListItem { return false; } } - - public void unregisterConversation() { - if (this.mJoinedConversation != null) { - this.mJoinedConversation.deregisterWithBookmark(); - } - this.mJoinedConversation = null; - } } diff --git a/src/main/java/de/pixart/messenger/entities/Conversation.java b/src/main/java/de/pixart/messenger/entities/Conversation.java index d9a12392e..223e26dd2 100644 --- a/src/main/java/de/pixart/messenger/entities/Conversation.java +++ b/src/main/java/de/pixart/messenger/entities/Conversation.java @@ -85,8 +85,6 @@ public class Conversation extends AbstractEntity implements Blockable, Comparabl private byte[] symmetricKey; - private Bookmark bookmark; - private boolean messagesLeftOnServer = true; private ChatState mOutgoingChatState = Config.DEFAULT_CHATSTATE; private ChatState mIncomingChatState = Config.DEFAULT_CHATSTATE; @@ -508,6 +506,7 @@ public class Conversation extends AbstractEntity implements Blockable, Comparabl public String getName() { if (getMode() == MODE_MULTI) { final String subject = getMucOptions().getSubject(); + Bookmark bookmark = getBookmark(); final String bookmarkName = bookmark != null ? bookmark.getBookmarkName() : null; if (subject != null && !subject.trim().isEmpty()) { @@ -826,20 +825,8 @@ public class Conversation extends AbstractEntity implements Blockable, Comparabl return this.symmetricKey; } - public void setBookmark(Bookmark bookmark) { - this.bookmark = bookmark; - this.bookmark.setConversation(this); - } - - public void deregisterWithBookmark() { - if (this.bookmark != null) { - this.bookmark.setConversation(null); - } - this.bookmark = null; - } - public Bookmark getBookmark() { - return this.bookmark; + return this.account.getBookmark(this.contactJid); } public Message findDuplicateMessage(Message message) { diff --git a/src/main/java/de/pixart/messenger/entities/MucOptions.java b/src/main/java/de/pixart/messenger/entities/MucOptions.java index d4ba4e9f8..c1dca783c 100644 --- a/src/main/java/de/pixart/messenger/entities/MucOptions.java +++ b/src/main/java/de/pixart/messenger/entities/MucOptions.java @@ -54,7 +54,7 @@ public class MucOptions { } public boolean isSelf(Jid counterpart) { - return counterpart.getResourcepart().equals(getActualNick()); + return counterpart.equals(self.getFullJid()); } public void resetChatState() { diff --git a/src/main/java/de/pixart/messenger/parser/PresenceParser.java b/src/main/java/de/pixart/messenger/parser/PresenceParser.java index ae2cdc4ad..a58b3d016 100644 --- a/src/main/java/de/pixart/messenger/parser/PresenceParser.java +++ b/src/main/java/de/pixart/messenger/parser/PresenceParser.java @@ -66,8 +66,7 @@ public class PresenceParser extends AbstractParser implements if (item != null && !from.isBareJid()) { mucOptions.setError(MucOptions.Error.NONE); MucOptions.User user = parseItem(conversation, item, from); - if (codes.contains(MucOptions.STATUS_CODE_SELF_PRESENCE) - || ((codes.isEmpty() || codes.contains(MucOptions.STATUS_CODE_ROOM_CREATED)) && jid.equals(item.getAttributeAsJid("jid")))) { + if (codes.contains(MucOptions.STATUS_CODE_SELF_PRESENCE) || (codes.contains(MucOptions.STATUS_CODE_ROOM_CREATED) && jid.equals(item.getAttributeAsJid("jid")))) { if (mucOptions.setOnline()) { mXmppConnectionService.getAvatarService().clear(mucOptions); } diff --git a/src/main/java/de/pixart/messenger/services/XmppConnectionService.java b/src/main/java/de/pixart/messenger/services/XmppConnectionService.java index 2a458414c..2fb1e6ae3 100644 --- a/src/main/java/de/pixart/messenger/services/XmppConnectionService.java +++ b/src/main/java/de/pixart/messenger/services/XmppConnectionService.java @@ -1553,15 +1553,15 @@ public class XmppConnectionService extends Service { } Conversation conversation = find(bookmark); if (conversation != null) { - conversation.setBookmark(bookmark); + bookmark.setConversation(conversation); } else if (bookmark.autojoin() && bookmark.getJid() != null && autojoin) { conversation = findOrCreateConversation(account, bookmark.getJid(), true, true, false); - conversation.setBookmark(bookmark); + bookmark.setConversation(conversation); } } } } - account.setBookmarks(new ArrayList<>(bookmarks.values())); + account.setBookmarks(new CopyOnWriteArrayList<>(bookmarks.values())); } else { Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": could not fetch bookmarks"); } @@ -2664,7 +2664,10 @@ public class XmppConnectionService extends Service { if (account.getStatus() == Account.State.ONLINE || now) { sendPresencePacket(conversation.getAccount(), mPresenceGenerator.leave(conversation.getMucOptions())); conversation.getMucOptions().setOffline(); - conversation.deregisterWithBookmark(); + Bookmark bookmark = conversation.getBookmark(); + if (bookmark != null) { + bookmark.setConversation(null); + } Log.d(Config.LOGTAG, conversation.getAccount().getJid().toBareJid() + ": leaving muc " + conversation.getJid()); } else { account.pendingConferenceLeaves.add(conversation); @@ -4176,7 +4179,7 @@ public class XmppConnectionService extends Service { bookmark.setAutojoin(getPreferences().getBoolean("autojoin", getResources().getBoolean(R.bool.autojoin))); account.getBookmarks().add(bookmark); pushBookmarks(account); - conversation.setBookmark(bookmark); + bookmark.setConversation(conversation); } public boolean verifyFingerprints(Contact contact, List<XmppUri.Fingerprint> fingerprints) { diff --git a/src/main/java/de/pixart/messenger/ui/ConferenceDetailsActivity.java b/src/main/java/de/pixart/messenger/ui/ConferenceDetailsActivity.java index 6c41187e0..21442bcd1 100644 --- a/src/main/java/de/pixart/messenger/ui/ConferenceDetailsActivity.java +++ b/src/main/java/de/pixart/messenger/ui/ConferenceDetailsActivity.java @@ -396,8 +396,7 @@ public class ConferenceDetailsActivity extends XmppActivity implements OnConvers if (mConversation == null) { return true; } - Account account = mConversation.getAccount(); - if (account.hasBookmarkFor(mConversation.getJid().toBareJid())) { + if (mConversation.getBookmark() != null) { menuItemSaveBookmark.setVisible(false); menuItemDeleteBookmark.setVisible(true); } else { @@ -568,7 +567,7 @@ public class ConferenceDetailsActivity extends XmppActivity implements OnConvers protected void deleteBookmark() { Account account = mConversation.getAccount(); Bookmark bookmark = mConversation.getBookmark(); - mConversation.deregisterWithBookmark(); + bookmark.setConversation(null); account.getBookmarks().remove(bookmark); xmppConnectionService.pushBookmarks(account); updateView(); diff --git a/src/main/java/de/pixart/messenger/ui/StartConversationActivity.java b/src/main/java/de/pixart/messenger/ui/StartConversationActivity.java index 2f0f69ee8..83427a713 100644 --- a/src/main/java/de/pixart/messenger/ui/StartConversationActivity.java +++ b/src/main/java/de/pixart/messenger/ui/StartConversationActivity.java @@ -361,7 +361,7 @@ public class StartConversationActivity extends XmppActivity implements OnRosterU return; } Conversation conversation = xmppConnectionService.findOrCreateConversation(bookmark.getAccount(), jid, true, true, true); - conversation.setBookmark(bookmark); + bookmark.setConversation(conversation); if (!bookmark.autojoin() && getPreferences().getBoolean("autojoin", getResources().getBoolean(R.bool.autojoin))) { bookmark.setAutojoin(true); xmppConnectionService.pushBookmarks(bookmark.getAccount()); @@ -412,7 +412,7 @@ public class StartConversationActivity extends XmppActivity implements OnRosterU @Override public void onClick(DialogInterface dialog, int which) { - bookmark.unregisterConversation(); + bookmark.setConversation(null); Account account = bookmark.getAccount(); account.getBookmarks().remove(bookmark); xmppConnectionService.pushBookmarks(account); @@ -517,7 +517,7 @@ public class StartConversationActivity extends XmppActivity implements OnRosterU xmppConnectionService.pushBookmarks(account); final Conversation conversation = xmppConnectionService .findOrCreateConversation(account, conferenceJid, true, true, true); - conversation.setBookmark(bookmark); + bookmark.setConversation(conversation); dialog.dismiss(); mCurrentDialog = null; switchToConversation(conversation); |