From 79b7cec94acfbba683e1c78d1576866c5fbd5d61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Tue, 19 May 2015 18:27:04 +0200 Subject: [PATCH] Use Mode.ALPHANUMERIC for QR codes to save space --- .../keychain/ui/QrCodeViewActivity.java | 9 +++++---- .../keychain/ui/ViewKeyActivity.java | 7 +++++-- .../keychain/ui/ViewKeyAdvShareFragment.java | 7 +++++-- .../keychain/ui/util/QrCodeUtils.java | 10 +++++++++- 4 files changed, 24 insertions(+), 9 deletions(-) diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/QrCodeViewActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/QrCodeViewActivity.java index d4858ee5d..e54852f1b 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/QrCodeViewActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/QrCodeViewActivity.java @@ -85,11 +85,12 @@ public class QrCodeViewActivity extends BaseActivity { ActivityCompat.finishAfterTransition(QrCodeViewActivity.this); } - String fingerprint = KeyFormattingUtils.convertFingerprintToHex(blob); - String qrCodeContent = Constants.FINGERPRINT_SCHEME + ":" + fingerprint; - + Uri uri = new Uri.Builder() + .scheme(Constants.FINGERPRINT_SCHEME) + .opaquePart(KeyFormattingUtils.convertFingerprintToHex(blob)) + .build(); // create a minimal size qr code, we can keep this in ram no problem - final Bitmap qrCode = QrCodeUtils.getQRCodeBitmap(qrCodeContent, 0); + final Bitmap qrCode = QrCodeUtils.getQRCodeBitmap(uri, 0); mQrCode.getViewTreeObserver().addOnGlobalLayoutListener( new OnGlobalLayoutListener() { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java index 4237deff0..9968855f7 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java @@ -731,9 +731,12 @@ public class ViewKeyActivity extends BaseNfcActivity implements AsyncTask loadTask = new AsyncTask() { protected Bitmap doInBackground(Void... unused) { - String qrCodeContent = Constants.FINGERPRINT_SCHEME + ":" + fingerprint; + Uri uri = new Uri.Builder() + .scheme(Constants.FINGERPRINT_SCHEME) + .opaquePart(fingerprint) + .build(); // render with minimal size - return QrCodeUtils.getQRCodeBitmap(qrCodeContent, 0); + return QrCodeUtils.getQRCodeBitmap(uri, 0); } protected void onPostExecute(Bitmap qrCode) { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyAdvShareFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyAdvShareFragment.java index fde0f62fd..479ddae0b 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyAdvShareFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyAdvShareFragment.java @@ -386,9 +386,12 @@ public class ViewKeyAdvShareFragment extends LoaderFragment implements AsyncTask loadTask = new AsyncTask() { protected Bitmap doInBackground(Void... unused) { - String qrCodeContent = Constants.FINGERPRINT_SCHEME + ":" + fingerprint; + Uri uri = new Uri.Builder() + .scheme(Constants.FINGERPRINT_SCHEME) + .opaquePart(fingerprint) + .build(); // render with minimal size - return QrCodeUtils.getQRCodeBitmap(qrCodeContent, 0); + return QrCodeUtils.getQRCodeBitmap(uri, 0); } protected void onPostExecute(Bitmap qrCode) { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/util/QrCodeUtils.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/util/QrCodeUtils.java index 5f71abdab..a6394a3fb 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/util/QrCodeUtils.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/util/QrCodeUtils.java @@ -20,6 +20,7 @@ package org.sufficientlysecure.keychain.ui.util; import android.graphics.Bitmap; import android.graphics.Color; +import android.net.Uri; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; @@ -33,17 +34,24 @@ import org.sufficientlysecure.keychain.KeychainApplication; import org.sufficientlysecure.keychain.util.Log; import java.util.Hashtable; +import java.util.Locale; /** * Copied from Bitcoin Wallet */ public class QrCodeUtils { + public static Bitmap getQRCodeBitmap(final Uri uri, final int size) { + // for URIs we want alphanumeric encoding to save space, thus make everything upper case! + // zxing will then select Mode.ALPHANUMERIC internally + return getQRCodeBitmap(uri.toString().toUpperCase(Locale.ENGLISH), size); + } + /** * Generate Bitmap with QR Code based on input. * @return QR Code as Bitmap */ - public static Bitmap getQRCodeBitmap(final String input, final int size) { + private static Bitmap getQRCodeBitmap(final String input, final int size) { try {