From caa0740919f2acae636dfdd6bdcd971a3bf6473c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Mon, 10 Mar 2014 17:22:19 +0100 Subject: [PATCH] Improve hightlight query code: make cursor abstract, tidy up code --- .../adapter/HighlightQueryCursorAdapter.java | 71 ++++++++++--------- .../ui/adapter/KeyListPublicAdapter.java | 17 ++--- .../ui/adapter/KeyListSecretAdapter.java | 7 +- .../ui/adapter/SelectKeyCursorAdapter.java | 9 +-- 4 files changed, 50 insertions(+), 54 deletions(-) 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 index 1c41cbf03..fd7a2dc30 100644 --- 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 @@ -1,19 +1,34 @@ +/* + * Copyright (C) 2014 Dominik Schürmann + * + * 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 . + */ + package org.sufficientlysecure.keychain.ui.adapter; -import android.annotation.SuppressLint; import android.content.Context; import android.database.Cursor; +import android.support.v4.widget.CursorAdapter; import android.text.Spannable; import android.text.style.ForegroundColorSpan; -import android.view.View; -import android.view.ViewGroup; -import android.support.v4.widget.CursorAdapter; + +import org.sufficientlysecure.keychain.R; import java.util.regex.Matcher; import java.util.regex.Pattern; - -public class HighlightQueryCursorAdapter extends CursorAdapter { +public abstract class HighlightQueryCursorAdapter extends CursorAdapter { private String mCurQuery; @@ -22,40 +37,30 @@ public class HighlightQueryCursorAdapter extends CursorAdapter { 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){ + public void setSearchQuery(String searchQuery) { mCurQuery = searchQuery; } - public String getSearchQuery(){ + public String getSearchQuery() { return mCurQuery; } - protected Spannable highlightSearchKey(String text) { - Spannable highlight; - Pattern pattern; - Matcher matcher; + protected Spannable highlightSearchQuery(String text) { + Spannable highlight = Spannable.Factory.getInstance().newSpannable(text); - 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); + if (mCurQuery != null) { + Pattern pattern = Pattern.compile("(?i)" + mCurQuery); + Matcher matcher = pattern.matcher(text); + if (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; } - 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 888284ec0..0cd2606a1 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 @@ -92,12 +92,12 @@ public class KeyListPublicAdapter extends HighlightQueryCursorAdapter implements String userId = cursor.getString(mIndexUserId); String[] userIdSplit = PgpKeyHelper.splitUserId(userId); if (userIdSplit[0] != null) { - mainUserId.setText(userIdSplit[0]); + mainUserId.setText(highlightSearchQuery(userIdSplit[0])); } else { mainUserId.setText(R.string.user_id_no_name); } if (userIdSplit[1] != null) { - mainUserIdRest.setText(userIdSplit[1]); + mainUserIdRest.setText(highlightSearchQuery(userIdSplit[1])); mainUserIdRest.setVisibility(View.VISIBLE); } else { mainUserIdRest.setVisibility(View.GONE); @@ -109,12 +109,6 @@ public class KeyListPublicAdapter extends HighlightQueryCursorAdapter implements } else { revoked.setVisibility(View.GONE); } - String query = getSearchQuery(); - - if(query != null){ - mainUserId.setText(highlightSearchKey(userIdSplit[0])); - mainUserIdRest.setText(highlightSearchKey(userIdSplit[1])); - } } @Override @@ -225,11 +219,12 @@ public class KeyListPublicAdapter extends HighlightQueryCursorAdapter implements /** * Change color for multi-selection */ - // default color - v.setBackgroundColor(Color.TRANSPARENT); if (mSelection.get(position) != null && mSelection.get(position).booleanValue()) { - // this is a selected position, change color! + // color for selected items v.setBackgroundColor(parent.getResources().getColor(R.color.emphasis)); + } else { + // default color + v.setBackgroundColor(Color.TRANSPARENT); } return v; } diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/KeyListSecretAdapter.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/KeyListSecretAdapter.java index 11d1e8c17..a86508597 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/KeyListSecretAdapter.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/KeyListSecretAdapter.java @@ -126,11 +126,12 @@ public class KeyListSecretAdapter extends CursorAdapter { /** * Change color for multi-selection */ - // default color - v.setBackgroundColor(Color.TRANSPARENT); if (mSelection.get(position) != null) { - // this is a selected position, change color! + // color for selected items v.setBackgroundColor(parent.getResources().getColor(R.color.emphasis)); + } else { + // default color + v.setBackgroundColor(Color.TRANSPARENT); } return v; } 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 a7ef0785b..6d67a0e65 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 @@ -104,12 +104,12 @@ public class SelectKeyCursorAdapter extends HighlightQueryCursorAdapter { String[] userIdSplit = PgpKeyHelper.splitUserId(userId); if (userIdSplit[0] != null) { - mainUserId.setText(userIdSplit[0]); + mainUserId.setText(highlightSearchQuery(userIdSplit[0])); } else { mainUserId.setText(R.string.user_id_no_name); } if (userIdSplit[1] != null) { - mainUserIdRest.setText(userIdSplit[1]); + mainUserIdRest.setText(highlightSearchQuery(userIdSplit[1])); } else { mainUserIdRest.setText(""); } @@ -158,11 +158,6 @@ public class SelectKeyCursorAdapter extends HighlightQueryCursorAdapter { mainUserIdRest.setEnabled(valid); keyId.setEnabled(valid); status.setEnabled(valid); - String query = getSearchQuery(); - if(query != null){ - mainUserId.setText(highlightSearchKey(userIdSplit[0])); - mainUserIdRest.setText(highlightSearchKey(userIdSplit[1])); - } } @Override