From 34127032de45b6f860d998daf3adfebcb029e0fc Mon Sep 17 00:00:00 2001 From: MetalNeo <53447663+MetalNeo@users.noreply.github.com> Date: Mon, 20 Dec 2021 13:13:08 +0000 Subject: Added Setting for Separate NSFW Download Folder (#584) Add an option to separate NSFW media downloads --- .../activities/ViewImageOrGifActivity.java | 4 ++++ .../activities/ViewRedditGalleryActivity.java | 5 +++++ .../infinityforreddit/activities/ViewVideoActivity.java | 2 ++ .../adapters/PostRecyclerViewAdapter.java | 3 +++ .../fragments/ViewRedditGalleryImageOrGifFragment.java | 4 ++++ .../fragments/ViewRedditGalleryVideoFragment.java | 4 ++++ .../infinityforreddit/services/DownloadMediaService.java | 9 +++++++-- .../services/DownloadRedditVideoService.java | 9 ++++++++- .../settings/DownloadLocationPreferenceFragment.java | 16 ++++++++++++++++ .../infinityforreddit/utils/SharedPreferencesUtils.java | 2 ++ app/src/main/res/values/strings.xml | 2 ++ app/src/main/res/xml/download_location_preferences.xml | 9 +++++++++ 12 files changed, 66 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewImageOrGifActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewImageOrGifActivity.java index 4e303c29..0c105691 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewImageOrGifActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewImageOrGifActivity.java @@ -83,6 +83,7 @@ public class ViewImageOrGifActivity extends AppCompatActivity implements SetAsWa 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"; + public static final String EXTRA_IS_NSFW = "EIN"; private static final int PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE = 0; @BindView(R.id.progress_bar_view_image_or_gif_activity) ProgressBar mProgressBar; @@ -112,6 +113,7 @@ public class ViewImageOrGifActivity extends AppCompatActivity implements SetAsWa private String mImageFileName; private String mSubredditName; private boolean isGif = true; + private boolean isNsfw; @Override protected void onCreate(Bundle savedInstanceState) { @@ -165,6 +167,7 @@ public class ViewImageOrGifActivity extends AppCompatActivity implements SetAsWa mImageFileName = intent.getStringExtra(EXTRA_FILE_NAME_KEY); String postTitle = intent.getStringExtra(EXTRA_POST_TITLE_KEY); mSubredditName = intent.getStringExtra(EXTRA_SUBREDDIT_OR_USERNAME_KEY); + isNsfw = intent.getBooleanExtra(EXTRA_IS_NSFW, false); boolean useBottomAppBar = mSharedPreferences.getBoolean(SharedPreferencesUtils.USE_BOTTOM_TOOLBAR_IN_MEDIA_VIEWER, false); if (postTitle != null) { @@ -368,6 +371,7 @@ public class ViewImageOrGifActivity extends AppCompatActivity implements SetAsWa 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); + intent.putExtra(DownloadMediaService.EXTRA_IS_NSFW, isNsfw); ContextCompat.startForegroundService(this, intent); Toast.makeText(this, R.string.download_started, Toast.LENGTH_SHORT).show(); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewRedditGalleryActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewRedditGalleryActivity.java index 753c2767..659ff7a8 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewRedditGalleryActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewRedditGalleryActivity.java @@ -57,6 +57,7 @@ public class ViewRedditGalleryActivity extends AppCompatActivity implements SetA public static final String EXTRA_REDDIT_GALLERY = "ERG"; public static final String EXTRA_SUBREDDIT_NAME = "ESN"; + public static final String EXTRA_IS_NSFW = "EIN"; @BindView(R.id.hauler_view_view_reddit_gallery_activity) HaulerView haulerView; @@ -70,6 +71,7 @@ public class ViewRedditGalleryActivity extends AppCompatActivity implements SetA private SectionsPagerAdapter sectionsPagerAdapter; private ArrayList gallery; private String subredditName; + private boolean isNsfw; private boolean useBottomAppBar; @Override @@ -155,6 +157,7 @@ public class ViewRedditGalleryActivity extends AppCompatActivity implements SetA return; } subredditName = getIntent().getStringExtra(EXTRA_SUBREDDIT_NAME); + isNsfw = getIntent().getBooleanExtra(EXTRA_IS_NSFW, false); if (sharedPreferences.getBoolean(SharedPreferencesUtils.SWIPE_VERTICALLY_TO_GO_BACK_FROM_MEDIA, true)) { haulerView.setOnDragDismissedListener(dragDirection -> { @@ -290,6 +293,7 @@ public class ViewRedditGalleryActivity extends AppCompatActivity implements SetA bundle.putString(ViewRedditGalleryVideoFragment.EXTRA_SUBREDDIT_NAME, subredditName); bundle.putInt(ViewRedditGalleryVideoFragment.EXTRA_INDEX, position); bundle.putInt(ViewRedditGalleryVideoFragment.EXTRA_MEDIA_COUNT, gallery.size()); + bundle.putBoolean(ViewRedditGalleryVideoFragment.EXTRA_IS_NSFW, isNsfw); fragment.setArguments(bundle); return fragment; } else { @@ -299,6 +303,7 @@ public class ViewRedditGalleryActivity extends AppCompatActivity implements SetA bundle.putString(ViewRedditGalleryImageOrGifFragment.EXTRA_SUBREDDIT_NAME, subredditName); bundle.putInt(ViewRedditGalleryImageOrGifFragment.EXTRA_INDEX, position); bundle.putInt(ViewRedditGalleryImageOrGifFragment.EXTRA_MEDIA_COUNT, gallery.size()); + bundle.putBoolean(ViewRedditGalleryImageOrGifFragment.EXTRA_IS_NSFW, false); fragment.setArguments(bundle); return fragment; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewVideoActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewVideoActivity.java index 93c9649e..2727e40a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewVideoActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewVideoActivity.java @@ -895,11 +895,13 @@ public class ViewVideoActivity extends AppCompatActivity { 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); + intent.putExtra(DownloadMediaService.EXTRA_IS_NSFW, isNSFW); } else { 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_NSFW, isNSFW); } ContextCompat.startForegroundService(this, intent); Toast.makeText(this, R.string.download_started, Toast.LENGTH_SHORT).show(); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java index edd0ee82..eb2dd24e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java @@ -1968,6 +1968,7 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter videoResponse = downloadFile.downloadFile(videoUrl).execute(); if (videoResponse.isSuccessful() && videoResponse.body() != null) { String externalCacheDirectoryPath = externalCacheDirectory.getAbsolutePath() + "/"; - String destinationFileDirectory = sharedPreferences.getString(SharedPreferencesUtils.VIDEO_DOWNLOAD_LOCATION, ""); + String destinationFileDirectory; + if (isNsfw && sharedPreferences.getBoolean(SharedPreferencesUtils.SEPARATE_NSFW_FOLDER, false)) { + destinationFileDirectory = sharedPreferences.getString(SharedPreferencesUtils.NSFW_DOWNLOAD_LOCATION, ""); + } else { + destinationFileDirectory = sharedPreferences.getString(SharedPreferencesUtils.VIDEO_DOWNLOAD_LOCATION, ""); + } String destinationFileUriString; boolean isDefaultDestination; if (destinationFileDirectory.equals("")) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/settings/DownloadLocationPreferenceFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/settings/DownloadLocationPreferenceFragment.java index 633cd581..331f7de6 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/settings/DownloadLocationPreferenceFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/settings/DownloadLocationPreferenceFragment.java @@ -24,10 +24,12 @@ public class DownloadLocationPreferenceFragment extends PreferenceFragmentCompat private static final int IMAGE_DOWNLOAD_LOCATION_REQUEST_CODE = 10; private static final int GIF_DOWNLOAD_LOCATION_REQUEST_CODE = 11; private static final int VIDEO_DOWNLOAD_LOCATION_REQUEST_CODE = 12; + private static final int NSFW_DOWNLOAD_LOCATION_REQUEST_CODE = 13; Preference imageDownloadLocationPreference; Preference gifDownloadLocationPreference; Preference videoDownloadLocationPreference; + Preference nsfwDownloadLocationPreference; private Activity activity; @Inject @Named("default") @@ -40,7 +42,21 @@ public class DownloadLocationPreferenceFragment extends PreferenceFragmentCompat imageDownloadLocationPreference = findPreference(SharedPreferencesUtils.IMAGE_DOWNLOAD_LOCATION); gifDownloadLocationPreference = findPreference(SharedPreferencesUtils.GIF_DOWNLOAD_LOCATION); videoDownloadLocationPreference = findPreference(SharedPreferencesUtils.VIDEO_DOWNLOAD_LOCATION); + nsfwDownloadLocationPreference = findPreference(SharedPreferencesUtils.NSFW_DOWNLOAD_LOCATION); + if (nsfwDownloadLocationPreference != null) { + String downloadLocation = sharedPreferences.getString(SharedPreferencesUtils.NSFW_DOWNLOAD_LOCATION, ""); + if (!downloadLocation.equals("")) { + nsfwDownloadLocationPreference.setSummary(downloadLocation); + } + + nsfwDownloadLocationPreference.setOnPreferenceClickListener(preference -> { + Intent intent = new Intent(ACTION_OPEN_DOCUMENT_TREE); + intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION); + startActivityForResult(intent, NSFW_DOWNLOAD_LOCATION_REQUEST_CODE); + return true; + }); + } if (imageDownloadLocationPreference != null) { String downloadLocation = sharedPreferences.getString(SharedPreferencesUtils.IMAGE_DOWNLOAD_LOCATION, ""); if (!downloadLocation.equals("")) { 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 285c8d58..6ee508c5 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/SharedPreferencesUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/SharedPreferencesUtils.java @@ -130,6 +130,8 @@ 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 NSFW_DOWNLOAD_LOCATION = "nsfw_download_location"; + public static final String SEPARATE_NSFW_FOLDER = "separate_nsfw_folder"; public static final String SEPARATE_FOLDER_FOR_EACH_SUBREDDIT = "separate_folder_for_each_subreddit"; public static final String VIBRATE_WHEN_ACTION_TRIGGERED = "vibrate_when_action_triggered"; public static final String DISABLE_SWIPING_BETWEEN_TABS = "disable_swiping_between_tabs"; diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index ded35ce5..6d795c87 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -516,6 +516,8 @@ Image Download Location Gif Download Location Video Download Location + NSFW Download Location + Separate Folder for NSFW Posts Separate Folder for Each Subreddit Swipe Action Haptic Feedback diff --git a/app/src/main/res/xml/download_location_preferences.xml b/app/src/main/res/xml/download_location_preferences.xml index e8215fe9..4e2c1f7a 100644 --- a/app/src/main/res/xml/download_location_preferences.xml +++ b/app/src/main/res/xml/download_location_preferences.xml @@ -11,6 +11,15 @@ app:key="video_download_location" app:title="@string/settings_video_download_location_title" /> + + + +