Create custom HighlightQueryCursorAdapter

This commit is contained in:
Miroojin Bakshi 2014-03-10 21:10:04 +05:30
parent b927c0f26a
commit 98da2d0c48
3 changed files with 70 additions and 68 deletions

View File

@ -0,0 +1,61 @@
package org.sufficientlysecure.keychain.ui.adapter;
import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;
import android.text.Spannable;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.ViewGroup;
import android.support.v4.widget.CursorAdapter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class HighlightQueryCursorAdapter extends CursorAdapter {
private String mCurQuery;
public HighlightQueryCursorAdapter(Context context, Cursor c, int flags) {
super(context, c, flags);
mCurQuery = null;
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return null;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
}
public void setSearchQuery(String searchQuery){
mCurQuery = searchQuery;
}
public String getSearchQuery(){
return mCurQuery;
}
protected Spannable highlightSearchKey(String text) {
Spannable highlight;
Pattern pattern;
Matcher matcher;
highlight = Spannable.Factory.getInstance().newSpannable(text);;
pattern = Pattern.compile("(?i)" + mCurQuery);
matcher = pattern.matcher(text);
if (matcher.find()) {
highlight.setSpan(
new ForegroundColorSpan(0xFF33B5E5),
matcher.start(),
matcher.end(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
return highlight;
}
}

View File

@ -19,8 +19,7 @@ package org.sufficientlysecure.keychain.ui.adapter;
import java.util.HashMap;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.R;
@ -34,10 +33,6 @@ import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Color;
import android.support.v4.widget.CursorAdapter;
import android.text.Html;
import android.text.Spannable;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@ -46,19 +41,17 @@ import android.widget.TextView;
/**
* Implements StickyListHeadersAdapter from library
*/
public class KeyListPublicAdapter extends CursorAdapter implements StickyListHeadersAdapter {
public class KeyListPublicAdapter extends HighlightQueryCursorAdapter implements StickyListHeadersAdapter {
private LayoutInflater mInflater;
private int mSectionColumnIndex;
private int mIndexUserId;
private int mIndexIsRevoked;
private String mCurQuery;
@SuppressLint("UseSparseArrays")
private HashMap<Integer, Boolean> mSelection = new HashMap<Integer, Boolean>();
public KeyListPublicAdapter(Context context, Cursor c, int flags, int sectionColumnIndex) {
super(context, c, flags);
mCurQuery = null;
mInflater = LayoutInflater.from(context);
mSectionColumnIndex = sectionColumnIndex;
initIndex(c);
@ -84,10 +77,6 @@ public class KeyListPublicAdapter extends CursorAdapter implements StickyListHea
}
}
public void setSearchQuery(String searchQuery){
mCurQuery = searchQuery;
}
/**
* Bind cursor data to the item list view
* <p/>
@ -120,7 +109,9 @@ public class KeyListPublicAdapter extends CursorAdapter implements StickyListHea
} else {
revoked.setVisibility(View.GONE);
}
if(mCurQuery != null){
String query = getSearchQuery();
if(query != null){
mainUserId.setText(highlightSearchKey(userIdSplit[0]));
mainUserIdRest.setText(highlightSearchKey(userIdSplit[1]));
}
@ -242,24 +233,5 @@ public class KeyListPublicAdapter extends CursorAdapter implements StickyListHea
}
return v;
}
private Spannable highlightSearchKey(String text) {
Spannable highlight;
Pattern pattern;
Matcher matcher;
String orig_str;
orig_str = Html.fromHtml(text).toString();
highlight = (Spannable) Html.fromHtml(text);
pattern = Pattern.compile("(?i)" + mCurQuery);
matcher = pattern.matcher(orig_str);
if (matcher.find()) {
highlight.setSpan(
new ForegroundColorSpan(0xFF33B5E5),
matcher.start(),
matcher.end(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
return highlight;
}
}

View File

@ -25,10 +25,6 @@ import org.sufficientlysecure.keychain.provider.KeychainContract.UserIds;
import android.content.Context;
import android.database.Cursor;
import android.support.v4.widget.CursorAdapter;
import android.text.Html;
import android.text.Spannable;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@ -36,10 +32,9 @@ import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class SelectKeyCursorAdapter extends CursorAdapter {
public class SelectKeyCursorAdapter extends HighlightQueryCursorAdapter {
protected int mKeyType;
@ -53,7 +48,6 @@ public class SelectKeyCursorAdapter extends CursorAdapter {
public final static String PROJECTION_ROW_AVAILABLE = "available";
public final static String PROJECTION_ROW_VALID = "valid";
private String mCurQuery;
public SelectKeyCursorAdapter(Context context, Cursor c, int flags, ListView listView,
int keyType) {
@ -62,7 +56,6 @@ public class SelectKeyCursorAdapter extends CursorAdapter {
mInflater = LayoutInflater.from(context);
mListView = listView;
mKeyType = keyType;
mCurQuery = null;
initIndex(c);
}
@ -165,8 +158,8 @@ public class SelectKeyCursorAdapter extends CursorAdapter {
mainUserIdRest.setEnabled(valid);
keyId.setEnabled(valid);
status.setEnabled(valid);
if(mCurQuery != null){
String query = getSearchQuery();
if(query != null){
mainUserId.setText(highlightSearchKey(userIdSplit[0]));
mainUserIdRest.setText(highlightSearchKey(userIdSplit[1]));
}
@ -176,28 +169,4 @@ public class SelectKeyCursorAdapter extends CursorAdapter {
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return mInflater.inflate(R.layout.select_key_item, null);
}
public void setSearchQuery(String searchQuery){
mCurQuery = searchQuery;
}
private Spannable highlightSearchKey(String text) {
Spannable highlight;
Pattern pattern;
Matcher matcher;
String orig_str;
orig_str = Html.fromHtml(text).toString();
highlight = (Spannable) Html.fromHtml(text);
pattern = Pattern.compile("(?i)" + mCurQuery);
matcher = pattern.matcher(orig_str);
if (matcher.find()) {
highlight.setSpan(
new ForegroundColorSpan(0xFF33B5E5),
matcher.start(),
matcher.end(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
return highlight;
}
}