From 1a8e888243782d042969134375cebb8bcd3b1af5 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Thu, 19 Nov 2020 23:42:37 +0800 Subject: Fix crash when downloading media. Run AsyncTask in parallel in DownloadMediaService and DownloadRedditVideoService. --- .../adapters/CommentAndPostRecyclerViewAdapter.java | 2 -- .../infinityforreddit/services/DownloadMediaService.java | 14 ++++++-------- .../services/DownloadRedditVideoService.java | 9 ++++----- 3 files changed, 10 insertions(+), 15 deletions(-) (limited to 'app/src') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentAndPostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentAndPostRecyclerViewAdapter.java index 42c6b706..6204934f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentAndPostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentAndPostRecyclerViewAdapter.java @@ -862,7 +862,6 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter 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(); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/services/DownloadRedditVideoService.java b/app/src/main/java/ml/docilealligator/infinityforreddit/services/DownloadRedditVideoService.java index c68be79b..5b744a9d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/services/DownloadRedditVideoService.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/services/DownloadRedditVideoService.java @@ -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 { -- cgit v1.2.3