fixed race condition that prevented bookmark nick to be used

This commit is contained in:
Daniel Gultsch 2018-11-10 17:33:24 +01:00
parent ccf9c42d0d
commit 306e12b24e
4 changed files with 39 additions and 12 deletions

View File

@ -82,6 +82,11 @@ public class Bookmark extends Element implements ListItem {
return this.jid; return this.jid;
} }
public Jid getFullJid() {
final String nick = getNick();
return jid == null || nick == null || nick.trim().isEmpty() ? jid : jid.withResource(nick);
}
@Override @Override
public List<Tag> getTags(Context context) { public List<Tag> getTags(Context context) {
ArrayList<Tag> tags = new ArrayList<>(); ArrayList<Tag> tags = new ArrayList<>();
@ -155,7 +160,12 @@ public class Bookmark extends Element implements ListItem {
if (this.conversation != null) { if (this.conversation != null) {
this.conversation.clear(); this.conversation.clear();
} }
if (conversation == null) {
this.conversation = null;
} else {
this.conversation = new WeakReference<>(conversation); this.conversation = new WeakReference<>(conversation);
conversation.getMucOptions().notifyOfBookmarkNick(getNick());
}
} }
public String getBookmarkName() { public String getBookmarkName() {

View File

@ -42,6 +42,9 @@ public class MucOptions {
private Error error = Error.NONE; private Error error = Error.NONE;
private User self; private User self;
private String password = null; private String password = null;
private boolean tookProposedNickFromBookmark = false;
public MucOptions(Conversation conversation) { public MucOptions(Conversation conversation) {
this.account = conversation.getAccount(); this.account = conversation.getAccount();
this.conversation = conversation; this.conversation = conversation;
@ -94,6 +97,16 @@ public class MucOptions {
} }
} }
public boolean isTookProposedNickFromBookmark() {
return tookProposedNickFromBookmark;
}
void notifyOfBookmarkNick(String nick) {
if (nick != null && nick.trim().equals(getSelf().getFullJid().getResource())) {
this.tookProposedNickFromBookmark = true;
}
}
public boolean mamSupport() { public boolean mamSupport() {
return MessageArchiveService.Version.has(getFeatures()); return MessageArchiveService.Version.has(getFeatures());
} }
@ -374,10 +387,11 @@ public class MucOptions {
} }
private String getProposedNick() { private String getProposedNick() {
if (conversation.getBookmark() != null final Bookmark bookmark = this.conversation.getBookmark();
&& conversation.getBookmark().getNick() != null final String bookmarkedNick = bookmark == null ? null : bookmark.getNick();
&& !conversation.getBookmark().getNick().trim().isEmpty()) { if (bookmarkedNick != null && !bookmarkedNick.trim().isEmpty()) {
return conversation.getBookmark().getNick().trim(); this.tookProposedNickFromBookmark = true;
return bookmarkedNick.trim();
} else if (!conversation.getJid().isBareJid()) { } else if (!conversation.getJid().isBareJid()) {
return conversation.getJid().getResource(); return conversation.getJid().getResource();
} else { } else {

View File

@ -1405,7 +1405,7 @@ public class XmppConnectionService extends Service {
if (conversation != null) { if (conversation != null) {
bookmark.setConversation(conversation); bookmark.setConversation(conversation);
} else if (bookmark.autojoin() && bookmark.getJid() != null && autojoin) { } else if (bookmark.autojoin() && bookmark.getJid() != null && autojoin) {
conversation = findOrCreateConversation(account, bookmark.getJid(), true, true, false); conversation = findOrCreateConversation(account, bookmark.getFullJid(), true, true, false);
bookmark.setConversation(conversation); bookmark.setConversation(conversation);
} }
} }
@ -2439,6 +2439,7 @@ public class XmppConnectionService extends Service {
public void persistSelfNick(MucOptions.User self) { public void persistSelfNick(MucOptions.User self) {
final Conversation conversation = self.getConversation(); final Conversation conversation = self.getConversation();
final boolean tookProposedNickFromBookmark = conversation.getMucOptions().isTookProposedNickFromBookmark();
Jid full = self.getFullJid(); Jid full = self.getFullJid();
if (!full.equals(conversation.getJid())) { if (!full.equals(conversation.getJid())) {
Log.d(Config.LOGTAG, "nick changed. updating"); Log.d(Config.LOGTAG, "nick changed. updating");
@ -2446,8 +2447,10 @@ public class XmppConnectionService extends Service {
databaseBackend.updateConversation(conversation); databaseBackend.updateConversation(conversation);
} }
Bookmark bookmark = conversation.getBookmark(); final Bookmark bookmark = conversation.getBookmark();
if (bookmark != null && !full.getResource().equals(bookmark.getNick())) { final String bookmarkedNick = bookmark == null ? null : bookmark.getNick();
if (bookmark != null && (tookProposedNickFromBookmark || TextUtils.isEmpty(bookmarkedNick)) && !full.getResource().equals(bookmarkedNick)) {
Log.d(Config.LOGTAG, conversation.getAccount().getJid().asBareJid() + ": persist nick '" + full.getResource() + "' into bookmark for " + conversation.getJid().asBareJid());
bookmark.setNick(full.getResource()); bookmark.setNick(full.getResource());
pushBookmarks(bookmark.getAccount()); pushBookmarks(bookmark.getAccount());
} }

View File

@ -404,7 +404,7 @@ public class StartConversationActivity extends XmppActivity implements XmppConne
} }
protected void openConversationsForBookmark(Bookmark bookmark) { protected void openConversationsForBookmark(Bookmark bookmark) {
Jid jid = bookmark.getJid(); final Jid jid = bookmark.getFullJid();
if (jid == null) { if (jid == null) {
Toast.makeText(this, R.string.invalid_jid, Toast.LENGTH_SHORT).show(); Toast.makeText(this, R.string.invalid_jid, Toast.LENGTH_SHORT).show();
return; return;