Show different notifications when downloading Reddit videos.

This commit is contained in:
Alex Ning 2020-06-06 23:23:19 +08:00
parent 22576dc003
commit 98bbd0ff68
2 changed files with 34 additions and 11 deletions

View File

@ -6,6 +6,7 @@ import android.app.NotificationManager;
import android.app.Service; import android.app.Service;
import android.content.ContentResolver; import android.content.ContentResolver;
import android.content.ContentValues; import android.content.ContentValues;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.media.MediaCodec; import android.media.MediaCodec;
import android.media.MediaExtractor; import android.media.MediaExtractor;
@ -87,15 +88,18 @@ public class DownloadRedditVideoService extends Service {
manager.createNotificationChannel(serviceChannel); 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); ml.docilealligator.infinityforreddit.API.DownloadRedditVideo downloadRedditVideo = retrofit.create(ml.docilealligator.infinityforreddit.API.DownloadRedditVideo.class);
downloadRedditVideo.downloadFile(videoUrl).enqueue(new Callback<ResponseBody>() { downloadRedditVideo.downloadFile(videoUrl).enqueue(new Callback<ResponseBody>() {
@Override @Override
public void onResponse(@NonNull Call<ResponseBody> call, @NonNull Response<ResponseBody> videoResponse) { public void onResponse(@NonNull Call<ResponseBody> call, @NonNull Response<ResponseBody> videoResponse) {
if (videoResponse.isSuccessful() && videoResponse.body() != null) { if (videoResponse.isSuccessful() && videoResponse.body() != null) {
String fileNameWithoutExtension = intent.getStringExtra(EXTRA_SUBREDDIT) updateNotification(R.string.downloading_reddit_video_audio_track, fileNameWithoutExtension + ".mp3");
+ "-" + intent.getStringExtra(EXTRA_POST_ID);
downloadRedditVideo.downloadFile(audioUrl).enqueue(new Callback<ResponseBody>() { downloadRedditVideo.downloadFile(audioUrl).enqueue(new Callback<ResponseBody>() {
@Override @Override
@ -104,6 +108,8 @@ public class DownloadRedditVideoService extends Service {
if (directory != null) { if (directory != null) {
String directoryPath = directory.getAbsolutePath() + "/"; String directoryPath = directory.getAbsolutePath() + "/";
if (audioResponse.isSuccessful() && audioResponse.body() != null) { if (audioResponse.isSuccessful() && audioResponse.body() != null) {
updateNotification(R.string.downloading_reddit_video_muxing, null);
String videoFilePath = writeResponseBodyToDisk(videoResponse.body(), directoryPath + fileNameWithoutExtension+ "-cache.mp4"); String videoFilePath = writeResponseBodyToDisk(videoResponse.body(), directoryPath + fileNameWithoutExtension+ "-cache.mp4");
if (videoFilePath != null) { if (videoFilePath != null) {
String audioFilePath = writeResponseBodyToDisk(audioResponse.body(), directoryPath + fileNameWithoutExtension + "-cache.mp3"); String audioFilePath = writeResponseBodyToDisk(audioResponse.body(), directoryPath + fileNameWithoutExtension + "-cache.mp3");
@ -118,6 +124,8 @@ public class DownloadRedditVideoService extends Service {
new File(audioFilePath).delete(); new File(audioFilePath).delete();
new File(outputFilePath).delete(); new File(outputFilePath).delete();
updateNotification(R.string.downloading_reddit_video_finished, fileNameWithoutExtension + ".mp4");
EventBus.getDefault().post(new DownloadRedditVideoEvent(true)); EventBus.getDefault().post(new DownloadRedditVideoEvent(true));
stopService(); stopService();
@ -161,6 +169,8 @@ public class DownloadRedditVideoService extends Service {
public void successful() { public void successful() {
new File(videoFilePath).delete(); new File(videoFilePath).delete();
updateNotification(R.string.downloading_reddit_video_finished, null);
EventBus.getDefault().post(new DownloadRedditVideoEvent(true)); EventBus.getDefault().post(new DownloadRedditVideoEvent(true));
stopService(); stopService();
@ -213,15 +223,27 @@ public class DownloadRedditVideoService extends Service {
return START_NOT_STICKY; return START_NOT_STICKY;
} }
private Notification createNotification(int stringResId) { private Notification createNotification(int stringResId, String fileName) {
return new NotificationCompat.Builder(this, NotificationUtils.CHANNEL_ID_DOWNLOAD_REDDIT_VIDEO) NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NotificationUtils.CHANNEL_ID_DOWNLOAD_REDDIT_VIDEO);
.setContentTitle(getString(stringResId)) if (fileName != null) {
.setContentText(getString(R.string.please_wait)) builder.setContentTitle(getString(stringResId, fileName));
} else {
builder.setContentTitle(getString(stringResId));
}
return builder.setContentText(getString(R.string.please_wait))
.setSmallIcon(R.drawable.ic_notification) .setSmallIcon(R.drawable.ic_notification)
.setColor(mCustomThemeWrapper.getColorPrimaryLightTheme()) .setColor(mCustomThemeWrapper.getColorPrimaryLightTheme())
.build(); .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) { private String writeResponseBodyToDisk(ResponseBody body, String filePath) {
try { try {
File file = new File(filePath); File file = new File(filePath);
@ -248,7 +270,6 @@ public class DownloadRedditVideoService extends Service {
outputStream.write(fileReader, 0, read); outputStream.write(fileReader, 0, read);
fileSizeDownloaded += read; fileSizeDownloaded += read;
Log.i("asdfsadf", "file download: " + fileSizeDownloaded + " of " + fileSize); Log.i("asdfsadf", "file download: " + fileSizeDownloaded + " of " + fileSize);
} }
@ -349,8 +370,7 @@ public class DownloadRedditVideoService extends Service {
} }
private void stopService() { private void stopService() {
stopForeground(true); stopForeground(false);
stopSelf();
} }
private static class CopyFileAsyncTask extends AsyncTask<Void, Void, Void> { private static class CopyFileAsyncTask extends AsyncTask<Void, Void, Void> {

View File

@ -740,6 +740,9 @@
<string name="error_fetching_imgur_media">Cannot load images</string> <string name="error_fetching_imgur_media">Cannot load images</string>
<string name="downloading_reddit_video">Downloading video.</string> <string name="downloading_reddit_video">Downloading Video %1$s</string>
<string name="downloading_reddit_video_audio_track">Downloading Audio Track For %1$s</string>
<string name="downloading_reddit_video_muxing">Muxing Video</string>
<string name="downloading_reddit_video_finished">%1$s Downloaded</string>
</resources> </resources>