mirror of
https://codeberg.org/Bazsalanszky/Infinity-For-Lemmy.git
synced 2024-11-07 11:17:25 +01:00
Support download location in ViewVideoActivity. Remove MediaDownloader and MediaDownloaderImpl.
This commit is contained in:
parent
1ee48844d2
commit
567f5c5ce6
@ -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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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
|
||||||
|
@ -1,7 +0,0 @@
|
|||||||
package ml.docilealligator.infinityforreddit;
|
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
|
|
||||||
public interface MediaDownloader {
|
|
||||||
void download(String url, String fileName, Context ctx);
|
|
||||||
}
|
|
@ -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();*/
|
|
||||||
}
|
|
||||||
}
|
|
@ -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", "/");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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,60 +123,70 @@ 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, "");
|
||||||
updateNotification(R.string.downloading_reddit_video_audio_track, destinationFileName, null);
|
String destinationFileUriString;
|
||||||
downloadFile.downloadFile(audioUrl).enqueue(new Callback<ResponseBody>() {
|
boolean isDefaultDestination;
|
||||||
@Override
|
if (destinationFileDirectory.equals("")) {
|
||||||
public void onResponse(@NonNull Call<ResponseBody> call, @NonNull Response<ResponseBody> audioResponse) {
|
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
|
||||||
if (audioResponse.isSuccessful() && audioResponse.body() != null) {
|
File directory = getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
|
||||||
String videoFilePath = directoryPath + fileName + "-cache.mp4";
|
if (directory != null) {
|
||||||
String audioFilePath = directoryPath + fileName + "-cache.mp3";
|
String directoryPath = directory.getAbsolutePath() + "/Infinity/";
|
||||||
String outputFilePath = directoryPath + fileName + ".mp4";
|
File infinityDir = new File(directoryPath);
|
||||||
new SaveTempMuxAndCopyAsyncTask(videoResponse.body(),
|
if (!infinityDir.exists() && !infinityDir.mkdir()) {
|
||||||
audioResponse.body(), videoFilePath, audioFilePath, outputFilePath,
|
downloadFinished(null, destinationFileName, ERROR_CANNOT_GET_DESTINATION_DIRECTORY);
|
||||||
destinationFileName, getContentResolver(),
|
return;
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
updateNotification(R.string.downloading_reddit_video_audio_track, destinationFileName, null);
|
||||||
public void onFailure(@NonNull Call<ResponseBody> call, @NonNull Throwable t) {
|
downloadFile.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, destinationFileUriString, isDefaultDestination,
|
||||||
|
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 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) {
|
||||||
@ -209,25 +200,29 @@ 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
|
@Override
|
||||||
public void updateProgressNotification(int stringResId) {
|
public void onFailure(@NonNull Call<ResponseBody> call, @NonNull Throwable t) {
|
||||||
updateNotification(stringResId, fileName, null);
|
String videoFilePath = directoryPath + fileName + "-cache.mp4";
|
||||||
}
|
new SaveTempMuxAndCopyAsyncTask(videoResponse.body(),
|
||||||
}).execute();
|
null, videoFilePath, null, null,
|
||||||
}
|
destinationFileName, destinationFileUriString, isDefaultDestination,
|
||||||
|
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 {
|
} 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);
|
||||||
copyToDestination(outputFilePath);
|
try {
|
||||||
|
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);
|
||||||
copyToDestination(videoFilePath);
|
try {
|
||||||
|
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,101 +507,80 @@ public class DownloadRedditVideoService extends Service {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void copyToDestination(String srcPath) {
|
private void copyToDestination(String srcPath) throws IOException {
|
||||||
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
|
if (isDefaultDestination) {
|
||||||
if (!copy(new File(srcPath), destinationFileName)) {
|
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
|
||||||
errorCode = ERROR_MUXED_VIDEO_FILE_CANNOT_SAVE;
|
InputStream in = new FileInputStream(srcPath);
|
||||||
|
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 {
|
||||||
|
ContentValues contentValues = new ContentValues();
|
||||||
|
contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, destinationFileName);
|
||||||
|
contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "video/*");
|
||||||
|
contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, destinationFileUriString);
|
||||||
|
contentValues.put(MediaStore.Video.Media.IS_PENDING, 1);
|
||||||
|
|
||||||
|
OutputStream stream = null;
|
||||||
|
Uri uri = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
final Uri contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
|
||||||
|
uri = contentResolver.insert(contentUri, contentValues);
|
||||||
|
|
||||||
|
if (uri == null) {
|
||||||
|
throw new IOException("Failed to create new MediaStore record.");
|
||||||
|
}
|
||||||
|
|
||||||
|
stream = contentResolver.openOutputStream(uri);
|
||||||
|
|
||||||
|
if (stream == null) {
|
||||||
|
throw new IOException("Failed to get output stream.");
|
||||||
|
}
|
||||||
|
|
||||||
|
InputStream in = new FileInputStream(srcPath);
|
||||||
|
|
||||||
|
byte[] buf = new byte[1024];
|
||||||
|
int len;
|
||||||
|
while ((len = in.read(buf)) > 0) {
|
||||||
|
stream.write(buf, 0, len);
|
||||||
|
}
|
||||||
|
|
||||||
|
contentValues.clear();
|
||||||
|
contentValues.put(MediaStore.Video.Media.IS_PENDING, 0);
|
||||||
|
contentResolver.update(uri, contentValues, null, null);
|
||||||
|
destinationFileUriString = uri.toString();
|
||||||
|
} catch (IOException e) {
|
||||||
|
if (uri != null) {
|
||||||
|
// Don't leave an orphan entry in the MediaStore
|
||||||
|
contentResolver.delete(uri, null, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
throw e;
|
||||||
|
} finally {
|
||||||
|
if (stream != null) {
|
||||||
|
stream.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
try {
|
OutputStream stream = contentResolver.openOutputStream(Uri.parse(destinationFileUriString));
|
||||||
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.put(MediaStore.MediaColumns.DISPLAY_NAME, outputFileName);
|
|
||||||
contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "video/*");
|
|
||||||
contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, relativeLocation);
|
|
||||||
contentValues.put(MediaStore.Video.Media.IS_PENDING, 1);
|
|
||||||
|
|
||||||
OutputStream stream = null;
|
|
||||||
Uri uri = null;
|
|
||||||
|
|
||||||
try {
|
|
||||||
final Uri contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
|
|
||||||
uri = contentResolver.insert(contentUri, contentValues);
|
|
||||||
|
|
||||||
if (uri == null) {
|
|
||||||
throw new IOException("Failed to create new MediaStore record.");
|
|
||||||
}
|
|
||||||
|
|
||||||
stream = contentResolver.openOutputStream(uri);
|
|
||||||
|
|
||||||
if (stream == null) {
|
if (stream == null) {
|
||||||
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;
|
||||||
while ((len = in.read(buf)) > 0) {
|
while ((len = in.read(buf)) > 0) {
|
||||||
stream.write(buf, 0, len);
|
stream.write(buf, 0, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
contentValues.clear();
|
|
||||||
contentValues.put(MediaStore.Images.Media.IS_PENDING, 0);
|
|
||||||
contentResolver.update(uri, contentValues, null, null);
|
|
||||||
destinationFileUri = uri;
|
|
||||||
} catch (IOException e) {
|
|
||||||
if (uri != null) {
|
|
||||||
// Don't leave an orphan entry in the MediaStore
|
|
||||||
contentResolver.delete(uri, null, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
throw e;
|
|
||||||
} finally {
|
|
||||||
if (stream != null) {
|
|
||||||
stream.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean copy(File src, String outputFileName) {
|
|
||||||
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];
|
|
||||||
int len;
|
|
||||||
while ((len = in.read(buf)) > 0) {
|
|
||||||
out.write(buf, 0, len);
|
|
||||||
}
|
|
||||||
|
|
||||||
src.delete();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
src.delete();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
src.delete();
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user