Use DownloadVideoService to download videos.

This commit is contained in:
Alex Ning 2020-08-09 10:41:24 +08:00
parent 74a4206cd2
commit 4b4785171c
8 changed files with 153 additions and 86 deletions

View File

@ -35,7 +35,7 @@
android:windowSoftInputMode="adjustResize" />
<service
android:name=".Service.DownloadRedditVideoService"
android:name=".Service.DownloadVideoService"
android:enabled="true"
android:exported="false" />

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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();
}
}

View File

@ -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;

View File

@ -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);
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)
);
}
startForeground(
NotificationUtils.DOWNLOAD_REDDIT_VIDEO_NOTIFICATION_ID,
createNotification(R.string.downloading_reddit_video, fileName + ".mp4", 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 {
@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 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();
}
}
});
} 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 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();
}
});
@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));
}
}
}

View File

@ -1,5 +1,5 @@
<resources>
<string name="application_name">Infinity</string>
<string name="application_name" translatable="false">Infinity</string>
<string name="login_activity_label">Login</string>
<string name="search_activity_label">Search</string>
<string name="comment_activity_label">Send Comment</string>
@ -125,9 +125,6 @@
<string name="cannot_fetch_sidebar">Cannot fetch sidebar</string>
<string name="cannot_fetch_multireddit">Cannot fetch multireddit info</string>
<string name="gilded_count">x%1$d</string>
<string name="title_activity_view_user_detail">ViewUserDetailActivity</string>
<string name="subscribe">Subscribe</string>
<string name="unsubscribe">Unsubscribe</string>
<string name="subscribed">Subscribed</string>"
@ -765,7 +762,7 @@
<string name="parse_theme_failed">Parse Theme Failed</string>
<string name="share_theme_to_subreddit_dialog_title">Share it to r/Infinity_For_Reddit?</string>
<string name="share_theme_to_subreddit_dialog_message">The theme configuration is copied!
Do you want to share it to r/Infinity so that other people can use it?</string>
Do you want to share it to r/Infinity_For_Reddit so that other people can use it?</string>
<string name="duplicate_theme_name_dialog_title">Duplicate Theme Found</string>
<string name="duplicate_theme_name_dialog_message">A theme in the database is also called %1$s. Do you want to change the name of this imported theme?</string>
<string name="rename">Rename</string>
@ -837,6 +834,7 @@
<string name="downloading_reddit_video_failed_cannot_save_audio">Download failed: cannot save audio to cache directory</string>
<string name="downloading_reddit_video_failed_cannot_mux">Download failed: cannot mux video and audio</string>
<string name="downloading_reddit_video_failed_cannot_save_mux_video">Download failed: cannot save the video to public directory</string>
<string name="downloading_video">Downloading Video</string>
<string name="wallpaper_set">Wallpaper set</string>
<string name="error_set_wallpaper">Cannot set wallpaper</string>