Colorize fingerprint

This commit is contained in:
uberspot 2014-03-09 04:09:00 +02:00
parent 50d9908baf
commit 059e57c5d0
2 changed files with 28 additions and 12 deletions

View File

@ -87,7 +87,7 @@ public class ProviderHelper {
}
/**
* Retrieves the actual PGPPublicKeyRing object from the database blob based on the maserKeyId
* Retrieves the actual PGPPublicKeyRing object from the database blob based on the masterKeyId
*/
public static PGPPublicKeyRing getPGPPublicKeyRingByMasterKeyId(Context context,
long masterKeyId) {
@ -110,11 +110,8 @@ public class ProviderHelper {
*/
public static PGPPublicKey getPGPPublicKeyByKeyId(Context context, long keyId) {
PGPPublicKeyRing keyRing = getPGPPublicKeyRingByKeyId(context, keyId);
if (keyRing == null) {
return null;
}
return keyRing.getPublicKey(keyId);
return (keyRing == null)? null : keyRing.getPublicKey(keyId);
}
/**
@ -149,11 +146,8 @@ public class ProviderHelper {
*/
public static PGPSecretKey getPGPSecretKeyByKeyId(Context context, long keyId) {
PGPSecretKeyRing keyRing = getPGPSecretKeyRingByKeyId(context, keyId);
if (keyRing == null) {
return null;
}
return keyRing.getSecretKey(keyId);
return (keyRing == null) ? null : keyRing.getSecretKey(keyId);
}
/**

View File

@ -19,13 +19,17 @@ package org.sufficientlysecure.keychain.ui;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.format.DateFormat;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@ -257,10 +261,10 @@ public class ViewKeyMainFragment extends Fragment implements
// FALLBACK for old database entries
fingerprintBlob = ProviderHelper.getFingerprint(getActivity(), mDataUri);
}
String fingerprint = PgpKeyHelper.convertFingerprintToHex(fingerprintBlob, true);
fingerprint = fingerprint.replace(" ", "\n");
String fingerprint = PgpKeyHelper.convertFingerprintToHex(fingerprintBlob, true)
.replace(" ", "\n");
mFingerprint.setText(fingerprint);
mFingerprint.setText(colorizeFingerprint(fingerprint));
}
mKeysAdapter.swapCursor(data);
@ -271,6 +275,24 @@ public class ViewKeyMainFragment extends Fragment implements
}
}
private SpannableStringBuilder colorizeFingerprint(String fingerprint) {
SpannableStringBuilder sb = new SpannableStringBuilder(fingerprint);
ForegroundColorSpan fcs = new ForegroundColorSpan(Color.BLACK);
// for each 4 characters of the fingerprint + 1 space
for (int i = 0; i < fingerprint.length(); i += 5) {
String fourChars = fingerprint.substring(i, Math.min(i + 4, fingerprint.length()));
// Create a foreground color by converting the 4 fingerprint chars to an int hashcode
// and then converting that int to hex to use as a color
fcs = new ForegroundColorSpan(
Color.parseColor(String.format("#%06X", (0xFFFFFF & fourChars.hashCode()))));
sb.setSpan(fcs, i, Math.min(i+4, fingerprint.length()), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
}
return sb;
}
/**
* This is called when the last Cursor provided to onLoadFinished() above is about to be closed.
* We need to make sure we are no longer using it.