Highlight search query in SelectPublicKeyFragment and

KeyListPublicFragment
This commit is contained in:
Miroojin Bakshi 2014-03-10 15:25:09 +05:30
parent 8d85aa5876
commit b927c0f26a
4 changed files with 72 additions and 3 deletions

View File

@ -272,8 +272,8 @@ public class KeyListPublicFragment extends Fragment implements SearchView.OnQuer
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
// Swap the new cursor in. (The framework will take care of closing the
// old cursor once we return.)
mAdapter.setSearchQuery(mCurQuery);
mAdapter.swapCursor(data);
mStickyList.setAdapter(mAdapter);
// NOTE: Not supported by StickyListHeader, but reimplemented here

View File

@ -311,6 +311,7 @@ public class SelectPublicKeyFragment extends ListFragmentWorkaround implements T
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
// Swap the new cursor in. (The framework will take care of closing the
// old cursor once we return.)
mAdapter.setSearchQuery(mCurQuery);
mAdapter.swapCursor(data);
// The list should now be shown.

View File

@ -19,6 +19,8 @@ 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;
@ -33,6 +35,9 @@ 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,13 +51,14 @@ public class KeyListPublicAdapter extends CursorAdapter implements StickyListHea
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);
@ -78,6 +84,10 @@ public class KeyListPublicAdapter extends CursorAdapter implements StickyListHea
}
}
public void setSearchQuery(String searchQuery){
mCurQuery = searchQuery;
}
/**
* Bind cursor data to the item list view
* <p/>
@ -110,6 +120,10 @@ public class KeyListPublicAdapter extends CursorAdapter implements StickyListHea
} else {
revoked.setVisibility(View.GONE);
}
if(mCurQuery != null){
mainUserId.setText(highlightSearchKey(userIdSplit[0]));
mainUserIdRest.setText(highlightSearchKey(userIdSplit[1]));
}
}
@Override
@ -228,5 +242,24 @@ 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

@ -26,6 +26,9 @@ 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;
@ -33,6 +36,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 {
protected int mKeyType;
@ -47,6 +53,7 @@ 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) {
@ -55,7 +62,7 @@ public class SelectKeyCursorAdapter extends CursorAdapter {
mInflater = LayoutInflater.from(context);
mListView = listView;
mKeyType = keyType;
mCurQuery = null;
initIndex(c);
}
@ -158,6 +165,11 @@ public class SelectKeyCursorAdapter extends CursorAdapter {
mainUserIdRest.setEnabled(valid);
keyId.setEnabled(valid);
status.setEnabled(valid);
if(mCurQuery != null){
mainUserId.setText(highlightSearchKey(userIdSplit[0]));
mainUserIdRest.setText(highlightSearchKey(userIdSplit[1]));
}
}
@Override
@ -165,4 +177,27 @@ public class SelectKeyCursorAdapter extends CursorAdapter {
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;
}
}