Fixes a crash while autocompleting a mention. Also, a small bit of refactoring.

This commit is contained in:
Vavassor 2017-07-20 17:54:41 -04:00
parent f6c309e7db
commit ab537ef3cb

View file

@ -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) { private void pickMedia(Uri uri, long mediaSize) {
ContentResolver contentResolver = getContentResolver(); ContentResolver contentResolver = getContentResolver();
if (mediaSize == MediaUtils.MEDIA_SIZE_UNKNOWN) { 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); displayTransientError(R.string.error_media_upload_image_or_video);
return; return;
} }
MediaMetadataRetriever retriever = new MediaMetadataRetriever(); Bitmap bitmap = getVideoThumbnail(this, uri);
retriever.setDataSource(this, uri); if (bitmap != null) {
Bitmap source = retriever.getFrameAtTime(); addMediaToQueue(QueuedMedia.Type.VIDEO, bitmap, uri, mediaSize);
if (source == null) { } else {
displayTransientError(R.string.error_media_upload_opening); 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; break;
} }
case "image": { case "image": {
InputStream stream; Bitmap bitmap = getImageThumbnail(contentResolver, uri);
try { if (bitmap != null) {
stream = contentResolver.openInputStream(uri); addMediaToQueue(QueuedMedia.Type.IMAGE, bitmap, uri, mediaSize);
} catch (FileNotFoundException e) { } else {
displayTransientError(R.string.error_media_upload_opening); 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; break;
} }
default: { default: {
@ -1596,13 +1612,13 @@ public class ComposeActivity extends BaseActivity implements ComposeOptionsFragm
private class MentionAutoCompleteAdapter extends ArrayAdapter<Account> implements Filterable { private class MentionAutoCompleteAdapter extends ArrayAdapter<Account> implements Filterable {
private ArrayList<Account> resultList; private ArrayList<Account> resultList;
private
@LayoutRes @LayoutRes
int layoutId; private int layoutId;
MentionAutoCompleteAdapter(Context context, @LayoutRes int resource) { MentionAutoCompleteAdapter(Context context, @LayoutRes int resource) {
super(context, resource); super(context, resource);
layoutId = resource; layoutId = resource;
resultList = new ArrayList<>();
} }
@Override @Override
@ -1628,9 +1644,13 @@ public class ComposeActivity extends BaseActivity implements ComposeOptionsFragm
protected FilterResults performFiltering(CharSequence constraint) { protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults(); FilterResults filterResults = new FilterResults();
if (constraint != null) { if (constraint != null) {
resultList = autocompleteMention(constraint.toString()); ArrayList<Account> accounts = autocompleteMention(constraint.toString());
filterResults.values = resultList; synchronized (this) {
filterResults.count = resultList.size(); resultList.clear();
resultList.addAll(accounts);
}
filterResults.values = accounts;
filterResults.count = accounts.size();
} }
return filterResults; return filterResults;
} }