k-9/k9mail/src/main/java/com/fsck/k9/activity/FolderListFilter.java

131 lines
3.8 KiB
Java
Raw Normal View History

2010-10-03 07:08:42 -04:00
package com.fsck.k9.activity;
import java.util.ArrayList;
import java.util.List;
2014-04-07 14:35:16 -04:00
import java.util.Locale;
2010-10-03 07:08:42 -04:00
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 {
2010-10-03 07:08:42 -04:00
/**
* 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.
*/
2014-10-04 06:45:45 -04:00
private List<T> mOriginalValues = null;
2010-10-03 07:08:42 -04:00
/**
* Create a filter for a list of folders.
*
* @param folderNames
*/
public FolderListFilter(final ArrayAdapter<T> folderNames) {
2010-10-03 07:08:42 -04:00
this.mFolders = folderNames;
}
/**
* Do the actual search.
* {@inheritDoc}
*
* @see #publishResults(CharSequence, FilterResults)
*/
@Override
protected FilterResults performFiltering(CharSequence searchTerm) {
2010-10-03 07:08:42 -04:00
FilterResults results = new FilterResults();
// Copy the values from mFolders to mOriginalValues if this is the
// first time this method is called.
if (mOriginalValues == null) {
2010-10-03 07:08:42 -04:00
int count = mFolders.getCount();
mOriginalValues = new ArrayList<T>(count);
for (int i = 0; i < count; i++) {
2010-10-03 07:08:42 -04:00
mOriginalValues.add(mFolders.getItem(i));
}
}
2014-04-07 14:35:16 -04:00
Locale locale = Locale.getDefault();
if ((searchTerm == null) || (searchTerm.length() == 0)) {
2014-10-04 06:45:45 -04:00
List<T> list = new ArrayList<T>(mOriginalValues);
2010-10-03 07:08:42 -04:00
results.values = list;
results.count = list.size();
} else {
2014-04-07 14:35:16 -04:00
final String searchTermString = searchTerm.toString().toLowerCase(locale);
2010-10-03 07:08:42 -04:00
final String[] words = searchTermString.split(" ");
final int wordCount = words.length;
2014-10-04 06:45:45 -04:00
final List<T> values = mOriginalValues;
2010-10-03 07:08:42 -04:00
2014-10-04 06:45:45 -04:00
final List<T> newValues = new ArrayList<T>();
2010-10-03 07:08:42 -04:00
for (final T value : values) {
2014-04-07 14:35:16 -04:00
final String valueText = value.toString().toLowerCase(locale);
2010-10-03 07:08:42 -04:00
for (int k = 0; k < wordCount; k++) {
if (valueText.contains(words[k])) {
2010-10-03 07:08:42 -04:00
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) {
2010-10-03 07:08:42 -04:00
// Don't notify for every change
mFolders.setNotifyOnChange(false);
try {
2011-03-22 03:07:32 -04:00
//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();
} finally {
2011-03-22 03:07:32 -04:00
// restore notification status
mFolders.setNotifyOnChange(true);
2010-10-03 07:08:42 -04:00
}
}
public void invalidate() {
2010-10-03 07:08:42 -04:00
mOriginalValues = null;
}
}