fetch bookmarks from server

This commit is contained in:
iNPUTmice 2014-07-14 11:47:42 +02:00
parent 876a076e12
commit e2f886d121
8 changed files with 254 additions and 17 deletions

View File

@ -41,6 +41,7 @@
android:layout_marginTop="8dp" android:layout_marginTop="8dp"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/save_as_bookmark" /> android:text="@string/save_as_bookmark"
android:checked="true" />
</LinearLayout> </LinearLayout>

View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/context_join_conference"
android:title="@string/join_conference"/>
<item
android:id="@+id/context_delete_conference"
android:title="@string/delete_bookmark"/>
</menu>

View File

@ -267,4 +267,5 @@
<string name="conference_address">Conference address</string> <string name="conference_address">Conference address</string>
<string name="conference_address_example">room@conference.example.com</string> <string name="conference_address_example">room@conference.example.com</string>
<string name="save_as_bookmark">Save as bookmark</string> <string name="save_as_bookmark">Save as bookmark</string>
<string name="delete_bookmark">Delete bookmark</string>
</resources> </resources>

View File

@ -1,6 +1,8 @@
package eu.siacs.conversations.entities; package eu.siacs.conversations.entities;
import java.security.interfaces.DSAPublicKey; import java.security.interfaces.DSAPublicKey;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale; import java.util.Locale;
import net.java.otr4j.crypto.OtrCryptoEngineImpl; import net.java.otr4j.crypto.OtrCryptoEngineImpl;
@ -68,6 +70,8 @@ public class Account extends AbstractEntity{
private Roster roster = null; private Roster roster = null;
private List<Bookmark> bookmarks = new ArrayList<Bookmark>();
public Account() { public Account() {
this.uuid = "0"; this.uuid = "0";
} }
@ -297,4 +301,12 @@ public class Account extends AbstractEntity{
} }
return this.roster; return this.roster;
} }
public void setBookmarks(List<Bookmark> bookmarks) {
this.bookmarks = bookmarks;
}
public List<Bookmark> getBookmarks() {
return this.bookmarks;
}
} }

View File

@ -0,0 +1,94 @@
package eu.siacs.conversations.entities;
import java.util.Locale;
import eu.siacs.conversations.xml.Element;
public class Bookmark implements ListItem {
private Account account;
private String jid;
private String nick;
private String displayName;
private boolean autojoin;
public Bookmark(Account account) {
this.account = account;
}
public static Bookmark parse(Element element, Account account) {
Bookmark bookmark = new Bookmark(account);
bookmark.setJid(element.getAttribute("jid"));
bookmark.setDisplayName(element.getAttribute("name"));
String autojoin = element.getAttribute("autojoin");
if (autojoin!=null && (autojoin.equals("true")||autojoin.equals("1"))) {
bookmark.setAutojoin(true);
} else {
bookmark.setAutojoin(false);
}
Element nick = element.findChild("nick");
if (nick!=null) {
bookmark.setNick(nick.getContent());
}
return bookmark;
}
public void setAutojoin(boolean autojoin) {
this.autojoin = autojoin;
}
public void setDisplayName(String name) {
this.displayName = name;
}
public void setJid(String jid) {
this.jid = jid;
}
public void setNick(String nick) {
this.nick = nick;
}
@Override
public int compareTo(ListItem another) {
return this.getDisplayName().compareToIgnoreCase(another.getDisplayName());
}
@Override
public String getDisplayName() {
if (displayName!=null) {
return displayName;
} else {
return this.jid.split("@")[0];
}
}
@Override
public String getJid() {
return this.jid.toLowerCase(Locale.US);
}
public String getNick() {
return this.nick;
}
public boolean autojoin() {
return autojoin;
}
@Override
public String getProfilePhoto() {
return null;
}
public boolean match(String needle) {
return needle == null
|| getJid().contains(needle.toLowerCase(Locale.US))
|| getDisplayName().toLowerCase(Locale.US)
.contains(needle.toLowerCase(Locale.US));
}
public Account getAccount() {
return this.account;
}
}

View File

@ -64,6 +64,8 @@ public class Conversation extends AbstractEntity {
private boolean otrSessionNeedsStarting = false; private boolean otrSessionNeedsStarting = false;
private Bookmark bookmark;
public Conversation(String name, Account account, String contactJid, public Conversation(String name, Account account, String contactJid,
int mode) { int mode) {
this(java.util.UUID.randomUUID().toString(), name, null, account this(java.util.UUID.randomUUID().toString(), name, null, account
@ -375,4 +377,8 @@ public class Conversation extends AbstractEntity {
public byte[] getSymmetricKey() { public byte[] getSymmetricKey() {
return this.symmetricKey; return this.symmetricKey;
} }
public void setBookmark(Bookmark bookmark) {
this.bookmark = bookmark;
}
} }

View File

@ -20,6 +20,7 @@ import net.java.otr4j.session.Session;
import net.java.otr4j.session.SessionStatus; import net.java.otr4j.session.SessionStatus;
import eu.siacs.conversations.crypto.PgpEngine; import eu.siacs.conversations.crypto.PgpEngine;
import eu.siacs.conversations.entities.Account; import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.entities.Bookmark;
import eu.siacs.conversations.entities.Contact; import eu.siacs.conversations.entities.Contact;
import eu.siacs.conversations.entities.Conversation; import eu.siacs.conversations.entities.Conversation;
import eu.siacs.conversations.entities.Message; import eu.siacs.conversations.entities.Message;
@ -245,9 +246,13 @@ public class XmppConnectionService extends Service {
return message; return message;
} }
public Conversation findMuc(String name, Account account) { public Conversation findMuc(Bookmark bookmark) {
return findMuc(bookmark.getJid(), bookmark.getAccount());
}
public Conversation findMuc(String jid, Account account) {
for (Conversation conversation : this.conversations) { for (Conversation conversation : this.conversations) {
if (conversation.getContactJid().split("/")[0].equals(name) if (conversation.getContactJid().split("/")[0].equals(jid)
&& (conversation.getAccount() == account)) { && (conversation.getAccount() == account)) {
return conversation; return conversation;
} }
@ -466,6 +471,7 @@ public class XmppConnectionService extends Service {
account.getRoster().clearPresences(); account.getRoster().clearPresences();
account.clearPresences(); // self presences account.clearPresences(); // self presences
fetchRosterFromServer(account); fetchRosterFromServer(account);
fetchBookmarks(account);
sendPresencePacket(account, mPresenceGenerator.sendPresence(account)); sendPresencePacket(account, mPresenceGenerator.sendPresence(account));
connectMultiModeConversations(account); connectMultiModeConversations(account);
updateConversationUi(); updateConversationUi();
@ -661,6 +667,45 @@ public class XmppConnectionService extends Service {
}); });
} }
public void fetchBookmarks(Account account) {
IqPacket iqPacket = new IqPacket(IqPacket.TYPE_GET);
Element query = iqPacket.query("jabber:iq:private");
query.addChild("storage", "storage:bookmarks");
OnIqPacketReceived callback = new OnIqPacketReceived() {
@Override
public void onIqPacketReceived(Account account, IqPacket packet) {
Element query = packet.query();
List<Bookmark> bookmarks = new ArrayList<Bookmark>();
Element storage = query.findChild("storage", "storage:bookmarks");
if (storage!=null) {
for(Element item : storage.getChildren()) {
if (item.getName().equals("conference")) {
Log.d(LOGTAG,item.toString());
Bookmark bookmark = Bookmark.parse(item,account);
bookmarks.add(bookmark);
if (bookmark.autojoin()) {
Log.d(LOGTAG,"has autojoin");
Conversation conversation = findMuc(bookmark);
if (conversation!=null) {
Log.d(LOGTAG,"conversation existed. adding bookmark");
conversation.setBookmark(bookmark);
} else {
Log.d(LOGTAG,"creating new conversation");
conversation = findOrCreateConversation(account, bookmark.getJid(), true);
conversation.setBookmark(bookmark);
}
}
}
}
}
account.setBookmarks(bookmarks);
}
};
sendIqPacket(account, iqPacket, callback);
}
private void mergePhoneContactsWithRoster() { private void mergePhoneContactsWithRoster() {
PhoneHelper.loadPhoneContacts(getApplicationContext(), PhoneHelper.loadPhoneContacts(getApplicationContext(),
new OnPhoneContactsLoadedListener() { new OnPhoneContactsLoadedListener() {
@ -1297,6 +1342,10 @@ public class XmppConnectionService extends Service {
account.getXmppConnection().sendPresencePacket(packet); account.getXmppConnection().sendPresencePacket(packet);
} }
public void sendIqPacket(Account account, IqPacket packet, OnIqPacketReceived callback) {
account.getXmppConnection().sendIqPacket(packet, callback);
}
public MessageGenerator getMessageGenerator() { public MessageGenerator getMessageGenerator() {
return this.mMessageGenerator; return this.mMessageGenerator;
} }

View File

@ -37,6 +37,7 @@ import android.widget.Spinner;
import android.widget.TextView; import android.widget.TextView;
import eu.siacs.conversations.R; import eu.siacs.conversations.R;
import eu.siacs.conversations.entities.Account; import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.entities.Bookmark;
import eu.siacs.conversations.entities.Contact; import eu.siacs.conversations.entities.Contact;
import eu.siacs.conversations.entities.Conversation; import eu.siacs.conversations.entities.Conversation;
import eu.siacs.conversations.entities.ListItem; import eu.siacs.conversations.entities.ListItem;
@ -64,6 +65,9 @@ public class StartConversation extends XmppActivity {
private EditText mSearchEditText; private EditText mSearchEditText;
public int conference_context_id;
public int contact_context_id;
private TabListener mTabListener = new TabListener() { private TabListener mTabListener = new TabListener() {
@Override @Override
@ -115,7 +119,7 @@ public class StartConversation extends XmppActivity {
imm.hideSoftInputFromWindow(mSearchEditText.getWindowToken(), imm.hideSoftInputFromWindow(mSearchEditText.getWindowToken(),
InputMethodManager.HIDE_IMPLICIT_ONLY); InputMethodManager.HIDE_IMPLICIT_ONLY);
mSearchEditText.setText(""); mSearchEditText.setText("");
filterContacts(null); filter(null);
return true; return true;
} }
}; };
@ -123,7 +127,7 @@ public class StartConversation extends XmppActivity {
@Override @Override
public void afterTextChanged(Editable editable) { public void afterTextChanged(Editable editable) {
filterContacts(editable.toString()); filter(editable.toString());
} }
@Override @Override
@ -172,9 +176,19 @@ public class StartConversation extends XmppActivity {
mConferenceAdapter = new ListItemAdapter(conferences); mConferenceAdapter = new ListItemAdapter(conferences);
mConferenceListFragment.setListAdapter(mConferenceAdapter); mConferenceListFragment.setListAdapter(mConferenceAdapter);
mConferenceListFragment.setContextMenu(R.menu.conference_context);
mConferenceListFragment.setOnListItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
openConversationForBookmark(position);
}
});
mContactsAdapter = new ListItemAdapter(contacts); mContactsAdapter = new ListItemAdapter(contacts);
mContactsListFragment.setListAdapter(mContactsAdapter); mContactsListFragment.setListAdapter(mContactsAdapter);
mContactsListFragment.setContextMenu(R.menu.contact_context);
mContactsListFragment mContactsListFragment
.setOnListItemClickListener(new OnItemClickListener() { .setOnListItemClickListener(new OnItemClickListener() {
@ -192,18 +206,35 @@ public class StartConversation extends XmppActivity {
Conversation conversation = xmppConnectionService Conversation conversation = xmppConnectionService
.findOrCreateConversation(contact.getAccount(), .findOrCreateConversation(contact.getAccount(),
contact.getJid(), false); contact.getJid(), false);
switchToConversation(conversation, null, false); switchToConversation(conversation);
} }
protected void openDetailsForContact(int position) { protected void openConversationForContact() {
int position = contact_context_id;
openConversationForContact(position);
}
protected void openConversationForBookmark() {
openConversationForBookmark(conference_context_id);
}
protected void openConversationForBookmark(int position) {
Bookmark bookmark = (Bookmark) conferences.get(position);
Conversation conversation = xmppConnectionService.findOrCreateConversation(bookmark.getAccount(), bookmark.getJid(), true);
switchToConversation(conversation);
}
protected void openDetailsForContact() {
int position = contact_context_id;
Contact contact = (Contact) contacts.get(position); Contact contact = (Contact) contacts.get(position);
switchToContactDetails(contact); switchToContactDetails(contact);
} }
protected void deleteContact(int position) { protected void deleteContact() {
int position = contact_context_id;
Contact contact = (Contact) contacts.get(position); Contact contact = (Contact) contacts.get(position);
xmppConnectionService.deleteContactOnServer(contact); xmppConnectionService.deleteContactOnServer(contact);
filterContacts(mSearchEditText.getText().toString()); filter(mSearchEditText.getText().toString());
} }
protected void showCreateContactDialog() { protected void showCreateContactDialog() {
@ -339,9 +370,9 @@ public class StartConversation extends XmppActivity {
@Override @Override
void onBackendConnected() { void onBackendConnected() {
if (mSearchEditText != null) { if (mSearchEditText != null) {
filterContacts(mSearchEditText.getText().toString()); filter(mSearchEditText.getText().toString());
} else { } else {
filterContacts(null); filter(null);
} }
this.mActivatedAccounts.clear(); this.mActivatedAccounts.clear();
for (Account account : xmppConnectionService.getAccounts()) { for (Account account : xmppConnectionService.getAccounts()) {
@ -354,6 +385,11 @@ public class StartConversation extends XmppActivity {
.getKnownConferenceHosts(); .getKnownConferenceHosts();
} }
protected void filter(String needle) {
this.filterContacts(needle);
this.filterConferences(needle);
}
protected void filterContacts(String needle) { protected void filterContacts(String needle) {
this.contacts.clear(); this.contacts.clear();
for (Account account : xmppConnectionService.getAccounts()) { for (Account account : xmppConnectionService.getAccounts()) {
@ -369,6 +405,21 @@ public class StartConversation extends XmppActivity {
mContactsAdapter.notifyDataSetChanged(); mContactsAdapter.notifyDataSetChanged();
} }
protected void filterConferences(String needle) {
this.conferences.clear();
for (Account account : xmppConnectionService.getAccounts()) {
if (account.getStatus() != Account.STATUS_DISABLED) {
for(Bookmark bookmark : account.getBookmarks()) {
if (bookmark.match(needle)) {
this.conferences.add(bookmark);
}
}
}
}
Collections.sort(this.conferences);
mConferenceAdapter.notifyDataSetChanged();
}
private void onTabChanged() { private void onTabChanged() {
invalidateOptionsMenu(); invalidateOptionsMenu();
} }
@ -403,7 +454,11 @@ public class StartConversation extends XmppActivity {
public static class MyListFragment extends ListFragment { public static class MyListFragment extends ListFragment {
private AdapterView.OnItemClickListener mOnItemClickListener; private AdapterView.OnItemClickListener mOnItemClickListener;
private int mContextPosition = -1; private int mResContextMenu;
public void setContextMenu(int res) {
this.mResContextMenu = res;
}
@Override @Override
public void onListItemClick(ListView l, View v, int position, long id) { public void onListItemClick(ListView l, View v, int position, long id) {
@ -426,10 +481,15 @@ public class StartConversation extends XmppActivity {
public void onCreateContextMenu(ContextMenu menu, View v, public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) { ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo); super.onCreateContextMenu(menu, v, menuInfo);
getActivity().getMenuInflater().inflate(R.menu.contact_context, StartConversation activity = (StartConversation) getActivity();
activity.getMenuInflater().inflate(mResContextMenu,
menu); menu);
AdapterView.AdapterContextMenuInfo acmi = (AdapterContextMenuInfo) menuInfo; AdapterView.AdapterContextMenuInfo acmi = (AdapterContextMenuInfo) menuInfo;
this.mContextPosition = acmi.position; if (mResContextMenu == R.menu.conference_context) {
activity.conference_context_id = acmi.position;
} else {
activity.contact_context_id = acmi.position;
}
} }
@Override @Override
@ -437,13 +497,16 @@ public class StartConversation extends XmppActivity {
StartConversation activity = (StartConversation) getActivity(); StartConversation activity = (StartConversation) getActivity();
switch (item.getItemId()) { switch (item.getItemId()) {
case R.id.context_start_conversation: case R.id.context_start_conversation:
activity.openConversationForContact(mContextPosition); activity.openConversationForContact();
break; break;
case R.id.context_contact_details: case R.id.context_contact_details:
activity.openDetailsForContact(mContextPosition); activity.openDetailsForContact();
break; break;
case R.id.context_delete_contact: case R.id.context_delete_contact:
activity.deleteContact(mContextPosition); activity.deleteContact();
break;
case R.id.context_join_conference:
activity.openConversationForBookmark();
break; break;
} }
return true; return true;