Add search support for v11+

This commit is contained in:
Moxie Marlinspike 2012-07-31 13:53:00 -07:00
parent 0f1fda23a5
commit edb286a44d
5 changed files with 52 additions and 8 deletions

11
res/layout/empty2.xml Normal file
View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_menu_search_holo_dark"
android:visibility="invisible"/>
</FrameLayout>

View File

@ -1,8 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="Batch Mode"
android:id="@+id/menu_batch_mode"
android:icon="@android:drawable/ic_menu_share" />
<item android:title="Search"
android:id="@+id/menu_search"

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/fake_menu_item"
android:actionLayout="@layout/empty2"
android:showAsAction="ifRoom" />
</menu>

View File

@ -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();
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<Cursor> onCreateLoader(int arg0, Bundle arg1) {
return new ConversationListLoader(getActivity(), null);
return new ConversationListLoader(getActivity(), queryFilter);
}
@Override

View File

@ -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() {
if (filter != null && filter.trim().length() != 0) {
List<String> numbers = ContactAccessor.getInstance()
.getNumbersForThreadSearchFilter(filter, context.getContentResolver());
return DatabaseFactory.getThreadDatabase(context).getFilteredConversationList(numbers);
} else {
return DatabaseFactory.getThreadDatabase(context).getConversationList();
}
}
}