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

View File

@ -178,8 +178,7 @@ public class DownloadMediaService extends Service {
NotificationManager.IMPORTANCE_LOW
);
NotificationManagerCompat manager = NotificationManagerCompat.from(this);
manager.createNotificationChannel(serviceChannel);
notificationManager.createNotificationChannel(serviceChannel);
}
switch (mediaType) {
@ -275,7 +274,7 @@ public class DownloadMediaService extends Service {
new SaveImageOrGifAndCopyToExternalStorageAsyncTask(response.body(), mediaType,
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) {
builder.setContentTitle(fileName).setContentText(getString(R.string.downloading))
.setProgress(100, 0, false);
builder.setContentTitle(fileName).setContentText(getString(R.string.downloading)).setProgress(100, 0, false);
return builder.setSmallIcon(R.drawable.ic_notification)
.setColor(mCustomThemeWrapper.getColorPrimaryLightTheme())
.build();
@ -424,9 +422,9 @@ public class DownloadMediaService extends Service {
contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, destinationFileName);
contentValues.put(MediaStore.MediaColumns.MIME_TYPE, mediaType == EXTRA_MEDIA_TYPE_VIDEO ? "video/*" : "image/*");
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);
if (uri == null) {
@ -446,7 +444,7 @@ public class DownloadMediaService extends Service {
stream.write(buf, 0, len);
}
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);
destinationFileUriString = uri.toString();
}

View File

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