mirror of
https://github.com/moparisthebest/k-9
synced 2025-02-07 02:30:10 -05:00
Better text filter for ChooseFolder (folder is displayed if search term is found anywhere in the folder name, not just at the beginning)
Heavily based on a patch provided by Marcus.Wolschon Fixes issue 2098
This commit is contained in:
parent
1f725a2d5c
commit
2e6a67c2a7
@ -9,6 +9,7 @@ import android.view.View;
|
||||
import android.view.Window;
|
||||
import android.widget.AdapterView;
|
||||
import android.widget.ArrayAdapter;
|
||||
import android.widget.Filter;
|
||||
import android.widget.ListView;
|
||||
import android.widget.TextView;
|
||||
import com.fsck.k9.*;
|
||||
@ -26,7 +27,7 @@ public class ChooseFolder extends K9ListActivity
|
||||
String mSelectFolder;
|
||||
Account mAccount;
|
||||
MessageReference mMessageReference;
|
||||
ArrayAdapter<String> adapter;
|
||||
ArrayAdapter<String> mAdapter;
|
||||
private ChooseFolderHandler mHandler = new ChooseFolderHandler();
|
||||
String heldInbox = null;
|
||||
boolean hideCurrentFolder = true;
|
||||
@ -49,7 +50,6 @@ public class ChooseFolder extends K9ListActivity
|
||||
|
||||
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
|
||||
getListView().setFastScrollEnabled(true);
|
||||
getListView().setTextFilterEnabled(true);
|
||||
getListView().setItemsCanFocus(false);
|
||||
getListView().setChoiceMode(ListView.CHOICE_MODE_NONE);
|
||||
Intent intent = getIntent();
|
||||
@ -73,13 +73,25 @@ public class ChooseFolder extends K9ListActivity
|
||||
if (mFolder == null)
|
||||
mFolder = "";
|
||||
|
||||
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
|
||||
mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1)
|
||||
{
|
||||
private Filter myFilter = null;
|
||||
|
||||
setListAdapter(adapter);
|
||||
@Override
|
||||
public Filter getFilter()
|
||||
{
|
||||
if (myFilter == null)
|
||||
{
|
||||
myFilter = new FolderListFilter<String>(this);
|
||||
}
|
||||
return myFilter;
|
||||
}
|
||||
};
|
||||
|
||||
setListAdapter(mAdapter);
|
||||
|
||||
|
||||
MessagingController.getInstance(getApplication()).listFolders(mAccount,
|
||||
false, mListener);
|
||||
MessagingController.getInstance(getApplication()).listFolders(mAccount, false, mListener);
|
||||
|
||||
|
||||
this.getListView().setOnItemClickListener(new AdapterView.OnItemClickListener()
|
||||
@ -120,7 +132,14 @@ public class ChooseFolder extends K9ListActivity
|
||||
setProgressBarIndeterminateVisibility(msg.arg1 != 0);
|
||||
break;
|
||||
case MSG_DATA_CHANGED:
|
||||
adapter.notifyDataSetChanged();
|
||||
mAdapter.notifyDataSetChanged();
|
||||
|
||||
/*
|
||||
* Only enable the text filter after the list has been
|
||||
* populated to avoid possible race conditions because our
|
||||
* FolderListFilter isn't really thread-safe.
|
||||
*/
|
||||
getListView().setTextFilterEnabled(true);
|
||||
break;
|
||||
case MSG_SET_SELECTED_FOLDER:
|
||||
getListView().setSelection(msg.arg1);
|
||||
@ -261,20 +280,20 @@ public class ChooseFolder extends K9ListActivity
|
||||
return aName.compareToIgnoreCase(bName);
|
||||
}
|
||||
});
|
||||
adapter.setNotifyOnChange(false);
|
||||
adapter.clear();
|
||||
mAdapter.setNotifyOnChange(false);
|
||||
mAdapter.clear();
|
||||
int selectedFolder = -1;
|
||||
int position = 0;
|
||||
for (String name : localFolders)
|
||||
{
|
||||
if (K9.INBOX.equalsIgnoreCase(name))
|
||||
{
|
||||
adapter.add(getString(R.string.special_mailbox_name_inbox));
|
||||
mAdapter.add(getString(R.string.special_mailbox_name_inbox));
|
||||
heldInbox = name;
|
||||
}
|
||||
else
|
||||
{
|
||||
adapter.add(name);
|
||||
mAdapter.add(name);
|
||||
}
|
||||
|
||||
if (mSelectFolder != null)
|
||||
|
138
src/com/fsck/k9/activity/FolderListFilter.java
Normal file
138
src/com/fsck/k9/activity/FolderListFilter.java
Normal file
@ -0,0 +1,138 @@
|
||||
package com.fsck.k9.activity;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import android.util.Log;
|
||||
import android.widget.ArrayAdapter;
|
||||
import android.widget.Filter;
|
||||
|
||||
import com.fsck.k9.K9;
|
||||
|
||||
/**
|
||||
* Filter to search for occurences of the search-expression in any place of the
|
||||
* folder-name instead of doing jsut a prefix-search.
|
||||
*
|
||||
* @author Marcus@Wolschon.biz
|
||||
*/
|
||||
public class FolderListFilter<T> extends Filter
|
||||
{
|
||||
/**
|
||||
* ArrayAdapter that contains the list of folders displayed in the
|
||||
* ListView.
|
||||
* This object is modified by {@link #publishResults} to reflect the
|
||||
* changes due to the filtering performed by {@link #performFiltering}.
|
||||
* This in turn will change the folders displayed in the ListView.
|
||||
*/
|
||||
private ArrayAdapter<T> mFolders;
|
||||
|
||||
/**
|
||||
* All folders.
|
||||
*/
|
||||
private ArrayList<T> mOriginalValues = null;
|
||||
|
||||
/**
|
||||
* Create a filter for a list of folders.
|
||||
*
|
||||
* @param folderNames
|
||||
*/
|
||||
public FolderListFilter(final ArrayAdapter<T> folderNames)
|
||||
{
|
||||
this.mFolders = folderNames;
|
||||
}
|
||||
|
||||
/**
|
||||
* Do the actual search.
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @see #publishResults(CharSequence, FilterResults)
|
||||
*/
|
||||
@Override
|
||||
protected FilterResults performFiltering(CharSequence searchTerm)
|
||||
{
|
||||
FilterResults results = new FilterResults();
|
||||
|
||||
// Copy the values from mFolders to mOriginalValues if this is the
|
||||
// first time this method is called.
|
||||
if (mOriginalValues == null)
|
||||
{
|
||||
int count = mFolders.getCount();
|
||||
mOriginalValues = new ArrayList<T>(count);
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
mOriginalValues.add(mFolders.getItem(i));
|
||||
}
|
||||
}
|
||||
|
||||
if ((searchTerm == null) || (searchTerm.length() == 0))
|
||||
{
|
||||
ArrayList<T> list = new ArrayList<T>(mOriginalValues);
|
||||
results.values = list;
|
||||
results.count = list.size();
|
||||
}
|
||||
else
|
||||
{
|
||||
final String searchTermString = searchTerm.toString().toLowerCase();
|
||||
final String[] words = searchTermString.split(" ");
|
||||
final int wordCount = words.length;
|
||||
|
||||
final ArrayList<T> values = mOriginalValues;
|
||||
final int count = values.size();
|
||||
|
||||
final ArrayList<T> newValues = new ArrayList<T>();
|
||||
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
final T value = values.get(i);
|
||||
final String valueText = value.toString().toLowerCase();
|
||||
|
||||
for (int k = 0; k < wordCount; k++)
|
||||
{
|
||||
if (valueText.contains(words[k]))
|
||||
{
|
||||
newValues.add(value);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
results.values = newValues;
|
||||
results.count = newValues.size();
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
/**
|
||||
* Publish the results to the user-interface.
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
protected void publishResults(CharSequence constraint, FilterResults results)
|
||||
{
|
||||
// Don't notify for every change
|
||||
mFolders.setNotifyOnChange(false);
|
||||
|
||||
//noinspection unchecked
|
||||
final List<T> folders = (List<T>) results.values;
|
||||
mFolders.clear();
|
||||
if (folders != null)
|
||||
{
|
||||
for (T folder : folders)
|
||||
{
|
||||
if (folder != null)
|
||||
{
|
||||
mFolders.add(folder);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Log.w(K9.LOG_TAG, "FolderListFilter.publishResults - null search-result ");
|
||||
}
|
||||
|
||||
// Send notification that the data set changed now
|
||||
mFolders.notifyDataSetChanged();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user