From ab537ef3cbacaeca6e2a19ca3ffc6403ac5c1dbe Mon Sep 17 00:00:00 2001 From: Vavassor Date: Thu, 20 Jul 2017 17:54:41 -0400 Subject: [PATCH] Fixes a crash while autocompleting a mention. Also, a small bit of refactoring. --- .../keylesspalace/tusky/ComposeActivity.java | 92 +++++++++++-------- 1 file changed, 56 insertions(+), 36 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/ComposeActivity.java b/app/src/main/java/com/keylesspalace/tusky/ComposeActivity.java index 0baa6bf9..9df2a9f2 100644 --- a/app/src/main/java/com/keylesspalace/tusky/ComposeActivity.java +++ b/app/src/main/java/com/keylesspalace/tusky/ComposeActivity.java @@ -1342,6 +1342,45 @@ public class ComposeActivity extends BaseActivity implements ComposeOptionsFragm } } + @Nullable + private static Bitmap getImageThumbnail(ContentResolver contentResolver, Uri uri) { + InputStream stream; + try { + stream = contentResolver.openInputStream(uri); + } catch (FileNotFoundException e) { + return null; + } + Bitmap source = BitmapFactory.decodeStream(stream); + if (source == null) { + IOUtils.closeQuietly(stream); + return null; + } + Bitmap bitmap = ThumbnailUtils.extractThumbnail(source, THUMBNAIL_SIZE, THUMBNAIL_SIZE); + source.recycle(); + try { + if (stream != null) { + stream.close(); + } + } catch (IOException e) { + bitmap.recycle(); + return null; + } + return bitmap; + } + + @Nullable + private static Bitmap getVideoThumbnail(Context context, Uri uri) { + MediaMetadataRetriever retriever = new MediaMetadataRetriever(); + retriever.setDataSource(context, uri); + Bitmap source = retriever.getFrameAtTime(); + if (source == null) { + return null; + } + Bitmap bitmap = ThumbnailUtils.extractThumbnail(source, THUMBNAIL_SIZE, THUMBNAIL_SIZE); + source.recycle(); + return bitmap; + } + private void pickMedia(Uri uri, long mediaSize) { ContentResolver contentResolver = getContentResolver(); if (mediaSize == MediaUtils.MEDIA_SIZE_UNKNOWN) { @@ -1362,44 +1401,21 @@ public class ComposeActivity extends BaseActivity implements ComposeOptionsFragm displayTransientError(R.string.error_media_upload_image_or_video); return; } - MediaMetadataRetriever retriever = new MediaMetadataRetriever(); - retriever.setDataSource(this, uri); - Bitmap source = retriever.getFrameAtTime(); - if (source == null) { + Bitmap bitmap = getVideoThumbnail(this, uri); + if (bitmap != null) { + addMediaToQueue(QueuedMedia.Type.VIDEO, bitmap, uri, mediaSize); + } else { displayTransientError(R.string.error_media_upload_opening); - return; } - Bitmap bitmap = ThumbnailUtils.extractThumbnail(source, THUMBNAIL_SIZE, THUMBNAIL_SIZE); - source.recycle(); - addMediaToQueue(QueuedMedia.Type.VIDEO, bitmap, uri, mediaSize); break; } case "image": { - InputStream stream; - try { - stream = contentResolver.openInputStream(uri); - } catch (FileNotFoundException e) { + Bitmap bitmap = getImageThumbnail(contentResolver, uri); + if (bitmap != null) { + addMediaToQueue(QueuedMedia.Type.IMAGE, bitmap, uri, mediaSize); + } else { displayTransientError(R.string.error_media_upload_opening); - return; } - Bitmap source = BitmapFactory.decodeStream(stream); - if (source == null) { - IOUtils.closeQuietly(stream); - displayTransientError(R.string.error_media_upload_opening); - return; - } - Bitmap bitmap = ThumbnailUtils.extractThumbnail(source, THUMBNAIL_SIZE, THUMBNAIL_SIZE); - source.recycle(); - try { - if (stream != null) { - stream.close(); - } - } catch (IOException e) { - bitmap.recycle(); - displayTransientError(R.string.error_media_upload_opening); - return; - } - addMediaToQueue(QueuedMedia.Type.IMAGE, bitmap, uri, mediaSize); break; } default: { @@ -1596,13 +1612,13 @@ public class ComposeActivity extends BaseActivity implements ComposeOptionsFragm private class MentionAutoCompleteAdapter extends ArrayAdapter implements Filterable { private ArrayList resultList; - private @LayoutRes - int layoutId; + private int layoutId; MentionAutoCompleteAdapter(Context context, @LayoutRes int resource) { super(context, resource); layoutId = resource; + resultList = new ArrayList<>(); } @Override @@ -1628,9 +1644,13 @@ public class ComposeActivity extends BaseActivity implements ComposeOptionsFragm protected FilterResults performFiltering(CharSequence constraint) { FilterResults filterResults = new FilterResults(); if (constraint != null) { - resultList = autocompleteMention(constraint.toString()); - filterResults.values = resultList; - filterResults.count = resultList.size(); + ArrayList accounts = autocompleteMention(constraint.toString()); + synchronized (this) { + resultList.clear(); + resultList.addAll(accounts); + } + filterResults.values = accounts; + filterResults.count = accounts.size(); } return filterResults; }