conversations-classic/src/main/java/eu/siacs/conversations/entities/Bookmark.java

174 lines
4.3 KiB
Java
Raw Normal View History

2014-10-22 16:38:44 +00:00
package eu.siacs.conversations.entities;
import android.content.Context;
import android.support.annotation.NonNull;
2018-03-23 15:52:05 +00:00
import android.support.annotation.Nullable;
import java.lang.ref.WeakReference;
2014-11-16 16:21:21 +00:00
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
2014-10-22 16:38:44 +00:00
2014-11-16 22:58:30 +00:00
import eu.siacs.conversations.utils.UIHelper;
2014-10-22 16:38:44 +00:00
import eu.siacs.conversations.xml.Element;
import eu.siacs.conversations.xmpp.InvalidJid;
2018-03-05 17:30:40 +00:00
import rocks.xmpp.addr.Jid;
2014-10-22 16:38:44 +00:00
public class Bookmark extends Element implements ListItem {
private Account account;
private WeakReference<Conversation> conversation;
2018-03-11 11:06:07 +00:00
private Jid jid;
2014-10-22 16:38:44 +00:00
public Bookmark(final Account account, final Jid jid) {
2014-10-22 16:38:44 +00:00
super("conference");
2018-03-11 11:06:07 +00:00
this.jid = jid;
this.setAttribute("jid", jid.toString());
2014-10-22 16:38:44 +00:00
this.account = account;
}
private Bookmark(Account account) {
super("conference");
this.account = account;
}
public static Bookmark parse(Element element, Account account) {
Bookmark bookmark = new Bookmark(account);
bookmark.setAttributes(element.getAttributes());
bookmark.setChildren(element.getChildren());
bookmark.jid = InvalidJid.getNullForInvalid(bookmark.getAttributeAsJid("jid"));
2014-10-22 16:38:44 +00:00
return bookmark;
}
public void setAutojoin(boolean autojoin) {
if (autojoin) {
this.setAttribute("autojoin", "true");
} else {
this.setAttribute("autojoin", "false");
}
}
@Override
public int compareTo(final @NonNull ListItem another) {
2014-11-16 22:58:30 +00:00
return this.getDisplayName().compareToIgnoreCase(
another.getDisplayName());
}
2014-10-22 16:38:44 +00:00
@Override
public String getDisplayName() {
final Conversation c = getConversation();
2018-03-23 15:52:05 +00:00
final String name = getBookmarkName();
if (c != null) {
return c.getName().toString();
2018-03-23 15:52:05 +00:00
} else if (printableValue(name, false)) {
return name.trim();
2014-10-22 16:38:44 +00:00
} else {
2016-06-16 18:38:35 +00:00
Jid jid = this.getJid();
2018-03-23 15:52:05 +00:00
return jid != null && jid.getLocal() != null ? jid.getLocal() : "";
2014-10-22 16:38:44 +00:00
}
}
2018-03-23 15:52:05 +00:00
public static boolean printableValue(@Nullable String value, boolean permitNone) {
return value != null && !value.trim().isEmpty() && (permitNone || !"None".equals(value));
}
public static boolean printableValue(@Nullable String value) {
return printableValue(value, true);
}
2014-10-22 16:38:44 +00:00
@Override
public Jid getJid() {
2018-03-11 11:06:07 +00:00
return this.jid;
2014-10-22 16:38:44 +00:00
}
2014-11-16 16:21:21 +00:00
@Override
public List<Tag> getTags(Context context) {
2016-06-16 18:38:35 +00:00
ArrayList<Tag> tags = new ArrayList<>();
2014-11-16 22:58:30 +00:00
for (Element element : getChildren()) {
if (element.getName().equals("group") && element.getContent() != null) {
String group = element.getContent();
2017-12-05 13:15:10 +00:00
tags.add(new Tag(group, UIHelper.getColorForName(group,true)));
2014-11-16 22:58:30 +00:00
}
}
return tags;
2014-11-16 16:21:21 +00:00
}
2014-10-22 16:38:44 +00:00
public String getNick() {
return this.findChildContent("nick");
2014-10-22 16:38:44 +00:00
}
2014-11-16 22:58:30 +00:00
public void setNick(String nick) {
Element element = this.findChild("nick");
if (element == null) {
element = this.addChild("nick");
}
element.setContent(nick);
}
2014-10-22 16:38:44 +00:00
public boolean autojoin() {
2014-12-05 00:54:16 +00:00
return this.getAttributeAsBoolean("autojoin");
2014-10-22 16:38:44 +00:00
}
public String getPassword() {
return this.findChildContent("password");
2014-10-22 16:38:44 +00:00
}
2014-11-16 22:58:30 +00:00
public void setPassword(String password) {
Element element = this.findChild("password");
if (element != null) {
element.setContent(password);
}
}
@Override
public boolean match(Context context, String needle) {
2014-11-16 22:58:30 +00:00
if (needle == null) {
return true;
}
needle = needle.toLowerCase(Locale.US);
2014-12-03 09:45:47 +00:00
final Jid jid = getJid();
return (jid != null && jid.toString().contains(needle)) ||
getDisplayName().toLowerCase(Locale.US).contains(needle) ||
matchInTag(context, needle);
2014-10-22 16:38:44 +00:00
}
private boolean matchInTag(Context context, String needle) {
needle = needle.toLowerCase(Locale.US);
for (Tag tag : getTags(context)) {
if (tag.getName().toLowerCase(Locale.US).contains(needle)) {
2014-11-16 22:58:30 +00:00
return true;
}
}
return false;
2014-10-22 16:38:44 +00:00
}
2014-11-16 22:58:30 +00:00
public Account getAccount() {
return this.account;
2014-10-22 16:38:44 +00:00
}
public synchronized Conversation getConversation() {
return this.conversation != null ? this.conversation.get() : null;
2014-10-22 16:38:44 +00:00
}
public synchronized void setConversation(Conversation conversation) {
if (this.conversation != null) {
this.conversation.clear();
}
this.conversation = new WeakReference<>(conversation);
2014-11-16 22:58:30 +00:00
}
public String getBookmarkName() {
2014-10-22 16:38:44 +00:00
return this.getAttribute("name");
}
public boolean setBookmarkName(String name) {
String before = getBookmarkName();
if (name != null && !name.equals(before)) {
this.setAttribute("name", name);
return true;
} else {
return false;
}
}
2014-10-22 16:38:44 +00:00
}