Refactoring

This commit is contained in:
M66B 2020-05-04 12:52:45 +02:00
parent 300a23a023
commit 2132a823d4
1 changed files with 26 additions and 21 deletions

View File

@ -80,6 +80,7 @@ class ImageHelper {
private static final int DOWNLOAD_TIMEOUT = 15 * 1000; // milliseconds
private static final int MAX_REDIRECTS = 10;
private static final int MAX_PROBE = 64 * 1024; // bytes
private static final int SLOW_CONNECTION = 2 * 1024; // Kbps
static Bitmap generateIdenticon(@NonNull String email, int size, int pixels, Context context) {
@ -577,27 +578,7 @@ class ImageHelper {
break;
}
BufferedInputStream is = new BufferedInputStream(urlConnection.getInputStream());
Log.i("Probe " + source);
is.mark(64 * 1024);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(is, null, options);
int scaleToPixels = dm.widthPixels;
int factor = 1;
while (options.outWidth / factor > scaleToPixels)
factor *= 2;
Log.i("Download " + source + " factor=" + factor);
is.reset();
if (factor > 1) {
options.inJustDecodeBounds = false;
options.inSampleSize = factor;
bm = BitmapFactory.decodeStream(is, null, options);
} else
bm = BitmapFactory.decodeStream(is);
bm = getScaledBitmap(urlConnection.getInputStream(), source, dm);
} finally {
if (urlConnection != null)
urlConnection.disconnect();
@ -620,6 +601,30 @@ class ImageHelper {
return d;
}
private static Bitmap getScaledBitmap(InputStream is, String source, DisplayMetrics dm) throws IOException {
BufferedInputStream bis = new BufferedInputStream(is);
Log.i("Probe " + source);
bis.mark(MAX_PROBE);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(bis, null, options);
int scaleToPixels = dm.widthPixels;
int factor = 1;
while (options.outWidth / factor > scaleToPixels)
factor *= 2;
Log.i("Download " + source + " factor=" + factor);
bis.reset();
if (factor > 1) {
options.inJustDecodeBounds = false;
options.inSampleSize = factor;
return BitmapFactory.decodeStream(bis, null, options);
} else
return BitmapFactory.decodeStream(bis);
}
@NonNull
private static File getCacheFile(Context context, long id, String source) {
File dir = new File(context.getCacheDir(), "images");