From 15613894f0730850479daddafec405994c1ab8f7 Mon Sep 17 00:00:00 2001 From: Jake McGinty Date: Wed, 3 Dec 2014 09:25:32 +0000 Subject: [PATCH] load image previews asynchronously // FREEBIE --- res/layout/media_preview_activity.xml | 21 ++++++ res/values/strings.xml | 1 + .../securesms/MediaPreviewActivity.java | 65 ++++++++++++++----- 3 files changed, 71 insertions(+), 16 deletions(-) diff --git a/res/layout/media_preview_activity.xml b/res/layout/media_preview_activity.xml index dfef0dd411..a1b2202ad6 100644 --- a/res/layout/media_preview_activity.xml +++ b/res/layout/media_preview_activity.xml @@ -1,6 +1,7 @@ @@ -11,4 +12,24 @@ android:contentDescription="@string/media_preview_activity__image_content_description" android:visibility="gone"/> + + + + diff --git a/res/values/strings.xml b/res/values/strings.xml index 981004cf74..99f813f5d2 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -763,6 +763,7 @@ You + Failed to preview this image Save diff --git a/src/org/thoughtcrime/securesms/MediaPreviewActivity.java b/src/org/thoughtcrime/securesms/MediaPreviewActivity.java index 49458a6ec6..d38dbaf35a 100644 --- a/src/org/thoughtcrime/securesms/MediaPreviewActivity.java +++ b/src/org/thoughtcrime/securesms/MediaPreviewActivity.java @@ -19,8 +19,10 @@ package org.thoughtcrime.securesms; import android.annotation.TargetApi; import android.content.ContentUris; import android.content.DialogInterface; -import android.graphics.BitmapFactory; +import android.graphics.Bitmap; import android.net.Uri; +import android.opengl.GLES20; +import android.os.AsyncTask; import android.os.Build.VERSION; import android.os.Build.VERSION_CODES; import android.os.Bundle; @@ -31,12 +33,15 @@ import android.view.MenuItem; import android.view.View; import android.view.WindowManager; import android.widget.ImageView; +import android.widget.TextView; import android.widget.Toast; import org.thoughtcrime.securesms.crypto.MasterSecret; import org.thoughtcrime.securesms.database.DatabaseFactory; import org.thoughtcrime.securesms.providers.PartProvider; import org.thoughtcrime.securesms.recipients.Recipient; +import org.thoughtcrime.securesms.util.BitmapDecodingException; +import org.thoughtcrime.securesms.util.BitmapUtil; import org.thoughtcrime.securesms.util.DateUtils; import org.thoughtcrime.securesms.util.DynamicLanguage; import org.thoughtcrime.securesms.util.SaveAttachmentTask; @@ -61,6 +66,8 @@ public class MediaPreviewActivity extends PassphraseRequiredActionBarActivity { private MasterSecret masterSecret; + private View loadingView; + private TextView errorText; private ImageView image; private PhotoViewAttacher imageAttacher; private Uri mediaUri; @@ -119,18 +126,10 @@ public class MediaPreviewActivity extends PassphraseRequiredActionBarActivity { finish(); } - try { - Log.w(TAG, "Loading Part URI: " + mediaUri); + Log.w(TAG, "Loading Part URI: " + mediaUri); - final InputStream is = getInputStream(mediaUri, masterSecret); - - if (mediaType != null && mediaType.startsWith("image/")) { - displayImage(is); - } - } catch (IOException ioe) { - Log.w(TAG, ioe); - Toast.makeText(getApplicationContext(), "Could not read the media", Toast.LENGTH_LONG).show(); - finish(); + if (mediaType != null && mediaType.startsWith("image/")) { + displayImage(); } } @@ -148,14 +147,48 @@ public class MediaPreviewActivity extends PassphraseRequiredActionBarActivity { } private void initializeResources() { + loadingView = findViewById(R.id.loading_indicator); + errorText = (TextView) findViewById(R.id.error); image = (ImageView) findViewById(R.id.image); imageAttacher = new PhotoViewAttacher(image); } - private void displayImage(final InputStream is) { - image.setImageBitmap(BitmapFactory.decodeStream(is)); - image.setVisibility(View.VISIBLE); - imageAttacher.update(); + private void displayImage() { + new AsyncTask() { + @Override + protected Bitmap doInBackground(Void... params) { + try { + int[] maxTextureSizeParams = new int[1]; + GLES20.glGetIntegerv(GLES20.GL_MAX_TEXTURE_SIZE, maxTextureSizeParams, 0); + int maxTextureSize = Math.max(maxTextureSizeParams[0], 2048); + Log.w(TAG, "reported GL_MAX_TEXTURE_SIZE: " + maxTextureSize); + return BitmapUtil.createScaledBitmap(getInputStream(mediaUri, masterSecret), + getInputStream(mediaUri, masterSecret), + maxTextureSize, maxTextureSize); + } catch (IOException | BitmapDecodingException e) { + return null; + } + } + + @Override + protected void onPreExecute() { + loadingView.setVisibility(View.VISIBLE); + } + + @Override + protected void onPostExecute(Bitmap bitmap) { + loadingView.setVisibility(View.GONE); + + if (bitmap == null) { + errorText.setText(R.string.MediaPreviewActivity_cant_display); + errorText.setVisibility(View.VISIBLE); + } else { + image.setImageBitmap(bitmap); + image.setVisibility(View.VISIBLE); + imageAttacher.update(); + } + } + }.execute(); } private void saveToDisk() {