From 3bdf84b6a0170264a20795e7956a68421f312fa3 Mon Sep 17 00:00:00 2001 From: iNPUTmice Date: Mon, 7 Jul 2014 09:32:10 +0200 Subject: [PATCH] filter contacts and starting confercenes --- res/menu/start_conversation.xml | 12 +- .../siacs/conversations/entities/Contact.java | 21 ++- .../conversations/entities/ListItem.java | 7 + .../conversations/ui/StartConversation.java | 171 ++++++++++++++++-- .../siacs/conversations/utils/UIHelper.java | 4 +- 5 files changed, 185 insertions(+), 30 deletions(-) create mode 100644 src/eu/siacs/conversations/entities/ListItem.java diff --git a/res/menu/start_conversation.xml b/res/menu/start_conversation.xml index 89ba675a1..3daff29a3 100644 --- a/res/menu/start_conversation.xml +++ b/res/menu/start_conversation.xml @@ -1,12 +1,6 @@ - + { + public String getDisplayName(); + public String getJid(); + public String getProfilePhoto(); +} diff --git a/src/eu/siacs/conversations/ui/StartConversation.java b/src/eu/siacs/conversations/ui/StartConversation.java index ba6038197..3cf09a977 100644 --- a/src/eu/siacs/conversations/ui/StartConversation.java +++ b/src/eu/siacs/conversations/ui/StartConversation.java @@ -1,44 +1,71 @@ package eu.siacs.conversations.ui; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + import android.app.ActionBar; import android.app.ActionBar.Tab; import android.app.ActionBar.TabListener; import android.app.Fragment; import android.app.FragmentTransaction; import android.app.ListFragment; +import android.content.Context; import android.os.Bundle; import android.support.v13.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; +import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; +import android.view.MenuItem.OnActionExpandListener; +import android.view.View; +import android.view.ViewGroup; +import android.widget.AdapterView; +import android.widget.AdapterView.OnItemClickListener; +import android.widget.ArrayAdapter; +import android.widget.ImageView; +import android.widget.ListView; +import android.widget.SearchView; +import android.widget.SearchView.OnQueryTextListener; +import android.widget.TextView; import eu.siacs.conversations.R; +import eu.siacs.conversations.entities.Account; +import eu.siacs.conversations.entities.Contact; +import eu.siacs.conversations.entities.Conversation; +import eu.siacs.conversations.entities.ListItem; +import eu.siacs.conversations.utils.UIHelper; public class StartConversation extends XmppActivity { private Tab mContactsTab; private Tab mConferencesTab; private ViewPager mViewPager; - private ListFragment mContactsListFragment = new ListFragment(); - private ListFragment mConferenceListFragment = new ListFragment(); + private SearchView mSearchView; + + private MyListFragment mContactsListFragment = new MyListFragment(); + private List contacts = new ArrayList(); + private ArrayAdapter mContactsAdapter; + + private MyListFragment mConferenceListFragment = new MyListFragment(); + private List conferences = new ArrayList(); + private ArrayAdapter mConferenceAdapter; private TabListener mTabListener = new TabListener() { @Override public void onTabUnselected(Tab tab, FragmentTransaction ft) { - // TODO Auto-generated method stub - + return; } @Override public void onTabSelected(Tab tab, FragmentTransaction ft) { mViewPager.setCurrentItem(tab.getPosition()); - invalidateOptionsMenu(); + onTabChanged(); } @Override public void onTabReselected(Tab tab, FragmentTransaction ft) { - // TODO Auto-generated method stub - + return; } }; @@ -46,7 +73,33 @@ public class StartConversation extends XmppActivity { @Override public void onPageSelected(int position) { getActionBar().setSelectedNavigationItem(position); + onTabChanged(); + } + }; + private OnQueryTextListener mOnQueryTextListener = new OnQueryTextListener() { + + @Override + public boolean onQueryTextSubmit(String query) { + return true; + } + + @Override + public boolean onQueryTextChange(String newText) { + filterContacts(newText); + return true; + } + }; + private OnActionExpandListener mOnSearchActionExpandListener = new OnActionExpandListener() { + + @Override + public boolean onMenuItemActionExpand(MenuItem item) { + return true; + } + + @Override + public boolean onMenuItemActionCollapse(MenuItem item) { invalidateOptionsMenu(); + return true; } }; @@ -65,18 +118,17 @@ public class StartConversation extends XmppActivity { actionBar.addTab(mContactsTab); actionBar.addTab(mConferencesTab); - mViewPager.setOnPageChangeListener(mOnPageChangeListener); mViewPager.setAdapter(new FragmentPagerAdapter(getFragmentManager()) { - + @Override public int getCount() { return 2; } - + @Override public Fragment getItem(int position) { - if (position==0) { + if (position == 0) { return mContactsListFragment; } else { return mConferenceListFragment; @@ -84,18 +136,44 @@ public class StartConversation extends XmppActivity { } }); + mConferenceAdapter = new ListItemAdapter(conferences); + mConferenceListFragment.setListAdapter(mConferenceAdapter); + + mContactsAdapter = new ListItemAdapter(contacts); + mContactsListFragment.setListAdapter(mContactsAdapter); + mContactsListFragment + .setOnListItemClickListener(new OnItemClickListener() { + + @Override + public void onItemClick(AdapterView arg0, View arg1, + int position, long arg3) { + Contact contact = (Contact) contacts.get(position); + Conversation conversation = xmppConnectionService + .findOrCreateConversation(contact.getAccount(), + contact.getJid(), false); + switchToConversation(conversation, null, false); + } + }); + } - + @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.start_conversation, menu); - MenuItem menuCreateContact = (MenuItem) menu.findItem(R.id.action_create_contact); - MenuItem menuCreateConference = (MenuItem) menu.findItem(R.id.action_create_conference); + MenuItem menuCreateContact = (MenuItem) menu + .findItem(R.id.action_create_contact); + MenuItem menuCreateConference = (MenuItem) menu + .findItem(R.id.action_create_conference); + MenuItem menuSearch = (MenuItem) menu.findItem(R.id.action_search); if (getActionBar().getSelectedNavigationIndex() == 0) { menuCreateConference.setVisible(false); } else { menuCreateContact.setVisible(false); } + mSearchView = (SearchView) menuSearch.getActionView(); + mSearchView.setOnQueryTextListener(this.mOnQueryTextListener); + menuSearch + .setOnActionExpandListener(this.mOnSearchActionExpandListener); return true; } @@ -110,8 +188,71 @@ public class StartConversation extends XmppActivity { @Override void onBackendConnected() { - // TODO Auto-generated method stub + filterContacts(null); + } + + protected void filterContacts(String needle) { + this.contacts.clear(); + for (Account account : xmppConnectionService.getAccounts()) { + if (account.getStatus() != Account.STATUS_DISABLED) { + for (Contact contact : account.getRoster().getContacts()) { + if (contact.showInRoster() && contact.match(needle)) { + this.contacts.add(contact); + } + } + } + } + Collections.sort(this.contacts); + mContactsAdapter.notifyDataSetChanged(); + } + + private void onTabChanged() { + if (mSearchView == null || mSearchView.isIconified()) { + invalidateOptionsMenu(); + } + } + + private class ListItemAdapter extends ArrayAdapter { + + public ListItemAdapter(List objects) { + super(getApplicationContext(), 0, objects); + } + + @Override + public View getView(int position, View view, ViewGroup parent) { + LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); + ListItem item = getItem(position); + if (view == null) { + view = (View) inflater.inflate(R.layout.contact, null); + } + TextView name = (TextView) view + .findViewById(R.id.contact_display_name); + TextView jid = (TextView) view.findViewById(R.id.contact_jid); + ImageView picture = (ImageView) view + .findViewById(R.id.contact_photo); + + jid.setText(item.getJid()); + name.setText(item.getDisplayName()); + picture.setImageBitmap(UIHelper.getContactPicture(item, 48, + this.getContext(), false)); + return view; + } } + public static class MyListFragment extends ListFragment { + private AdapterView.OnItemClickListener mOnItemClickListener; + + @Override + public void onListItemClick(ListView l, View v, int position, long id) { + if (mOnItemClickListener != null) { + mOnItemClickListener.onItemClick(l, v, position, id); + } + } + + public void setOnListItemClickListener(AdapterView.OnItemClickListener l) { + this.mOnItemClickListener = l; + } + } + } diff --git a/src/eu/siacs/conversations/utils/UIHelper.java b/src/eu/siacs/conversations/utils/UIHelper.java index 7f24943fe..059d9fad8 100644 --- a/src/eu/siacs/conversations/utils/UIHelper.java +++ b/src/eu/siacs/conversations/utils/UIHelper.java @@ -13,11 +13,11 @@ import eu.siacs.conversations.R; import eu.siacs.conversations.entities.Account; import eu.siacs.conversations.entities.Contact; import eu.siacs.conversations.entities.Conversation; +import eu.siacs.conversations.entities.ListItem; import eu.siacs.conversations.entities.Message; import eu.siacs.conversations.entities.MucOptions.User; import eu.siacs.conversations.ui.ConversationActivity; import eu.siacs.conversations.ui.ManageAccountActivity; - import android.app.Activity; import android.app.AlertDialog; import android.app.Notification; @@ -239,7 +239,7 @@ public class UIHelper { } } - public static Bitmap getContactPicture(Contact contact, int dpSize, + public static Bitmap getContactPicture(ListItem contact, int dpSize, Context context, boolean notification) { String uri = contact.getProfilePhoto(); if (uri == null) {