mirror of
https://github.com/moparisthebest/open-keychain
synced 2025-02-24 07:21:49 -05:00
profiling says: caching qrCode bitmaps is a good idea
This commit is contained in:
parent
18a88d35be
commit
c08fe2b50d
@ -24,6 +24,7 @@ import android.content.ContentResolver;
|
|||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.res.Resources;
|
import android.content.res.Resources;
|
||||||
|
import android.graphics.Bitmap;
|
||||||
import android.graphics.PorterDuff;
|
import android.graphics.PorterDuff;
|
||||||
import android.graphics.drawable.Drawable;
|
import android.graphics.drawable.Drawable;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
@ -40,6 +41,8 @@ import org.sufficientlysecure.keychain.util.Preferences;
|
|||||||
import org.sufficientlysecure.keychain.util.TlsHelper;
|
import org.sufficientlysecure.keychain.util.TlsHelper;
|
||||||
|
|
||||||
import java.security.Security;
|
import java.security.Security;
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
|
||||||
public class KeychainApplication extends Application {
|
public class KeychainApplication extends Application {
|
||||||
|
|
||||||
@ -100,6 +103,17 @@ public class KeychainApplication extends Application {
|
|||||||
checkConsolidateRecovery();
|
checkConsolidateRecovery();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static HashMap<String,Bitmap> qrCodeCache = new HashMap<>();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onTrimMemory(int level) {
|
||||||
|
super.onTrimMemory(level);
|
||||||
|
|
||||||
|
if (level >= TRIM_MEMORY_UI_HIDDEN) {
|
||||||
|
qrCodeCache.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Restart consolidate process if it has been interruped before
|
* Restart consolidate process if it has been interruped before
|
||||||
*/
|
*/
|
||||||
|
@ -29,6 +29,7 @@ import com.google.zxing.qrcode.QRCodeWriter;
|
|||||||
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
|
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
|
||||||
|
|
||||||
import org.sufficientlysecure.keychain.Constants;
|
import org.sufficientlysecure.keychain.Constants;
|
||||||
|
import org.sufficientlysecure.keychain.KeychainApplication;
|
||||||
import org.sufficientlysecure.keychain.util.Log;
|
import org.sufficientlysecure.keychain.util.Log;
|
||||||
|
|
||||||
import java.util.Hashtable;
|
import java.util.Hashtable;
|
||||||
@ -40,21 +41,25 @@ public class QrCodeUtils {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Generate Bitmap with QR Code based on input.
|
* Generate Bitmap with QR Code based on input.
|
||||||
*
|
|
||||||
* @param input
|
|
||||||
* @param size
|
|
||||||
* @return QR Code as Bitmap
|
* @return QR Code as Bitmap
|
||||||
*/
|
*/
|
||||||
public static Bitmap getQRCodeBitmap(final String input, final int size) {
|
public static Bitmap getQRCodeBitmap(final String input, final int size) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
final Hashtable<EncodeHintType, Object> hints = new Hashtable<>();
|
|
||||||
|
// the qrCodeCache is handled in KeychainApplication so we can
|
||||||
|
// properly react to onTrimMemory calls
|
||||||
|
Bitmap bitmap = KeychainApplication.qrCodeCache.get(input);
|
||||||
|
if (bitmap == null) {
|
||||||
|
|
||||||
|
Hashtable<EncodeHintType, Object> hints = new Hashtable<>();
|
||||||
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
|
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
|
||||||
final BitMatrix result = new QRCodeWriter().encode(input, BarcodeFormat.QR_CODE, size,
|
BitMatrix result = new QRCodeWriter().encode(input, BarcodeFormat.QR_CODE, size,
|
||||||
size, hints);
|
size, hints);
|
||||||
|
|
||||||
final int width = result.getWidth();
|
int width = result.getWidth();
|
||||||
final int height = result.getHeight();
|
int height = result.getHeight();
|
||||||
final int[] pixels = new int[width * height];
|
int[] pixels = new int[width * height];
|
||||||
|
|
||||||
for (int y = 0; y < height; y++) {
|
for (int y = 0; y < height; y++) {
|
||||||
final int offset = y * width;
|
final int offset = y * width;
|
||||||
@ -63,13 +68,18 @@ public class QrCodeUtils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
|
bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
|
||||||
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
|
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
|
||||||
|
|
||||||
|
KeychainApplication.qrCodeCache.put(input, bitmap);
|
||||||
|
}
|
||||||
|
|
||||||
return bitmap;
|
return bitmap;
|
||||||
} catch (final WriterException e) {
|
} catch (WriterException e) {
|
||||||
Log.e(Constants.TAG, "QrCodeUtils", e);
|
Log.e(Constants.TAG, "QrCodeUtils", e);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user