Separate folder for each subreddit in download locations is available.

This commit is contained in:
Alex Ning 2020-09-10 12:07:59 +08:00
parent 6d8dcc8fe7
commit 2df26c3faa
15 changed files with 188 additions and 78 deletions

View File

@ -82,17 +82,17 @@ public class LinkResolverActivity extends AppCompatActivity {
Intent intent = new Intent(this, ViewImageOrGifActivity.class); Intent intent = new Intent(this, ViewImageOrGifActivity.class);
String url = uri.toString(); String url = uri.toString();
String fileName = url.substring(url.lastIndexOf('/') + 1); String fileName = url.substring(url.lastIndexOf('/') + 1);
intent.putExtra(ViewImageOrGifActivity.IMAGE_URL_KEY, url); intent.putExtra(ViewImageOrGifActivity.EXTRA_IMAGE_URL_KEY, url);
intent.putExtra(ViewImageOrGifActivity.FILE_NAME_KEY, fileName); intent.putExtra(ViewImageOrGifActivity.EXTRA_FILE_NAME_KEY, fileName);
intent.putExtra(ViewImageOrGifActivity.POST_TITLE_KEY, fileName); intent.putExtra(ViewImageOrGifActivity.EXTRA_POST_TITLE_KEY, fileName);
startActivity(intent); startActivity(intent);
} else if (path.endsWith("gif")) { } else if (path.endsWith("gif")) {
Intent intent = new Intent(this, ViewImageOrGifActivity.class); Intent intent = new Intent(this, ViewImageOrGifActivity.class);
String url = uri.toString(); String url = uri.toString();
String fileName = url.substring(url.lastIndexOf('/') + 1); String fileName = url.substring(url.lastIndexOf('/') + 1);
intent.putExtra(ViewImageOrGifActivity.GIF_URL_KEY, url); intent.putExtra(ViewImageOrGifActivity.EXTRA_GIF_URL_KEY, url);
intent.putExtra(ViewImageOrGifActivity.FILE_NAME_KEY, fileName); intent.putExtra(ViewImageOrGifActivity.EXTRA_FILE_NAME_KEY, fileName);
intent.putExtra(ViewImageOrGifActivity.POST_TITLE_KEY, fileName); intent.putExtra(ViewImageOrGifActivity.EXTRA_POST_TITLE_KEY, fileName);
startActivity(intent); startActivity(intent);
} else if (path.endsWith("mp4")) { } else if (path.endsWith("mp4")) {
Intent intent = new Intent(this, ViewVideoActivity.class); Intent intent = new Intent(this, ViewVideoActivity.class);

View File

@ -71,10 +71,11 @@ import ml.docilealligator.infinityforreddit.WallpaperSetter;
public class ViewImageOrGifActivity extends AppCompatActivity implements SetAsWallpaperCallback { public class ViewImageOrGifActivity extends AppCompatActivity implements SetAsWallpaperCallback {
public static final String IMAGE_URL_KEY = "IUK"; public static final String EXTRA_IMAGE_URL_KEY = "EIUK";
public static final String GIF_URL_KEY = "GUK"; public static final String EXTRA_GIF_URL_KEY = "EGUK";
public static final String FILE_NAME_KEY = "FNK"; public static final String EXTRA_FILE_NAME_KEY = "EFNK";
public static final String POST_TITLE_KEY = "PTK"; 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; private static final int PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE = 0;
@BindView(R.id.progress_bar_view_image_or_gif_activity) @BindView(R.id.progress_bar_view_image_or_gif_activity)
ProgressBar mProgressBar; ProgressBar mProgressBar;
@ -90,6 +91,7 @@ public class ViewImageOrGifActivity extends AppCompatActivity implements SetAsWa
private RequestManager glide; private RequestManager glide;
private String mImageUrl; private String mImageUrl;
private String mImageFileName; private String mImageFileName;
private String mSubredditName;
private boolean isGif = true; private boolean isGif = true;
@Override @Override
@ -134,13 +136,14 @@ public class ViewImageOrGifActivity extends AppCompatActivity implements SetAsWa
glide = Glide.with(this); glide = Glide.with(this);
Intent intent = getIntent(); Intent intent = getIntent();
mImageUrl = intent.getStringExtra(GIF_URL_KEY); mImageUrl = intent.getStringExtra(EXTRA_GIF_URL_KEY);
if (mImageUrl == null) { if (mImageUrl == null) {
isGif = false; isGif = false;
mImageUrl = intent.getStringExtra(IMAGE_URL_KEY); mImageUrl = intent.getStringExtra(EXTRA_IMAGE_URL_KEY);
} }
mImageFileName = intent.getStringExtra(FILE_NAME_KEY); mImageFileName = intent.getStringExtra(EXTRA_FILE_NAME_KEY);
String postTitle = intent.getStringExtra(POST_TITLE_KEY); String postTitle = intent.getStringExtra(EXTRA_POST_TITLE_KEY);
mSubredditName = intent.getStringExtra(EXTRA_SUBREDDIT_OR_USERNAME_KEY);
if (postTitle != null) { if (postTitle != null) {
setTitle(Html.fromHtml(String.format("<small>%s</small>", postTitle))); setTitle(Html.fromHtml(String.format("<small>%s</small>", postTitle)));
@ -338,6 +341,7 @@ public class ViewImageOrGifActivity extends AppCompatActivity implements SetAsWa
intent.putExtra(DownloadMediaService.EXTRA_URL, mImageUrl); 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_MEDIA_TYPE, isGif ? DownloadMediaService.EXTRA_MEDIA_TYPE_GIF : DownloadMediaService.EXTRA_MEDIA_TYPE_IMAGE);
intent.putExtra(DownloadMediaService.EXTRA_FILE_NAME, mImageFileName); intent.putExtra(DownloadMediaService.EXTRA_FILE_NAME, mImageFileName);
intent.putExtra(DownloadMediaService.EXTRA_SUBREDDIT_NAME, mSubredditName);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(intent); startForegroundService(intent);
} else { } else {

View File

@ -44,6 +44,7 @@ import ml.docilealligator.infinityforreddit.WallpaperSetter;
public class ViewRedditGalleryActivity extends AppCompatActivity implements SetAsWallpaperCallback { public class ViewRedditGalleryActivity extends AppCompatActivity implements SetAsWallpaperCallback {
public static final String EXTRA_REDDIT_GALLERY = "ERG"; 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) @BindView(R.id.hauler_view_view_reddit_gallery_activity)
HaulerView haulerView; HaulerView haulerView;
@ -51,6 +52,7 @@ public class ViewRedditGalleryActivity extends AppCompatActivity implements SetA
ViewPager viewPager; ViewPager viewPager;
private SectionsPagerAdapter sectionsPagerAdapter; private SectionsPagerAdapter sectionsPagerAdapter;
private ArrayList<Post.Gallery> gallery; private ArrayList<Post.Gallery> gallery;
private String subredditName;
@Inject @Inject
@Named("default") @Named("default")
SharedPreferences sharedPreferences; SharedPreferences sharedPreferences;
@ -97,6 +99,7 @@ public class ViewRedditGalleryActivity extends AppCompatActivity implements SetA
finish(); finish();
return; return;
} }
subredditName = getIntent().getStringExtra(EXTRA_SUBREDDIT_NAME);
haulerView.setOnDragDismissedListener(dragDirection -> { haulerView.setOnDragDismissedListener(dragDirection -> {
int slide = dragDirection == DragDirection.UP ? R.anim.slide_out_up : R.anim.slide_out_down; 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(); ViewRedditGalleryVideoFragment fragment = new ViewRedditGalleryVideoFragment();
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
bundle.putParcelable(ViewRedditGalleryVideoFragment.EXTRA_REDDIT_GALLERY_VIDEO, media); bundle.putParcelable(ViewRedditGalleryVideoFragment.EXTRA_REDDIT_GALLERY_VIDEO, media);
bundle.putString(ViewRedditGalleryVideoFragment.EXTRA_SUBREDDIT_NAME, subredditName);
fragment.setArguments(bundle); fragment.setArguments(bundle);
return fragment; return fragment;
} else { } else {
ViewRedditGalleryImageOrGifFragment fragment = new ViewRedditGalleryImageOrGifFragment(); ViewRedditGalleryImageOrGifFragment fragment = new ViewRedditGalleryImageOrGifFragment();
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
bundle.putParcelable(ViewRedditGalleryImageOrGifFragment.EXTRA_REDDIT_GALLERY_MEDIA, media); bundle.putParcelable(ViewRedditGalleryImageOrGifFragment.EXTRA_REDDIT_GALLERY_MEDIA, media);
bundle.putString(ViewRedditGalleryImageOrGifFragment.EXTRA_SUBREDDIT_NAME, subredditName);
fragment.setArguments(bundle); fragment.setArguments(bundle);
return fragment; return fragment;
} }

View File

@ -357,8 +357,9 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp
glide.load(subredditData.getBannerUrl()).into(bannerImageView); glide.load(subredditData.getBannerUrl()).into(bannerImageView);
bannerImageView.setOnClickListener(view -> { bannerImageView.setOnClickListener(view -> {
Intent intent = new Intent(ViewSubredditDetailActivity.this, ViewImageOrGifActivity.class); Intent intent = new Intent(ViewSubredditDetailActivity.this, ViewImageOrGifActivity.class);
intent.putExtra(ViewImageOrGifActivity.IMAGE_URL_KEY, subredditData.getBannerUrl()); intent.putExtra(ViewImageOrGifActivity.EXTRA_IMAGE_URL_KEY, subredditData.getBannerUrl());
intent.putExtra(ViewImageOrGifActivity.FILE_NAME_KEY, subredditName + "-banner.jpg"); intent.putExtra(ViewImageOrGifActivity.EXTRA_FILE_NAME_KEY, subredditName + "-banner.jpg");
intent.putExtra(ViewImageOrGifActivity.EXTRA_SUBREDDIT_OR_USERNAME_KEY, subredditName);
startActivity(intent); startActivity(intent);
}); });
} }
@ -378,8 +379,9 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp
.into(iconGifImageView); .into(iconGifImageView);
iconGifImageView.setOnClickListener(view -> { iconGifImageView.setOnClickListener(view -> {
Intent intent = new Intent(ViewSubredditDetailActivity.this, ViewImageOrGifActivity.class); Intent intent = new Intent(ViewSubredditDetailActivity.this, ViewImageOrGifActivity.class);
intent.putExtra(ViewImageOrGifActivity.IMAGE_URL_KEY, subredditData.getIconUrl()); intent.putExtra(ViewImageOrGifActivity.EXTRA_IMAGE_URL_KEY, subredditData.getIconUrl());
intent.putExtra(ViewImageOrGifActivity.FILE_NAME_KEY, subredditName + "-icon.jpg"); intent.putExtra(ViewImageOrGifActivity.EXTRA_FILE_NAME_KEY, subredditName + "-icon.jpg");
intent.putExtra(ViewImageOrGifActivity.EXTRA_SUBREDDIT_OR_USERNAME_KEY, subredditName);
startActivity(intent); startActivity(intent);
}); });
} }

View File

@ -305,8 +305,9 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele
glide.load(userData.getBanner()).into(bannerImageView); glide.load(userData.getBanner()).into(bannerImageView);
bannerImageView.setOnClickListener(view -> { bannerImageView.setOnClickListener(view -> {
Intent intent = new Intent(this, ViewImageOrGifActivity.class); Intent intent = new Intent(this, ViewImageOrGifActivity.class);
intent.putExtra(ViewImageOrGifActivity.IMAGE_URL_KEY, userData.getBanner()); intent.putExtra(ViewImageOrGifActivity.EXTRA_IMAGE_URL_KEY, userData.getBanner());
intent.putExtra(ViewImageOrGifActivity.FILE_NAME_KEY, username + "-banner.jpg"); intent.putExtra(ViewImageOrGifActivity.EXTRA_FILE_NAME_KEY, username + "-banner.jpg");
intent.putExtra(ViewImageOrGifActivity.EXTRA_SUBREDDIT_OR_USERNAME_KEY, username);
startActivity(intent); startActivity(intent);
}); });
} }
@ -325,8 +326,9 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele
iconGifImageView.setOnClickListener(view -> { iconGifImageView.setOnClickListener(view -> {
Intent intent = new Intent(this, ViewImageOrGifActivity.class); Intent intent = new Intent(this, ViewImageOrGifActivity.class);
intent.putExtra(ViewImageOrGifActivity.IMAGE_URL_KEY, userData.getIconUrl()); intent.putExtra(ViewImageOrGifActivity.EXTRA_IMAGE_URL_KEY, userData.getIconUrl());
intent.putExtra(ViewImageOrGifActivity.FILE_NAME_KEY, username + "-icon.jpg"); intent.putExtra(ViewImageOrGifActivity.EXTRA_FILE_NAME_KEY, username + "-icon.jpg");
intent.putExtra(ViewImageOrGifActivity.EXTRA_SUBREDDIT_OR_USERNAME_KEY, username);
startActivity(intent); startActivity(intent);
}); });
} }

View File

@ -438,6 +438,7 @@ public class ViewVideoActivity extends AppCompatActivity {
intent.putExtra(DownloadMediaService.EXTRA_URL, videoDownloadUrl); intent.putExtra(DownloadMediaService.EXTRA_URL, videoDownloadUrl);
intent.putExtra(DownloadMediaService.EXTRA_MEDIA_TYPE, DownloadMediaService.EXTRA_MEDIA_TYPE_VIDEO); intent.putExtra(DownloadMediaService.EXTRA_MEDIA_TYPE, DownloadMediaService.EXTRA_MEDIA_TYPE_VIDEO);
intent.putExtra(DownloadMediaService.EXTRA_FILE_NAME, videoFileName); intent.putExtra(DownloadMediaService.EXTRA_FILE_NAME, videoFileName);
intent.putExtra(DownloadMediaService.EXTRA_SUBREDDIT_NAME, subredditName);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(intent); startForegroundService(intent);
} else { } else {

View File

@ -2560,10 +2560,11 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
mActivity.startActivity(intent); mActivity.startActivity(intent);
} else if (mPost.getPostType() == Post.GIF_TYPE) { } else if (mPost.getPostType() == Post.GIF_TYPE) {
Intent intent = new Intent(mActivity, ViewImageOrGifActivity.class); 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"); + "-" + mPost.getId() + ".gif");
intent.putExtra(ViewImageOrGifActivity.GIF_URL_KEY, mPost.getVideoUrl()); intent.putExtra(ViewImageOrGifActivity.EXTRA_GIF_URL_KEY, mPost.getVideoUrl());
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); mActivity.startActivity(intent);
} }
}); });
@ -2655,17 +2656,19 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
mImageView.setOnClickListener(view -> { mImageView.setOnClickListener(view -> {
if (mPost.getPostType() == Post.IMAGE_TYPE) { if (mPost.getPostType() == Post.IMAGE_TYPE) {
Intent intent = new Intent(mActivity, ViewImageOrGifActivity.class); Intent intent = new Intent(mActivity, ViewImageOrGifActivity.class);
intent.putExtra(ViewImageOrGifActivity.IMAGE_URL_KEY, mPost.getUrl()); intent.putExtra(ViewImageOrGifActivity.EXTRA_IMAGE_URL_KEY, mPost.getUrl());
intent.putExtra(ViewImageOrGifActivity.FILE_NAME_KEY, mPost.getSubredditNamePrefixed().substring(2) intent.putExtra(ViewImageOrGifActivity.EXTRA_FILE_NAME_KEY, mPost.getSubredditNamePrefixed().substring(2)
+ "-" + mPost.getId().substring(3) + ".jpg"); + "-" + 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); mActivity.startActivity(intent);
} else if (mPost.getPostType() == Post.GIF_TYPE) { } else if (mPost.getPostType() == Post.GIF_TYPE) {
Intent intent = new Intent(mActivity, ViewImageOrGifActivity.class); 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"); + "-" + mPost.getId() + ".gif");
intent.putExtra(ViewImageOrGifActivity.GIF_URL_KEY, mPost.getVideoUrl()); intent.putExtra(ViewImageOrGifActivity.EXTRA_GIF_URL_KEY, mPost.getVideoUrl());
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); mActivity.startActivity(intent);
} }
}); });
@ -2951,6 +2954,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
mImageView.setOnClickListener(view -> { mImageView.setOnClickListener(view -> {
Intent intent = new Intent(mActivity, ViewRedditGalleryActivity.class); Intent intent = new Intent(mActivity, ViewRedditGalleryActivity.class);
intent.putParcelableArrayListExtra(ViewRedditGalleryActivity.EXTRA_REDDIT_GALLERY, mPost.getGallery()); intent.putParcelableArrayListExtra(ViewRedditGalleryActivity.EXTRA_REDDIT_GALLERY, mPost.getGallery());
intent.putExtra(ViewRedditGalleryActivity.EXTRA_SUBREDDIT_NAME, mPost.getSubredditName());
mActivity.startActivity(intent); mActivity.startActivity(intent);
}); });

View File

@ -2049,10 +2049,11 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView
mActivity.startActivity(intent); mActivity.startActivity(intent);
} else if (post.getPostType() == Post.GIF_TYPE) { } else if (post.getPostType() == Post.GIF_TYPE) {
Intent intent = new Intent(mActivity, ViewImageOrGifActivity.class); Intent intent = new Intent(mActivity, ViewImageOrGifActivity.class);
intent.putExtra(ViewImageOrGifActivity.FILE_NAME_KEY, post.getSubredditName() intent.putExtra(ViewImageOrGifActivity.EXTRA_FILE_NAME_KEY, post.getSubredditName()
+ "-" + post.getId() + ".gif"); + "-" + post.getId() + ".gif");
intent.putExtra(ViewImageOrGifActivity.GIF_URL_KEY, post.getVideoUrl()); intent.putExtra(ViewImageOrGifActivity.EXTRA_GIF_URL_KEY, post.getVideoUrl());
intent.putExtra(ViewImageOrGifActivity.POST_TITLE_KEY, post.getTitle()); intent.putExtra(ViewImageOrGifActivity.EXTRA_POST_TITLE_KEY, post.getTitle());
intent.putExtra(ViewImageOrGifActivity.EXTRA_SUBREDDIT_OR_USERNAME_KEY, post.getSubredditName());
mActivity.startActivity(intent); mActivity.startActivity(intent);
} }
} }
@ -2152,17 +2153,19 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView
if (post != null) { if (post != null) {
if (post.getPostType() == Post.IMAGE_TYPE) { if (post.getPostType() == Post.IMAGE_TYPE) {
Intent intent = new Intent(mActivity, ViewImageOrGifActivity.class); Intent intent = new Intent(mActivity, ViewImageOrGifActivity.class);
intent.putExtra(ViewImageOrGifActivity.IMAGE_URL_KEY, post.getUrl()); intent.putExtra(ViewImageOrGifActivity.EXTRA_IMAGE_URL_KEY, post.getUrl());
intent.putExtra(ViewImageOrGifActivity.FILE_NAME_KEY, post.getSubredditName() intent.putExtra(ViewImageOrGifActivity.EXTRA_FILE_NAME_KEY, post.getSubredditName()
+ "-" + post.getId() + ".jpg"); + "-" + post.getId() + ".jpg");
intent.putExtra(ViewImageOrGifActivity.POST_TITLE_KEY, post.getTitle()); intent.putExtra(ViewImageOrGifActivity.EXTRA_POST_TITLE_KEY, post.getTitle());
intent.putExtra(ViewImageOrGifActivity.EXTRA_SUBREDDIT_OR_USERNAME_KEY, post.getSubredditName());
mActivity.startActivity(intent); mActivity.startActivity(intent);
} else { } else {
Intent intent = new Intent(mActivity, ViewImageOrGifActivity.class); Intent intent = new Intent(mActivity, ViewImageOrGifActivity.class);
intent.putExtra(ViewImageOrGifActivity.FILE_NAME_KEY, post.getSubredditName() intent.putExtra(ViewImageOrGifActivity.EXTRA_FILE_NAME_KEY, post.getSubredditName()
+ "-" + post.getId() + ".gif"); + "-" + post.getId() + ".gif");
intent.putExtra(ViewImageOrGifActivity.GIF_URL_KEY, post.getVideoUrl()); intent.putExtra(ViewImageOrGifActivity.EXTRA_GIF_URL_KEY, post.getVideoUrl());
intent.putExtra(ViewImageOrGifActivity.POST_TITLE_KEY, post.getTitle()); intent.putExtra(ViewImageOrGifActivity.EXTRA_POST_TITLE_KEY, post.getTitle());
intent.putExtra(ViewImageOrGifActivity.EXTRA_SUBREDDIT_OR_USERNAME_KEY, post.getSubredditName());
mActivity.startActivity(intent); mActivity.startActivity(intent);
} }
} }
@ -2474,6 +2477,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView
if (post != null) { if (post != null) {
Intent intent = new Intent(mActivity, ViewRedditGalleryActivity.class); Intent intent = new Intent(mActivity, ViewRedditGalleryActivity.class);
intent.putParcelableArrayListExtra(ViewRedditGalleryActivity.EXTRA_REDDIT_GALLERY, post.getGallery()); intent.putParcelableArrayListExtra(ViewRedditGalleryActivity.EXTRA_REDDIT_GALLERY, post.getGallery());
intent.putExtra(ViewRedditGalleryActivity.EXTRA_SUBREDDIT_NAME, post.getSubredditName());
mActivity.startActivity(intent); mActivity.startActivity(intent);
} }
}); });
@ -2783,9 +2787,10 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView
switch (post.getPostType()) { switch (post.getPostType()) {
case Post.IMAGE_TYPE: { case Post.IMAGE_TYPE: {
Intent intent = new Intent(mActivity, ViewImageOrGifActivity.class); Intent intent = new Intent(mActivity, ViewImageOrGifActivity.class);
intent.putExtra(ViewImageOrGifActivity.IMAGE_URL_KEY, post.getUrl()); intent.putExtra(ViewImageOrGifActivity.EXTRA_IMAGE_URL_KEY, post.getUrl());
intent.putExtra(ViewImageOrGifActivity.FILE_NAME_KEY, post.getSubredditName() intent.putExtra(ViewImageOrGifActivity.EXTRA_FILE_NAME_KEY, post.getSubredditName()
+ "-" + post.getId() + ".jpg"); + "-" + post.getId() + ".jpg");
intent.putExtra(ViewImageOrGifActivity.EXTRA_SUBREDDIT_OR_USERNAME_KEY, post.getSubredditName());
mActivity.startActivity(intent); mActivity.startActivity(intent);
break; break;
} }
@ -2804,9 +2809,10 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView
case Post.GIF_TYPE: { case Post.GIF_TYPE: {
Intent intent = new Intent(mActivity, ViewImageOrGifActivity.class); Intent intent = new Intent(mActivity, ViewImageOrGifActivity.class);
intent.setData(Uri.parse(post.getVideoUrl())); intent.setData(Uri.parse(post.getVideoUrl()));
intent.putExtra(ViewImageOrGifActivity.FILE_NAME_KEY, post.getSubredditName() intent.putExtra(ViewImageOrGifActivity.EXTRA_FILE_NAME_KEY, post.getSubredditName()
+ "-" + post.getId() + ".gif"); + "-" + post.getId() + ".gif");
intent.putExtra(ViewImageOrGifActivity.GIF_URL_KEY, post.getVideoUrl()); intent.putExtra(ViewImageOrGifActivity.EXTRA_GIF_URL_KEY, post.getVideoUrl());
intent.putExtra(ViewImageOrGifActivity.EXTRA_SUBREDDIT_OR_USERNAME_KEY, post.getSubredditName());
mActivity.startActivity(intent); mActivity.startActivity(intent);
break; break;
} }
@ -2823,6 +2829,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView
case Post.GALLERY_TYPE: { case Post.GALLERY_TYPE: {
Intent intent = new Intent(mActivity, ViewRedditGalleryActivity.class); Intent intent = new Intent(mActivity, ViewRedditGalleryActivity.class);
intent.putExtra(ViewRedditGalleryActivity.EXTRA_REDDIT_GALLERY, post.getGallery()); intent.putExtra(ViewRedditGalleryActivity.EXTRA_REDDIT_GALLERY, post.getGallery());
intent.putExtra(ViewRedditGalleryActivity.EXTRA_SUBREDDIT_NAME, post.getSubredditName());
mActivity.startActivity(intent); mActivity.startActivity(intent);
} }
} }
@ -2839,6 +2846,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView
if (post.getPostType() == Post.GALLERY_TYPE) { if (post.getPostType() == Post.GALLERY_TYPE) {
Intent intent = new Intent(mActivity, ViewRedditGalleryActivity.class); Intent intent = new Intent(mActivity, ViewRedditGalleryActivity.class);
intent.putExtra(ViewRedditGalleryActivity.EXTRA_REDDIT_GALLERY, post.getGallery()); intent.putExtra(ViewRedditGalleryActivity.EXTRA_REDDIT_GALLERY, post.getGallery());
intent.putExtra(ViewRedditGalleryActivity.EXTRA_SUBREDDIT_NAME, post.getSubredditName());
mActivity.startActivity(intent); mActivity.startActivity(intent);
} else { } else {
Intent intent = new Intent(mActivity, LinkResolverActivity.class); Intent intent = new Intent(mActivity, LinkResolverActivity.class);

View File

@ -58,6 +58,7 @@ import ml.docilealligator.infinityforreddit.SetAsWallpaperCallback;
public class ViewRedditGalleryImageOrGifFragment extends Fragment { public class ViewRedditGalleryImageOrGifFragment extends Fragment {
public static final String EXTRA_REDDIT_GALLERY_MEDIA = "ERGM"; public static final String EXTRA_REDDIT_GALLERY_MEDIA = "ERGM";
public static final String EXTRA_SUBREDDIT_NAME = "ESN";
private static final int PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE = 0; private static final int PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE = 0;
@BindView(R.id.progress_bar_view_reddit_gallery_image_or_gif_fragment) @BindView(R.id.progress_bar_view_reddit_gallery_image_or_gif_fragment)
@ -70,6 +71,7 @@ public class ViewRedditGalleryImageOrGifFragment extends Fragment {
private ViewRedditGalleryActivity activity; private ViewRedditGalleryActivity activity;
private RequestManager glide; private RequestManager glide;
private Post.Gallery media; private Post.Gallery media;
private String subredditName;
private boolean isDownloading = false; private boolean isDownloading = false;
private boolean isActionBarHidden = false; private boolean isActionBarHidden = false;
@ -89,6 +91,7 @@ public class ViewRedditGalleryImageOrGifFragment extends Fragment {
setHasOptionsMenu(true); setHasOptionsMenu(true);
media = getArguments().getParcelable(EXTRA_REDDIT_GALLERY_MEDIA); media = getArguments().getParcelable(EXTRA_REDDIT_GALLERY_MEDIA);
subredditName = getArguments().getString(EXTRA_SUBREDDIT_NAME);
glide = Glide.with(activity); glide = Glide.with(activity);
imageView.setImageViewFactory(new GlideImageViewFactory()); imageView.setImageViewFactory(new GlideImageViewFactory());
@ -269,6 +272,7 @@ public class ViewRedditGalleryImageOrGifFragment extends Fragment {
intent.putExtra(DownloadMediaService.EXTRA_URL, media.url); 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_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); intent.putExtra(DownloadMediaService.EXTRA_FILE_NAME, media.fileName);
intent.putExtra(DownloadMediaService.EXTRA_SUBREDDIT_NAME, subredditName);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
activity.startForegroundService(intent); activity.startForegroundService(intent);
} else { } else {

View File

@ -54,6 +54,7 @@ import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils;
public class ViewRedditGalleryVideoFragment extends Fragment { public class ViewRedditGalleryVideoFragment extends Fragment {
public static final String EXTRA_REDDIT_GALLERY_VIDEO = "EIV"; 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 IS_MUTE_STATE = "IMS";
private static final String POSITION_STATE = "PS"; private static final String POSITION_STATE = "PS";
private static final int PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE = 0; private static final int PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE = 0;
@ -63,6 +64,7 @@ public class ViewRedditGalleryVideoFragment extends Fragment {
ImageButton muteButton; ImageButton muteButton;
private Activity activity; private Activity activity;
private Post.Gallery galleryVideo; private Post.Gallery galleryVideo;
private String subredditName;
private SimpleExoPlayer player; private SimpleExoPlayer player;
private DataSource.Factory dataSourceFactory; private DataSource.Factory dataSourceFactory;
private boolean wasPlaying = false; private boolean wasPlaying = false;
@ -91,6 +93,7 @@ public class ViewRedditGalleryVideoFragment extends Fragment {
activity.setVolumeControlStream(AudioManager.STREAM_MUSIC); activity.setVolumeControlStream(AudioManager.STREAM_MUSIC);
galleryVideo = getArguments().getParcelable(EXTRA_REDDIT_GALLERY_VIDEO); galleryVideo = getArguments().getParcelable(EXTRA_REDDIT_GALLERY_VIDEO);
subredditName = getArguments().getString(EXTRA_SUBREDDIT_NAME);
if (!mSharedPreferences.getBoolean(SharedPreferencesUtils.VIDEO_PLAYER_IGNORE_NAV_BAR, false)) { if (!mSharedPreferences.getBoolean(SharedPreferencesUtils.VIDEO_PLAYER_IGNORE_NAV_BAR, false)) {
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT || getResources().getBoolean(R.bool.isTablet)) { 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_URL, galleryVideo.url);
intent.putExtra(DownloadMediaService.EXTRA_MEDIA_TYPE, DownloadMediaService.EXTRA_MEDIA_TYPE_VIDEO); intent.putExtra(DownloadMediaService.EXTRA_MEDIA_TYPE, DownloadMediaService.EXTRA_MEDIA_TYPE_VIDEO);
intent.putExtra(DownloadMediaService.EXTRA_FILE_NAME, galleryVideo.fileName); intent.putExtra(DownloadMediaService.EXTRA_FILE_NAME, galleryVideo.fileName);
intent.putExtra(DownloadMediaService.EXTRA_SUBREDDIT_NAME, subredditName);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
activity.startForegroundService(intent); activity.startForegroundService(intent);
} else { } else {

View File

@ -38,8 +38,8 @@ import ml.docilealligator.infinityforreddit.API.DownloadFile;
import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeWrapper;
import ml.docilealligator.infinityforreddit.Event.DownloadMediaEvent; import ml.docilealligator.infinityforreddit.Event.DownloadMediaEvent;
import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.Infinity;
import ml.docilealligator.infinityforreddit.Utils.NotificationUtils;
import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.Utils.NotificationUtils;
import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils;
import okhttp3.ResponseBody; import okhttp3.ResponseBody;
import retrofit2.Call; import retrofit2.Call;
@ -52,6 +52,7 @@ import static android.os.Environment.getExternalStoragePublicDirectory;
public class DownloadMediaService extends Service { public class DownloadMediaService extends Service {
public static final String EXTRA_URL = "EU"; public static final String EXTRA_URL = "EU";
public static final String EXTRA_FILE_NAME = "EFN"; 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 String EXTRA_MEDIA_TYPE = "EIG";
public static final int EXTRA_MEDIA_TYPE_IMAGE = 0; 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_GIF = 1;
@ -70,6 +71,7 @@ public class DownloadMediaService extends Service {
@Inject @Inject
CustomThemeWrapper mCustomThemeWrapper; CustomThemeWrapper mCustomThemeWrapper;
private int mediaType; private int mediaType;
private String mimeType;
public DownloadMediaService() { public DownloadMediaService() {
} }
@ -128,9 +130,10 @@ public class DownloadMediaService extends Service {
((Infinity) getApplication()).getAppComponent().inject(this); ((Infinity) getApplication()).getAppComponent().inject(this);
String fileUrl = intent.getStringExtra(EXTRA_URL); String fileUrl = intent.getStringExtra(EXTRA_URL);
String fileName; final String[] fileName = {intent.getStringExtra(EXTRA_FILE_NAME)};
fileName = intent.getStringExtra(EXTRA_FILE_NAME); String subredditName = intent.getStringExtra(EXTRA_SUBREDDIT_NAME);
mediaType = intent.getIntExtra(EXTRA_MEDIA_TYPE, EXTRA_MEDIA_TYPE_IMAGE); 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) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel serviceChannel; NotificationChannel serviceChannel;
@ -148,22 +151,24 @@ public class DownloadMediaService extends Service {
case EXTRA_MEDIA_TYPE_GIF: case EXTRA_MEDIA_TYPE_GIF:
startForeground( startForeground(
NotificationUtils.DOWNLOAD_GIF_NOTIFICATION_ID, NotificationUtils.DOWNLOAD_GIF_NOTIFICATION_ID,
createNotification(R.string.downloading_gif, fileName, null) createNotification(R.string.downloading_gif, fileName[0], null)
); );
break; break;
case EXTRA_MEDIA_TYPE_VIDEO: case EXTRA_MEDIA_TYPE_VIDEO:
startForeground( startForeground(
NotificationUtils.DOWNLOAD_VIDEO_NOTIFICATION_ID, NotificationUtils.DOWNLOAD_VIDEO_NOTIFICATION_ID,
createNotification(R.string.downloading_video, fileName, null) createNotification(R.string.downloading_video, fileName[0], null)
); );
break; break;
default: default:
startForeground( startForeground(
NotificationUtils.DOWNLOAD_IMAGE_NOTIFICATION_ID, 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<ResponseBody>() { retrofit.create(DownloadFile.class).downloadFile(fileUrl).enqueue(new Callback<ResponseBody>() {
@Override @Override
public void onResponse(@NonNull Call<ResponseBody> call, @NonNull Response<ResponseBody> response) { public void onResponse(@NonNull Call<ResponseBody> call, @NonNull Response<ResponseBody> response) {
@ -175,42 +180,75 @@ public class DownloadMediaService extends Service {
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) { if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
File directory = getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); File directory = getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
if (directory != null) { 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); File infinityDir = new File(directoryPath);
if (!infinityDir.exists() && !infinityDir.mkdir()) { if (!infinityDir.exists() && !infinityDir.mkdirs()) {
downloadFinished(null, fileName, ERROR_CANNOT_GET_DESTINATION_DIRECTORY); downloadFinished(null, fileName[0], ERROR_CANNOT_GET_DESTINATION_DIRECTORY);
return; return;
} }
destinationFileUriString = directoryPath + fileName; destinationFileUriString = directoryPath + fileName[0];
} else { } else {
downloadFinished(null, fileName, ERROR_CANNOT_GET_DESTINATION_DIRECTORY); downloadFinished(null, fileName[0], ERROR_CANNOT_GET_DESTINATION_DIRECTORY);
return; return;
} }
} else { } 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; isDefaultDestination = true;
} else { } else {
isDefaultDestination = false; 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) { if (picFile == null) {
downloadFinished(null, fileName, ERROR_CANNOT_GET_DESTINATION_DIRECTORY); downloadFinished(null, fileName[0], ERROR_CANNOT_GET_DESTINATION_DIRECTORY);
return; return;
} }
destinationFileUriString = picFile.getUri().toString(); destinationFileUriString = picFile.getUri().toString();
} }
new SaveImageOrGifAndCopyToExternalStorageAsyncTask(response.body(), mediaType, new SaveImageOrGifAndCopyToExternalStorageAsyncTask(response.body(), mediaType,
isDefaultDestination, fileName, destinationFileUriString, getContentResolver(), isDefaultDestination, fileName[0], destinationFileUriString, getContentResolver(),
new SaveImageOrGifAndCopyToExternalStorageAsyncTask.SaveImageOrGifAndCopyToExternalStorageAsyncTaskListener() { new SaveImageOrGifAndCopyToExternalStorageAsyncTask.SaveImageOrGifAndCopyToExternalStorageAsyncTaskListener() {
@Override @Override
public void finished(Uri destinationFileUri, int errorCode) { public void finished(Uri destinationFileUri, int errorCode) {
downloadFinished(destinationFileUri, fileName, errorCode); downloadFinished(destinationFileUri, fileName[0], errorCode);
} }
@Override @Override
public void updateProgressNotification(int stringResId) { public void updateProgressNotification(int stringResId) {
updateNotification(stringResId, fileName, null); updateNotification(stringResId, fileName[0], null);
} }
}).execute(); }).execute();
} }
@ -218,7 +256,7 @@ public class DownloadMediaService extends Service {
@Override @Override
public void onFailure(@NonNull Call<ResponseBody> call, @NonNull Throwable t) { public void onFailure(@NonNull Call<ResponseBody> 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); return super.onStartCommand(intent, flags, startId);
@ -263,7 +301,7 @@ public class DownloadMediaService extends Service {
(path, uri) -> { (path, uri) -> {
Intent intent = new Intent(); Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW); 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); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
updateNotification(R.string.downloading_media_finished, fileName, pendingIntent); updateNotification(R.string.downloading_media_finished, fileName, pendingIntent);
EventBus.getDefault().post(new DownloadMediaEvent(true)); EventBus.getDefault().post(new DownloadMediaEvent(true));
@ -362,12 +400,10 @@ public class DownloadMediaService extends Service {
outputStream.flush(); outputStream.flush();
} else { } else {
String relativeLocation = Environment.DIRECTORY_PICTURES + "/Infinity/";
ContentValues contentValues = new ContentValues(); ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, destinationFileName); contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, destinationFileName);
contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "image/*"); contentValues.put(MediaStore.MediaColumns.MIME_TYPE, mediaType == EXTRA_MEDIA_TYPE_VIDEO ? "video/*" : "image/*");
contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, relativeLocation); contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, destinationFileUriString);
contentValues.put(MediaStore.Images.Media.IS_PENDING, 1); contentValues.put(MediaStore.Images.Media.IS_PENDING, 1);
final Uri contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; final Uri contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;

View File

@ -94,7 +94,8 @@ public class DownloadRedditVideoService extends Service {
String videoUrl = intent.getStringExtra(EXTRA_VIDEO_URL); String videoUrl = intent.getStringExtra(EXTRA_VIDEO_URL);
String audioUrl = videoUrl.substring(0, videoUrl.lastIndexOf('/')) + "/DASH_audio.mp4"; 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) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel serviceChannel; NotificationChannel serviceChannel;
@ -110,13 +111,15 @@ public class DownloadRedditVideoService extends Service {
startForeground( startForeground(
NotificationUtils.DOWNLOAD_REDDIT_VIDEO_NOTIFICATION_ID, 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); DownloadFile downloadFile = retrofit.create(DownloadFile.class);
boolean separateDownloadFolder = sharedPreferences.getBoolean(SharedPreferencesUtils.SEPARATE_FOLDER_FOR_EACH_SUBREDDIT, false);
File directory = getExternalCacheDir(); File directory = getExternalCacheDir();
String destinationFileName = fileName + ".mp4"; String destinationFileName = fileNameWithoutExtension[0] + ".mp4";
if (directory != null) { if (directory != null) {
String directoryPath = directory.getAbsolutePath() + "/"; String directoryPath = directory.getAbsolutePath() + "/";
downloadFile.downloadFile(videoUrl).enqueue(new Callback<ResponseBody>() { downloadFile.downloadFile(videoUrl).enqueue(new Callback<ResponseBody>() {
@ -130,7 +133,7 @@ public class DownloadRedditVideoService extends Service {
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) { if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
File directory = getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); File directory = getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
if (directory != null) { if (directory != null) {
String directoryPath = directory.getAbsolutePath() + "/Infinity/"; String directoryPath = separateDownloadFolder ? directory.getAbsolutePath() + "/Infinity/" + subredditName + "/" : directory.getAbsolutePath() + "/Infinity/";
File infinityDir = new File(directoryPath); File infinityDir = new File(directoryPath);
if (!infinityDir.exists() && !infinityDir.mkdir()) { if (!infinityDir.exists() && !infinityDir.mkdir()) {
downloadFinished(null, destinationFileName, ERROR_CANNOT_GET_DESTINATION_DIRECTORY); downloadFinished(null, destinationFileName, ERROR_CANNOT_GET_DESTINATION_DIRECTORY);
@ -142,12 +145,42 @@ public class DownloadRedditVideoService extends Service {
return; return;
} }
} else { } else {
destinationFileUriString = Environment.DIRECTORY_PICTURES + "/Infinity/"; destinationFileUriString = separateDownloadFolder ? Environment.DIRECTORY_PICTURES + "/Infinity/" + subredditName + "/" : Environment.DIRECTORY_PICTURES + "/Infinity/";
} }
isDefaultDestination = true; isDefaultDestination = true;
} else { } else {
isDefaultDestination = false; 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) { if (picFile == null) {
downloadFinished(null, destinationFileName, ERROR_CANNOT_GET_DESTINATION_DIRECTORY); downloadFinished(null, destinationFileName, ERROR_CANNOT_GET_DESTINATION_DIRECTORY);
return; return;
@ -160,9 +193,9 @@ public class DownloadRedditVideoService extends Service {
@Override @Override
public void onResponse(@NonNull Call<ResponseBody> call, @NonNull Response<ResponseBody> audioResponse) { public void onResponse(@NonNull Call<ResponseBody> call, @NonNull Response<ResponseBody> audioResponse) {
if (audioResponse.isSuccessful() && audioResponse.body() != null) { if (audioResponse.isSuccessful() && audioResponse.body() != null) {
String videoFilePath = directoryPath + fileName + "-cache.mp4"; String videoFilePath = directoryPath + fileNameWithoutExtension[0] + "-cache.mp4";
String audioFilePath = directoryPath + fileName + "-cache.mp3"; String audioFilePath = directoryPath + fileNameWithoutExtension[0] + "-cache.mp3";
String outputFilePath = directoryPath + fileName + ".mp4"; String outputFilePath = directoryPath + fileNameWithoutExtension[0] + ".mp4";
new SaveTempMuxAndCopyAsyncTask(videoResponse.body(), new SaveTempMuxAndCopyAsyncTask(videoResponse.body(),
audioResponse.body(), videoFilePath, audioFilePath, outputFilePath, audioResponse.body(), videoFilePath, audioFilePath, outputFilePath,
destinationFileName, destinationFileUriString, isDefaultDestination, destinationFileName, destinationFileUriString, isDefaultDestination,
@ -182,7 +215,7 @@ public class DownloadRedditVideoService extends Service {
} }
}).execute(); }).execute();
} else { } else {
String videoFilePath = directoryPath + fileName + "-cache.mp4"; String videoFilePath = directoryPath + fileNameWithoutExtension[0] + "-cache.mp4";
new SaveTempMuxAndCopyAsyncTask(videoResponse.body(), new SaveTempMuxAndCopyAsyncTask(videoResponse.body(),
null, videoFilePath, null, null, null, videoFilePath, null, null,
destinationFileName, destinationFileUriString, isDefaultDestination, destinationFileName, destinationFileUriString, isDefaultDestination,
@ -204,7 +237,7 @@ public class DownloadRedditVideoService extends Service {
@Override @Override
public void onFailure(@NonNull Call<ResponseBody> call, @NonNull Throwable t) { public void onFailure(@NonNull Call<ResponseBody> call, @NonNull Throwable t) {
String videoFilePath = directoryPath + fileName + "-cache.mp4"; String videoFilePath = directoryPath + fileNameWithoutExtension[0] + "-cache.mp4";
new SaveTempMuxAndCopyAsyncTask(videoResponse.body(), new SaveTempMuxAndCopyAsyncTask(videoResponse.body(),
null, videoFilePath, null, null, null, videoFilePath, null, null,
destinationFileName, destinationFileUriString, isDefaultDestination, destinationFileName, destinationFileUriString, isDefaultDestination,

View File

@ -120,6 +120,7 @@ public class SharedPreferencesUtils {
public static final String IMAGE_DOWNLOAD_LOCATION = "image_download_location"; public static final String IMAGE_DOWNLOAD_LOCATION = "image_download_location";
public static final String GIF_DOWNLOAD_LOCATION = "gif_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 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_TABS_SHARED_PREFERENCES_FILE = "ml.docilealligator.infinityforreddit.main_page_tabs";
public static final String MAIN_PAGE_TAB_1_TITLE = "_main_page_tab_1_title"; public static final String MAIN_PAGE_TAB_1_TITLE = "_main_page_tab_1_title";

View File

@ -477,6 +477,7 @@
<string name="settings_image_download_location_title">Image Download Location</string> <string name="settings_image_download_location_title">Image Download Location</string>
<string name="settings_gif_download_location_title">Gif Download Location</string> <string name="settings_gif_download_location_title">Gif Download Location</string>
<string name="settings_video_download_location_title">Video Download Location</string> <string name="settings_video_download_location_title">Video Download Location</string>
<string name="settings_separate_folder_for_each_subreddit">Separate Folder for Each Subreddit</string>
<string name="no_link_available">Cannot get the link</string> <string name="no_link_available">Cannot get the link</string>

View File

@ -10,4 +10,9 @@
<Preference <Preference
app:key="video_download_location" app:key="video_download_location"
app:title="@string/settings_video_download_location_title" /> app:title="@string/settings_video_download_location_title" />
<SwitchPreference
app:defaultValue="false"
app:key="separate_folder_for_each_subreddit"
app:title="@string/settings_separate_folder_for_each_subreddit" />
</PreferenceScreen> </PreferenceScreen>