Fix crash when downloading media. Run AsyncTask in parallel in DownloadMediaService and DownloadRedditVideoService.

This commit is contained in:
Alex Ning 2020-11-19 23:42:37 +08:00
parent 4904d46cb2
commit 1a8e888243
3 changed files with 10 additions and 15 deletions

View File

@ -862,7 +862,6 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
((PostDetailVideoAutoplayViewHolder) holder).setVolume(mMuteAutoplayingVideos || (mPost.isNSFW() && mMuteNSFWVideo) ? 0f : 1f); ((PostDetailVideoAutoplayViewHolder) holder).setVolume(mMuteAutoplayingVideos || (mPost.isNSFW() && mMuteNSFWVideo) ? 0f : 1f);
if (mPost.isGfycat() || mPost.isRedgifs() && !mPost.isLoadGfyOrRedgifsVideoSuccess()) { if (mPost.isGfycat() || mPost.isRedgifs() && !mPost.isLoadGfyOrRedgifsVideoSuccess()) {
((PostDetailVideoAutoplayViewHolder) holder).mTypeTextView.setText("GFYCAT");
((PostDetailVideoAutoplayViewHolder) holder).fetchGfycatOrRedgifsVideoLinks = new FetchGfycatOrRedgifsVideoLinks(new FetchGfycatOrRedgifsVideoLinks.FetchGfycatOrRedgifsVideoLinksListener() { ((PostDetailVideoAutoplayViewHolder) holder).fetchGfycatOrRedgifsVideoLinks = new FetchGfycatOrRedgifsVideoLinks(new FetchGfycatOrRedgifsVideoLinks.FetchGfycatOrRedgifsVideoLinksListener() {
@Override @Override
public void success(String webm, String mp4) { public void success(String webm, String mp4) {
@ -881,7 +880,6 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
.fetchGfycatOrRedgifsVideoLinksInRecyclerViewAdapter(mGfycatRetrofit, mRedgifsRetrofit, .fetchGfycatOrRedgifsVideoLinksInRecyclerViewAdapter(mGfycatRetrofit, mRedgifsRetrofit,
mPost.getGfycatId(), mPost.isGfycat(), mAutomaticallyTryRedgifs); mPost.getGfycatId(), mPost.isGfycat(), mAutomaticallyTryRedgifs);
} else { } else {
((PostDetailVideoAutoplayViewHolder) holder).mTypeTextView.setText("VIDEO");
((PostDetailVideoAutoplayViewHolder) holder).bindVideoUri(Uri.parse(mPost.getVideoUrl())); ((PostDetailVideoAutoplayViewHolder) holder).bindVideoUri(Uri.parse(mPost.getVideoUrl()));
} }
} else if (holder instanceof PostDetailVideoAndGifPreviewHolder) { } else if (holder instanceof PostDetailVideoAndGifPreviewHolder) {

View File

@ -178,8 +178,7 @@ public class DownloadMediaService extends Service {
NotificationManager.IMPORTANCE_LOW NotificationManager.IMPORTANCE_LOW
); );
NotificationManagerCompat manager = NotificationManagerCompat.from(this); notificationManager.createNotificationChannel(serviceChannel);
manager.createNotificationChannel(serviceChannel);
} }
switch (mediaType) { switch (mediaType) {
@ -275,7 +274,7 @@ public class DownloadMediaService extends Service {
new SaveImageOrGifAndCopyToExternalStorageAsyncTask(response.body(), mediaType, new SaveImageOrGifAndCopyToExternalStorageAsyncTask(response.body(), mediaType,
isDefaultDestination, fileName[0], destinationFileUriString, getContentResolver(), isDefaultDestination, fileName[0], destinationFileUriString, getContentResolver(),
(destinationFileUri, errorCode) -> downloadFinished(destinationFileUri, errorCode)).execute(); (destinationFileUri, errorCode) -> downloadFinished(destinationFileUri, errorCode)).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} }
} }
@ -289,8 +288,7 @@ public class DownloadMediaService extends Service {
} }
private Notification createNotification(String fileName) { private Notification createNotification(String fileName) {
builder.setContentTitle(fileName).setContentText(getString(R.string.downloading)) builder.setContentTitle(fileName).setContentText(getString(R.string.downloading)).setProgress(100, 0, false);
.setProgress(100, 0, false);
return builder.setSmallIcon(R.drawable.ic_notification) return builder.setSmallIcon(R.drawable.ic_notification)
.setColor(mCustomThemeWrapper.getColorPrimaryLightTheme()) .setColor(mCustomThemeWrapper.getColorPrimaryLightTheme())
.build(); .build();
@ -424,9 +422,9 @@ public class DownloadMediaService extends Service {
contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, destinationFileName); contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, destinationFileName);
contentValues.put(MediaStore.MediaColumns.MIME_TYPE, mediaType == EXTRA_MEDIA_TYPE_VIDEO ? "video/*" : "image/*"); contentValues.put(MediaStore.MediaColumns.MIME_TYPE, mediaType == EXTRA_MEDIA_TYPE_VIDEO ? "video/*" : "image/*");
contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, destinationFileUriString); contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, destinationFileUriString);
contentValues.put(MediaStore.Images.Media.IS_PENDING, 1); contentValues.put(mediaType == EXTRA_MEDIA_TYPE_VIDEO ? MediaStore.Video.Media.IS_PENDING : MediaStore.Images.Media.IS_PENDING, 1);
final Uri contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; final Uri contentUri = mediaType == EXTRA_MEDIA_TYPE_VIDEO ? MediaStore.Video.Media.EXTERNAL_CONTENT_URI : MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
Uri uri = contentResolver.insert(contentUri, contentValues); Uri uri = contentResolver.insert(contentUri, contentValues);
if (uri == null) { if (uri == null) {
@ -446,7 +444,7 @@ public class DownloadMediaService extends Service {
stream.write(buf, 0, len); stream.write(buf, 0, len);
} }
contentValues.clear(); contentValues.clear();
contentValues.put(MediaStore.Images.Media.IS_PENDING, 0); contentValues.put(mediaType == EXTRA_MEDIA_TYPE_VIDEO ? MediaStore.Video.Media.IS_PENDING : MediaStore.Images.Media.IS_PENDING, 0);
contentResolver.update(uri, contentValues, null, null); contentResolver.update(uri, contentValues, null, null);
destinationFileUriString = uri.toString(); destinationFileUriString = uri.toString();
} }

View File

@ -138,8 +138,7 @@ public class DownloadRedditVideoService extends Service {
NotificationManager.IMPORTANCE_LOW NotificationManager.IMPORTANCE_LOW
); );
NotificationManagerCompat manager = NotificationManagerCompat.from(this); notificationManager.createNotificationChannel(serviceChannel);
manager.createNotificationChannel(serviceChannel);
} }
startForeground( startForeground(
@ -246,7 +245,7 @@ public class DownloadRedditVideoService extends Service {
public void updateProgressNotification(int stringResId) { public void updateProgressNotification(int stringResId) {
updateNotification(stringResId, -1, null); updateNotification(stringResId, -1, null);
} }
}).execute(); }).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} else { } else {
String videoFilePath = directoryPath + fileNameWithoutExtension[0] + "-cache.mp4"; String videoFilePath = directoryPath + fileNameWithoutExtension[0] + "-cache.mp4";
new SaveTempMuxAndCopyAsyncTask(videoResponse.body(), new SaveTempMuxAndCopyAsyncTask(videoResponse.body(),
@ -264,7 +263,7 @@ public class DownloadRedditVideoService extends Service {
public void updateProgressNotification(int stringResId) { public void updateProgressNotification(int stringResId) {
updateNotification(stringResId, -1, null); updateNotification(stringResId, -1, null);
} }
}).execute(); }).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} }
} }
@ -286,7 +285,7 @@ public class DownloadRedditVideoService extends Service {
public void updateProgressNotification(int stringResId) { public void updateProgressNotification(int stringResId) {
updateNotification(stringResId, -1, null); updateNotification(stringResId, -1, null);
} }
}).execute(); }).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} }
}); });
} else { } else {