From edb286a44dec035e52b7665df07470de93c82348 Mon Sep 17 00:00:00 2001 From: Moxie Marlinspike Date: Tue, 31 Jul 2012 13:53:00 -0700 Subject: [PATCH] Add search support for v11+ --- res/layout/empty2.xml | 11 ++++++++ res/menu/conversation_list.xml | 3 --- res/menu/conversation_list_empty.xml | 6 +++++ .../securesms/ConversationListFragment.java | 27 ++++++++++++++++--- .../loaders/ConversationListLoader.java | 13 +++++++-- 5 files changed, 52 insertions(+), 8 deletions(-) create mode 100644 res/layout/empty2.xml create mode 100644 res/menu/conversation_list_empty.xml diff --git a/res/layout/empty2.xml b/res/layout/empty2.xml new file mode 100644 index 0000000000..0e384ff840 --- /dev/null +++ b/res/layout/empty2.xml @@ -0,0 +1,11 @@ + + + + + diff --git a/res/menu/conversation_list.xml b/res/menu/conversation_list.xml index 9acd2f5fd3..87b5019cf4 100644 --- a/res/menu/conversation_list.xml +++ b/res/menu/conversation_list.xml @@ -1,8 +1,5 @@ - + + + \ No newline at end of file diff --git a/src/org/thoughtcrime/securesms/ConversationListFragment.java b/src/org/thoughtcrime/securesms/ConversationListFragment.java index 36f31f5131..9b09b1a985 100644 --- a/src/org/thoughtcrime/securesms/ConversationListFragment.java +++ b/src/org/thoughtcrime/securesms/ConversationListFragment.java @@ -20,6 +20,7 @@ import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.database.Cursor; +import android.os.Build; import android.os.Bundle; import android.support.v4.app.LoaderManager; import android.support.v4.content.Loader; @@ -50,6 +51,7 @@ public class ConversationListFragment extends SherlockListFragment private ConversationSelectedListener listener; private MasterSecret masterSecret; + private String queryFilter = ""; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) { @@ -75,9 +77,13 @@ public class ConversationListFragment extends SherlockListFragment @Override public void onPrepareOptionsMenu(Menu menu) { - if (this.masterSecret != null) { - MenuInflater inflater = this.getSherlockActivity().getSupportMenuInflater(); + MenuInflater inflater = this.getSherlockActivity().getSupportMenuInflater(); + + if (this.masterSecret != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { inflater.inflate(R.menu.conversation_list, menu); + initializeSearch((android.widget.SearchView)menu.findItem(R.id.menu_search).getActionView()); + } else { + inflater.inflate(R.menu.conversation_list_empty, menu); } super.onPrepareOptionsMenu(menu); @@ -96,6 +102,21 @@ public class ConversationListFragment extends SherlockListFragment initializeListAdapter(); } + private void initializeSearch(android.widget.SearchView searchView) { + searchView.setOnQueryTextListener(new android.widget.SearchView.OnQueryTextListener() { + @Override + public boolean onQueryTextSubmit(String query) { + ConversationListFragment.this.queryFilter = query; + ConversationListFragment.this.getLoaderManager().restartLoader(0, null, ConversationListFragment.this); + return true; + } + @Override + public boolean onQueryTextChange(String newText) { + return onQueryTextSubmit(newText); + } + }); + } + private void initializeBatchListener() { getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { @Override @@ -159,7 +180,7 @@ public class ConversationListFragment extends SherlockListFragment @Override public Loader onCreateLoader(int arg0, Bundle arg1) { - return new ConversationListLoader(getActivity(), null); + return new ConversationListLoader(getActivity(), queryFilter); } @Override diff --git a/src/org/thoughtcrime/securesms/database/loaders/ConversationListLoader.java b/src/org/thoughtcrime/securesms/database/loaders/ConversationListLoader.java index b55757eb8f..a6dabb9787 100644 --- a/src/org/thoughtcrime/securesms/database/loaders/ConversationListLoader.java +++ b/src/org/thoughtcrime/securesms/database/loaders/ConversationListLoader.java @@ -4,8 +4,11 @@ import android.content.Context; import android.database.Cursor; import android.support.v4.content.CursorLoader; +import org.thoughtcrime.securesms.contacts.ContactAccessor; import org.thoughtcrime.securesms.database.DatabaseFactory; +import java.util.List; + public class ConversationListLoader extends CursorLoader { private final String filter; @@ -19,7 +22,13 @@ public class ConversationListLoader extends CursorLoader { @Override public Cursor loadInBackground() { - return DatabaseFactory.getThreadDatabase(context).getConversationList(); - } + if (filter != null && filter.trim().length() != 0) { + List numbers = ContactAccessor.getInstance() + .getNumbersForThreadSearchFilter(filter, context.getContentResolver()); + return DatabaseFactory.getThreadDatabase(context).getFilteredConversationList(numbers); + } else { + return DatabaseFactory.getThreadDatabase(context).getConversationList(); + } + } }