diff options
author | Alex Ning <chineseperson5@gmail.com> | 2020-06-06 15:23:19 +0000 |
---|---|---|
committer | Alex Ning <chineseperson5@gmail.com> | 2020-06-06 15:23:19 +0000 |
commit | 98bbd0ff68df1aa749e5e4d6f91f11929b046113 (patch) | |
tree | fb355e215538e7448ac9e470eae4d213a9655756 /app/src/main/java | |
parent | 22576dc003ddaeb0c06f94eaf5f66598e5b3554f (diff) | |
download | infinity-for-reddit-98bbd0ff68df1aa749e5e4d6f91f11929b046113.tar infinity-for-reddit-98bbd0ff68df1aa749e5e4d6f91f11929b046113.tar.gz infinity-for-reddit-98bbd0ff68df1aa749e5e4d6f91f11929b046113.tar.bz2 infinity-for-reddit-98bbd0ff68df1aa749e5e4d6f91f11929b046113.tar.lz infinity-for-reddit-98bbd0ff68df1aa749e5e4d6f91f11929b046113.tar.xz infinity-for-reddit-98bbd0ff68df1aa749e5e4d6f91f11929b046113.tar.zst infinity-for-reddit-98bbd0ff68df1aa749e5e4d6f91f11929b046113.zip |
Show different notifications when downloading Reddit videos.
Diffstat (limited to 'app/src/main/java')
-rw-r--r-- | app/src/main/java/ml/docilealligator/infinityforreddit/Service/DownloadRedditVideoService.java | 40 |
1 files changed, 30 insertions, 10 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Service/DownloadRedditVideoService.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Service/DownloadRedditVideoService.java index e5885c90..4d94a2c5 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Service/DownloadRedditVideoService.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Service/DownloadRedditVideoService.java @@ -6,6 +6,7 @@ import android.app.NotificationManager; import android.app.Service; import android.content.ContentResolver; import android.content.ContentValues; +import android.content.Context; import android.content.Intent; import android.media.MediaCodec; import android.media.MediaExtractor; @@ -87,15 +88,18 @@ public class DownloadRedditVideoService extends Service { manager.createNotificationChannel(serviceChannel); } - startForeground(NotificationUtils.DOWNLOAD_REDDIT_VIDEO_NOTIFICATION_ID, createNotification(R.string.downloading_reddit_video)); + String fileNameWithoutExtension = intent.getStringExtra(EXTRA_SUBREDDIT) + + "-" + intent.getStringExtra(EXTRA_POST_ID); + + startForeground(NotificationUtils.DOWNLOAD_REDDIT_VIDEO_NOTIFICATION_ID, + createNotification(R.string.downloading_reddit_video, fileNameWithoutExtension + ".mp4")); ml.docilealligator.infinityforreddit.API.DownloadRedditVideo downloadRedditVideo = retrofit.create(ml.docilealligator.infinityforreddit.API.DownloadRedditVideo.class); downloadRedditVideo.downloadFile(videoUrl).enqueue(new Callback<ResponseBody>() { @Override public void onResponse(@NonNull Call<ResponseBody> call, @NonNull Response<ResponseBody> videoResponse) { if (videoResponse.isSuccessful() && videoResponse.body() != null) { - String fileNameWithoutExtension = intent.getStringExtra(EXTRA_SUBREDDIT) - + "-" + intent.getStringExtra(EXTRA_POST_ID); + updateNotification(R.string.downloading_reddit_video_audio_track, fileNameWithoutExtension + ".mp3"); downloadRedditVideo.downloadFile(audioUrl).enqueue(new Callback<ResponseBody>() { @Override @@ -104,6 +108,8 @@ public class DownloadRedditVideoService extends Service { if (directory != null) { String directoryPath = directory.getAbsolutePath() + "/"; if (audioResponse.isSuccessful() && audioResponse.body() != null) { + updateNotification(R.string.downloading_reddit_video_muxing, null); + String videoFilePath = writeResponseBodyToDisk(videoResponse.body(), directoryPath + fileNameWithoutExtension+ "-cache.mp4"); if (videoFilePath != null) { String audioFilePath = writeResponseBodyToDisk(audioResponse.body(), directoryPath + fileNameWithoutExtension + "-cache.mp3"); @@ -118,6 +124,8 @@ public class DownloadRedditVideoService extends Service { new File(audioFilePath).delete(); new File(outputFilePath).delete(); + updateNotification(R.string.downloading_reddit_video_finished, fileNameWithoutExtension + ".mp4"); + EventBus.getDefault().post(new DownloadRedditVideoEvent(true)); stopService(); @@ -161,6 +169,8 @@ public class DownloadRedditVideoService extends Service { public void successful() { new File(videoFilePath).delete(); + updateNotification(R.string.downloading_reddit_video_finished, null); + EventBus.getDefault().post(new DownloadRedditVideoEvent(true)); stopService(); @@ -213,15 +223,27 @@ public class DownloadRedditVideoService extends Service { return START_NOT_STICKY; } - private Notification createNotification(int stringResId) { - return new NotificationCompat.Builder(this, NotificationUtils.CHANNEL_ID_DOWNLOAD_REDDIT_VIDEO) - .setContentTitle(getString(stringResId)) - .setContentText(getString(R.string.please_wait)) + private Notification createNotification(int stringResId, String fileName) { + NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NotificationUtils.CHANNEL_ID_DOWNLOAD_REDDIT_VIDEO); + if (fileName != null) { + builder.setContentTitle(getString(stringResId, fileName)); + } else { + builder.setContentTitle(getString(stringResId)); + } + return builder.setContentText(getString(R.string.please_wait)) .setSmallIcon(R.drawable.ic_notification) .setColor(mCustomThemeWrapper.getColorPrimaryLightTheme()) .build(); } + private void updateNotification(int stringResId, String fileName) { + NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); + if (notificationManager != null) { + notificationManager.notify(NotificationUtils.DOWNLOAD_REDDIT_VIDEO_NOTIFICATION_ID, + createNotification(stringResId, fileName)); + } + } + private String writeResponseBodyToDisk(ResponseBody body, String filePath) { try { File file = new File(filePath); @@ -248,7 +270,6 @@ public class DownloadRedditVideoService extends Service { outputStream.write(fileReader, 0, read); fileSizeDownloaded += read; - Log.i("asdfsadf", "file download: " + fileSizeDownloaded + " of " + fileSize); } @@ -349,8 +370,7 @@ public class DownloadRedditVideoService extends Service { } private void stopService() { - stopForeground(true); - stopSelf(); + stopForeground(false); } private static class CopyFileAsyncTask extends AsyncTask<Void, Void, Void> { |