Improve hightlight query code: make cursor abstract, tidy up code

This commit is contained in:
Dominik Schürmann 2014-03-10 17:22:19 +01:00
parent 70d1e01a2b
commit caa0740919
4 changed files with 50 additions and 54 deletions

View File

@ -1,19 +1,34 @@
/*
* 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; package org.sufficientlysecure.keychain.ui.adapter;
import android.annotation.SuppressLint;
import android.content.Context; import android.content.Context;
import android.database.Cursor; import android.database.Cursor;
import android.support.v4.widget.CursorAdapter;
import android.text.Spannable; import android.text.Spannable;
import android.text.style.ForegroundColorSpan; import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.ViewGroup; import org.sufficientlysecure.keychain.R;
import android.support.v4.widget.CursorAdapter;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
public abstract class HighlightQueryCursorAdapter extends CursorAdapter {
public class HighlightQueryCursorAdapter extends CursorAdapter {
private String mCurQuery; private String mCurQuery;
@ -22,40 +37,30 @@ public class HighlightQueryCursorAdapter extends CursorAdapter {
mCurQuery = null; mCurQuery = null;
} }
@Override public void setSearchQuery(String searchQuery) {
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; mCurQuery = searchQuery;
} }
public String getSearchQuery(){ public String getSearchQuery() {
return mCurQuery; return mCurQuery;
} }
protected Spannable highlightSearchKey(String text) { protected Spannable highlightSearchQuery(String text) {
Spannable highlight; Spannable highlight = Spannable.Factory.getInstance().newSpannable(text);
Pattern pattern;
Matcher matcher;
highlight = Spannable.Factory.getInstance().newSpannable(text);; if (mCurQuery != null) {
pattern = Pattern.compile("(?i)" + mCurQuery); Pattern pattern = Pattern.compile("(?i)" + mCurQuery);
matcher = pattern.matcher(text); Matcher matcher = pattern.matcher(text);
if (matcher.find()) { if (matcher.find()) {
highlight.setSpan( highlight.setSpan(
new ForegroundColorSpan(0xFF33B5E5), new ForegroundColorSpan(mContext.getResources().getColor(R.color.emphasis)),
matcher.start(), matcher.start(),
matcher.end(), matcher.end(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
} }
return highlight; return highlight;
} else {
return highlight;
}
} }
} }

View File

@ -92,12 +92,12 @@ public class KeyListPublicAdapter extends HighlightQueryCursorAdapter implements
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) {
mainUserId.setText(userIdSplit[0]); mainUserId.setText(highlightSearchQuery(userIdSplit[0]));
} else { } else {
mainUserId.setText(R.string.user_id_no_name); mainUserId.setText(R.string.user_id_no_name);
} }
if (userIdSplit[1] != null) { if (userIdSplit[1] != null) {
mainUserIdRest.setText(userIdSplit[1]); mainUserIdRest.setText(highlightSearchQuery(userIdSplit[1]));
mainUserIdRest.setVisibility(View.VISIBLE); mainUserIdRest.setVisibility(View.VISIBLE);
} else { } else {
mainUserIdRest.setVisibility(View.GONE); mainUserIdRest.setVisibility(View.GONE);
@ -109,12 +109,6 @@ public class KeyListPublicAdapter extends HighlightQueryCursorAdapter implements
} else { } else {
revoked.setVisibility(View.GONE); revoked.setVisibility(View.GONE);
} }
String query = getSearchQuery();
if(query != null){
mainUserId.setText(highlightSearchKey(userIdSplit[0]));
mainUserIdRest.setText(highlightSearchKey(userIdSplit[1]));
}
} }
@Override @Override
@ -225,11 +219,12 @@ public class KeyListPublicAdapter extends HighlightQueryCursorAdapter implements
/** /**
* Change color for multi-selection * Change color for multi-selection
*/ */
if (mSelection.get(position) != null && mSelection.get(position).booleanValue()) {
// color for selected items
v.setBackgroundColor(parent.getResources().getColor(R.color.emphasis));
} else {
// default color // default color
v.setBackgroundColor(Color.TRANSPARENT); v.setBackgroundColor(Color.TRANSPARENT);
if (mSelection.get(position) != null && mSelection.get(position).booleanValue()) {
// this is a selected position, change color!
v.setBackgroundColor(parent.getResources().getColor(R.color.emphasis));
} }
return v; return v;
} }

View File

@ -126,11 +126,12 @@ public class KeyListSecretAdapter extends CursorAdapter {
/** /**
* Change color for multi-selection * Change color for multi-selection
*/ */
if (mSelection.get(position) != null) {
// color for selected items
v.setBackgroundColor(parent.getResources().getColor(R.color.emphasis));
} else {
// default color // default color
v.setBackgroundColor(Color.TRANSPARENT); v.setBackgroundColor(Color.TRANSPARENT);
if (mSelection.get(position) != null) {
// this is a selected position, change color!
v.setBackgroundColor(parent.getResources().getColor(R.color.emphasis));
} }
return v; return v;
} }

View File

@ -104,12 +104,12 @@ public class SelectKeyCursorAdapter extends HighlightQueryCursorAdapter {
String[] userIdSplit = PgpKeyHelper.splitUserId(userId); String[] userIdSplit = PgpKeyHelper.splitUserId(userId);
if (userIdSplit[0] != null) { if (userIdSplit[0] != null) {
mainUserId.setText(userIdSplit[0]); mainUserId.setText(highlightSearchQuery(userIdSplit[0]));
} else { } else {
mainUserId.setText(R.string.user_id_no_name); mainUserId.setText(R.string.user_id_no_name);
} }
if (userIdSplit[1] != null) { if (userIdSplit[1] != null) {
mainUserIdRest.setText(userIdSplit[1]); mainUserIdRest.setText(highlightSearchQuery(userIdSplit[1]));
} else { } else {
mainUserIdRest.setText(""); mainUserIdRest.setText("");
} }
@ -158,11 +158,6 @@ public class SelectKeyCursorAdapter extends HighlightQueryCursorAdapter {
mainUserIdRest.setEnabled(valid); mainUserIdRest.setEnabled(valid);
keyId.setEnabled(valid); keyId.setEnabled(valid);
status.setEnabled(valid); status.setEnabled(valid);
String query = getSearchQuery();
if(query != null){
mainUserId.setText(highlightSearchKey(userIdSplit[0]));
mainUserIdRest.setText(highlightSearchKey(userIdSplit[1]));
}
} }
@Override @Override