Support download location in ViewVideoActivity. Remove MediaDownloader and MediaDownloaderImpl.

This commit is contained in:
Alex Ning 2020-09-06 22:10:20 +08:00
parent 1ee48844d2
commit 567f5c5ce6
6 changed files with 200 additions and 299 deletions

View File

@ -431,10 +431,10 @@ public class ViewImageOrGifActivity extends AppCompatActivity implements SetAsWa
if (requestCode == PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE && grantResults.length > 0) { if (requestCode == PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE && grantResults.length > 0) {
if (grantResults[0] == PackageManager.PERMISSION_DENIED) { if (grantResults[0] == PackageManager.PERMISSION_DENIED) {
Toast.makeText(this, R.string.no_storage_permission, Toast.LENGTH_SHORT).show(); Toast.makeText(this, R.string.no_storage_permission, Toast.LENGTH_SHORT).show();
isDownloading = false;
} else if (grantResults[0] == PackageManager.PERMISSION_GRANTED && isDownloading) { } else if (grantResults[0] == PackageManager.PERMISSION_GRANTED && isDownloading) {
download(); download();
} }
isDownloading = false;
} }
} }

View File

@ -59,9 +59,8 @@ import ml.docilealligator.infinityforreddit.Font.ContentFontFamily;
import ml.docilealligator.infinityforreddit.Font.FontFamily; import ml.docilealligator.infinityforreddit.Font.FontFamily;
import ml.docilealligator.infinityforreddit.Font.TitleFontFamily; import ml.docilealligator.infinityforreddit.Font.TitleFontFamily;
import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.Infinity;
import ml.docilealligator.infinityforreddit.MediaDownloader;
import ml.docilealligator.infinityforreddit.MediaDownloaderImpl;
import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.Service.DownloadMediaService;
import ml.docilealligator.infinityforreddit.Service.DownloadRedditVideoService; import ml.docilealligator.infinityforreddit.Service.DownloadRedditVideoService;
import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils;
import retrofit2.Retrofit; import retrofit2.Retrofit;
@ -98,7 +97,6 @@ public class ViewVideoActivity extends AppCompatActivity {
private Uri mVideoUri; private Uri mVideoUri;
private SimpleExoPlayer player; private SimpleExoPlayer player;
private DataSource.Factory dataSourceFactory; private DataSource.Factory dataSourceFactory;
private MediaDownloader mediaDownloader;
private String videoDownloadUrl; private String videoDownloadUrl;
private String videoFileName; private String videoFileName;
@ -173,8 +171,6 @@ public class ViewVideoActivity extends AppCompatActivity {
overridePendingTransition(0, slide); overridePendingTransition(0, slide);
}); });
mediaDownloader = new MediaDownloaderImpl();
Intent intent = getIntent(); Intent intent = getIntent();
mVideoUri = intent.getData(); mVideoUri = intent.getData();
postTitle = intent.getStringExtra(EXTRA_POST_TITLE); postTitle = intent.getStringExtra(EXTRA_POST_TITLE);
@ -438,21 +434,28 @@ public class ViewVideoActivity extends AppCompatActivity {
isDownloading = false; isDownloading = false;
if (videoType != VIDEO_TYPE_NORMAL) { if (videoType != VIDEO_TYPE_NORMAL) {
mediaDownloader.download(videoDownloadUrl, videoFileName, this); Intent intent = new Intent(this, DownloadMediaService.class);
intent.putExtra(DownloadMediaService.EXTRA_URL, videoDownloadUrl);
intent.putExtra(DownloadMediaService.EXTRA_MEDIA_TYPE, DownloadMediaService.EXTRA_MEDIA_TYPE_VIDEO);
intent.putExtra(DownloadMediaService.EXTRA_FILE_NAME, videoFileName);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(intent);
} else {
startService(intent);
}
} else { } else {
Intent intent = new Intent(this, DownloadRedditVideoService.class); Intent intent = new Intent(this, DownloadRedditVideoService.class);
intent.putExtra(DownloadRedditVideoService.EXTRA_VIDEO_URL, videoDownloadUrl); intent.putExtra(DownloadRedditVideoService.EXTRA_VIDEO_URL, videoDownloadUrl);
intent.putExtra(DownloadRedditVideoService.EXTRA_POST_ID, id); intent.putExtra(DownloadRedditVideoService.EXTRA_POST_ID, id);
intent.putExtra(DownloadRedditVideoService.EXTRA_SUBREDDIT, subredditName); intent.putExtra(DownloadRedditVideoService.EXTRA_SUBREDDIT, subredditName);
intent.putExtra(DownloadRedditVideoService.EXTRA_IS_REDDIT_VIDEO, true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(intent); startForegroundService(intent);
} else { } else {
startService(intent); startService(intent);
} }
Toast.makeText(this, R.string.download_started, Toast.LENGTH_SHORT).show();
} }
Toast.makeText(this, R.string.download_started, Toast.LENGTH_SHORT).show();
} }
@Override @Override

View File

@ -1,7 +0,0 @@
package ml.docilealligator.infinityforreddit;
import android.content.Context;
public interface MediaDownloader {
void download(String url, String fileName, Context ctx);
}

View File

@ -1,76 +0,0 @@
package ml.docilealligator.infinityforreddit;
import android.app.DownloadManager;
import android.content.Context;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.widget.Toast;
import androidx.documentfile.provider.DocumentFile;
import java.io.File;
public class MediaDownloaderImpl implements MediaDownloader {
@Override
public void download(String url, String fileName, Context ctx) {
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setTitle(fileName);
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
//Android Q support
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
//ctx.getContentResolver().takePersistableUriPermission(Uri.parse("content://com.android.providers.downloads.documents/tree/downloads"), Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
request.setDestinationInExternalPublicDir(DocumentFile.fromTreeUri(ctx, Uri.parse("content://com.android.providers.downloads.documents/tree/downloads")).toString(), fileName);
//request.setDestinationInExternalPublicDir(Environment.DIRECTORY_PICTURES, fileName);
//request.setDestinationUri(Uri.parse(Paths.get("content://com.android.providers.downloads.documents/tree/downloads", fileName).toString()));
} else {
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString();
File directory = new File(path + "/Infinity/");
boolean saveToInfinityFolder = true;
if (!directory.exists()) {
if (!directory.mkdir()) {
saveToInfinityFolder = false;
}
} else {
if (directory.isFile()) {
if (!(directory.delete() && directory.mkdir())) {
saveToInfinityFolder = false;
}
}
}
if (saveToInfinityFolder) {
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_PICTURES + "/Infinity/", fileName);
} else {
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_PICTURES, fileName);
}
}
DownloadManager manager = (DownloadManager) ctx.getSystemService(Context.DOWNLOAD_SERVICE);
if (manager == null) {
Toast.makeText(ctx, R.string.download_failed, Toast.LENGTH_SHORT).show();
return;
}
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

@ -407,7 +407,6 @@ public class DownloadMediaService extends Service {
while ((len = in.read(buf)) > 0) { while ((len = in.read(buf)) > 0) {
stream.write(buf, 0, len); stream.write(buf, 0, len);
} }
destinationFileUriString = destinationFileUriString.replaceAll("%3A", ":").replaceAll("%2F", "/");
} }
} }
} }

View File

@ -9,6 +9,7 @@ import android.content.ContentResolver;
import android.content.ContentValues; import android.content.ContentValues;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences;
import android.media.MediaCodec; import android.media.MediaCodec;
import android.media.MediaExtractor; import android.media.MediaExtractor;
import android.media.MediaFormat; import android.media.MediaFormat;
@ -22,9 +23,9 @@ import android.os.IBinder;
import android.provider.MediaStore; import android.provider.MediaStore;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat; import androidx.core.app.NotificationManagerCompat;
import androidx.documentfile.provider.DocumentFile;
import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.EventBus;
@ -45,6 +46,7 @@ import ml.docilealligator.infinityforreddit.Event.DownloadRedditVideoEvent;
import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.Infinity;
import ml.docilealligator.infinityforreddit.NotificationUtils; import ml.docilealligator.infinityforreddit.NotificationUtils;
import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils;
import okhttp3.ResponseBody; import okhttp3.ResponseBody;
import retrofit2.Call; import retrofit2.Call;
import retrofit2.Callback; import retrofit2.Callback;
@ -58,8 +60,6 @@ public class DownloadRedditVideoService extends Service {
public static final String EXTRA_VIDEO_URL = "EVU"; public static final String EXTRA_VIDEO_URL = "EVU";
public static final String EXTRA_SUBREDDIT = "ES"; public static final String EXTRA_SUBREDDIT = "ES";
public static final String EXTRA_POST_ID = "EPI"; 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 NO_ERROR = -1;
private static final int ERROR_CANNOT_GET_CACHE_DIRECTORY = 0; private static final int ERROR_CANNOT_GET_CACHE_DIRECTORY = 0;
@ -68,14 +68,16 @@ public class DownloadRedditVideoService extends Service {
private static final int ERROR_AUDIO_FILE_CANNOT_SAVE = 3; private static final int ERROR_AUDIO_FILE_CANNOT_SAVE = 3;
private static final int ERROR_MUX_FAILED = 4; private static final int ERROR_MUX_FAILED = 4;
private static final int ERROR_MUXED_VIDEO_FILE_CANNOT_SAVE = 5; private static final int ERROR_MUXED_VIDEO_FILE_CANNOT_SAVE = 5;
private static final int ERROR_CANNOT_GET_DESTINATION_DIRECTORY = 6;
private boolean isRedditVideo;
@Inject @Inject
@Named("download_media") @Named("download_media")
Retrofit retrofit; Retrofit retrofit;
@Inject @Inject
CustomThemeWrapper mCustomThemeWrapper; @Named("default")
SharedPreferences sharedPreferences;
@Inject
CustomThemeWrapper customThemeWrapper;
String resultFile; String resultFile;
public DownloadRedditVideoService() { public DownloadRedditVideoService() {
@ -90,47 +92,26 @@ public class DownloadRedditVideoService extends Service {
public int onStartCommand(Intent intent, int flags, int startId) { public int onStartCommand(Intent intent, int flags, int startId) {
((Infinity) getApplication()).getAppComponent().inject(this); ((Infinity) getApplication()).getAppComponent().inject(this);
isRedditVideo = intent.getBooleanExtra(EXTRA_IS_REDDIT_VIDEO, false);
String videoUrl = intent.getStringExtra(EXTRA_VIDEO_URL); String videoUrl = intent.getStringExtra(EXTRA_VIDEO_URL);
String audioUrl = isRedditVideo ? videoUrl.substring(0, videoUrl.lastIndexOf('/')) + "/DASH_audio.mp4" : ""; String audioUrl = videoUrl.substring(0, videoUrl.lastIndexOf('/')) + "/DASH_audio.mp4";
String fileName; String fileName = intent.getStringExtra(EXTRA_SUBREDDIT) + "-" + intent.getStringExtra(EXTRA_POST_ID);
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) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel serviceChannel; NotificationChannel serviceChannel;
if (isRedditVideo) {
serviceChannel = new NotificationChannel( serviceChannel = new NotificationChannel(
NotificationUtils.CHANNEL_ID_DOWNLOAD_REDDIT_VIDEO, NotificationUtils.CHANNEL_ID_DOWNLOAD_REDDIT_VIDEO,
NotificationUtils.CHANNEL_DOWNLOAD_REDDIT_VIDEO, NotificationUtils.CHANNEL_DOWNLOAD_REDDIT_VIDEO,
NotificationManager.IMPORTANCE_LOW NotificationManager.IMPORTANCE_LOW
); );
} else {
serviceChannel = new NotificationChannel(
NotificationUtils.CHANNEL_ID_DOWNLOAD_VIDEO,
NotificationUtils.CHANNEL_DOWNLOAD_VIDEO,
NotificationManager.IMPORTANCE_LOW
);
}
NotificationManagerCompat manager = NotificationManagerCompat.from(this); NotificationManagerCompat manager = NotificationManagerCompat.from(this);
manager.createNotificationChannel(serviceChannel); manager.createNotificationChannel(serviceChannel);
} }
if (isRedditVideo) {
startForeground( startForeground(
NotificationUtils.DOWNLOAD_REDDIT_VIDEO_NOTIFICATION_ID, NotificationUtils.DOWNLOAD_REDDIT_VIDEO_NOTIFICATION_ID,
createNotification(R.string.downloading_reddit_video, fileName + ".mp4", null) createNotification(R.string.downloading_reddit_video, fileName + ".mp4", null)
); );
} else {
startForeground(
NotificationUtils.DOWNLOAD_VIDEO_NOTIFICATION_ID,
createNotification(R.string.downloading_video, fileName, null)
);
}
DownloadFile downloadFile = retrofit.create(DownloadFile.class); DownloadFile downloadFile = retrofit.create(DownloadFile.class);
@ -142,7 +123,38 @@ public class DownloadRedditVideoService extends Service {
@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) {
if (isRedditVideo) { String destinationFileDirectory = sharedPreferences.getString(SharedPreferencesUtils.VIDEO_DOWNLOAD_LOCATION, "");
String destinationFileUriString;
boolean isDefaultDestination;
if (destinationFileDirectory.equals("")) {
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
File directory = getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
if (directory != null) {
String directoryPath = directory.getAbsolutePath() + "/Infinity/";
File infinityDir = new File(directoryPath);
if (!infinityDir.exists() && !infinityDir.mkdir()) {
downloadFinished(null, destinationFileName, ERROR_CANNOT_GET_DESTINATION_DIRECTORY);
return;
}
destinationFileUriString = directoryPath + destinationFileName;
} else {
downloadFinished(null, destinationFileName, ERROR_CANNOT_GET_DESTINATION_DIRECTORY);
return;
}
} else {
destinationFileUriString = Environment.DIRECTORY_PICTURES + "/Infinity/";
}
isDefaultDestination = true;
} else {
isDefaultDestination = false;
DocumentFile picFile = DocumentFile.fromTreeUri(DownloadRedditVideoService.this, Uri.parse(destinationFileDirectory)).createFile("video/*", destinationFileName);
if (picFile == null) {
downloadFinished(null, destinationFileName, ERROR_CANNOT_GET_DESTINATION_DIRECTORY);
return;
}
destinationFileUriString = picFile.getUri().toString();
}
updateNotification(R.string.downloading_reddit_video_audio_track, destinationFileName, null); updateNotification(R.string.downloading_reddit_video_audio_track, destinationFileName, null);
downloadFile.downloadFile(audioUrl).enqueue(new Callback<ResponseBody>() { downloadFile.downloadFile(audioUrl).enqueue(new Callback<ResponseBody>() {
@Override @Override
@ -153,7 +165,8 @@ public class DownloadRedditVideoService extends Service {
String outputFilePath = directoryPath + fileName + ".mp4"; String outputFilePath = directoryPath + fileName + ".mp4";
new SaveTempMuxAndCopyAsyncTask(videoResponse.body(), new SaveTempMuxAndCopyAsyncTask(videoResponse.body(),
audioResponse.body(), videoFilePath, audioFilePath, outputFilePath, audioResponse.body(), videoFilePath, audioFilePath, outputFilePath,
destinationFileName, getContentResolver(), destinationFileName, destinationFileUriString, isDefaultDestination,
getContentResolver(),
new SaveTempMuxAndCopyAsyncTask.SaveTempMuxAndCopyAsyncTaskListener() { new SaveTempMuxAndCopyAsyncTask.SaveTempMuxAndCopyAsyncTaskListener() {
@Override @Override
public void finished(Uri destinationFileUri, int errorCode) { public void finished(Uri destinationFileUri, int errorCode) {
@ -170,10 +183,10 @@ public class DownloadRedditVideoService extends Service {
}).execute(); }).execute();
} else { } else {
String videoFilePath = directoryPath + fileName + "-cache.mp4"; String videoFilePath = directoryPath + fileName + "-cache.mp4";
String destinationFileName = fileName + ".mp4";
new SaveTempMuxAndCopyAsyncTask(videoResponse.body(), new SaveTempMuxAndCopyAsyncTask(videoResponse.body(),
null, videoFilePath, null, null, null, videoFilePath, null, null,
destinationFileName, getContentResolver(), destinationFileName, destinationFileUriString, isDefaultDestination,
getContentResolver(),
new SaveTempMuxAndCopyAsyncTask.SaveTempMuxAndCopyAsyncTaskListener() { new SaveTempMuxAndCopyAsyncTask.SaveTempMuxAndCopyAsyncTaskListener() {
@Override @Override
public void finished(Uri destinationFileUri, int errorCode) { public void finished(Uri destinationFileUri, int errorCode) {
@ -192,10 +205,10 @@ public class DownloadRedditVideoService extends Service {
@Override @Override
public void onFailure(@NonNull Call<ResponseBody> call, @NonNull Throwable t) { public void onFailure(@NonNull Call<ResponseBody> call, @NonNull Throwable t) {
String videoFilePath = directoryPath + fileName + "-cache.mp4"; String videoFilePath = directoryPath + fileName + "-cache.mp4";
String destinationFileName = fileName + ".mp4";
new SaveTempMuxAndCopyAsyncTask(videoResponse.body(), new SaveTempMuxAndCopyAsyncTask(videoResponse.body(),
null, videoFilePath, null, null, null, videoFilePath, null, null,
destinationFileName, getContentResolver(), destinationFileName, destinationFileUriString, isDefaultDestination,
getContentResolver(),
new SaveTempMuxAndCopyAsyncTask.SaveTempMuxAndCopyAsyncTaskListener() { new SaveTempMuxAndCopyAsyncTask.SaveTempMuxAndCopyAsyncTaskListener() {
@Override @Override
public void finished(Uri destinationFileUri, int errorCode) { public void finished(Uri destinationFileUri, int errorCode) {
@ -210,24 +223,6 @@ public class DownloadRedditVideoService extends Service {
}).execute(); }).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 { } else {
downloadFinished(null, destinationFileName, ERROR_VIDEO_FILE_CANNOT_DOWNLOAD); downloadFinished(null, destinationFileName, ERROR_VIDEO_FILE_CANNOT_DOWNLOAD);
} }
@ -266,6 +261,9 @@ public class DownloadRedditVideoService extends Service {
case ERROR_MUXED_VIDEO_FILE_CANNOT_SAVE: case ERROR_MUXED_VIDEO_FILE_CANNOT_SAVE:
updateNotification(R.string.downloading_reddit_video_failed_cannot_save_mux_video, fileName, null); updateNotification(R.string.downloading_reddit_video_failed_cannot_save_mux_video, fileName, null);
break; break;
case ERROR_CANNOT_GET_DESTINATION_DIRECTORY:
updateNotification(R.string.downloading_media_failed_cannot_save_to_destination_directory, fileName, null);
break;
} }
EventBus.getDefault().post(new DownloadRedditVideoEvent(false)); EventBus.getDefault().post(new DownloadRedditVideoEvent(false));
} else { } else {
@ -285,31 +283,21 @@ public class DownloadRedditVideoService extends Service {
} }
private Notification createNotification(int stringResId, String fileName, PendingIntent pendingIntent) { private Notification createNotification(int stringResId, String fileName, PendingIntent pendingIntent) {
NotificationCompat.Builder builder; NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NotificationUtils.CHANNEL_ID_DOWNLOAD_REDDIT_VIDEO);
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)); builder.setContentTitle(fileName).setContentText(getString(stringResId));
if (pendingIntent != null) { if (pendingIntent != null) {
builder.setContentIntent(pendingIntent); builder.setContentIntent(pendingIntent);
} }
return builder.setSmallIcon(R.drawable.ic_notification) return builder.setSmallIcon(R.drawable.ic_notification)
.setColor(mCustomThemeWrapper.getColorPrimaryLightTheme()) .setColor(customThemeWrapper.getColorPrimaryLightTheme())
.build(); .build();
} }
private void updateNotification(int stringResId, String fileName, PendingIntent pendingIntent) { private void updateNotification(int stringResId, String fileName, PendingIntent pendingIntent) {
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (notificationManager != null) { if (notificationManager != null) {
if (isRedditVideo) {
notificationManager.notify(NotificationUtils.DOWNLOAD_REDDIT_VIDEO_NOTIFICATION_ID, notificationManager.notify(NotificationUtils.DOWNLOAD_REDDIT_VIDEO_NOTIFICATION_ID,
createNotification(stringResId, fileName, pendingIntent)); createNotification(stringResId, fileName, pendingIntent));
} else {
notificationManager.notify(NotificationUtils.DOWNLOAD_VIDEO_NOTIFICATION_ID,
createNotification(stringResId, fileName, pendingIntent));
}
} }
} }
@ -321,14 +309,17 @@ public class DownloadRedditVideoService extends Service {
private String audioFilePath; private String audioFilePath;
private String outputFilePath; private String outputFilePath;
private String destinationFileName; private String destinationFileName;
@NonNull
private String destinationFileUriString;
private boolean isDefaultDestination;
private ContentResolver contentResolver; private ContentResolver contentResolver;
private SaveTempMuxAndCopyAsyncTaskListener saveTempMuxAndCopyAsyncTaskListener; private SaveTempMuxAndCopyAsyncTaskListener saveTempMuxAndCopyAsyncTaskListener;
private Uri destinationFileUri;
private int errorCode = NO_ERROR; private int errorCode = NO_ERROR;
public SaveTempMuxAndCopyAsyncTask(ResponseBody videoResponse, ResponseBody audioResponse, public SaveTempMuxAndCopyAsyncTask(ResponseBody videoResponse, ResponseBody audioResponse,
String videoFilePath, String audioFilePath, String outputFilePath, String videoFilePath, String audioFilePath, String outputFilePath,
String destinationFileName, ContentResolver contentResolver, String destinationFileName, @NonNull String destinationFileUriString,
boolean isDefaultDestination, ContentResolver contentResolver,
SaveTempMuxAndCopyAsyncTaskListener saveTempMuxAndCopyAsyncTaskListener) { SaveTempMuxAndCopyAsyncTaskListener saveTempMuxAndCopyAsyncTaskListener) {
this.videoResponse = videoResponse; this.videoResponse = videoResponse;
this.audioResponse = audioResponse; this.audioResponse = audioResponse;
@ -336,6 +327,8 @@ public class DownloadRedditVideoService extends Service {
this.audioFilePath = audioFilePath; this.audioFilePath = audioFilePath;
this.outputFilePath = outputFilePath; this.outputFilePath = outputFilePath;
this.destinationFileName = destinationFileName; this.destinationFileName = destinationFileName;
this.destinationFileUriString = destinationFileUriString;
this.isDefaultDestination = isDefaultDestination;
this.contentResolver = contentResolver; this.contentResolver = contentResolver;
this.saveTempMuxAndCopyAsyncTaskListener = saveTempMuxAndCopyAsyncTaskListener; this.saveTempMuxAndCopyAsyncTaskListener = saveTempMuxAndCopyAsyncTaskListener;
} }
@ -369,10 +362,20 @@ public class DownloadRedditVideoService extends Service {
} }
publishProgress(R.string.downloading_reddit_video_save_file_to_public_dir); publishProgress(R.string.downloading_reddit_video_save_file_to_public_dir);
try {
copyToDestination(outputFilePath); copyToDestination(outputFilePath);
} catch (IOException e) {
e.printStackTrace();
errorCode = ERROR_MUXED_VIDEO_FILE_CANNOT_SAVE;
}
} else { } else {
publishProgress(R.string.downloading_reddit_video_save_file_to_public_dir); publishProgress(R.string.downloading_reddit_video_save_file_to_public_dir);
try {
copyToDestination(videoFilePath); copyToDestination(videoFilePath);
} catch (IOException e) {
e.printStackTrace();
errorCode = ERROR_MUXED_VIDEO_FILE_CANNOT_SAVE;
}
} }
return null; return null;
} }
@ -380,7 +383,7 @@ public class DownloadRedditVideoService extends Service {
@Override @Override
protected void onPostExecute(Void aVoid) { protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid); super.onPostExecute(aVoid);
saveTempMuxAndCopyAsyncTaskListener.finished(destinationFileUri, errorCode); saveTempMuxAndCopyAsyncTaskListener.finished(Uri.parse(destinationFileUriString), errorCode);
} }
private String writeResponseBodyToDisk(ResponseBody body, String filePath) { private String writeResponseBodyToDisk(ResponseBody body, String filePath) {
@ -504,30 +507,23 @@ public class DownloadRedditVideoService extends Service {
return true; return true;
} }
private void copyToDestination(String srcPath) { private void copyToDestination(String srcPath) throws IOException {
if (isDefaultDestination) {
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) { if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
if (!copy(new File(srcPath), destinationFileName)) { InputStream in = new FileInputStream(srcPath);
errorCode = ERROR_MUXED_VIDEO_FILE_CANNOT_SAVE; OutputStream out = new FileOutputStream(destinationFileUriString);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
} }
new File(srcPath).delete();
} else { } else {
try {
copyFileQ(new File(srcPath), destinationFileName);
} catch (IOException e) {
e.printStackTrace();
errorCode = ERROR_MUXED_VIDEO_FILE_CANNOT_SAVE;
}
}
}
@RequiresApi(api = Build.VERSION_CODES.Q)
private void copyFileQ(File src, String outputFileName) throws IOException {
String relativeLocation = Environment.DIRECTORY_MOVIES + "/Infinity/";
ContentValues contentValues = new ContentValues(); ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, outputFileName); contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, destinationFileName);
contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "video/*"); contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "video/*");
contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, relativeLocation); contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, destinationFileUriString);
contentValues.put(MediaStore.Video.Media.IS_PENDING, 1); contentValues.put(MediaStore.Video.Media.IS_PENDING, 1);
OutputStream stream = null; OutputStream stream = null;
@ -547,7 +543,7 @@ public class DownloadRedditVideoService extends Service {
throw new IOException("Failed to get output stream."); throw new IOException("Failed to get output stream.");
} }
InputStream in = new FileInputStream(src); InputStream in = new FileInputStream(srcPath);
byte[] buf = new byte[1024]; byte[] buf = new byte[1024];
int len; int len;
@ -556,9 +552,9 @@ public class DownloadRedditVideoService extends Service {
} }
contentValues.clear(); contentValues.clear();
contentValues.put(MediaStore.Images.Media.IS_PENDING, 0); contentValues.put(MediaStore.Video.Media.IS_PENDING, 0);
contentResolver.update(uri, contentValues, null, null); contentResolver.update(uri, contentValues, null, null);
destinationFileUri = uri; destinationFileUriString = uri.toString();
} catch (IOException e) { } catch (IOException e) {
if (uri != null) { if (uri != null) {
// Don't leave an orphan entry in the MediaStore // Don't leave an orphan entry in the MediaStore
@ -572,33 +568,19 @@ public class DownloadRedditVideoService extends Service {
} }
} }
} }
} else {
OutputStream stream = contentResolver.openOutputStream(Uri.parse(destinationFileUriString));
if (stream == null) {
throw new IOException("Failed to get output stream.");
}
private boolean copy(File src, String outputFileName) { InputStream in = new FileInputStream(srcPath);
File directory = getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
if (directory != null) {
String directoryPath = directory.getAbsolutePath() + "/Infinity/";
String destinationFilePath = directoryPath + outputFileName;
destinationFileUri = Uri.parse(destinationFilePath);
try (InputStream in = new FileInputStream(src)) {
try (OutputStream out = new FileOutputStream(destinationFilePath)) {
byte[] buf = new byte[1024]; byte[] buf = new byte[1024];
int len; int len;
while ((len = in.read(buf)) > 0) { while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len); stream.write(buf, 0, len);
} }
src.delete();
return true;
}
} catch (IOException e) {
e.printStackTrace();
src.delete();
return false;
}
} else {
src.delete();
return false;
} }
} }