Refactor media download feature extracting to dedicated class.

This commit is contained in:
Hermes Junior 2020-06-15 22:34:34 +02:00 committed by OHermesJunior
parent d428baaa7b
commit a0d6c220c5
6 changed files with 94 additions and 190 deletions

View File

@ -1,7 +1,6 @@
package ml.docilealligator.infinityforreddit.Activity; package ml.docilealligator.infinityforreddit.Activity;
import android.Manifest; import android.Manifest;
import android.app.DownloadManager;
import android.app.WallpaperManager; import android.app.WallpaperManager;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
@ -15,7 +14,6 @@ import android.media.ThumbnailUtils;
import android.net.Uri; import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Environment;
import android.text.Html; import android.text.Html;
import android.util.DisplayMetrics; import android.util.DisplayMetrics;
import android.view.Menu; import android.view.Menu;
@ -65,6 +63,8 @@ import ml.docilealligator.infinityforreddit.Font.FontStyle;
import ml.docilealligator.infinityforreddit.Font.TitleFontFamily; import ml.docilealligator.infinityforreddit.Font.TitleFontFamily;
import ml.docilealligator.infinityforreddit.Font.TitleFontStyle; import ml.docilealligator.infinityforreddit.Font.TitleFontStyle;
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.SetAsWallpaperCallback; import ml.docilealligator.infinityforreddit.SetAsWallpaperCallback;
import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils;
@ -90,6 +90,7 @@ public class ViewImageOrGifActivity extends AppCompatActivity implements SetAsWa
@Inject @Inject
@Named("default") @Named("default")
SharedPreferences mSharedPreferences; SharedPreferences mSharedPreferences;
private MediaDownloader mediaDownloader;
private boolean isActionBarHidden = false; private boolean isActionBarHidden = false;
private boolean isDownloading = false; private boolean isDownloading = false;
private RequestManager glide; private RequestManager glide;
@ -134,6 +135,8 @@ public class ViewImageOrGifActivity extends AppCompatActivity implements SetAsWa
mHaulerView.setOnDragDismissedListener(dragDirection -> finish()); mHaulerView.setOnDragDismissedListener(dragDirection -> finish());
mediaDownloader = new MediaDownloaderImpl();
glide = Glide.with(this); glide = Glide.with(this);
Intent intent = getIntent(); Intent intent = getIntent();
@ -231,10 +234,10 @@ public class ViewImageOrGifActivity extends AppCompatActivity implements SetAsWa
PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE); PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE);
} else { } else {
// Permission has already been granted // Permission has already been granted
download(mImageUrl, mImageFileName); mediaDownloader.download(mImageUrl, mImageFileName, this);
} }
} else { } else {
download(mImageUrl, mImageFileName); mediaDownloader.download(mImageUrl, mImageFileName, this);
} }
return true; return true;
@ -345,56 +348,12 @@ public class ViewImageOrGifActivity extends AppCompatActivity implements SetAsWa
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();
} else if (grantResults[0] == PackageManager.PERMISSION_GRANTED && isDownloading) { } else if (grantResults[0] == PackageManager.PERMISSION_GRANTED && isDownloading) {
download(mImageUrl, mImageFileName); mediaDownloader.download(mImageUrl, mImageFileName, this);
} }
isDownloading = false; isDownloading = false;
} }
} }
void download(String mImageUrl, String mImageFileName) {
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(mImageUrl));
request.setTitle(mImageFileName);
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
//Android Q support
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_PICTURES, mImageFileName);
} 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/", mImageFileName);
} else {
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_PICTURES, mImageFileName);
}
}
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
if (manager == null) {
Toast.makeText(this, R.string.download_failed, Toast.LENGTH_SHORT).show();
return;
}
manager.enqueue(request);
Toast.makeText(this, R.string.download_started, Toast.LENGTH_SHORT).show();
}
private void setAsWallpaper(int setTo) { private void setAsWallpaper(int setTo) {
Toast.makeText(ViewImageOrGifActivity.this, R.string.save_image_first, Toast.LENGTH_SHORT).show(); Toast.makeText(ViewImageOrGifActivity.this, R.string.save_image_first, Toast.LENGTH_SHORT).show();
glide.asBitmap().load(mImageUrl).into(new CustomTarget<Bitmap>() { glide.asBitmap().load(mImageUrl).into(new CustomTarget<Bitmap>() {

View File

@ -1,8 +1,6 @@
package ml.docilealligator.infinityforreddit.Activity; package ml.docilealligator.infinityforreddit.Activity;
import android.Manifest; import android.Manifest;
import android.app.DownloadManager;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
@ -13,7 +11,6 @@ import android.media.AudioManager;
import android.net.Uri; import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Environment;
import android.text.Html; import android.text.Html;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
@ -49,8 +46,6 @@ import com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory;
import com.google.android.exoplayer2.util.Util; import com.google.android.exoplayer2.util.Util;
import com.thefuntasty.hauler.HaulerView; import com.thefuntasty.hauler.HaulerView;
import java.io.File;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named; import javax.inject.Named;
@ -61,6 +56,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.DownloadRedditVideoService; import ml.docilealligator.infinityforreddit.Service.DownloadRedditVideoService;
import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils;
@ -95,6 +92,7 @@ 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;
@ -162,6 +160,8 @@ public class ViewVideoActivity extends AppCompatActivity {
haulerView.setOnDragDismissedListener(dragDirection -> finish()); haulerView.setOnDragDismissedListener(dragDirection -> finish());
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);
@ -407,46 +407,7 @@ public class ViewVideoActivity extends AppCompatActivity {
isDownloading = false; isDownloading = false;
if (videoType != VIDEO_TYPE_NORMAL) { if (videoType != VIDEO_TYPE_NORMAL) {
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(videoDownloadUrl)); mediaDownloader.download(videoDownloadUrl, videoFileName, this);
request.setTitle(videoFileName);
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
//Android Q support
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_PICTURES, videoFileName);
} 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/", videoFileName);
} else {
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_PICTURES, videoFileName);
}
}
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
if (manager == null) {
Toast.makeText(this, R.string.download_failed, Toast.LENGTH_SHORT).show();
return;
}
manager.enqueue(request);
} 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);
@ -458,9 +419,9 @@ public class ViewVideoActivity extends AppCompatActivity {
} 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
protected void onSaveInstanceState(@NonNull Bundle outState) { protected void onSaveInstanceState(@NonNull Bundle outState) {

View File

@ -1,7 +1,6 @@
package ml.docilealligator.infinityforreddit.Fragment; package ml.docilealligator.infinityforreddit.Fragment;
import android.Manifest; import android.Manifest;
import android.app.DownloadManager;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
@ -10,7 +9,6 @@ import android.graphics.drawable.Drawable;
import android.net.Uri; import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.Menu; import android.view.Menu;
import android.view.MenuInflater; import android.view.MenuInflater;
@ -47,6 +45,8 @@ import ml.docilealligator.infinityforreddit.AsyncTask.SaveImageToFileAsyncTask;
import ml.docilealligator.infinityforreddit.BottomSheetFragment.SetAsWallpaperBottomSheetFragment; import ml.docilealligator.infinityforreddit.BottomSheetFragment.SetAsWallpaperBottomSheetFragment;
import ml.docilealligator.infinityforreddit.BuildConfig; import ml.docilealligator.infinityforreddit.BuildConfig;
import ml.docilealligator.infinityforreddit.ImgurMedia; import ml.docilealligator.infinityforreddit.ImgurMedia;
import ml.docilealligator.infinityforreddit.MediaDownloader;
import ml.docilealligator.infinityforreddit.MediaDownloaderImpl;
import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.R;
public class ViewImgurImageFragment extends Fragment { public class ViewImgurImageFragment extends Fragment {
@ -63,6 +63,7 @@ public class ViewImgurImageFragment extends Fragment {
private ViewImgurMediaActivity activity; private ViewImgurMediaActivity activity;
private RequestManager glide; private RequestManager glide;
private MediaDownloader mediaDownloader;
private ImgurMedia imgurMedia; private ImgurMedia imgurMedia;
private boolean isDownloading = false; private boolean isDownloading = false;
@ -81,6 +82,7 @@ public class ViewImgurImageFragment extends Fragment {
imgurMedia = getArguments().getParcelable(EXTRA_IMGUR_IMAGES); imgurMedia = getArguments().getParcelable(EXTRA_IMGUR_IMAGES);
glide = Glide.with(activity); glide = Glide.with(activity);
mediaDownloader = new MediaDownloaderImpl();
loadImage(); loadImage();
return rootView; return rootView;
@ -131,10 +133,10 @@ public class ViewImgurImageFragment extends Fragment {
PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE); PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE);
} else { } else {
// Permission has already been granted // Permission has already been granted
download(); mediaDownloader.download(imgurMedia.getLink(), imgurMedia.getFileName(), getContext());
} }
} else { } else {
download(); mediaDownloader.download(imgurMedia.getLink(), imgurMedia.getFileName(), getContext());
} }
return true; return true;
@ -151,7 +153,7 @@ public class ViewImgurImageFragment extends Fragment {
@Override @Override
public void saveSuccess(File imageFile) { public void saveSuccess(File imageFile) {
Uri uri = FileProvider.getUriForFile(activity, Uri uri = FileProvider.getUriForFile(activity,
BuildConfig.APPLICATION_ID + ".provider",imageFile); BuildConfig.APPLICATION_ID + ".provider", imageFile);
Intent shareIntent = new Intent(); Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND); shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri); shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
@ -200,56 +202,12 @@ public class ViewImgurImageFragment extends Fragment {
if (grantResults[0] == PackageManager.PERMISSION_DENIED) { if (grantResults[0] == PackageManager.PERMISSION_DENIED) {
Toast.makeText(activity, R.string.no_storage_permission, Toast.LENGTH_SHORT).show(); Toast.makeText(activity, R.string.no_storage_permission, Toast.LENGTH_SHORT).show();
} else if (grantResults[0] == PackageManager.PERMISSION_GRANTED && isDownloading) { } else if (grantResults[0] == PackageManager.PERMISSION_GRANTED && isDownloading) {
download(); mediaDownloader.download(imgurMedia.getLink(), imgurMedia.getFileName(), getContext());
} }
isDownloading = false; isDownloading = false;
} }
} }
private void download() {
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(imgurMedia.getLink()));
request.setTitle(imgurMedia.getFileName());
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
//Android Q support
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_PICTURES, imgurMedia.getFileName());
} 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/", imgurMedia.getFileName());
} else {
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_PICTURES, imgurMedia.getFileName());
}
}
DownloadManager manager = (DownloadManager) activity.getSystemService(Context.DOWNLOAD_SERVICE);
if (manager == null) {
Toast.makeText(activity, R.string.download_failed, Toast.LENGTH_SHORT).show();
return;
}
manager.enqueue(request);
Toast.makeText(activity, R.string.download_started, Toast.LENGTH_SHORT).show();
}
@Override @Override
public void onAttach(@NonNull Context context) { public void onAttach(@NonNull Context context) {
super.onAttach(context); super.onAttach(context);

View File

@ -2,7 +2,6 @@ package ml.docilealligator.infinityforreddit.Fragment;
import android.Manifest; import android.Manifest;
import android.app.Activity; import android.app.Activity;
import android.app.DownloadManager;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
@ -11,7 +10,6 @@ import android.media.AudioManager;
import android.net.Uri; import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.Menu; import android.view.Menu;
import android.view.MenuInflater; import android.view.MenuInflater;
@ -42,8 +40,6 @@ import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory; import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
import com.google.android.exoplayer2.util.Util; import com.google.android.exoplayer2.util.Util;
import java.io.File;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named; import javax.inject.Named;
@ -51,6 +47,8 @@ import butterknife.BindView;
import butterknife.ButterKnife; import butterknife.ButterKnife;
import ml.docilealligator.infinityforreddit.ImgurMedia; import ml.docilealligator.infinityforreddit.ImgurMedia;
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.Utils.SharedPreferencesUtils; import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils;
@ -71,6 +69,7 @@ public class ViewImgurVideoFragment extends Fragment {
private ImgurMedia imgurMedia; private ImgurMedia imgurMedia;
private SimpleExoPlayer player; private SimpleExoPlayer player;
private DataSource.Factory dataSourceFactory; private DataSource.Factory dataSourceFactory;
private MediaDownloader mediaDownloader;
private boolean wasPlaying = false; private boolean wasPlaying = false;
private boolean isMute = false; private boolean isMute = false;
private boolean isDownloading = false; private boolean isDownloading = false;
@ -131,6 +130,8 @@ public class ViewImgurVideoFragment extends Fragment {
} }
}); });
mediaDownloader = new MediaDownloaderImpl();
TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(); TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory();
TrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory); TrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);
player = ExoPlayerFactory.newSimpleInstance(activity, trackSelector); player = ExoPlayerFactory.newSimpleInstance(activity, trackSelector);
@ -190,47 +191,7 @@ public class ViewImgurVideoFragment extends Fragment {
private void download() { private void download() {
isDownloading = false; isDownloading = false;
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(imgurMedia.getLink())); mediaDownloader.download(imgurMedia.getLink(), imgurMedia.getFileName(), getContext());
request.setTitle(imgurMedia.getFileName());
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
//Android Q support
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_PICTURES, imgurMedia.getFileName());
} 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/", imgurMedia.getFileName());
} else {
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_PICTURES, imgurMedia.getFileName());
}
}
DownloadManager manager = (DownloadManager) activity.getSystemService(Context.DOWNLOAD_SERVICE);
if (manager == null) {
Toast.makeText(activity, R.string.download_failed, Toast.LENGTH_SHORT).show();
return;
}
manager.enqueue(request);
Toast.makeText(activity, R.string.download_started, Toast.LENGTH_SHORT).show();
} }
private void preparePlayer(Bundle savedInstanceState) { private void preparePlayer(Bundle savedInstanceState) {

View File

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

View File

@ -0,0 +1,58 @@
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 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) {
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_PICTURES, fileName);
} 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();
}
}