Add highlighting to keyserver/keybase search

This commit is contained in:
Thialfihar 2014-05-14 20:07:30 +02:00
parent ab81d8903a
commit 69ce66be94
5 changed files with 75 additions and 5 deletions

View File

@ -237,6 +237,7 @@ public class HkpKeyServer extends KeyServer {
final Matcher matcher = PUB_KEY_LINE.matcher(data);
while (matcher.find()) {
final ImportKeysListEntry entry = new ImportKeysListEntry();
entry.setQuery(query);
entry.setBitStrength(Integer.parseInt(matcher.group(3)));

View File

@ -51,6 +51,7 @@ public class ImportKeysListEntry implements Serializable, Parcelable {
public boolean secretKey;
public String mPrimaryUserId;
private String mExtraData;
private String mQuery;
private boolean mSelected;
@ -209,6 +210,14 @@ public class ImportKeysListEntry implements Serializable, Parcelable {
mExtraData = extraData;
}
public String getQuery() {
return mQuery;
}
public void setQuery(String query) {
mQuery = query;
}
/**
* Constructor for later querying from keyserver
*/

View File

@ -34,6 +34,7 @@ import java.util.TimeZone;
import java.util.WeakHashMap;
public class KeybaseKeyServer extends KeyServer {
private String mQuery;
@Override
public ArrayList<ImportKeysListEntry> search(String query) throws QueryException, TooManyResponses,
@ -86,15 +87,15 @@ public class KeybaseKeyServer extends KeyServer {
}
private ImportKeysListEntry makeEntry(JSONObject match) throws QueryException, JSONException {
final ImportKeysListEntry entry = new ImportKeysListEntry();
entry.setQuery(mQuery);
String keybaseId = JWalk.getString(match, "components", "username", "val");
String fullName = JWalk.getString(match, "components", "full_name", "val");
String fingerprint = JWalk.getString(match, "components", "key_fingerprint", "val");
fingerprint = fingerprint.replace(" ", "").toUpperCase(); // not strictly necessary but doesn't hurt
entry.setFingerprintHex(fingerprint);
// in anticipation of a full fingerprint, only use the last 16 chars as 64-bit key id
entry.setKeyIdHex("0x" + fingerprint.substring(Math.max(0, fingerprint.length() - 16)));
// store extra info, so we can query for the keybase id directly
entry.setExtraData(keybaseId);

View File

@ -33,6 +33,7 @@ import android.widget.TextView;
import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.keyimport.ImportKeysListEntry;
import org.sufficientlysecure.keychain.pgp.PgpKeyHelper;
import org.sufficientlysecure.keychain.util.Highlighter;
import java.util.ArrayList;
import java.util.Iterator;
@ -99,6 +100,7 @@ public class ImportKeysAdapter extends ArrayAdapter<ImportKeysListEntry> {
public View getView(int position, View convertView, ViewGroup parent) {
ImportKeysListEntry entry = mData.get(position);
Highlighter highlighter = new Highlighter(mActivity, entry.getQuery());
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
@ -128,7 +130,7 @@ public class ImportKeysAdapter extends ArrayAdapter<ImportKeysListEntry> {
+ " " + userIdSplit[0]);
holder.mainUserId.setTextColor(Color.RED);
} else {
holder.mainUserId.setText(userIdSplit[0]);
holder.mainUserId.setText(highlighter.highlight(userIdSplit[0]));
holder.mainUserId.setTextColor(Color.BLACK);
}
} else {
@ -139,7 +141,7 @@ public class ImportKeysAdapter extends ArrayAdapter<ImportKeysListEntry> {
// email
if (userIdSplit[1] != null) {
holder.mainUserIdRest.setVisibility(View.VISIBLE);
holder.mainUserIdRest.setText(userIdSplit[1]);
holder.mainUserIdRest.setText(highlighter.highlight(userIdSplit[1]));
} else {
holder.mainUserIdRest.setVisibility(View.GONE);
}
@ -182,7 +184,7 @@ public class ImportKeysAdapter extends ArrayAdapter<ImportKeysListEntry> {
String uid = it.next();
TextView uidView = (TextView) mInflater.inflate(
R.layout.import_keys_list_entry_user_id, null);
uidView.setText(uid);
uidView.setText(highlighter.highlight(uid));
holder.userIdsList.addView(uidView);
}
}

View File

@ -0,0 +1,57 @@
/*
* Copyright (C) 2014 Thialfihar <thi@thialfihar.org>
*
* 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.util;
import android.content.Context;
import android.text.Spannable;
import android.text.style.ForegroundColorSpan;
import org.sufficientlysecure.keychain.R;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Highlighter {
private Context mContext;
private String mQuery;
public Highlighter(Context context, String query) {
mContext = context;
mQuery = query;
}
public Spannable highlight(String text) {
Spannable highlight = Spannable.Factory.getInstance().newSpannable(text);
if (mQuery == null) {
return highlight;
}
Pattern pattern = Pattern.compile("(?i)(" + mQuery.trim().replaceAll("\\s+", "|") + ")");
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
highlight.setSpan(
new ForegroundColorSpan(mContext.getResources().getColor(R.color.emphasis)),
matcher.start(),
matcher.end(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
return highlight;
}
}