diff options
author | Alex Ning <chineseperson5@gmail.com> | 2020-08-09 02:41:24 +0000 |
---|---|---|
committer | Alex Ning <chineseperson5@gmail.com> | 2020-08-09 02:41:24 +0000 |
commit | 4b4785171cce739cb70bc0693a50edf97587f910 (patch) | |
tree | fae2387aa786f9261bb8040ddda722d8cc3d347f /app/src/main/java | |
parent | 74a4206cd217dbffd046b7a2f907140a63db0eee (diff) | |
download | infinity-for-reddit-4b4785171cce739cb70bc0693a50edf97587f910.tar infinity-for-reddit-4b4785171cce739cb70bc0693a50edf97587f910.tar.gz infinity-for-reddit-4b4785171cce739cb70bc0693a50edf97587f910.tar.bz2 infinity-for-reddit-4b4785171cce739cb70bc0693a50edf97587f910.tar.lz infinity-for-reddit-4b4785171cce739cb70bc0693a50edf97587f910.tar.xz infinity-for-reddit-4b4785171cce739cb70bc0693a50edf97587f910.tar.zst infinity-for-reddit-4b4785171cce739cb70bc0693a50edf97587f910.zip |
Use DownloadVideoService to download videos.
Diffstat (limited to 'app/src/main/java')
-rw-r--r-- | app/src/main/java/ml/docilealligator/infinityforreddit/API/DownloadVideo.java (renamed from app/src/main/java/ml/docilealligator/infinityforreddit/API/DownloadRedditVideo.java) | 2 | ||||
-rw-r--r-- | app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewVideoActivity.java | 11 | ||||
-rw-r--r-- | app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java | 4 | ||||
-rw-r--r-- | app/src/main/java/ml/docilealligator/infinityforreddit/MediaDownloaderImpl.java | 21 | ||||
-rw-r--r-- | app/src/main/java/ml/docilealligator/infinityforreddit/NotificationUtils.java | 3 | ||||
-rw-r--r-- | app/src/main/java/ml/docilealligator/infinityforreddit/Service/DownloadVideoService.java (renamed from app/src/main/java/ml/docilealligator/infinityforreddit/Service/DownloadRedditVideoService.java) | 190 |
6 files changed, 150 insertions, 81 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/API/DownloadRedditVideo.java b/app/src/main/java/ml/docilealligator/infinityforreddit/API/DownloadVideo.java index 7e51123d..e3d9d238 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/API/DownloadRedditVideo.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/API/DownloadVideo.java @@ -6,7 +6,7 @@ import retrofit2.http.GET; import retrofit2.http.Streaming; import retrofit2.http.Url; -public interface DownloadRedditVideo { +public interface DownloadVideo { @Streaming @GET() Call<ResponseBody> downloadFile(@Url String fileUrl); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewVideoActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewVideoActivity.java index 82621a6c..79ef0477 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewVideoActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewVideoActivity.java @@ -62,7 +62,7 @@ import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.MediaDownloader; import ml.docilealligator.infinityforreddit.MediaDownloaderImpl; import ml.docilealligator.infinityforreddit.R; -import ml.docilealligator.infinityforreddit.Service.DownloadRedditVideoService; +import ml.docilealligator.infinityforreddit.Service.DownloadVideoService; import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; import retrofit2.Retrofit; @@ -436,10 +436,11 @@ public class ViewVideoActivity extends AppCompatActivity { if (videoType != VIDEO_TYPE_NORMAL) { mediaDownloader.download(videoDownloadUrl, videoFileName, this); } else { - Intent intent = new Intent(this, DownloadRedditVideoService.class); - intent.putExtra(DownloadRedditVideoService.EXTRA_VIDEO_URL, videoDownloadUrl); - intent.putExtra(DownloadRedditVideoService.EXTRA_POST_ID, id); - intent.putExtra(DownloadRedditVideoService.EXTRA_SUBREDDIT, subredditName); + Intent intent = new Intent(this, DownloadVideoService.class); + intent.putExtra(DownloadVideoService.EXTRA_VIDEO_URL, videoDownloadUrl); + intent.putExtra(DownloadVideoService.EXTRA_POST_ID, id); + intent.putExtra(DownloadVideoService.EXTRA_SUBREDDIT, subredditName); + intent.putExtra(DownloadVideoService.EXTRA_IS_REDDIT_VIDEO, true); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { startForegroundService(intent); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java index 3542c686..2952e741 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java @@ -54,7 +54,7 @@ import ml.docilealligator.infinityforreddit.Fragment.SubscribedSubredditsListing import ml.docilealligator.infinityforreddit.Fragment.UserListingFragment; import ml.docilealligator.infinityforreddit.Fragment.ViewImgurVideoFragment; import ml.docilealligator.infinityforreddit.Fragment.ViewRedditGalleryVideoFragment; -import ml.docilealligator.infinityforreddit.Service.DownloadRedditVideoService; +import ml.docilealligator.infinityforreddit.Service.DownloadVideoService; import ml.docilealligator.infinityforreddit.Service.SubmitPostService; import ml.docilealligator.infinityforreddit.Settings.AdvancedPreferenceFragment; import ml.docilealligator.infinityforreddit.Settings.CustomizeMainPageTabsFragment; @@ -173,7 +173,7 @@ public interface AppComponent { void inject(ViewImgurVideoFragment viewImgurVideoFragment); - void inject(DownloadRedditVideoService downloadRedditVideoService); + void inject(DownloadVideoService downloadVideoService); void inject(MultiRedditListingFragment multiRedditListingFragment); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/MediaDownloaderImpl.java b/app/src/main/java/ml/docilealligator/infinityforreddit/MediaDownloaderImpl.java index 638bdf93..79002741 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/MediaDownloaderImpl.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/MediaDownloaderImpl.java @@ -1,19 +1,17 @@ package ml.docilealligator.infinityforreddit; -import android.app.DownloadManager; import android.content.Context; -import android.net.Uri; +import android.content.Intent; import android.os.Build; -import android.os.Environment; import android.widget.Toast; -import java.io.File; +import ml.docilealligator.infinityforreddit.Service.DownloadVideoService; public class MediaDownloaderImpl implements MediaDownloader { @Override public void download(String url, String fileName, Context ctx) { - DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url)); + /*DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url)); request.setTitle(fileName); request.allowScanningByMediaScanner(); @@ -53,6 +51,19 @@ public class MediaDownloaderImpl implements MediaDownloader { } manager.enqueue(request); + Toast.makeText(ctx, R.string.download_started, Toast.LENGTH_SHORT).show();*/ + + + Intent intent = new Intent(ctx, DownloadVideoService.class); + intent.putExtra(DownloadVideoService.EXTRA_VIDEO_URL, url); + intent.putExtra(DownloadVideoService.EXTRA_FILE_NAME, fileName); + intent.putExtra(DownloadVideoService.EXTRA_IS_REDDIT_VIDEO, false); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + ctx.startForegroundService(intent); + } else { + ctx.startService(intent); + } Toast.makeText(ctx, R.string.download_started, Toast.LENGTH_SHORT).show(); } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/NotificationUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/NotificationUtils.java index 155e4b02..8fe4b33e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/NotificationUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/NotificationUtils.java @@ -13,8 +13,11 @@ public class NotificationUtils { static final String CHANNEL_NEW_MESSAGES = "New Messages"; public static final String CHANNEL_ID_DOWNLOAD_REDDIT_VIDEO = "download_reddit_video"; public static final String CHANNEL_DOWNLOAD_REDDIT_VIDEO = "Download Reddit Video"; + public static final String CHANNEL_ID_DOWNLOAD_VIDEO = "download_video"; + public static final String CHANNEL_DOWNLOAD_VIDEO = "Download Video"; public static final int SUBMIT_POST_SERVICE_NOTIFICATION_ID = 10000; public static final int DOWNLOAD_REDDIT_VIDEO_NOTIFICATION_ID = 20000; + public static final int DOWNLOAD_VIDEO_NOTIFICATION_ID = 30000; private static final int SUMMARY_BASE_ID_UNREAD_MESSAGE = 0; private static final int NOTIFICATION_BASE_ID_UNREAD_MESSAGE = 1; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Service/DownloadRedditVideoService.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Service/DownloadVideoService.java index 562ff432..b3360bb4 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Service/DownloadRedditVideoService.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Service/DownloadVideoService.java @@ -39,7 +39,7 @@ import java.nio.ByteBuffer; import javax.inject.Inject; import javax.inject.Named; -import ml.docilealligator.infinityforreddit.API.DownloadRedditVideo; +import ml.docilealligator.infinityforreddit.API.DownloadVideo; import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.Event.DownloadRedditVideoEvent; import ml.docilealligator.infinityforreddit.Infinity; @@ -53,11 +53,13 @@ import retrofit2.Retrofit; import static android.os.Environment.getExternalStoragePublicDirectory; -public class DownloadRedditVideoService extends Service { +public class DownloadVideoService extends Service { public static final String EXTRA_VIDEO_URL = "EVU"; public static final String EXTRA_SUBREDDIT = "ES"; public static final String EXTRA_POST_ID = "EPI"; + public static final String EXTRA_IS_REDDIT_VIDEO = "EIRV"; + public static final String EXTRA_FILE_NAME = "EFN"; private static final int NO_ERROR = -1; private static final int ERROR_CANNOT_GET_CACHE_DIRECTORY = 0; @@ -67,6 +69,8 @@ public class DownloadRedditVideoService extends Service { private static final int ERROR_MUX_FAILED = 4; private static final int ERROR_MUXED_VIDEO_FILE_CANNOT_SAVE = 5; + private boolean isRedditVideo; + @Inject @Named("download_reddit_video") Retrofit retrofit; @@ -74,7 +78,7 @@ public class DownloadRedditVideoService extends Service { CustomThemeWrapper mCustomThemeWrapper; String resultFile; - public DownloadRedditVideoService() { + public DownloadVideoService() { } @Override @@ -86,63 +90,107 @@ public class DownloadRedditVideoService extends Service { public int onStartCommand(Intent intent, int flags, int startId) { ((Infinity) getApplication()).getAppComponent().inject(this); + isRedditVideo = intent.getBooleanExtra(EXTRA_IS_REDDIT_VIDEO, false); String videoUrl = intent.getStringExtra(EXTRA_VIDEO_URL); - String audioUrl = videoUrl.substring(0, videoUrl.lastIndexOf('/')) + "/audio"; + String audioUrl = isRedditVideo ? videoUrl.substring(0, videoUrl.lastIndexOf('/')) + "/audio" : ""; + String fileName; + if (isRedditVideo) { + fileName = intent.getStringExtra(EXTRA_SUBREDDIT) + "-" + intent.getStringExtra(EXTRA_POST_ID); + } else { + fileName = intent.getStringExtra(EXTRA_FILE_NAME); + } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - NotificationChannel serviceChannel = new NotificationChannel( - NotificationUtils.CHANNEL_ID_DOWNLOAD_REDDIT_VIDEO, - NotificationUtils.CHANNEL_DOWNLOAD_REDDIT_VIDEO, - NotificationManager.IMPORTANCE_LOW - ); + NotificationChannel serviceChannel; + if (isRedditVideo) { + serviceChannel = new NotificationChannel( + NotificationUtils.CHANNEL_ID_DOWNLOAD_REDDIT_VIDEO, + NotificationUtils.CHANNEL_DOWNLOAD_REDDIT_VIDEO, + NotificationManager.IMPORTANCE_LOW + ); + } else { + serviceChannel = new NotificationChannel( + NotificationUtils.CHANNEL_ID_DOWNLOAD_VIDEO, + NotificationUtils.CHANNEL_DOWNLOAD_VIDEO, + NotificationManager.IMPORTANCE_LOW + ); + } NotificationManagerCompat manager = NotificationManagerCompat.from(this); manager.createNotificationChannel(serviceChannel); } - String fileName = intent.getStringExtra(EXTRA_SUBREDDIT) + "-" + intent.getStringExtra(EXTRA_POST_ID); - - startForeground( - NotificationUtils.DOWNLOAD_REDDIT_VIDEO_NOTIFICATION_ID, - createNotification(R.string.downloading_reddit_video, fileName + ".mp4", null) - ); + if (isRedditVideo) { + startForeground( + NotificationUtils.DOWNLOAD_REDDIT_VIDEO_NOTIFICATION_ID, + createNotification(R.string.downloading_reddit_video, fileName + ".mp4", null) + ); + } else { + startForeground( + NotificationUtils.DOWNLOAD_VIDEO_NOTIFICATION_ID, + createNotification(R.string.downloading_video, fileName, null) + ); + } - DownloadRedditVideo downloadRedditVideo = retrofit.create(DownloadRedditVideo.class); + DownloadVideo downloadVideo = retrofit.create(DownloadVideo.class); File directory = getExternalCacheDir(); String destinationFileName = fileName + ".mp4"; if (directory != null) { String directoryPath = directory.getAbsolutePath() + "/"; - downloadRedditVideo.downloadFile(videoUrl).enqueue(new Callback<ResponseBody>() { + downloadVideo.downloadFile(videoUrl).enqueue(new Callback<ResponseBody>() { @Override public void onResponse(@NonNull Call<ResponseBody> call, @NonNull Response<ResponseBody> videoResponse) { if (videoResponse.isSuccessful() && videoResponse.body() != null) { - updateNotification(R.string.downloading_reddit_video_audio_track, destinationFileName, null); - downloadRedditVideo.downloadFile(audioUrl).enqueue(new Callback<ResponseBody>() { - @Override - public void onResponse(@NonNull Call<ResponseBody> call, @NonNull Response<ResponseBody> audioResponse) { - if (audioResponse.isSuccessful() && audioResponse.body() != null) { - String videoFilePath = directoryPath + fileName + "-cache.mp4"; - String audioFilePath = directoryPath + fileName + "-cache.mp3"; - String outputFilePath = directoryPath + fileName + ".mp4"; - new SaveTempMuxAndCopyAsyncTask(videoResponse.body(), - audioResponse.body(), videoFilePath, audioFilePath, outputFilePath, - destinationFileName, getContentResolver(), - new SaveTempMuxAndCopyAsyncTask.SaveTempMuxAndCopyAsyncTaskListener() { - @Override - public void finished(Uri destinationFileUri, int errorCode) { - new File(videoFilePath).delete(); - new File(audioFilePath).delete(); - new File(outputFilePath).delete(); - downloadFinished(destinationFileUri, destinationFileName, errorCode); - } + if (isRedditVideo) { + updateNotification(R.string.downloading_reddit_video_audio_track, destinationFileName, null); + downloadVideo.downloadFile(audioUrl).enqueue(new Callback<ResponseBody>() { + @Override + public void onResponse(@NonNull Call<ResponseBody> call, @NonNull Response<ResponseBody> audioResponse) { + if (audioResponse.isSuccessful() && audioResponse.body() != null) { + String videoFilePath = directoryPath + fileName + "-cache.mp4"; + String audioFilePath = directoryPath + fileName + "-cache.mp3"; + String outputFilePath = directoryPath + fileName + ".mp4"; + new SaveTempMuxAndCopyAsyncTask(videoResponse.body(), + audioResponse.body(), videoFilePath, audioFilePath, outputFilePath, + destinationFileName, getContentResolver(), + new SaveTempMuxAndCopyAsyncTask.SaveTempMuxAndCopyAsyncTaskListener() { + @Override + public void finished(Uri destinationFileUri, int errorCode) { + new File(videoFilePath).delete(); + new File(audioFilePath).delete(); + new File(outputFilePath).delete(); + downloadFinished(destinationFileUri, destinationFileName, errorCode); + } + + @Override + public void updateProgressNotification(int stringResId) { + updateNotification(stringResId, destinationFileName, null); + } + }).execute(); + } else { + String videoFilePath = directoryPath + fileName + "-cache.mp4"; + String destinationFileName = fileName + ".mp4"; + new SaveTempMuxAndCopyAsyncTask(videoResponse.body(), + null, videoFilePath, null, null, + destinationFileName, getContentResolver(), + new SaveTempMuxAndCopyAsyncTask.SaveTempMuxAndCopyAsyncTaskListener() { + @Override + public void finished(Uri destinationFileUri, int errorCode) { + new File(videoFilePath).delete(); + downloadFinished(destinationFileUri, destinationFileName, errorCode); + } + + @Override + public void updateProgressNotification(int stringResId) { + updateNotification(stringResId, destinationFileName, null); + } + }).execute(); + } + } - @Override - public void updateProgressNotification(int stringResId) { - updateNotification(stringResId, destinationFileName, null); - } - }).execute(); - } else { + @Override + public void onFailure(@NonNull Call<ResponseBody> call, @NonNull Throwable t) { String videoFilePath = directoryPath + fileName + "-cache.mp4"; String destinationFileName = fileName + ".mp4"; new SaveTempMuxAndCopyAsyncTask(videoResponse.body(), @@ -161,29 +209,25 @@ public class DownloadRedditVideoService extends Service { } }).execute(); } - } - - @Override - public void onFailure(@NonNull Call<ResponseBody> call, @NonNull Throwable t) { - String videoFilePath = directoryPath + fileName + "-cache.mp4"; - String destinationFileName = fileName + ".mp4"; - new SaveTempMuxAndCopyAsyncTask(videoResponse.body(), - null, videoFilePath, null, null, - destinationFileName, getContentResolver(), - new SaveTempMuxAndCopyAsyncTask.SaveTempMuxAndCopyAsyncTaskListener() { - @Override - public void finished(Uri destinationFileUri, int errorCode) { - new File(videoFilePath).delete(); - downloadFinished(destinationFileUri, destinationFileName, errorCode); - } - - @Override - public void updateProgressNotification(int stringResId) { - updateNotification(stringResId, destinationFileName, null); - } - }).execute(); - } - }); + }); + } else { + String videoFilePath = directoryPath + fileName; + new SaveTempMuxAndCopyAsyncTask(videoResponse.body(), + null, videoFilePath, null, null, + fileName, getContentResolver(), + new SaveTempMuxAndCopyAsyncTask.SaveTempMuxAndCopyAsyncTaskListener() { + @Override + public void finished(Uri destinationFileUri, int errorCode) { + new File(videoFilePath).delete(); + downloadFinished(destinationFileUri, fileName, errorCode); + } + + @Override + public void updateProgressNotification(int stringResId) { + updateNotification(stringResId, fileName, null); + } + }).execute(); + } } else { downloadFinished(null, destinationFileName, ERROR_VIDEO_FILE_CANNOT_DOWNLOAD); } @@ -241,7 +285,12 @@ public class DownloadRedditVideoService extends Service { } private Notification createNotification(int stringResId, String fileName, PendingIntent pendingIntent) { - NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NotificationUtils.CHANNEL_ID_DOWNLOAD_REDDIT_VIDEO); + NotificationCompat.Builder builder; + if (isRedditVideo) { + builder = new NotificationCompat.Builder(this, NotificationUtils.CHANNEL_ID_DOWNLOAD_REDDIT_VIDEO); + } else { + builder = new NotificationCompat.Builder(this, NotificationUtils.CHANNEL_ID_DOWNLOAD_VIDEO); + } builder.setContentTitle(fileName).setContentText(getString(stringResId)); if (pendingIntent != null) { builder.setContentIntent(pendingIntent); @@ -254,8 +303,13 @@ public class DownloadRedditVideoService extends Service { private void updateNotification(int stringResId, String fileName, PendingIntent pendingIntent) { NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); if (notificationManager != null) { - notificationManager.notify(NotificationUtils.DOWNLOAD_REDDIT_VIDEO_NOTIFICATION_ID, - createNotification(stringResId, fileName, pendingIntent)); + if (isRedditVideo) { + notificationManager.notify(NotificationUtils.DOWNLOAD_REDDIT_VIDEO_NOTIFICATION_ID, + createNotification(stringResId, fileName, pendingIntent)); + } else { + notificationManager.notify(NotificationUtils.DOWNLOAD_VIDEO_NOTIFICATION_ID, + createNotification(stringResId, fileName, pendingIntent)); + } } } |