From b927c0f26ace30651b3cb9df9d71741745563aaf Mon Sep 17 00:00:00 2001 From: Miroojin Bakshi Date: Mon, 10 Mar 2014 15:25:09 +0530 Subject: [PATCH 1/2] Highlight search query in SelectPublicKeyFragment and KeyListPublicFragment --- .../keychain/ui/KeyListPublicFragment.java | 2 +- .../keychain/ui/SelectPublicKeyFragment.java | 1 + .../ui/adapter/KeyListPublicAdapter.java | 35 +++++++++++++++++- .../ui/adapter/SelectKeyCursorAdapter.java | 37 ++++++++++++++++++- 4 files changed, 72 insertions(+), 3 deletions(-) diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListPublicFragment.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListPublicFragment.java index 40a016f55..59dd31a21 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListPublicFragment.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListPublicFragment.java @@ -272,8 +272,8 @@ public class KeyListPublicFragment extends Fragment implements SearchView.OnQuer public void onLoadFinished(Loader 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 diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/SelectPublicKeyFragment.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/SelectPublicKeyFragment.java index 6e338979a..c1c9aa705 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/SelectPublicKeyFragment.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/SelectPublicKeyFragment.java @@ -311,6 +311,7 @@ public class SelectPublicKeyFragment extends ListFragmentWorkaround implements T public void onLoadFinished(Loader 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. diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/KeyListPublicAdapter.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/KeyListPublicAdapter.java index 086d2c178..dcc368e7b 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/KeyListPublicAdapter.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/KeyListPublicAdapter.java @@ -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 mSelection = new HashMap(); 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 *

@@ -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; + } } diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/SelectKeyCursorAdapter.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/SelectKeyCursorAdapter.java index d44dd5890..03010f7a2 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/SelectKeyCursorAdapter.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/SelectKeyCursorAdapter.java @@ -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; + } } From 98da2d0c48eec809009e4c129ef13dd4a6f93cc3 Mon Sep 17 00:00:00 2001 From: Miroojin Bakshi Date: Mon, 10 Mar 2014 21:10:04 +0530 Subject: [PATCH 2/2] Create custom HighlightQueryCursorAdapter --- .../adapter/HighlightQueryCursorAdapter.java | 61 +++++++++++++++++++ .../ui/adapter/KeyListPublicAdapter.java | 38 ++---------- .../ui/adapter/SelectKeyCursorAdapter.java | 39 ++---------- 3 files changed, 70 insertions(+), 68 deletions(-) create mode 100644 OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/HighlightQueryCursorAdapter.java diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/HighlightQueryCursorAdapter.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/HighlightQueryCursorAdapter.java new file mode 100644 index 000000000..1c41cbf03 --- /dev/null +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/HighlightQueryCursorAdapter.java @@ -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; + } +} diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/KeyListPublicAdapter.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/KeyListPublicAdapter.java index dcc368e7b..888284ec0 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/KeyListPublicAdapter.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/KeyListPublicAdapter.java @@ -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 mSelection = new HashMap(); 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 *

@@ -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; - } } diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/SelectKeyCursorAdapter.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/SelectKeyCursorAdapter.java index 03010f7a2..a7ef0785b 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/SelectKeyCursorAdapter.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/SelectKeyCursorAdapter.java @@ -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; - } }