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;
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,17 +37,6 @@ 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) {
mCurQuery = searchQuery;
}
@ -41,21 +45,22 @@ public class HighlightQueryCursorAdapter extends CursorAdapter {
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 (mCurQuery != null) {
Pattern pattern = Pattern.compile("(?i)" + mCurQuery);
Matcher matcher = pattern.matcher(text);
if (matcher.find()) {
highlight.setSpan(
new ForegroundColorSpan(0xFF33B5E5),
new ForegroundColorSpan(mContext.getResources().getColor(R.color.emphasis)),
matcher.start(),
matcher.end(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
return highlight;
} else {
return highlight;
}
}
}

View File

@ -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
*/
if (mSelection.get(position) != null && mSelection.get(position).booleanValue()) {
// color for selected items
v.setBackgroundColor(parent.getResources().getColor(R.color.emphasis));
} else {
// default color
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;
}

View File

@ -126,11 +126,12 @@ public class KeyListSecretAdapter extends CursorAdapter {
/**
* 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
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;
}

View File

@ -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