Use Highlighter instead of HighlightQueryCursorAdapter

This commit is contained in:
Thialfihar 2014-05-14 20:52:17 +02:00
parent 69ce66be94
commit 84b754341d
3 changed files with 28 additions and 79 deletions

View File

@ -34,6 +34,7 @@ import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader; import android.support.v4.content.Loader;
import android.support.v4.view.MenuItemCompat; import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.ActionBarActivity; import android.support.v7.app.ActionBarActivity;
import android.support.v4.widget.CursorAdapter;
import android.support.v7.widget.SearchView; import android.support.v7.widget.SearchView;
import android.text.TextUtils; import android.text.TextUtils;
import android.view.ActionMode; import android.view.ActionMode;
@ -61,8 +62,8 @@ import org.sufficientlysecure.keychain.helper.ExportHelper;
import org.sufficientlysecure.keychain.pgp.PgpKeyHelper; import org.sufficientlysecure.keychain.pgp.PgpKeyHelper;
import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRingData; import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRingData;
import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings; import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings;
import org.sufficientlysecure.keychain.ui.adapter.HighlightQueryCursorAdapter;
import org.sufficientlysecure.keychain.ui.dialog.DeleteKeyDialogFragment; import org.sufficientlysecure.keychain.ui.dialog.DeleteKeyDialogFragment;
import org.sufficientlysecure.keychain.util.Highlighter;
import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.util.Log;
import java.util.Date; import java.util.Date;
@ -82,7 +83,7 @@ public class KeyListFragment extends LoaderFragment
private KeyListAdapter mAdapter; private KeyListAdapter mAdapter;
private StickyListHeadersListView mStickyList; private StickyListHeadersListView mStickyList;
private String mCurQuery; private String mQuery;
private SearchView mSearchView; private SearchView mSearchView;
// empty list layout // empty list layout
private BootstrapButton mButtonEmptyCreate; private BootstrapButton mButtonEmptyCreate;
@ -263,8 +264,8 @@ public class KeyListFragment extends LoaderFragment
Uri baseUri = KeyRings.buildUnifiedKeyRingsUri(); Uri baseUri = KeyRings.buildUnifiedKeyRingsUri();
String where = null; String where = null;
String whereArgs[] = null; String whereArgs[] = null;
if (mCurQuery != null) { if (mQuery != null) {
String[] words = mCurQuery.trim().split("\\s+"); String[] words = mQuery.trim().split("\\s+");
whereArgs = new String[words.length]; whereArgs = new String[words.length];
for (int i = 0; i < words.length; ++i) { for (int i = 0; i < words.length; ++i) {
if (where == null) { if (where == null) {
@ -286,7 +287,7 @@ public class KeyListFragment extends LoaderFragment
public void onLoadFinished(Loader<Cursor> loader, Cursor data) { public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
// Swap the new cursor in. (The framework will take care of closing the // Swap the new cursor in. (The framework will take care of closing the
// old cursor once we return.) // old cursor once we return.)
mAdapter.setSearchQuery(mCurQuery); mAdapter.setSearchQuery(mQuery);
mAdapter.swapCursor(data); mAdapter.swapCursor(data);
mStickyList.setAdapter(mAdapter); mStickyList.setAdapter(mAdapter);
@ -388,7 +389,7 @@ public class KeyListFragment extends LoaderFragment
@Override @Override
public boolean onMenuItemActionCollapse(MenuItem item) { public boolean onMenuItemActionCollapse(MenuItem item) {
mCurQuery = null; mQuery = null;
mSearchView.setQuery("", true); mSearchView.setQuery("", true);
getLoaderManager().restartLoader(0, null, KeyListFragment.this); getLoaderManager().restartLoader(0, null, KeyListFragment.this);
return true; return true;
@ -408,7 +409,7 @@ public class KeyListFragment extends LoaderFragment
// Called when the action bar search text has changed. Update // Called when the action bar search text has changed. Update
// the search filter, and restart the loader to do a new query // the search filter, and restart the loader to do a new query
// with this filter. // with this filter.
mCurQuery = !TextUtils.isEmpty(s) ? s : null; mQuery = !TextUtils.isEmpty(s) ? s : null;
getLoaderManager().restartLoader(0, null, this); getLoaderManager().restartLoader(0, null, this);
return true; return true;
} }
@ -416,7 +417,8 @@ public class KeyListFragment extends LoaderFragment
/** /**
* Implements StickyListHeadersAdapter from library * Implements StickyListHeadersAdapter from library
*/ */
private class KeyListAdapter extends HighlightQueryCursorAdapter implements StickyListHeadersAdapter { private class KeyListAdapter extends CursorAdapter implements StickyListHeadersAdapter {
private String mQuery;
private LayoutInflater mInflater; private LayoutInflater mInflater;
private HashMap<Integer, Boolean> mSelection = new HashMap<Integer, Boolean>(); private HashMap<Integer, Boolean> mSelection = new HashMap<Integer, Boolean>();
@ -427,6 +429,10 @@ public class KeyListFragment extends LoaderFragment
mInflater = LayoutInflater.from(context); mInflater = LayoutInflater.from(context);
} }
public void setSearchQuery(String query) {
mQuery = query;
}
@Override @Override
public Cursor swapCursor(Cursor newCursor) { public Cursor swapCursor(Cursor newCursor) {
return super.swapCursor(newCursor); return super.swapCursor(newCursor);
@ -465,18 +471,19 @@ public class KeyListFragment extends LoaderFragment
*/ */
@Override @Override
public void bindView(View view, Context context, Cursor cursor) { public void bindView(View view, Context context, Cursor cursor) {
Highlighter highlighter = new Highlighter(context, mQuery);
ItemViewHolder h = (ItemViewHolder) view.getTag(); ItemViewHolder h = (ItemViewHolder) view.getTag();
{ // set name and stuff, common to both key types { // set name and stuff, common to both key types
String userId = cursor.getString(INDEX_USER_ID); String userId = cursor.getString(INDEX_USER_ID);
String[] userIdSplit = PgpKeyHelper.splitUserId(userId); String[] userIdSplit = PgpKeyHelper.splitUserId(userId);
if (userIdSplit[0] != null) { if (userIdSplit[0] != null) {
h.mMainUserId.setText(highlightSearchQuery(userIdSplit[0])); h.mMainUserId.setText(highlighter.highlight(userIdSplit[0]));
} else { } else {
h.mMainUserId.setText(R.string.user_id_no_name); h.mMainUserId.setText(R.string.user_id_no_name);
} }
if (userIdSplit[1] != null) { if (userIdSplit[1] != null) {
h.mMainUserIdRest.setText(highlightSearchQuery(userIdSplit[1])); h.mMainUserIdRest.setText(highlighter.highlight(userIdSplit[1]));
h.mMainUserIdRest.setVisibility(View.VISIBLE); h.mMainUserIdRest.setVisibility(View.VISIBLE);
} else { } else {
h.mMainUserIdRest.setVisibility(View.GONE); h.mMainUserIdRest.setVisibility(View.GONE);

View File

@ -1,66 +0,0 @@
/*
* Copyright (C) 2014 Dominik Schürmann <dominik@dominikschuermann.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.sufficientlysecure.keychain.ui.adapter;
import android.content.Context;
import android.database.Cursor;
import android.support.v4.widget.CursorAdapter;
import android.text.Spannable;
import android.text.style.ForegroundColorSpan;
import org.sufficientlysecure.keychain.R;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public abstract class HighlightQueryCursorAdapter extends CursorAdapter {
private String mCurQuery;
public HighlightQueryCursorAdapter(Context context, Cursor c, int flags) {
super(context, c, flags);
mCurQuery = null;
}
public void setSearchQuery(String searchQuery) {
mCurQuery = searchQuery;
}
public String getSearchQuery() {
return mCurQuery;
}
protected Spannable highlightSearchQuery(String text) {
Spannable highlight = Spannable.Factory.getInstance().newSpannable(text);
if (mCurQuery != null) {
Pattern pattern = Pattern.compile("(?i)(" + mCurQuery.trim().replaceAll("\\s+", "|") + ")");
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
highlight.setSpan(
new ForegroundColorSpan(mContext.getResources().getColor(R.color.emphasis)),
matcher.start(),
matcher.end(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
return highlight;
} else {
return highlight;
}
}
}

View File

@ -19,6 +19,7 @@ package org.sufficientlysecure.keychain.ui.adapter;
import android.content.Context; import android.content.Context;
import android.database.Cursor; import android.database.Cursor;
import android.support.v4.widget.CursorAdapter;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
@ -29,6 +30,7 @@ import android.widget.TextView;
import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.pgp.PgpKeyHelper; import org.sufficientlysecure.keychain.pgp.PgpKeyHelper;
import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings; import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings;
import org.sufficientlysecure.keychain.util.Highlighter;
import java.util.Date; import java.util.Date;
@ -36,8 +38,9 @@ import java.util.Date;
/** /**
* Yes this class is abstract! * Yes this class is abstract!
*/ */
abstract public class SelectKeyCursorAdapter extends HighlightQueryCursorAdapter { abstract public class SelectKeyCursorAdapter extends CursorAdapter {
private String mQuery;
private LayoutInflater mInflater; private LayoutInflater mInflater;
protected int mIndexUserId, mIndexMasterKeyId, mIndexRevoked, mIndexExpiry; protected int mIndexUserId, mIndexMasterKeyId, mIndexRevoked, mIndexExpiry;
@ -48,6 +51,10 @@ abstract public class SelectKeyCursorAdapter extends HighlightQueryCursorAdapter
initIndex(c); initIndex(c);
} }
public void setSearchQuery(String query) {
mQuery = query;
}
@Override @Override
public Cursor swapCursor(Cursor newCursor) { public Cursor swapCursor(Cursor newCursor) {
initIndex(newCursor); initIndex(newCursor);
@ -101,19 +108,20 @@ abstract public class SelectKeyCursorAdapter extends HighlightQueryCursorAdapter
@Override @Override
public void bindView(View view, Context context, Cursor cursor) { public void bindView(View view, Context context, Cursor cursor) {
Highlighter highlighter = new Highlighter(context, mQuery);
ViewHolderItem h = (ViewHolderItem) view.getTag(); ViewHolderItem h = (ViewHolderItem) view.getTag();
String userId = cursor.getString(mIndexUserId); String userId = cursor.getString(mIndexUserId);
String[] userIdSplit = PgpKeyHelper.splitUserId(userId); String[] userIdSplit = PgpKeyHelper.splitUserId(userId);
if (userIdSplit[0] != null) { if (userIdSplit[0] != null) {
h.mainUserId.setText(highlightSearchQuery(userIdSplit[0])); h.mainUserId.setText(highlighter.highlight(userIdSplit[0]));
} else { } else {
h.mainUserId.setText(R.string.user_id_no_name); h.mainUserId.setText(R.string.user_id_no_name);
} }
if (userIdSplit[1] != null) { if (userIdSplit[1] != null) {
h.mainUserIdRest.setVisibility(View.VISIBLE); h.mainUserIdRest.setVisibility(View.VISIBLE);
h.mainUserIdRest.setText(highlightSearchQuery(userIdSplit[1])); h.mainUserIdRest.setText(highlighter.highlight(userIdSplit[1]));
} else { } else {
h.mainUserIdRest.setVisibility(View.GONE); h.mainUserIdRest.setVisibility(View.GONE);
} }