diff options
Diffstat (limited to 'app/src')
12 files changed, 158 insertions, 91 deletions
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 690f1026..912d1051 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -25,7 +25,7 @@ android:usesCleartextTraffic="true" tools:replace="android:label"> <service - android:name=".Service.DownloadImageService" + android:name=".Service.DownloadMediaService" android:enabled="true" android:exported="false"></service> @@ -42,7 +42,7 @@ android:windowSoftInputMode="adjustResize" /> <service - android:name=".Service.DownloadVideoService" + android:name=".Service.DownloadRedditVideoService" android:enabled="true" android:exported="false" /> 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 22a921e6..3e517ae1 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewImageOrGifActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewImageOrGifActivity.java @@ -63,10 +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.Service.DownloadImageService; +import ml.docilealligator.infinityforreddit.Service.DownloadMediaService; import ml.docilealligator.infinityforreddit.SetAsWallpaperCallback; import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; import ml.docilealligator.infinityforreddit.WallpaperSetter; @@ -87,7 +85,6 @@ 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,8 +131,6 @@ public class ViewImageOrGifActivity extends AppCompatActivity implements SetAsWa Slidr.attach(this, new SlidrConfig.Builder().position(SlidrPosition.VERTICAL).distanceThreshold(0.125f).build()); - mediaDownloader = new MediaDownloaderImpl(); - glide = Glide.with(this); Intent intent = getIntent(); @@ -339,10 +334,10 @@ public class ViewImageOrGifActivity extends AppCompatActivity implements SetAsWa private void download() { isDownloading = false; - Intent intent = new Intent(this, DownloadImageService.class); - intent.putExtra(DownloadImageService.EXTRA_URL, mImageUrl); - intent.putExtra(DownloadImageService.EXTRA_IS_GIF, isGif); - intent.putExtra(DownloadImageService.EXTRA_FILE_NAME, mImageFileName); + Intent intent = new Intent(this, DownloadMediaService.class); + intent.putExtra(DownloadMediaService.EXTRA_URL, mImageUrl); + intent.putExtra(DownloadMediaService.EXTRA_MEDIA_TYPE, isGif ? DownloadMediaService.EXTRA_MEDIA_TYPE_GIF : DownloadMediaService.EXTRA_MEDIA_TYPE_IMAGE); + intent.putExtra(DownloadMediaService.EXTRA_FILE_NAME, mImageFileName); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { startForegroundService(intent); } else { @@ -437,7 +432,7 @@ 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) { - mediaDownloader.download(mImageUrl, mImageFileName, this); + download(); } isDownloading = false; } 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 c1c2c45c..ae709246 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewVideoActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewVideoActivity.java @@ -62,7 +62,7 @@ import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.MediaDownloader; import ml.docilealligator.infinityforreddit.MediaDownloaderImpl; import ml.docilealligator.infinityforreddit.R; -import ml.docilealligator.infinityforreddit.Service.DownloadVideoService; +import ml.docilealligator.infinityforreddit.Service.DownloadRedditVideoService; import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; import retrofit2.Retrofit; @@ -440,11 +440,11 @@ public class ViewVideoActivity extends AppCompatActivity { if (videoType != VIDEO_TYPE_NORMAL) { mediaDownloader.download(videoDownloadUrl, videoFileName, this); } else { - Intent intent = new Intent(this, DownloadVideoService.class); - intent.putExtra(DownloadVideoService.EXTRA_VIDEO_URL, videoDownloadUrl); - intent.putExtra(DownloadVideoService.EXTRA_POST_ID, id); - intent.putExtra(DownloadVideoService.EXTRA_SUBREDDIT, subredditName); - intent.putExtra(DownloadVideoService.EXTRA_IS_REDDIT_VIDEO, true); + Intent intent = new Intent(this, DownloadRedditVideoService.class); + intent.putExtra(DownloadRedditVideoService.EXTRA_VIDEO_URL, videoDownloadUrl); + intent.putExtra(DownloadRedditVideoService.EXTRA_POST_ID, id); + intent.putExtra(DownloadRedditVideoService.EXTRA_SUBREDDIT, subredditName); + intent.putExtra(DownloadRedditVideoService.EXTRA_IS_REDDIT_VIDEO, true); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { startForegroundService(intent); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java index 0d3e6aec..add19533 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java @@ -54,8 +54,8 @@ import ml.docilealligator.infinityforreddit.Fragment.SubscribedSubredditsListing import ml.docilealligator.infinityforreddit.Fragment.UserListingFragment; import ml.docilealligator.infinityforreddit.Fragment.ViewImgurVideoFragment; import ml.docilealligator.infinityforreddit.Fragment.ViewRedditGalleryVideoFragment; -import ml.docilealligator.infinityforreddit.Service.DownloadImageService; -import ml.docilealligator.infinityforreddit.Service.DownloadVideoService; +import ml.docilealligator.infinityforreddit.Service.DownloadMediaService; +import ml.docilealligator.infinityforreddit.Service.DownloadRedditVideoService; import ml.docilealligator.infinityforreddit.Service.SubmitPostService; import ml.docilealligator.infinityforreddit.Settings.AdvancedPreferenceFragment; import ml.docilealligator.infinityforreddit.Settings.CustomizeMainPageTabsFragment; @@ -173,7 +173,7 @@ public interface AppComponent { void inject(ViewImgurVideoFragment viewImgurVideoFragment); - void inject(DownloadVideoService downloadVideoService); + void inject(DownloadRedditVideoService downloadRedditVideoService); void inject(MultiRedditListingFragment multiRedditListingFragment); @@ -191,7 +191,7 @@ public interface AppComponent { void inject(CustomizeMainPageTabsFragment customizeMainPageTabsFragment); - void inject(DownloadImageService downloadImageService); + void inject(DownloadMediaService downloadMediaService); void inject(DownloadLocationPreferenceFragment downloadLocationPreferenceFragment); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Event/DownloadImageOrGifEvent.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Event/DownloadMediaEvent.java index 61e87c1f..174b49ea 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Event/DownloadImageOrGifEvent.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Event/DownloadMediaEvent.java @@ -1,9 +1,9 @@ package ml.docilealligator.infinityforreddit.Event; -public class DownloadImageOrGifEvent { +public class DownloadMediaEvent { public boolean isSuccessful; - public DownloadImageOrGifEvent(boolean isSuccessful) { + public DownloadMediaEvent(boolean isSuccessful) { this.isSuccessful = isSuccessful; } } 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 6a801091..29101dd6 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/ViewImgurImageFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/ViewImgurImageFragment.java @@ -45,10 +45,8 @@ import ml.docilealligator.infinityforreddit.AsyncTask.SaveBitmapImageToFileAsync 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; -import ml.docilealligator.infinityforreddit.Service.DownloadImageService; +import ml.docilealligator.infinityforreddit.Service.DownloadMediaService; import ml.docilealligator.infinityforreddit.SetAsWallpaperCallback; public class ViewImgurImageFragment extends Fragment { @@ -65,7 +63,6 @@ public class ViewImgurImageFragment extends Fragment { private ViewImgurMediaActivity activity; private RequestManager glide; - private MediaDownloader mediaDownloader; private ImgurMedia imgurMedia; private boolean isDownloading = false; private boolean isActionBarHidden = false; @@ -85,7 +82,6 @@ public class ViewImgurImageFragment extends Fragment { imgurMedia = getArguments().getParcelable(EXTRA_IMGUR_IMAGES); glide = Glide.with(activity); - mediaDownloader = new MediaDownloaderImpl(); loadImage(); imageView.setOnClickListener(view -> { @@ -236,10 +232,10 @@ public class ViewImgurImageFragment extends Fragment { private void download() { isDownloading = false; - Intent intent = new Intent(activity, DownloadImageService.class); - intent.putExtra(DownloadImageService.EXTRA_URL, imgurMedia.getLink()); - intent.putExtra(DownloadImageService.EXTRA_IS_GIF, false); - intent.putExtra(DownloadImageService.EXTRA_FILE_NAME, imgurMedia.getFileName()); + Intent intent = new Intent(activity, DownloadMediaService.class); + intent.putExtra(DownloadMediaService.EXTRA_URL, imgurMedia.getLink()); + intent.putExtra(DownloadMediaService.EXTRA_MEDIA_TYPE, DownloadMediaService.EXTRA_MEDIA_TYPE_IMAGE); + intent.putExtra(DownloadMediaService.EXTRA_FILE_NAME, imgurMedia.getFileName()); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { activity.startForegroundService(intent); } else { 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 1e968f57..154901bb 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/ViewImgurVideoFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/ViewImgurVideoFragment.java @@ -3,6 +3,7 @@ package ml.docilealligator.infinityforreddit.Fragment; import android.Manifest; import android.app.Activity; import android.content.Context; +import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.content.res.Configuration; @@ -46,9 +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.Service.DownloadMediaService; import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; public class ViewImgurVideoFragment extends Fragment { @@ -65,7 +65,6 @@ 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; @@ -128,8 +127,6 @@ public class ViewImgurVideoFragment extends Fragment { } }); - mediaDownloader = new MediaDownloaderImpl(); - TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(); TrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory); player = ExoPlayerFactory.newSimpleInstance(activity, trackSelector); @@ -188,7 +185,16 @@ public class ViewImgurVideoFragment extends Fragment { private void download() { isDownloading = false; - mediaDownloader.download(imgurMedia.getLink(), imgurMedia.getFileName(), getContext()); + Intent intent = new Intent(activity, DownloadMediaService.class); + intent.putExtra(DownloadMediaService.EXTRA_URL, imgurMedia.getLink()); + intent.putExtra(DownloadMediaService.EXTRA_MEDIA_TYPE, DownloadMediaService.EXTRA_MEDIA_TYPE_VIDEO); + intent.putExtra(DownloadMediaService.EXTRA_FILE_NAME, imgurMedia.getFileName()); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + activity.startForegroundService(intent); + } else { + activity.startService(intent); + } + Toast.makeText(activity, R.string.download_started, Toast.LENGTH_SHORT).show(); } private void preparePlayer(Bundle savedInstanceState) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/ViewRedditGalleryImageOrGifFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/ViewRedditGalleryImageOrGifFragment.java index eb809fa5..a34e8628 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/ViewRedditGalleryImageOrGifFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/ViewRedditGalleryImageOrGifFragment.java @@ -50,11 +50,9 @@ import ml.docilealligator.infinityforreddit.AsyncTask.SaveBitmapImageToFileAsync import ml.docilealligator.infinityforreddit.AsyncTask.SaveGIFToFileAsyncTask; import ml.docilealligator.infinityforreddit.BottomSheetFragment.SetAsWallpaperBottomSheetFragment; import ml.docilealligator.infinityforreddit.BuildConfig; -import ml.docilealligator.infinityforreddit.MediaDownloader; -import ml.docilealligator.infinityforreddit.MediaDownloaderImpl; import ml.docilealligator.infinityforreddit.Post.Post; import ml.docilealligator.infinityforreddit.R; -import ml.docilealligator.infinityforreddit.Service.DownloadImageService; +import ml.docilealligator.infinityforreddit.Service.DownloadMediaService; import ml.docilealligator.infinityforreddit.SetAsWallpaperCallback; public class ViewRedditGalleryImageOrGifFragment extends Fragment { @@ -71,7 +69,6 @@ public class ViewRedditGalleryImageOrGifFragment extends Fragment { private ViewRedditGalleryActivity activity; private RequestManager glide; - private MediaDownloader mediaDownloader; private Post.Gallery media; private boolean isDownloading = false; private boolean isActionBarHidden = false; @@ -93,7 +90,6 @@ public class ViewRedditGalleryImageOrGifFragment extends Fragment { media = getArguments().getParcelable(EXTRA_REDDIT_GALLERY_MEDIA); glide = Glide.with(activity); - mediaDownloader = new MediaDownloaderImpl(); imageView.setImageViewFactory(new GlideImageViewFactory()); @@ -269,10 +265,10 @@ public class ViewRedditGalleryImageOrGifFragment extends Fragment { private void download() { isDownloading = false; - Intent intent = new Intent(activity, DownloadImageService.class); - intent.putExtra(DownloadImageService.EXTRA_URL, media.url); - intent.putExtra(DownloadImageService.EXTRA_IS_GIF, media.mediaType == Post.Gallery.TYPE_GIF); - intent.putExtra(DownloadImageService.EXTRA_FILE_NAME, media.fileName); + Intent intent = new Intent(activity, DownloadMediaService.class); + intent.putExtra(DownloadMediaService.EXTRA_URL, media.url); + intent.putExtra(DownloadMediaService.EXTRA_MEDIA_TYPE, media.mediaType == Post.Gallery.TYPE_GIF ? DownloadMediaService.EXTRA_MEDIA_TYPE_GIF: DownloadMediaService.EXTRA_MEDIA_TYPE_IMAGE); + intent.putExtra(DownloadMediaService.EXTRA_FILE_NAME, media.fileName); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { activity.startForegroundService(intent); } else { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/ViewRedditGalleryVideoFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/ViewRedditGalleryVideoFragment.java index 34962aab..3357b44e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/ViewRedditGalleryVideoFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/ViewRedditGalleryVideoFragment.java @@ -3,6 +3,7 @@ package ml.docilealligator.infinityforreddit.Fragment; import android.Manifest; import android.app.Activity; import android.content.Context; +import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.content.res.Configuration; @@ -45,10 +46,9 @@ import javax.inject.Named; import butterknife.BindView; import butterknife.ButterKnife; import ml.docilealligator.infinityforreddit.Infinity; -import ml.docilealligator.infinityforreddit.MediaDownloader; -import ml.docilealligator.infinityforreddit.MediaDownloaderImpl; import ml.docilealligator.infinityforreddit.Post.Post; import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.Service.DownloadMediaService; import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; public class ViewRedditGalleryVideoFragment extends Fragment { @@ -65,7 +65,6 @@ public class ViewRedditGalleryVideoFragment extends Fragment { private Post.Gallery galleryVideo; private SimpleExoPlayer player; private DataSource.Factory dataSourceFactory; - private MediaDownloader mediaDownloader; private boolean wasPlaying = false; private boolean isMute = false; private boolean isDownloading = false; @@ -128,8 +127,6 @@ public class ViewRedditGalleryVideoFragment extends Fragment { } }); - mediaDownloader = new MediaDownloaderImpl(); - TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(); TrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory); player = ExoPlayerFactory.newSimpleInstance(activity, trackSelector); @@ -188,7 +185,16 @@ public class ViewRedditGalleryVideoFragment extends Fragment { private void download() { isDownloading = false; - mediaDownloader.download(galleryVideo.url, galleryVideo.fileName, getContext()); + Intent intent = new Intent(activity, DownloadMediaService.class); + intent.putExtra(DownloadMediaService.EXTRA_URL, galleryVideo.url); + intent.putExtra(DownloadMediaService.EXTRA_MEDIA_TYPE, DownloadMediaService.EXTRA_MEDIA_TYPE_VIDEO); + intent.putExtra(DownloadMediaService.EXTRA_FILE_NAME, galleryVideo.fileName); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + activity.startForegroundService(intent); + } else { + activity.startService(intent); + } + Toast.makeText(activity, R.string.download_started, Toast.LENGTH_SHORT).show(); } private void preparePlayer(Bundle savedInstanceState) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Service/DownloadImageService.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Service/DownloadMediaService.java index 06d7449e..51eea592 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Service/DownloadImageService.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Service/DownloadMediaService.java @@ -36,7 +36,7 @@ import javax.inject.Named; import ml.docilealligator.infinityforreddit.API.DownloadFile; import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeWrapper; -import ml.docilealligator.infinityforreddit.Event.DownloadImageOrGifEvent; +import ml.docilealligator.infinityforreddit.Event.DownloadMediaEvent; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.NotificationUtils; import ml.docilealligator.infinityforreddit.R; @@ -49,10 +49,13 @@ import retrofit2.Retrofit; import static android.os.Environment.getExternalStoragePublicDirectory; -public class DownloadImageService extends Service { +public class DownloadMediaService extends Service { public static final String EXTRA_URL = "EU"; public static final String EXTRA_FILE_NAME = "EFN"; - public static final String EXTRA_IS_GIF = "EIG"; + public static final String EXTRA_MEDIA_TYPE = "EIG"; + public static final int EXTRA_MEDIA_TYPE_IMAGE = 0; + public static final int EXTRA_MEDIA_TYPE_GIF = 1; + public static final int EXTRA_MEDIA_TYPE_VIDEO = 2; private static final int NO_ERROR = -1; private static final int ERROR_CANNOT_GET_DESTINATION_DIRECTORY = 0; @@ -66,9 +69,9 @@ public class DownloadImageService extends Service { SharedPreferences mSharedPreferences; @Inject CustomThemeWrapper mCustomThemeWrapper; - private boolean isGif; + private int mediaType; - public DownloadImageService() { + public DownloadMediaService() { } @Override @@ -76,6 +79,50 @@ public class DownloadImageService extends Service { return null; } + private String getNotificationChannelId() { + switch (mediaType) { + case EXTRA_MEDIA_TYPE_GIF: + return NotificationUtils.CHANNEL_ID_DOWNLOAD_GIF; + case EXTRA_MEDIA_TYPE_VIDEO: + return NotificationUtils.CHANNEL_ID_DOWNLOAD_VIDEO; + default: + return NotificationUtils.CHANNEL_ID_DOWNLOAD_IMAGE; + } + } + + private String getNotificationChannel() { + switch (mediaType) { + case EXTRA_MEDIA_TYPE_GIF: + return NotificationUtils.CHANNEL_DOWNLOAD_GIF; + case EXTRA_MEDIA_TYPE_VIDEO: + return NotificationUtils.CHANNEL_DOWNLOAD_VIDEO; + default: + return NotificationUtils.CHANNEL_DOWNLOAD_IMAGE; + } + } + + private int getNotificationId() { + switch (mediaType) { + case EXTRA_MEDIA_TYPE_GIF: + return NotificationUtils.DOWNLOAD_GIF_NOTIFICATION_ID; + case EXTRA_MEDIA_TYPE_VIDEO: + return NotificationUtils.DOWNLOAD_VIDEO_NOTIFICATION_ID; + default: + return NotificationUtils.DOWNLOAD_IMAGE_NOTIFICATION_ID; + } + } + + private String getDownloadLocation() { + switch (mediaType) { + case EXTRA_MEDIA_TYPE_GIF: + return mSharedPreferences.getString(SharedPreferencesUtils.GIF_DOWNLOAD_LOCATION, ""); + case EXTRA_MEDIA_TYPE_VIDEO: + return mSharedPreferences.getString(SharedPreferencesUtils.VIDEO_DOWNLOAD_LOCATION, ""); + default: + return mSharedPreferences.getString(SharedPreferencesUtils.IMAGE_DOWNLOAD_LOCATION, ""); + } + } + @Override public int onStartCommand(Intent intent, int flags, int startId) { ((Infinity) getApplication()).getAppComponent().inject(this); @@ -83,13 +130,13 @@ public class DownloadImageService extends Service { String fileUrl = intent.getStringExtra(EXTRA_URL); String fileName; fileName = intent.getStringExtra(EXTRA_FILE_NAME); - isGif = intent.getBooleanExtra(EXTRA_IS_GIF, false); + mediaType = intent.getIntExtra(EXTRA_MEDIA_TYPE, EXTRA_MEDIA_TYPE_IMAGE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel serviceChannel; serviceChannel = new NotificationChannel( - isGif ? NotificationUtils.CHANNEL_ID_DOWNLOAD_GIF : NotificationUtils.CHANNEL_ID_DOWNLOAD_IMAGE, - isGif ? NotificationUtils.CHANNEL_DOWNLOAD_GIF : NotificationUtils.CHANNEL_DOWNLOAD_IMAGE, + getNotificationChannelId(), + getNotificationChannel(), NotificationManager.IMPORTANCE_LOW ); @@ -97,17 +144,31 @@ public class DownloadImageService extends Service { manager.createNotificationChannel(serviceChannel); } - startForeground( - isGif ? NotificationUtils.DOWNLOAD_GIF_NOTIFICATION_ID : NotificationUtils.DOWNLOAD_IMAGE_NOTIFICATION_ID, - createNotification(isGif ? R.string.downloading_gif : R.string.downloading_image, fileName, null) - ); + switch (mediaType) { + case EXTRA_MEDIA_TYPE_GIF: + startForeground( + NotificationUtils.DOWNLOAD_GIF_NOTIFICATION_ID, + createNotification(R.string.downloading_gif, fileName, null) + ); + break; + case EXTRA_MEDIA_TYPE_VIDEO: + startForeground( + NotificationUtils.DOWNLOAD_VIDEO_NOTIFICATION_ID, + createNotification(R.string.downloading_video, fileName, null) + ); + break; + default: + startForeground( + NotificationUtils.DOWNLOAD_IMAGE_NOTIFICATION_ID, + createNotification(R.string.downloading_image, fileName, null) + ); + } retrofit.create(DownloadFile.class).downloadFile(fileUrl).enqueue(new Callback<ResponseBody>() { @Override public void onResponse(@NonNull Call<ResponseBody> call, @NonNull Response<ResponseBody> response) { if (response.isSuccessful() && response.body() != null) { - String destinationFileDirectory = isGif ? mSharedPreferences.getString(SharedPreferencesUtils.GIF_DOWNLOAD_LOCATION, "") : - mSharedPreferences.getString(SharedPreferencesUtils.IMAGE_DOWNLOAD_LOCATION, ""); + String destinationFileDirectory = getDownloadLocation(); String destinationFileUriString; boolean isDefaultDestination; if (destinationFileDirectory.equals("")) { @@ -131,7 +192,7 @@ public class DownloadImageService extends Service { isDefaultDestination = true; } else { isDefaultDestination = false; - DocumentFile picFile = DocumentFile.fromTreeUri(DownloadImageService.this, Uri.parse(destinationFileDirectory)).createFile("image/*", fileName); + DocumentFile picFile = DocumentFile.fromTreeUri(DownloadMediaService.this, Uri.parse(destinationFileDirectory)).createFile("image/*", fileName); if (picFile == null) { downloadFinished(null, fileName, ERROR_CANNOT_GET_DESTINATION_DIRECTORY); return; @@ -139,7 +200,7 @@ public class DownloadImageService extends Service { destinationFileUriString = picFile.getUri().toString(); } - new SaveImageOrGifAndCopyToExternalStorageAsyncTask(response.body(), isGif, + new SaveImageOrGifAndCopyToExternalStorageAsyncTask(response.body(), mediaType, isDefaultDestination, fileName, destinationFileUriString, getContentResolver(), new SaveImageOrGifAndCopyToExternalStorageAsyncTask.SaveImageOrGifAndCopyToExternalStorageAsyncTaskListener() { @Override @@ -165,7 +226,7 @@ public class DownloadImageService extends Service { private Notification createNotification(int stringResId, String fileName, PendingIntent pendingIntent) { NotificationCompat.Builder builder; - builder = new NotificationCompat.Builder(this, isGif ? NotificationUtils.CHANNEL_ID_DOWNLOAD_GIF : NotificationUtils.CHANNEL_ID_DOWNLOAD_IMAGE); + builder = new NotificationCompat.Builder(this, getNotificationChannelId()); builder.setContentTitle(fileName).setContentText(getString(stringResId)); if (pendingIntent != null) { builder.setContentIntent(pendingIntent); @@ -178,8 +239,7 @@ public class DownloadImageService extends Service { private void updateNotification(int stringResId, String fileName, PendingIntent pendingIntent) { NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); if (notificationManager != null) { - notificationManager.notify(isGif ? NotificationUtils.DOWNLOAD_GIF_NOTIFICATION_ID : NotificationUtils.DOWNLOAD_IMAGE_NOTIFICATION_ID, - createNotification(stringResId, fileName, pendingIntent)); + notificationManager.notify(getNotificationId(), createNotification(stringResId, fileName, pendingIntent)); } } @@ -190,13 +250,13 @@ public class DownloadImageService extends Service { updateNotification(R.string.downloading_image_or_gif_failed_cannot_get_destination_directory, fileName, null); break; case ERROR_FILE_CANNOT_DOWNLOAD: - updateNotification(isGif ? R.string.downloading_gif_failed_cannot_download_gif : R.string.downloading_image_failed_cannot_download_image, fileName, null); + updateNotification(R.string.downloading_media_failed_cannot_download_media, fileName, null); break; case ERROR_FILE_CANNOT_SAVE: - updateNotification(isGif ? R.string.downloading_gif_failed_cannot_save_gif : R.string.downloading_image_failed_cannot_save_image, fileName, null); + updateNotification(R.string.downloading_media_failed_cannot_save_to_destination_directory, fileName, null); break; } - EventBus.getDefault().post(new DownloadImageOrGifEvent(false)); + EventBus.getDefault().post(new DownloadMediaEvent(false)); } else { MediaScannerConnection.scanFile( this, new String[]{destinationFileUri.toString()}, null, @@ -205,8 +265,8 @@ public class DownloadImageService extends Service { intent.setAction(android.content.Intent.ACTION_VIEW); intent.setDataAndType(destinationFileUri, "image/*"); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); - updateNotification(isGif ? R.string.downloading_gif_finished : R.string.downloading_image_finished, fileName, pendingIntent); - EventBus.getDefault().post(new DownloadImageOrGifEvent(true)); + updateNotification(R.string.downloading_media_finished, fileName, pendingIntent); + EventBus.getDefault().post(new DownloadMediaEvent(true)); } ); } @@ -216,7 +276,7 @@ public class DownloadImageService extends Service { private static class SaveImageOrGifAndCopyToExternalStorageAsyncTask extends AsyncTask<Void, Integer, Void> { private ResponseBody response; - private boolean isGif; + private int mediaType; private boolean isDefaultDestination; private String destinationFileName; @NonNull @@ -230,14 +290,14 @@ public class DownloadImageService extends Service { void updateProgressNotification(int stringResId); } - public SaveImageOrGifAndCopyToExternalStorageAsyncTask(ResponseBody response, boolean isGif, + public SaveImageOrGifAndCopyToExternalStorageAsyncTask(ResponseBody response, int mediaType, boolean isDefaultDestination, String destinationFileName, @NonNull String destinationFileUriString, ContentResolver contentResolver, SaveImageOrGifAndCopyToExternalStorageAsyncTaskListener saveImageOrGifAndCopyToExternalStorageAsyncTaskListener) { this.response = response; - this.isGif = isGif; + this.mediaType = mediaType; this.isDefaultDestination = isDefaultDestination; this.destinationFileName = destinationFileName; this.destinationFileUriString = destinationFileUriString; @@ -253,7 +313,17 @@ public class DownloadImageService extends Service { @Override protected Void doInBackground(Void... voids) { - publishProgress(isGif ? R.string.downloading_gif_save_gif : R.string.downloading_image_save_image); + switch (mediaType) { + case EXTRA_MEDIA_TYPE_IMAGE: + publishProgress(R.string.downloading_image_save_image); + break; + case EXTRA_MEDIA_TYPE_GIF: + publishProgress(R.string.downloading_gif_save_gif); + break; + case EXTRA_MEDIA_TYPE_VIDEO: + publishProgress(R.string.downloading_video_save_video); + } + try { writeResponseBodyToDisk(response); } catch (IOException e) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Service/DownloadVideoService.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Service/DownloadRedditVideoService.java index 4926785f..ef58a747 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Service/DownloadVideoService.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Service/DownloadRedditVideoService.java @@ -53,7 +53,7 @@ import retrofit2.Retrofit; import static android.os.Environment.getExternalStoragePublicDirectory; -public class DownloadVideoService extends Service { +public class DownloadRedditVideoService extends Service { public static final String EXTRA_VIDEO_URL = "EVU"; public static final String EXTRA_SUBREDDIT = "ES"; @@ -78,7 +78,7 @@ public class DownloadVideoService extends Service { CustomThemeWrapper mCustomThemeWrapper; String resultFile; - public DownloadVideoService() { + public DownloadRedditVideoService() { } @Override diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 5d31c088..b694941d 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -843,18 +843,16 @@ <string name="downloading_reddit_video_failed_cannot_mux">Download failed: cannot mux video and audio</string> <string name="downloading_reddit_video_failed_cannot_save_mux_video">Download failed: cannot save the video to public directory</string> <string name="downloading_video">Downloading Video</string> + <string name="downloading_video_save_video">Saving Video</string> <string name="downloading_image">Downloading Image</string> <string name="downloading_gif">Downloading Gif</string> <string name="downloading_image_save_image">Saving Image</string> <string name="downloading_gif_save_gif">Saving Gif</string> - <string name="downloading_image_finished">Downloaded</string> - <string name="downloading_gif_finished">Downloaded</string> + <string name="downloading_media_finished">Downloaded</string> <string name="downloading_image_or_gif_failed_cannot_get_destination_directory">Download failed: cannot access destination directory</string> - <string name="downloading_image_failed_cannot_download_image">Download image failed</string> - <string name="downloading_image_failed_cannot_save_image">Download failed: cannot save image to destination directory</string> - <string name="downloading_gif_failed_cannot_download_gif">Download gif failed</string> - <string name="downloading_gif_failed_cannot_save_gif">Download failed: cannot save gif to destination directory</string> + <string name="downloading_media_failed_cannot_download_media">Download failed</string> + <string name="downloading_media_failed_cannot_save_to_destination_directory">Download failed: cannot save the file to destination directory</string> <string name="wallpaper_set">Wallpaper set</string> <string name="error_set_wallpaper">Cannot set wallpaper</string> |