From 35df965b58a9505f27be34c3a68309211c2e6d88 Mon Sep 17 00:00:00 2001 From: Christian Schneppe Date: Sat, 26 Oct 2019 18:10:48 +0200 Subject: Bookmarks2: support retraction --- .../java/de/pixart/messenger/entities/Account.java | 56 ++++++++++++++-------- .../de/pixart/messenger/entities/Bookmark.java | 20 ++++---- 2 files changed, 46 insertions(+), 30 deletions(-) (limited to 'src/main/java/de/pixart/messenger/entities') diff --git a/src/main/java/de/pixart/messenger/entities/Account.java b/src/main/java/de/pixart/messenger/entities/Account.java index 0590c9d72..fe31dbf07 100644 --- a/src/main/java/de/pixart/messenger/entities/Account.java +++ b/src/main/java/de/pixart/messenger/entities/Account.java @@ -16,11 +16,12 @@ import java.security.PublicKey; import java.security.interfaces.DSAPublicKey; import java.util.ArrayList; import java.util.Collection; +import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Locale; +import java.util.Map; import java.util.Set; -import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CopyOnWriteArraySet; import de.pixart.messenger.Config; @@ -92,7 +93,7 @@ public class Account extends AbstractEntity implements AvatarService.Avatarable private XmppConnection xmppConnection = null; private long mEndGracePeriod = 0L; private String otrFingerprint; - private List bookmarks = new CopyOnWriteArrayList<>(); + private final Map bookmarks = new HashMap<>(); private Presence.Status presenceStatus = Presence.Status.ONLINE; private String presenceStatusMessage = null; @@ -502,36 +503,51 @@ public class Account extends AbstractEntity implements AvatarService.Avatarable return this.roster; } - public List getBookmarks() { - return this.bookmarks; + public Collection getBookmarks() { + return this.bookmarks.values(); } - public void setBookmarks(final CopyOnWriteArrayList bookmarks) { - this.bookmarks = bookmarks; + public void setBookmarks(Map bookmarks) { + synchronized (this.bookmarks) { + this.bookmarks.clear(); + this.bookmarks.putAll(bookmarks); + } + } + + public void putBookmark(Bookmark bookmark) { + synchronized (this.bookmarks) { + this.bookmarks.put(bookmark.getJid(), bookmark); + } + } + + public void removeBookmark(Bookmark bookmark) { + synchronized (this.bookmarks) { + this.bookmarks.remove(bookmark.getJid()); + } + } + + public void removeBookmark(Jid jid) { + synchronized (this.bookmarks) { + this.bookmarks.remove(jid); + } } public Set getBookmarkedJids() { - final Set jids = new HashSet<>(); - for (final Bookmark bookmark : this.bookmarks) { - final Jid jid = bookmark.getJid(); - if (jid != null) { - jids.add(jid.asBareJid()); - } + synchronized (this.bookmarks) { + return new HashSet<>(this.bookmarks.keySet()); } - return jids; } - public boolean hasBookmarkFor(final Jid conferenceJid) { - return getBookmark(conferenceJid) != null; + public boolean hasBookmarkFor(final Jid jid) { + synchronized (this.bookmarks) { + return this.bookmarks.containsKey(jid.asBareJid()); + } } Bookmark getBookmark(final Jid jid) { - for (final Bookmark bookmark : this.bookmarks) { - if (bookmark.getJid() != null && jid.asBareJid().equals(bookmark.getJid().asBareJid())) { - return bookmark; - } + synchronized (this.bookmarks) { + return this.bookmarks.get(jid.asBareJid()); } - 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 c7f33af3e..d017eefed 100644 --- a/src/main/java/de/pixart/messenger/entities/Bookmark.java +++ b/src/main/java/de/pixart/messenger/entities/Bookmark.java @@ -7,11 +7,11 @@ import androidx.annotation.Nullable; import java.lang.ref.WeakReference; import java.util.ArrayList; -import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Locale; +import java.util.Map; import de.pixart.messenger.utils.Namespace; import de.pixart.messenger.utils.StringUtils; @@ -38,43 +38,43 @@ public class Bookmark extends Element implements ListItem { this.account = account; } - public static Collection parseFromStorage(Element storage, Account account) { + public static Map parseFromStorage(Element storage, Account account) { if (storage == null) { - return Collections.emptyList(); + return Collections.emptyMap(); } final HashMap bookmarks = new HashMap<>(); for (final Element item : storage.getChildren()) { if (item.getName().equals("conference")) { final Bookmark bookmark = Bookmark.parse(item, account); if (bookmark != null) { - final Bookmark old = bookmarks.put(bookmark.getJid(), bookmark); + final Bookmark old = bookmarks.put(bookmark.jid, bookmark); if (old != null && old.getBookmarkName() != null && bookmark.getBookmarkName() == null) { bookmark.setBookmarkName(old.getBookmarkName()); } } } } - return bookmarks.values(); + return bookmarks; } - public static Collection parseFromPubsub(Element pubsub, Account account) { + public static Map parseFromPubsub(Element pubsub, Account account) { if (pubsub == null) { - return Collections.emptyList(); + return Collections.emptyMap(); } final Element items = pubsub.findChild("items"); if (items != null && Namespace.BOOKMARK.equals(items.getAttribute("node"))) { - final List bookmarks = new ArrayList<>(); + final Map bookmarks = new HashMap<>(); for (Element item : items.getChildren()) { if (item.getName().equals("item")) { final Bookmark bookmark = Bookmark.parseFromItem(item, account); if (bookmark != null) { - bookmarks.add(bookmark); + bookmarks.put(bookmark.jid, bookmark); } } } return bookmarks; } - return Collections.emptyList(); + return Collections.emptyMap(); } public static Bookmark parse(Element element, Account account) { -- cgit v1.2.3