From 3d93cb37b92598969a748934ea582e67bdd6ac54 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Sun, 1 Dec 2019 20:05:27 +0800 Subject: [PATCH] Downloading video without sound is now available. Minor bugs fixed. --- app/src/main/AndroidManifest.xml | 1 + .../Activity/MainActivity.java | 73 +++++++++---------- .../Activity/ViewImageActivity.java | 2 +- .../Activity/ViewVideoActivity.java | 60 ++++++++++++++- .../CommentAndPostRecyclerViewAdapter.java | 5 +- .../Adapter/PostRecyclerViewAdapter.java | 10 ++- .../Fragment/PostFragment.java | 11 ++- .../infinityforreddit/ParsePost.java | 5 ++ .../infinityforreddit/Post.java | 11 +++ .../infinityforreddit/PostDataSource.java | 2 +- .../infinityforreddit/Utils/JSONUtils.java | 1 + gradle.properties | 2 +- 12 files changed, 127 insertions(+), 56 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 3ddc6003..0104eb95 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -21,6 +21,7 @@ android:theme="@style/AppTheme" android:usesCleartextTraffic="true" tools:replace="android:label"> + = Build.VERSION_CODES.Q) { + request.setDestinationInExternalPublicDir(Environment.DIRECTORY_PICTURES, videoFileName); + } else { + String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString(); + File directory = new File(path + "/Infinity/"); + boolean saveToInfinityFolder = true; + if (!directory.exists()) { + if (!directory.mkdir()) { + saveToInfinityFolder = false; + } + } else { + if (directory.isFile()) { + if (!(directory.delete() && directory.mkdir())) { + saveToInfinityFolder = false; + } + } + } + + if (saveToInfinityFolder) { + request.setDestinationInExternalPublicDir(Environment.DIRECTORY_PICTURES + "/Infinity/", videoFileName); + } else { + request.setDestinationInExternalPublicDir(Environment.DIRECTORY_PICTURES, videoFileName); + } + } + + DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE); + + if (manager == null) { + Toast.makeText(this, R.string.download_failed, Toast.LENGTH_SHORT).show(); + return; + } + + manager.enqueue(request); + Toast.makeText(this, R.string.download_started, Toast.LENGTH_SHORT).show(); } @Override 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 bed2287c..2ec8178a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java @@ -474,8 +474,9 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter { Intent intent = new Intent(mActivity, ViewVideoActivity.class); intent.setData(videoUri); - intent.putExtra(ViewVideoActivity.SUBREDDIT_KEY, mPost.getSubredditName()); - intent.putExtra(ViewVideoActivity.ID_KEY, mPost.getId()); + intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_DOWNLOAD_URL, mPost.getVideoDownloadUrl()); + intent.putExtra(ViewVideoActivity.EXTRA_SUBREDDIT, mPost.getSubredditName()); + intent.putExtra(ViewVideoActivity.EXTRA_ID, mPost.getId()); mActivity.startActivity(intent); }); break; 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 25dd2e76..85006d28 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java @@ -473,8 +473,9 @@ public class PostRecyclerViewAdapter extends PagedListAdapter { Intent intent = new Intent(mContext, ViewVideoActivity.class); intent.setData(videoUri); - intent.putExtra(ViewVideoActivity.SUBREDDIT_KEY, subredditName); - intent.putExtra(ViewVideoActivity.ID_KEY, fullName); + intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_DOWNLOAD_URL, post.getVideoDownloadUrl()); + intent.putExtra(ViewVideoActivity.EXTRA_SUBREDDIT, subredditName); + intent.putExtra(ViewVideoActivity.EXTRA_ID, fullName); mContext.startActivity(intent); }); break; @@ -1021,8 +1022,9 @@ public class PostRecyclerViewAdapter extends PagedListAdapter { Intent intent = new Intent(mContext, ViewVideoActivity.class); intent.setData(videoUri); - intent.putExtra(ViewVideoActivity.SUBREDDIT_KEY, subredditName); - intent.putExtra(ViewVideoActivity.ID_KEY, fullName); + intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_DOWNLOAD_URL, post.getVideoDownloadUrl()); + intent.putExtra(ViewVideoActivity.EXTRA_SUBREDDIT, subredditName); + intent.putExtra(ViewVideoActivity.EXTRA_ID, fullName); mContext.startActivity(intent); }); break; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java index 76999e26..53dcb587 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java @@ -230,7 +230,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } }; - mSwipeRefreshLayout.setOnRefreshListener(() -> mPostViewModel.refresh()); + mSwipeRefreshLayout.setOnRefreshListener(this::refresh); TypedValue typedValue = new TypedValue(); activity.getTheme().resolveAttribute(R.attr.cardViewBackgroundColor, typedValue, true); @@ -540,14 +540,13 @@ public class PostFragment extends Fragment implements FragmentCommunicator { @Override public void refresh() { - mPostViewModel.refresh(); - if (isInLazyMode) { - stopLazyMode(); - } - mAdapter.removeFooter(); mFetchPostInfoLinearLayout.setVisibility(View.GONE); hasPost = false; + if (isInLazyMode) { + stopLazyMode(); + } + mPostViewModel.refresh(); } private void showErrorView(int stringResId) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java index bc913cea..2cdf4be1 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java @@ -161,6 +161,7 @@ public class ParsePost { JSONObject redditVideoObject = data.getJSONObject(JSONUtils.MEDIA_KEY).getJSONObject(JSONUtils.REDDIT_VIDEO_KEY); int postType = Post.VIDEO_TYPE; String videoUrl = Html.fromHtml(redditVideoObject.getString(JSONUtils.HLS_URL_KEY)).toString(); + String videoDownloadUrl = redditVideoObject.getString(JSONUtils.FALLBACK_URL_KEY); post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, formattedPostTime, postTimeMillis, title, previewUrl, thumbnailPreviewUrl, permalink, score, postType, voteType, @@ -169,12 +170,15 @@ public class ParsePost { post.setPreviewWidth(previewWidth); post.setPreviewHeight(previewHeight); post.setVideoUrl(videoUrl); + post.setVideoDownloadUrl(videoDownloadUrl); } else if (data.has(JSONUtils.PREVIEW_KEY)) { if (data.getJSONObject(JSONUtils.PREVIEW_KEY).has(JSONUtils.REDDIT_VIDEO_PREVIEW_KEY)) { //Gif video post (HLS) int postType = Post.VIDEO_TYPE; String videoUrl = Html.fromHtml(data.getJSONObject(JSONUtils.PREVIEW_KEY) .getJSONObject(JSONUtils.REDDIT_VIDEO_PREVIEW_KEY).getString(JSONUtils.HLS_URL_KEY)).toString(); + String videoDownloadUrl = data.getJSONObject(JSONUtils.PREVIEW_KEY) + .getJSONObject(JSONUtils.REDDIT_VIDEO_PREVIEW_KEY).getString(JSONUtils.FALLBACK_URL_KEY); post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, formattedPostTime, postTimeMillis, title, previewUrl, thumbnailPreviewUrl, permalink, score, @@ -183,6 +187,7 @@ public class ParsePost { post.setPreviewWidth(previewWidth); post.setPreviewHeight(previewHeight); post.setVideoUrl(videoUrl); + post.setVideoDownloadUrl(videoDownloadUrl); } else { if (url.endsWith("jpg") || url.endsWith("png")) { //Image post diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Post.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Post.java index e2135910..58c1ccea 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Post.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Post.java @@ -44,6 +44,7 @@ public class Post implements Parcelable { private String thumbnailPreviewUrl; private String url; private String videoUrl; + private String videoDownloadUrl; private String permalink; private String flair; private long postTimeMillis; @@ -181,6 +182,7 @@ public class Post implements Parcelable { thumbnailPreviewUrl = in.readString(); url = in.readString(); videoUrl = in.readString(); + videoDownloadUrl = in.readString(); permalink = in.readString(); flair = in.readString(); score = in.readInt(); @@ -293,6 +295,14 @@ public class Post implements Parcelable { this.videoUrl = videoUrl; } + public String getVideoDownloadUrl() { + return videoDownloadUrl; + } + + public void setVideoDownloadUrl(String videoDownloadUrl) { + this.videoDownloadUrl = videoDownloadUrl; + } + public String getPermalink() { return permalink; } @@ -433,6 +443,7 @@ public class Post implements Parcelable { parcel.writeString(thumbnailPreviewUrl); parcel.writeString(url); parcel.writeString(videoUrl); + parcel.writeString(videoDownloadUrl); parcel.writeString(permalink); parcel.writeString(flair); parcel.writeInt(score); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java index 4a0b81c1..17970951 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java @@ -155,7 +155,7 @@ public class PostDataSource extends PageKeyedDataSource { this.params = params; this.callback = callback; - if ("".equals(params.key) || "null".equals(params.key)) { + if (params == null || "".equals(params.key) || "null".equals(params.key)) { return; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/JSONUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/JSONUtils.java index 0ac098ce..d0979f5e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/JSONUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/JSONUtils.java @@ -37,6 +37,7 @@ public class JSONUtils { public static final String MEDIA_KEY = "media"; public static final String REDDIT_VIDEO_KEY = "reddit_video"; public static final String HLS_URL_KEY = "hls_url"; + public static final String FALLBACK_URL_KEY = "fallback_url"; public static final String IS_VIDEO_KEY = "is_video"; public static final String CROSSPOST_PARENT_LIST = "crosspost_parent_list"; public static final String REDDIT_VIDEO_PREVIEW_KEY = "reddit_video_preview"; diff --git a/gradle.properties b/gradle.properties index 9e6fce10..6398c371 100644 --- a/gradle.properties +++ b/gradle.properties @@ -11,7 +11,7 @@ # The setting is particularly useful for tweaking memory settings. android.enableJetifier=true android.useAndroidX=true -org.gradle.jvmargs=-Xmx1536m +org.gradle.jvmargs=-Xmx2048m # When configured, Gradle will run in incubating parallel mode. # This option should only be used with decoupled projects. More details, visit