diff options
Diffstat (limited to '')
6 files changed, 94 insertions, 190 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewImageOrGifActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewImageOrGifActivity.java index 62c7726d..e332afda 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewImageOrGifActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewImageOrGifActivity.java @@ -1,7 +1,6 @@ package ml.docilealligator.infinityforreddit.Activity; import android.Manifest; -import android.app.DownloadManager; import android.app.WallpaperManager; import android.content.Context; import android.content.Intent; @@ -15,7 +14,6 @@ import android.media.ThumbnailUtils; import android.net.Uri; import android.os.Build; import android.os.Bundle; -import android.os.Environment; import android.text.Html; import android.util.DisplayMetrics; 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.TitleFontStyle; import ml.docilealligator.infinityforreddit.Infinity; +import ml.docilealligator.infinityforreddit.MediaDownloader; +import ml.docilealligator.infinityforreddit.MediaDownloaderImpl; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.SetAsWallpaperCallback; import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; @@ -90,6 +90,7 @@ public class ViewImageOrGifActivity extends AppCompatActivity implements SetAsWa @Inject @Named("default") SharedPreferences mSharedPreferences; + private MediaDownloader mediaDownloader; private boolean isActionBarHidden = false; private boolean isDownloading = false; private RequestManager glide; @@ -134,6 +135,8 @@ public class ViewImageOrGifActivity extends AppCompatActivity implements SetAsWa mHaulerView.setOnDragDismissedListener(dragDirection -> finish()); + mediaDownloader = new MediaDownloaderImpl(); + glide = Glide.with(this); Intent intent = getIntent(); @@ -231,10 +234,10 @@ public class ViewImageOrGifActivity extends AppCompatActivity implements SetAsWa PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE); } else { // Permission has already been granted - download(mImageUrl, mImageFileName); + mediaDownloader.download(mImageUrl, mImageFileName, this); } } else { - download(mImageUrl, mImageFileName); + mediaDownloader.download(mImageUrl, mImageFileName, this); } return true; @@ -345,56 +348,12 @@ public class ViewImageOrGifActivity extends AppCompatActivity implements SetAsWa if (grantResults[0] == PackageManager.PERMISSION_DENIED) { Toast.makeText(this, R.string.no_storage_permission, Toast.LENGTH_SHORT).show(); } else if (grantResults[0] == PackageManager.PERMISSION_GRANTED && isDownloading) { - download(mImageUrl, mImageFileName); + mediaDownloader.download(mImageUrl, mImageFileName, this); } 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) { Toast.makeText(ViewImageOrGifActivity.this, R.string.save_image_first, Toast.LENGTH_SHORT).show(); glide.asBitmap().load(mImageUrl).into(new CustomTarget<Bitmap>() { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewVideoActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewVideoActivity.java index 1bccf67b..5bb95350 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewVideoActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewVideoActivity.java @@ -1,8 +1,6 @@ package ml.docilealligator.infinityforreddit.Activity; import android.Manifest; -import android.app.DownloadManager; -import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.PackageManager; @@ -13,7 +11,6 @@ import android.media.AudioManager; import android.net.Uri; import android.os.Build; import android.os.Bundle; -import android.os.Environment; import android.text.Html; import android.view.Menu; import android.view.MenuItem; @@ -49,8 +46,6 @@ import com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory; import com.google.android.exoplayer2.util.Util; import com.thefuntasty.hauler.HaulerView; -import java.io.File; - import javax.inject.Inject; 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.TitleFontFamily; import ml.docilealligator.infinityforreddit.Infinity; +import ml.docilealligator.infinityforreddit.MediaDownloader; +import ml.docilealligator.infinityforreddit.MediaDownloaderImpl; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.Service.DownloadRedditVideoService; import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; @@ -95,6 +92,7 @@ public class ViewVideoActivity extends AppCompatActivity { private Uri mVideoUri; private SimpleExoPlayer player; private DataSource.Factory dataSourceFactory; + private MediaDownloader mediaDownloader; private String videoDownloadUrl; private String videoFileName; @@ -162,6 +160,8 @@ public class ViewVideoActivity extends AppCompatActivity { haulerView.setOnDragDismissedListener(dragDirection -> finish()); + mediaDownloader = new MediaDownloaderImpl(); + Intent intent = getIntent(); mVideoUri = intent.getData(); postTitle = intent.getStringExtra(EXTRA_POST_TITLE); @@ -407,46 +407,7 @@ public class ViewVideoActivity extends AppCompatActivity { isDownloading = false; if (videoType != VIDEO_TYPE_NORMAL) { - DownloadManager.Request request = new DownloadManager.Request(Uri.parse(videoDownloadUrl)); - 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); + mediaDownloader.download(videoDownloadUrl, videoFileName, this); } else { Intent intent = new Intent(this, DownloadRedditVideoService.class); intent.putExtra(DownloadRedditVideoService.EXTRA_VIDEO_URL, videoDownloadUrl); @@ -458,8 +419,8 @@ public class ViewVideoActivity extends AppCompatActivity { } else { 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 diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/ViewImgurImageFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/ViewImgurImageFragment.java index 99ebd248..90fc231e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/ViewImgurImageFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/ViewImgurImageFragment.java @@ -1,7 +1,6 @@ package ml.docilealligator.infinityforreddit.Fragment; import android.Manifest; -import android.app.DownloadManager; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; @@ -10,7 +9,6 @@ import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.Build; import android.os.Bundle; -import android.os.Environment; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; @@ -47,6 +45,8 @@ import ml.docilealligator.infinityforreddit.AsyncTask.SaveImageToFileAsyncTask; import ml.docilealligator.infinityforreddit.BottomSheetFragment.SetAsWallpaperBottomSheetFragment; import ml.docilealligator.infinityforreddit.BuildConfig; import ml.docilealligator.infinityforreddit.ImgurMedia; +import ml.docilealligator.infinityforreddit.MediaDownloader; +import ml.docilealligator.infinityforreddit.MediaDownloaderImpl; import ml.docilealligator.infinityforreddit.R; public class ViewImgurImageFragment extends Fragment { @@ -63,6 +63,7 @@ public class ViewImgurImageFragment extends Fragment { private ViewImgurMediaActivity activity; private RequestManager glide; + private MediaDownloader mediaDownloader; private ImgurMedia imgurMedia; private boolean isDownloading = false; @@ -81,6 +82,7 @@ public class ViewImgurImageFragment extends Fragment { imgurMedia = getArguments().getParcelable(EXTRA_IMGUR_IMAGES); glide = Glide.with(activity); + mediaDownloader = new MediaDownloaderImpl(); loadImage(); return rootView; @@ -131,10 +133,10 @@ public class ViewImgurImageFragment extends Fragment { PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE); } else { // Permission has already been granted - download(); + mediaDownloader.download(imgurMedia.getLink(), imgurMedia.getFileName(), getContext()); } } else { - download(); + mediaDownloader.download(imgurMedia.getLink(), imgurMedia.getFileName(), getContext()); } return true; @@ -151,7 +153,7 @@ public class ViewImgurImageFragment extends Fragment { @Override public void saveSuccess(File imageFile) { Uri uri = FileProvider.getUriForFile(activity, - BuildConfig.APPLICATION_ID + ".provider",imageFile); + BuildConfig.APPLICATION_ID + ".provider", imageFile); Intent shareIntent = new Intent(); shareIntent.setAction(Intent.ACTION_SEND); shareIntent.putExtra(Intent.EXTRA_STREAM, uri); @@ -200,56 +202,12 @@ public class ViewImgurImageFragment extends Fragment { if (grantResults[0] == PackageManager.PERMISSION_DENIED) { Toast.makeText(activity, R.string.no_storage_permission, Toast.LENGTH_SHORT).show(); } else if (grantResults[0] == PackageManager.PERMISSION_GRANTED && isDownloading) { - download(); + mediaDownloader.download(imgurMedia.getLink(), imgurMedia.getFileName(), getContext()); } 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 public void onAttach(@NonNull Context context) { super.onAttach(context); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/ViewImgurVideoFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/ViewImgurVideoFragment.java index 3ec6eb39..dd3853d0 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/ViewImgurVideoFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/ViewImgurVideoFragment.java @@ -2,7 +2,6 @@ package ml.docilealligator.infinityforreddit.Fragment; import android.Manifest; import android.app.Activity; -import android.app.DownloadManager; import android.content.Context; import android.content.SharedPreferences; import android.content.pm.PackageManager; @@ -11,7 +10,6 @@ import android.media.AudioManager; import android.net.Uri; import android.os.Build; import android.os.Bundle; -import android.os.Environment; import android.view.LayoutInflater; import android.view.Menu; 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.util.Util; -import java.io.File; - import javax.inject.Inject; import javax.inject.Named; @@ -51,6 +47,8 @@ import butterknife.BindView; import butterknife.ButterKnife; import ml.docilealligator.infinityforreddit.ImgurMedia; import ml.docilealligator.infinityforreddit.Infinity; +import ml.docilealligator.infinityforreddit.MediaDownloader; +import ml.docilealligator.infinityforreddit.MediaDownloaderImpl; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; @@ -71,6 +69,7 @@ public class ViewImgurVideoFragment extends Fragment { private ImgurMedia imgurMedia; private SimpleExoPlayer player; private DataSource.Factory dataSourceFactory; + private MediaDownloader mediaDownloader; private boolean wasPlaying = false; private boolean isMute = false; private boolean isDownloading = false; @@ -131,6 +130,8 @@ public class ViewImgurVideoFragment extends Fragment { } }); + mediaDownloader = new MediaDownloaderImpl(); + TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(); TrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory); player = ExoPlayerFactory.newSimpleInstance(activity, trackSelector); @@ -190,47 +191,7 @@ public class ViewImgurVideoFragment extends Fragment { private void download() { isDownloading = false; - 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(); + mediaDownloader.download(imgurMedia.getLink(), imgurMedia.getFileName(), getContext()); } private void preparePlayer(Bundle savedInstanceState) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/MediaDownloader.java b/app/src/main/java/ml/docilealligator/infinityforreddit/MediaDownloader.java new file mode 100644 index 00000000..9967a6a4 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/MediaDownloader.java @@ -0,0 +1,7 @@ +package ml.docilealligator.infinityforreddit; + +import android.content.Context; + +public interface MediaDownloader { + void download(String url, String fileName, Context ctx); +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/MediaDownloaderImpl.java b/app/src/main/java/ml/docilealligator/infinityforreddit/MediaDownloaderImpl.java new file mode 100644 index 00000000..638bdf93 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/MediaDownloaderImpl.java @@ -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(); + } +} |