diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/LinkResolverActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/LinkResolverActivity.java index f318c1b5..4d5e3a09 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/LinkResolverActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/LinkResolverActivity.java @@ -82,17 +82,17 @@ public class LinkResolverActivity extends AppCompatActivity { Intent intent = new Intent(this, ViewImageOrGifActivity.class); String url = uri.toString(); String fileName = url.substring(url.lastIndexOf('/') + 1); - intent.putExtra(ViewImageOrGifActivity.IMAGE_URL_KEY, url); - intent.putExtra(ViewImageOrGifActivity.FILE_NAME_KEY, fileName); - intent.putExtra(ViewImageOrGifActivity.POST_TITLE_KEY, fileName); + intent.putExtra(ViewImageOrGifActivity.EXTRA_IMAGE_URL_KEY, url); + intent.putExtra(ViewImageOrGifActivity.EXTRA_FILE_NAME_KEY, fileName); + intent.putExtra(ViewImageOrGifActivity.EXTRA_POST_TITLE_KEY, fileName); startActivity(intent); } else if (path.endsWith("gif")) { Intent intent = new Intent(this, ViewImageOrGifActivity.class); String url = uri.toString(); String fileName = url.substring(url.lastIndexOf('/') + 1); - intent.putExtra(ViewImageOrGifActivity.GIF_URL_KEY, url); - intent.putExtra(ViewImageOrGifActivity.FILE_NAME_KEY, fileName); - intent.putExtra(ViewImageOrGifActivity.POST_TITLE_KEY, fileName); + intent.putExtra(ViewImageOrGifActivity.EXTRA_GIF_URL_KEY, url); + intent.putExtra(ViewImageOrGifActivity.EXTRA_FILE_NAME_KEY, fileName); + intent.putExtra(ViewImageOrGifActivity.EXTRA_POST_TITLE_KEY, fileName); startActivity(intent); } else if (path.endsWith("mp4")) { Intent intent = new Intent(this, ViewVideoActivity.class); 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 32a3ab0d..f580ff16 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewImageOrGifActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewImageOrGifActivity.java @@ -71,10 +71,11 @@ import ml.docilealligator.infinityforreddit.WallpaperSetter; public class ViewImageOrGifActivity extends AppCompatActivity implements SetAsWallpaperCallback { - public static final String IMAGE_URL_KEY = "IUK"; - public static final String GIF_URL_KEY = "GUK"; - public static final String FILE_NAME_KEY = "FNK"; - public static final String POST_TITLE_KEY = "PTK"; + public static final String EXTRA_IMAGE_URL_KEY = "EIUK"; + public static final String EXTRA_GIF_URL_KEY = "EGUK"; + public static final String EXTRA_FILE_NAME_KEY = "EFNK"; + public static final String EXTRA_SUBREDDIT_OR_USERNAME_KEY = "ESOUK"; + public static final String EXTRA_POST_TITLE_KEY = "EPTK"; private static final int PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE = 0; @BindView(R.id.progress_bar_view_image_or_gif_activity) ProgressBar mProgressBar; @@ -90,6 +91,7 @@ public class ViewImageOrGifActivity extends AppCompatActivity implements SetAsWa private RequestManager glide; private String mImageUrl; private String mImageFileName; + private String mSubredditName; private boolean isGif = true; @Override @@ -134,13 +136,14 @@ public class ViewImageOrGifActivity extends AppCompatActivity implements SetAsWa glide = Glide.with(this); Intent intent = getIntent(); - mImageUrl = intent.getStringExtra(GIF_URL_KEY); + mImageUrl = intent.getStringExtra(EXTRA_GIF_URL_KEY); if (mImageUrl == null) { isGif = false; - mImageUrl = intent.getStringExtra(IMAGE_URL_KEY); + mImageUrl = intent.getStringExtra(EXTRA_IMAGE_URL_KEY); } - mImageFileName = intent.getStringExtra(FILE_NAME_KEY); - String postTitle = intent.getStringExtra(POST_TITLE_KEY); + mImageFileName = intent.getStringExtra(EXTRA_FILE_NAME_KEY); + String postTitle = intent.getStringExtra(EXTRA_POST_TITLE_KEY); + mSubredditName = intent.getStringExtra(EXTRA_SUBREDDIT_OR_USERNAME_KEY); if (postTitle != null) { setTitle(Html.fromHtml(String.format("%s", postTitle))); @@ -338,6 +341,7 @@ public class ViewImageOrGifActivity extends AppCompatActivity implements SetAsWa 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); + intent.putExtra(DownloadMediaService.EXTRA_SUBREDDIT_NAME, mSubredditName); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { startForegroundService(intent); } else { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewRedditGalleryActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewRedditGalleryActivity.java index f46d66b7..6fab69e2 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewRedditGalleryActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewRedditGalleryActivity.java @@ -44,6 +44,7 @@ import ml.docilealligator.infinityforreddit.WallpaperSetter; public class ViewRedditGalleryActivity extends AppCompatActivity implements SetAsWallpaperCallback { public static final String EXTRA_REDDIT_GALLERY = "ERG"; + public static final String EXTRA_SUBREDDIT_NAME = "ESN"; @BindView(R.id.hauler_view_view_reddit_gallery_activity) HaulerView haulerView; @@ -51,6 +52,7 @@ public class ViewRedditGalleryActivity extends AppCompatActivity implements SetA ViewPager viewPager; private SectionsPagerAdapter sectionsPagerAdapter; private ArrayList gallery; + private String subredditName; @Inject @Named("default") SharedPreferences sharedPreferences; @@ -97,6 +99,7 @@ public class ViewRedditGalleryActivity extends AppCompatActivity implements SetA finish(); return; } + subredditName = getIntent().getStringExtra(EXTRA_SUBREDDIT_NAME); haulerView.setOnDragDismissedListener(dragDirection -> { int slide = dragDirection == DragDirection.UP ? R.anim.slide_out_up : R.anim.slide_out_down; @@ -218,12 +221,14 @@ public class ViewRedditGalleryActivity extends AppCompatActivity implements SetA ViewRedditGalleryVideoFragment fragment = new ViewRedditGalleryVideoFragment(); Bundle bundle = new Bundle(); bundle.putParcelable(ViewRedditGalleryVideoFragment.EXTRA_REDDIT_GALLERY_VIDEO, media); + bundle.putString(ViewRedditGalleryVideoFragment.EXTRA_SUBREDDIT_NAME, subredditName); fragment.setArguments(bundle); return fragment; } else { ViewRedditGalleryImageOrGifFragment fragment = new ViewRedditGalleryImageOrGifFragment(); Bundle bundle = new Bundle(); bundle.putParcelable(ViewRedditGalleryImageOrGifFragment.EXTRA_REDDIT_GALLERY_MEDIA, media); + bundle.putString(ViewRedditGalleryImageOrGifFragment.EXTRA_SUBREDDIT_NAME, subredditName); fragment.setArguments(bundle); return fragment; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewSubredditDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewSubredditDetailActivity.java index 2230f81f..f253fa65 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewSubredditDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewSubredditDetailActivity.java @@ -357,8 +357,9 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp glide.load(subredditData.getBannerUrl()).into(bannerImageView); bannerImageView.setOnClickListener(view -> { Intent intent = new Intent(ViewSubredditDetailActivity.this, ViewImageOrGifActivity.class); - intent.putExtra(ViewImageOrGifActivity.IMAGE_URL_KEY, subredditData.getBannerUrl()); - intent.putExtra(ViewImageOrGifActivity.FILE_NAME_KEY, subredditName + "-banner.jpg"); + intent.putExtra(ViewImageOrGifActivity.EXTRA_IMAGE_URL_KEY, subredditData.getBannerUrl()); + intent.putExtra(ViewImageOrGifActivity.EXTRA_FILE_NAME_KEY, subredditName + "-banner.jpg"); + intent.putExtra(ViewImageOrGifActivity.EXTRA_SUBREDDIT_OR_USERNAME_KEY, subredditName); startActivity(intent); }); } @@ -378,8 +379,9 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp .into(iconGifImageView); iconGifImageView.setOnClickListener(view -> { Intent intent = new Intent(ViewSubredditDetailActivity.this, ViewImageOrGifActivity.class); - intent.putExtra(ViewImageOrGifActivity.IMAGE_URL_KEY, subredditData.getIconUrl()); - intent.putExtra(ViewImageOrGifActivity.FILE_NAME_KEY, subredditName + "-icon.jpg"); + intent.putExtra(ViewImageOrGifActivity.EXTRA_IMAGE_URL_KEY, subredditData.getIconUrl()); + intent.putExtra(ViewImageOrGifActivity.EXTRA_FILE_NAME_KEY, subredditName + "-icon.jpg"); + intent.putExtra(ViewImageOrGifActivity.EXTRA_SUBREDDIT_OR_USERNAME_KEY, subredditName); startActivity(intent); }); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewUserDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewUserDetailActivity.java index e2099faa..955e0a38 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewUserDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewUserDetailActivity.java @@ -305,8 +305,9 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele glide.load(userData.getBanner()).into(bannerImageView); bannerImageView.setOnClickListener(view -> { Intent intent = new Intent(this, ViewImageOrGifActivity.class); - intent.putExtra(ViewImageOrGifActivity.IMAGE_URL_KEY, userData.getBanner()); - intent.putExtra(ViewImageOrGifActivity.FILE_NAME_KEY, username + "-banner.jpg"); + intent.putExtra(ViewImageOrGifActivity.EXTRA_IMAGE_URL_KEY, userData.getBanner()); + intent.putExtra(ViewImageOrGifActivity.EXTRA_FILE_NAME_KEY, username + "-banner.jpg"); + intent.putExtra(ViewImageOrGifActivity.EXTRA_SUBREDDIT_OR_USERNAME_KEY, username); startActivity(intent); }); } @@ -325,8 +326,9 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele iconGifImageView.setOnClickListener(view -> { Intent intent = new Intent(this, ViewImageOrGifActivity.class); - intent.putExtra(ViewImageOrGifActivity.IMAGE_URL_KEY, userData.getIconUrl()); - intent.putExtra(ViewImageOrGifActivity.FILE_NAME_KEY, username + "-icon.jpg"); + intent.putExtra(ViewImageOrGifActivity.EXTRA_IMAGE_URL_KEY, userData.getIconUrl()); + intent.putExtra(ViewImageOrGifActivity.EXTRA_FILE_NAME_KEY, username + "-icon.jpg"); + intent.putExtra(ViewImageOrGifActivity.EXTRA_SUBREDDIT_OR_USERNAME_KEY, username); startActivity(intent); }); } 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 0ff6ee0c..8e26f0e0 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewVideoActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewVideoActivity.java @@ -438,6 +438,7 @@ public class ViewVideoActivity extends AppCompatActivity { intent.putExtra(DownloadMediaService.EXTRA_URL, videoDownloadUrl); intent.putExtra(DownloadMediaService.EXTRA_MEDIA_TYPE, DownloadMediaService.EXTRA_MEDIA_TYPE_VIDEO); intent.putExtra(DownloadMediaService.EXTRA_FILE_NAME, videoFileName); + intent.putExtra(DownloadMediaService.EXTRA_SUBREDDIT_NAME, subredditName); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { startForegroundService(intent); } else { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java index 3ba327aa..c86b4d38 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java @@ -2560,10 +2560,11 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter { if (mPost.getPostType() == Post.IMAGE_TYPE) { Intent intent = new Intent(mActivity, ViewImageOrGifActivity.class); - intent.putExtra(ViewImageOrGifActivity.IMAGE_URL_KEY, mPost.getUrl()); - intent.putExtra(ViewImageOrGifActivity.FILE_NAME_KEY, mPost.getSubredditNamePrefixed().substring(2) + intent.putExtra(ViewImageOrGifActivity.EXTRA_IMAGE_URL_KEY, mPost.getUrl()); + intent.putExtra(ViewImageOrGifActivity.EXTRA_FILE_NAME_KEY, mPost.getSubredditNamePrefixed().substring(2) + "-" + mPost.getId().substring(3) + ".jpg"); - intent.putExtra(ViewImageOrGifActivity.POST_TITLE_KEY, mPost.getTitle()); + intent.putExtra(ViewImageOrGifActivity.EXTRA_POST_TITLE_KEY, mPost.getTitle()); + intent.putExtra(ViewImageOrGifActivity.EXTRA_SUBREDDIT_OR_USERNAME_KEY, mPost.getSubredditName()); mActivity.startActivity(intent); } else if (mPost.getPostType() == Post.GIF_TYPE) { Intent intent = new Intent(mActivity, ViewImageOrGifActivity.class); - intent.putExtra(ViewImageOrGifActivity.FILE_NAME_KEY, mPost.getSubredditName() + intent.putExtra(ViewImageOrGifActivity.EXTRA_FILE_NAME_KEY, mPost.getSubredditName() + "-" + mPost.getId() + ".gif"); - intent.putExtra(ViewImageOrGifActivity.GIF_URL_KEY, mPost.getVideoUrl()); - intent.putExtra(ViewImageOrGifActivity.POST_TITLE_KEY, mPost.getTitle()); + intent.putExtra(ViewImageOrGifActivity.EXTRA_GIF_URL_KEY, mPost.getVideoUrl()); + intent.putExtra(ViewImageOrGifActivity.EXTRA_POST_TITLE_KEY, mPost.getTitle()); + intent.putExtra(ViewImageOrGifActivity.EXTRA_SUBREDDIT_OR_USERNAME_KEY, mPost.getSubredditName()); mActivity.startActivity(intent); } }); @@ -2951,6 +2954,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter { Intent intent = new Intent(mActivity, ViewRedditGalleryActivity.class); intent.putParcelableArrayListExtra(ViewRedditGalleryActivity.EXTRA_REDDIT_GALLERY, mPost.getGallery()); + intent.putExtra(ViewRedditGalleryActivity.EXTRA_SUBREDDIT_NAME, mPost.getSubredditName()); mActivity.startActivity(intent); }); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java index 7c7d8f53..cde3919c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java @@ -2049,10 +2049,11 @@ public class PostRecyclerViewAdapter extends PagedListAdapter= 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 3357b44e..3ec55cb7 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/ViewRedditGalleryVideoFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/ViewRedditGalleryVideoFragment.java @@ -54,6 +54,7 @@ import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; public class ViewRedditGalleryVideoFragment extends Fragment { public static final String EXTRA_REDDIT_GALLERY_VIDEO = "EIV"; + public static final String EXTRA_SUBREDDIT_NAME = "ESN"; private static final String IS_MUTE_STATE = "IMS"; private static final String POSITION_STATE = "PS"; private static final int PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE = 0; @@ -63,6 +64,7 @@ public class ViewRedditGalleryVideoFragment extends Fragment { ImageButton muteButton; private Activity activity; private Post.Gallery galleryVideo; + private String subredditName; private SimpleExoPlayer player; private DataSource.Factory dataSourceFactory; private boolean wasPlaying = false; @@ -91,6 +93,7 @@ public class ViewRedditGalleryVideoFragment extends Fragment { activity.setVolumeControlStream(AudioManager.STREAM_MUSIC); galleryVideo = getArguments().getParcelable(EXTRA_REDDIT_GALLERY_VIDEO); + subredditName = getArguments().getString(EXTRA_SUBREDDIT_NAME); if (!mSharedPreferences.getBoolean(SharedPreferencesUtils.VIDEO_PLAYER_IGNORE_NAV_BAR, false)) { if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT || getResources().getBoolean(R.bool.isTablet)) { @@ -189,6 +192,7 @@ public class ViewRedditGalleryVideoFragment extends Fragment { 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); + intent.putExtra(DownloadMediaService.EXTRA_SUBREDDIT_NAME, subredditName); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { activity.startForegroundService(intent); } else { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Service/DownloadMediaService.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Service/DownloadMediaService.java index de89e476..a1b625ef 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Service/DownloadMediaService.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Service/DownloadMediaService.java @@ -38,8 +38,8 @@ import ml.docilealligator.infinityforreddit.API.DownloadFile; import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.Event.DownloadMediaEvent; import ml.docilealligator.infinityforreddit.Infinity; -import ml.docilealligator.infinityforreddit.Utils.NotificationUtils; import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.Utils.NotificationUtils; import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; import okhttp3.ResponseBody; import retrofit2.Call; @@ -52,6 +52,7 @@ import static android.os.Environment.getExternalStoragePublicDirectory; 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_SUBREDDIT_NAME = "ESN"; 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; @@ -70,6 +71,7 @@ public class DownloadMediaService extends Service { @Inject CustomThemeWrapper mCustomThemeWrapper; private int mediaType; + private String mimeType; public DownloadMediaService() { } @@ -128,9 +130,10 @@ public class DownloadMediaService extends Service { ((Infinity) getApplication()).getAppComponent().inject(this); String fileUrl = intent.getStringExtra(EXTRA_URL); - String fileName; - fileName = intent.getStringExtra(EXTRA_FILE_NAME); + final String[] fileName = {intent.getStringExtra(EXTRA_FILE_NAME)}; + String subredditName = intent.getStringExtra(EXTRA_SUBREDDIT_NAME); mediaType = intent.getIntExtra(EXTRA_MEDIA_TYPE, EXTRA_MEDIA_TYPE_IMAGE); + mimeType = mediaType == EXTRA_MEDIA_TYPE_VIDEO ? "video/*" : "image/*"; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel serviceChannel; @@ -148,22 +151,24 @@ public class DownloadMediaService extends Service { case EXTRA_MEDIA_TYPE_GIF: startForeground( NotificationUtils.DOWNLOAD_GIF_NOTIFICATION_ID, - createNotification(R.string.downloading_gif, fileName, null) + createNotification(R.string.downloading_gif, fileName[0], null) ); break; case EXTRA_MEDIA_TYPE_VIDEO: startForeground( NotificationUtils.DOWNLOAD_VIDEO_NOTIFICATION_ID, - createNotification(R.string.downloading_video, fileName, null) + createNotification(R.string.downloading_video, fileName[0], null) ); break; default: startForeground( NotificationUtils.DOWNLOAD_IMAGE_NOTIFICATION_ID, - createNotification(R.string.downloading_image, fileName, null) + createNotification(R.string.downloading_image, fileName[0], null) ); } + boolean separateDownloadFolder = mSharedPreferences.getBoolean(SharedPreferencesUtils.SEPARATE_FOLDER_FOR_EACH_SUBREDDIT, false); + retrofit.create(DownloadFile.class).downloadFile(fileUrl).enqueue(new Callback() { @Override public void onResponse(@NonNull Call call, @NonNull Response response) { @@ -175,42 +180,75 @@ public class DownloadMediaService extends Service { if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) { File directory = getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); if (directory != null) { - String directoryPath = directory.getAbsolutePath() + "/Infinity/"; + String directoryPath = separateDownloadFolder && subredditName != null && !subredditName.equals("") ? directory.getAbsolutePath() + "/Infinity/" + subredditName + "/" : directory.getAbsolutePath() + "/Infinity/"; File infinityDir = new File(directoryPath); - if (!infinityDir.exists() && !infinityDir.mkdir()) { - downloadFinished(null, fileName, ERROR_CANNOT_GET_DESTINATION_DIRECTORY); + if (!infinityDir.exists() && !infinityDir.mkdirs()) { + downloadFinished(null, fileName[0], ERROR_CANNOT_GET_DESTINATION_DIRECTORY); return; } - destinationFileUriString = directoryPath + fileName; + destinationFileUriString = directoryPath + fileName[0]; } else { - downloadFinished(null, fileName, ERROR_CANNOT_GET_DESTINATION_DIRECTORY); + downloadFinished(null, fileName[0], ERROR_CANNOT_GET_DESTINATION_DIRECTORY); return; } } else { - destinationFileUriString = Environment.DIRECTORY_PICTURES + "/Infinity/"; + String dir = mediaType == EXTRA_MEDIA_TYPE_VIDEO ? Environment.DIRECTORY_MOVIES : Environment.DIRECTORY_PICTURES; + destinationFileUriString = separateDownloadFolder && subredditName != null && !subredditName.equals("") ? dir + "/Infinity/" + subredditName + "/" : dir + "/Infinity/"; } isDefaultDestination = true; } else { isDefaultDestination = false; - DocumentFile picFile = DocumentFile.fromTreeUri(DownloadMediaService.this, Uri.parse(destinationFileDirectory)).createFile("image/*", fileName); + DocumentFile picFile; + DocumentFile dir; + if (separateDownloadFolder && subredditName != null && !subredditName.equals("")) { + dir = DocumentFile.fromTreeUri(DownloadMediaService.this, Uri.parse(destinationFileDirectory)); + if (dir == null) { + downloadFinished(null, fileName[0], ERROR_CANNOT_GET_DESTINATION_DIRECTORY); + return; + } + dir = dir.findFile(subredditName); + if (dir == null) { + dir = DocumentFile.fromTreeUri(DownloadMediaService.this, Uri.parse(destinationFileDirectory)).createDirectory(subredditName); + if (dir == null) { + downloadFinished(null, fileName[0], ERROR_CANNOT_GET_DESTINATION_DIRECTORY); + return; + } + } + } else { + dir = DocumentFile.fromTreeUri(DownloadMediaService.this, Uri.parse(destinationFileDirectory)); + if (dir == null) { + downloadFinished(null, fileName[0], ERROR_CANNOT_GET_DESTINATION_DIRECTORY); + return; + } + } + DocumentFile checkForDuplicates = dir.findFile(fileName[0]); + int extensionPosition = fileName[0].lastIndexOf('.'); + String extension = fileName[0].substring(extensionPosition); + int num = 1; + while (checkForDuplicates != null) { + fileName[0] = fileName[0].substring(0, extensionPosition) + " (" + num + ")" + extension; + checkForDuplicates = dir.findFile(fileName[0]); + num++; + } + picFile = dir.createFile(mimeType, fileName[0]); if (picFile == null) { - downloadFinished(null, fileName, ERROR_CANNOT_GET_DESTINATION_DIRECTORY); + downloadFinished(null, fileName[0], ERROR_CANNOT_GET_DESTINATION_DIRECTORY); return; } destinationFileUriString = picFile.getUri().toString(); } new SaveImageOrGifAndCopyToExternalStorageAsyncTask(response.body(), mediaType, - isDefaultDestination, fileName, destinationFileUriString, getContentResolver(), + isDefaultDestination, fileName[0], destinationFileUriString, getContentResolver(), new SaveImageOrGifAndCopyToExternalStorageAsyncTask.SaveImageOrGifAndCopyToExternalStorageAsyncTaskListener() { @Override public void finished(Uri destinationFileUri, int errorCode) { - downloadFinished(destinationFileUri, fileName, errorCode); + downloadFinished(destinationFileUri, fileName[0], errorCode); } @Override public void updateProgressNotification(int stringResId) { - updateNotification(stringResId, fileName, null); + updateNotification(stringResId, fileName[0], null); } }).execute(); } @@ -218,7 +256,7 @@ public class DownloadMediaService extends Service { @Override public void onFailure(@NonNull Call call, @NonNull Throwable t) { - downloadFinished(null, fileName, ERROR_FILE_CANNOT_DOWNLOAD); + downloadFinished(null, fileName[0], ERROR_FILE_CANNOT_DOWNLOAD); } }); return super.onStartCommand(intent, flags, startId); @@ -263,7 +301,7 @@ public class DownloadMediaService extends Service { (path, uri) -> { Intent intent = new Intent(); intent.setAction(android.content.Intent.ACTION_VIEW); - intent.setDataAndType(destinationFileUri, "image/*"); + intent.setDataAndType(destinationFileUri, mimeType); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); updateNotification(R.string.downloading_media_finished, fileName, pendingIntent); EventBus.getDefault().post(new DownloadMediaEvent(true)); @@ -362,12 +400,10 @@ public class DownloadMediaService extends Service { outputStream.flush(); } else { - String relativeLocation = Environment.DIRECTORY_PICTURES + "/Infinity/"; - ContentValues contentValues = new ContentValues(); contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, destinationFileName); - contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "image/*"); - contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, relativeLocation); + contentValues.put(MediaStore.MediaColumns.MIME_TYPE, mediaType == EXTRA_MEDIA_TYPE_VIDEO ? "video/*" : "image/*"); + contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, destinationFileUriString); contentValues.put(MediaStore.Images.Media.IS_PENDING, 1); final Uri contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Service/DownloadRedditVideoService.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Service/DownloadRedditVideoService.java index 71931b82..70c24757 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Service/DownloadRedditVideoService.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Service/DownloadRedditVideoService.java @@ -94,7 +94,8 @@ public class DownloadRedditVideoService extends Service { String videoUrl = intent.getStringExtra(EXTRA_VIDEO_URL); String audioUrl = videoUrl.substring(0, videoUrl.lastIndexOf('/')) + "/DASH_audio.mp4"; - String fileName = intent.getStringExtra(EXTRA_SUBREDDIT) + "-" + intent.getStringExtra(EXTRA_POST_ID); + String subredditName = intent.getStringExtra(EXTRA_SUBREDDIT); + final String[] fileNameWithoutExtension = {subredditName + "-" + intent.getStringExtra(EXTRA_POST_ID)}; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel serviceChannel; @@ -110,13 +111,15 @@ public class DownloadRedditVideoService extends Service { startForeground( NotificationUtils.DOWNLOAD_REDDIT_VIDEO_NOTIFICATION_ID, - createNotification(R.string.downloading_reddit_video, fileName + ".mp4", null) + createNotification(R.string.downloading_reddit_video, fileNameWithoutExtension[0] + ".mp4", null) ); DownloadFile downloadFile = retrofit.create(DownloadFile.class); + boolean separateDownloadFolder = sharedPreferences.getBoolean(SharedPreferencesUtils.SEPARATE_FOLDER_FOR_EACH_SUBREDDIT, false); + File directory = getExternalCacheDir(); - String destinationFileName = fileName + ".mp4"; + String destinationFileName = fileNameWithoutExtension[0] + ".mp4"; if (directory != null) { String directoryPath = directory.getAbsolutePath() + "/"; downloadFile.downloadFile(videoUrl).enqueue(new Callback() { @@ -130,7 +133,7 @@ public class DownloadRedditVideoService extends Service { if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) { File directory = getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); if (directory != null) { - String directoryPath = directory.getAbsolutePath() + "/Infinity/"; + String directoryPath = separateDownloadFolder ? directory.getAbsolutePath() + "/Infinity/" + subredditName + "/" : directory.getAbsolutePath() + "/Infinity/"; File infinityDir = new File(directoryPath); if (!infinityDir.exists() && !infinityDir.mkdir()) { downloadFinished(null, destinationFileName, ERROR_CANNOT_GET_DESTINATION_DIRECTORY); @@ -142,12 +145,42 @@ public class DownloadRedditVideoService extends Service { return; } } else { - destinationFileUriString = Environment.DIRECTORY_PICTURES + "/Infinity/"; + destinationFileUriString = separateDownloadFolder ? Environment.DIRECTORY_PICTURES + "/Infinity/" + subredditName + "/" : Environment.DIRECTORY_PICTURES + "/Infinity/"; } isDefaultDestination = true; } else { isDefaultDestination = false; - DocumentFile picFile = DocumentFile.fromTreeUri(DownloadRedditVideoService.this, Uri.parse(destinationFileDirectory)).createFile("video/*", destinationFileName); + DocumentFile picFile; + DocumentFile dir; + if (separateDownloadFolder) { + dir = DocumentFile.fromTreeUri(DownloadRedditVideoService.this, Uri.parse(destinationFileDirectory)); + if (dir == null) { + downloadFinished(null, destinationFileName, ERROR_CANNOT_GET_DESTINATION_DIRECTORY); + return; + } + dir = dir.findFile(subredditName); + if (dir == null) { + dir = DocumentFile.fromTreeUri(DownloadRedditVideoService.this, Uri.parse(destinationFileDirectory)).createDirectory(subredditName); + if (dir == null) { + downloadFinished(null, destinationFileName, ERROR_CANNOT_GET_DESTINATION_DIRECTORY); + return; + } + } + } else { + dir = DocumentFile.fromTreeUri(DownloadRedditVideoService.this, Uri.parse(destinationFileDirectory)); + if (dir == null) { + downloadFinished(null, destinationFileName, ERROR_CANNOT_GET_DESTINATION_DIRECTORY); + return; + } + } + DocumentFile checkForDuplicates = dir.findFile(destinationFileName); + int num = 1; + while (checkForDuplicates != null) { + fileNameWithoutExtension[0] = fileNameWithoutExtension[0] + " (" + num + ")"; + checkForDuplicates = dir.findFile(fileNameWithoutExtension[0] + ".mp4"); + num++; + } + picFile = dir.createFile("video/*", fileNameWithoutExtension[0] + ".mp4"); if (picFile == null) { downloadFinished(null, destinationFileName, ERROR_CANNOT_GET_DESTINATION_DIRECTORY); return; @@ -160,9 +193,9 @@ public class DownloadRedditVideoService extends Service { @Override public void onResponse(@NonNull Call call, @NonNull Response audioResponse) { if (audioResponse.isSuccessful() && audioResponse.body() != null) { - String videoFilePath = directoryPath + fileName + "-cache.mp4"; - String audioFilePath = directoryPath + fileName + "-cache.mp3"; - String outputFilePath = directoryPath + fileName + ".mp4"; + String videoFilePath = directoryPath + fileNameWithoutExtension[0] + "-cache.mp4"; + String audioFilePath = directoryPath + fileNameWithoutExtension[0] + "-cache.mp3"; + String outputFilePath = directoryPath + fileNameWithoutExtension[0] + ".mp4"; new SaveTempMuxAndCopyAsyncTask(videoResponse.body(), audioResponse.body(), videoFilePath, audioFilePath, outputFilePath, destinationFileName, destinationFileUriString, isDefaultDestination, @@ -182,7 +215,7 @@ public class DownloadRedditVideoService extends Service { } }).execute(); } else { - String videoFilePath = directoryPath + fileName + "-cache.mp4"; + String videoFilePath = directoryPath + fileNameWithoutExtension[0] + "-cache.mp4"; new SaveTempMuxAndCopyAsyncTask(videoResponse.body(), null, videoFilePath, null, null, destinationFileName, destinationFileUriString, isDefaultDestination, @@ -204,7 +237,7 @@ public class DownloadRedditVideoService extends Service { @Override public void onFailure(@NonNull Call call, @NonNull Throwable t) { - String videoFilePath = directoryPath + fileName + "-cache.mp4"; + String videoFilePath = directoryPath + fileNameWithoutExtension[0] + "-cache.mp4"; new SaveTempMuxAndCopyAsyncTask(videoResponse.body(), null, videoFilePath, null, null, destinationFileName, destinationFileUriString, isDefaultDestination, diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java index 16df65e4..dccf3e1b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java @@ -120,6 +120,7 @@ public class SharedPreferencesUtils { public static final String IMAGE_DOWNLOAD_LOCATION = "image_download_location"; public static final String GIF_DOWNLOAD_LOCATION = "gif_download_location"; public static final String VIDEO_DOWNLOAD_LOCATION = "video_download_location"; + public static final String SEPARATE_FOLDER_FOR_EACH_SUBREDDIT = "separate_folder_for_each_subreddit"; public static final String MAIN_PAGE_TABS_SHARED_PREFERENCES_FILE = "ml.docilealligator.infinityforreddit.main_page_tabs"; public static final String MAIN_PAGE_TAB_1_TITLE = "_main_page_tab_1_title"; diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index f9ef4518..f1801b33 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -477,6 +477,7 @@ Image Download Location Gif Download Location Video Download Location + Separate Folder for Each Subreddit Cannot get the link diff --git a/app/src/main/res/xml/download_location_preferences.xml b/app/src/main/res/xml/download_location_preferences.xml index 7954cb87..e8215fe9 100644 --- a/app/src/main/res/xml/download_location_preferences.xml +++ b/app/src/main/res/xml/download_location_preferences.xml @@ -10,4 +10,9 @@ + + \ No newline at end of file