aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/pixart/messenger/entities
diff options
context:
space:
mode:
authorChristian Schneppe <christian.schneppe@pix-art.de>2019-10-26 18:10:48 +0200
committerChristian Schneppe <christian.schneppe@pix-art.de>2019-10-26 18:10:48 +0200
commit35df965b58a9505f27be34c3a68309211c2e6d88 (patch)
tree0747dfdf4d3421249489d4b3140195d053cf7c2f /src/main/java/de/pixart/messenger/entities
parent8d7727fcdc400dcd05babe4ec9b78fa0d1f56717 (diff)
Bookmarks2: support retraction
Diffstat (limited to 'src/main/java/de/pixart/messenger/entities')
-rw-r--r--src/main/java/de/pixart/messenger/entities/Account.java56
-rw-r--r--src/main/java/de/pixart/messenger/entities/Bookmark.java20
2 files changed, 46 insertions, 30 deletions
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<Bookmark> bookmarks = new CopyOnWriteArrayList<>();
+ private final Map<Jid, Bookmark> 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<Bookmark> getBookmarks() {
- return this.bookmarks;
+ public Collection<Bookmark> getBookmarks() {
+ return this.bookmarks.values();
}
- public void setBookmarks(final CopyOnWriteArrayList<Bookmark> bookmarks) {
- this.bookmarks = bookmarks;
+ public void setBookmarks(Map<Jid, Bookmark> 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<Jid> getBookmarkedJids() {
- final Set<Jid> 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<Bookmark> parseFromStorage(Element storage, Account account) {
+ public static Map<Jid, Bookmark> parseFromStorage(Element storage, Account account) {
if (storage == null) {
- return Collections.emptyList();
+ return Collections.emptyMap();
}
final HashMap<Jid, Bookmark> 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<Bookmark> parseFromPubsub(Element pubsub, Account account) {
+ public static Map<Jid, Bookmark> 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<Bookmark> bookmarks = new ArrayList<>();
+ final Map<Jid, Bookmark> 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) {