From 059e57c5d0b9192308d7d0ae036a731a038f5d6c Mon Sep 17 00:00:00 2001 From: uberspot Date: Sun, 9 Mar 2014 04:09:00 +0200 Subject: [PATCH] Colorize fingerprint --- .../keychain/provider/ProviderHelper.java | 12 ++------ .../keychain/ui/ViewKeyMainFragment.java | 28 +++++++++++++++++-- 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java index 1a16f6db3..ac0692213 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java @@ -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); } /** diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyMainFragment.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyMainFragment.java index 7b83db87f..f8db19c16 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyMainFragment.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyMainFragment.java @@ -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.