From 8c445fb766b071d8a7f9cca0f8b0f31dbbe6195b Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Sun, 12 Dec 2021 22:09:09 +0800 Subject: Streamable videos autoplay. --- .../FetchGfycatOrRedgifsVideoLinks.java | 26 +--- .../infinityforreddit/FetchStreamableVideo.java | 26 ++++ .../adapters/PostDetailRecyclerViewAdapter.java | 79 +++++++--- .../adapters/PostRecyclerViewAdapter.java | 170 ++++++++++++++------- .../infinityforreddit/fragments/PostFragment.java | 17 ++- .../fragments/ViewPostDetailFragment.java | 8 +- .../infinityforreddit/post/Post.java | 14 +- 7 files changed, 224 insertions(+), 116 deletions(-) (limited to 'app/src') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchGfycatOrRedgifsVideoLinks.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchGfycatOrRedgifsVideoLinks.java index 16e27ff5..e97b0ff4 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchGfycatOrRedgifsVideoLinks.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchGfycatOrRedgifsVideoLinks.java @@ -15,18 +15,12 @@ import retrofit2.Response; import retrofit2.Retrofit; public class FetchGfycatOrRedgifsVideoLinks { - private FetchGfycatOrRedgifsVideoLinksListener fetchGfycatOrRedgifsVideoLinksListener; - Call gfycatCall; public interface FetchGfycatOrRedgifsVideoLinksListener { void success(String webm, String mp4); void failed(int errorCode); } - public FetchGfycatOrRedgifsVideoLinks(FetchGfycatOrRedgifsVideoLinksListener fetchGfycatOrRedgifsVideoLinksListener) { - this.fetchGfycatOrRedgifsVideoLinksListener = fetchGfycatOrRedgifsVideoLinksListener; - } - public static void fetchGfycatOrRedgifsVideoLinks(Executor executor, Handler handler, Retrofit gfycatRetrofit, String gfycatId, FetchGfycatOrRedgifsVideoLinksListener fetchGfycatOrRedgifsVideoLinksListener) { @@ -46,20 +40,20 @@ public class FetchGfycatOrRedgifsVideoLinks { } - public void fetchGfycatOrRedgifsVideoLinksInRecyclerViewAdapter(Executor executor, Handler handler, - Retrofit gfycatRetrofit, Retrofit redgifsRetrofit, - String gfycatId, boolean isGfycatVideo, - boolean automaticallyTryRedgifs) { + public static void fetchGfycatOrRedgifsVideoLinksInRecyclerViewAdapter(Executor executor, Handler handler, + Call gfycatCall, + String gfycatId, boolean isGfycatVideo, + boolean automaticallyTryRedgifs, + FetchGfycatOrRedgifsVideoLinksListener fetchGfycatOrRedgifsVideoLinksListener) { executor.execute(() -> { - gfycatCall = (isGfycatVideo ? gfycatRetrofit : redgifsRetrofit).create(GfycatAPI.class).getGfycatData(gfycatId); try { Response response = gfycatCall.execute(); if (response.isSuccessful()) { parseGfycatVideoLinks(handler, response.body(), fetchGfycatOrRedgifsVideoLinksListener); } else { if (response.code() == 404 && isGfycatVideo && automaticallyTryRedgifs) { - fetchGfycatOrRedgifsVideoLinksInRecyclerViewAdapter(executor, handler, gfycatRetrofit, - redgifsRetrofit, gfycatId, false, false); + fetchGfycatOrRedgifsVideoLinksInRecyclerViewAdapter(executor, handler, gfycatCall.clone(), + gfycatId, false, false, fetchGfycatOrRedgifsVideoLinksListener); } else { handler.post(() -> fetchGfycatOrRedgifsVideoLinksListener.failed(response.code())); } @@ -71,12 +65,6 @@ public class FetchGfycatOrRedgifsVideoLinks { }); } - public void cancel() { - if (gfycatCall != null && !gfycatCall.isCanceled()) { - gfycatCall.cancel(); - } - } - private static void parseGfycatVideoLinks(Handler handler, String response, FetchGfycatOrRedgifsVideoLinksListener fetchGfycatOrRedgifsVideoLinksListener) { try { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchStreamableVideo.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchStreamableVideo.java index ac7c2116..c310539a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchStreamableVideo.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchStreamableVideo.java @@ -12,6 +12,7 @@ import java.util.concurrent.Executor; import ml.docilealligator.infinityforreddit.apis.StreamableAPI; import ml.docilealligator.infinityforreddit.utils.JSONUtils; +import retrofit2.Call; import retrofit2.Response; import retrofit2.Retrofit; @@ -42,6 +43,31 @@ public class FetchStreamableVideo { }); } + public static void fetchStreamableVideoInRecyclerViewAdapter(Executor executor, Handler handler, Call streamableCall, + FetchStreamableVideoListener fetchStreamableVideoListener) { + executor.execute(() -> { + try { + Response response = streamableCall.execute(); + if (response.isSuccessful()) { + JSONObject jsonObject = new JSONObject(response.body()); + String title = jsonObject.getString(JSONUtils.TITLE_KEY); + JSONObject filesObject = jsonObject.getJSONObject(JSONUtils.FILES_KEY); + StreamableVideo.Media mp4 = parseMedia(filesObject.getJSONObject(JSONUtils.MP4_KEY)); + StreamableVideo.Media mp4Mobile = parseMedia(filesObject.getJSONObject(JSONUtils.MP4_MOBILE_KEY)); + if (mp4 == null && mp4Mobile == null) { + handler.post(fetchStreamableVideoListener::failed); + return; + } + handler.post(() -> fetchStreamableVideoListener.success(new StreamableVideo(title, mp4, mp4Mobile))); + } else { + handler.post(fetchStreamableVideoListener::failed); + } + } catch (IOException | JSONException e) { + e.printStackTrace(); + } + }); + } + @Nullable private static StreamableVideo.Media parseMedia(JSONObject jsonObject) { try { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostDetailRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostDetailRecyclerViewAdapter.java index 4157db56..644c5ddc 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostDetailRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostDetailRecyclerViewAdapter.java @@ -80,10 +80,12 @@ import jp.wasabeef.glide.transformations.BlurTransformation; import jp.wasabeef.glide.transformations.RoundedCornersTransformation; import me.saket.bettermovementmethod.BetterLinkMovementMethod; import ml.docilealligator.infinityforreddit.FetchGfycatOrRedgifsVideoLinks; +import ml.docilealligator.infinityforreddit.FetchStreamableVideo; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.SaveMemoryCenterInisdeDownsampleStrategy; import ml.docilealligator.infinityforreddit.SaveThing; +import ml.docilealligator.infinityforreddit.StreamableVideo; import ml.docilealligator.infinityforreddit.VoteThing; import ml.docilealligator.infinityforreddit.activities.CommentActivity; import ml.docilealligator.infinityforreddit.activities.FilteredPostsActivity; @@ -94,6 +96,8 @@ import ml.docilealligator.infinityforreddit.activities.ViewRedditGalleryActivity import ml.docilealligator.infinityforreddit.activities.ViewSubredditDetailActivity; import ml.docilealligator.infinityforreddit.activities.ViewUserDetailActivity; import ml.docilealligator.infinityforreddit.activities.ViewVideoActivity; +import ml.docilealligator.infinityforreddit.apis.GfycatAPI; +import ml.docilealligator.infinityforreddit.apis.StreamableAPI; import ml.docilealligator.infinityforreddit.asynctasks.LoadSubredditIcon; import ml.docilealligator.infinityforreddit.asynctasks.LoadUserData; import ml.docilealligator.infinityforreddit.bottomsheetfragments.CopyTextBottomSheetFragment; @@ -112,6 +116,7 @@ import ml.docilealligator.infinityforreddit.utils.APIUtils; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; import ml.docilealligator.infinityforreddit.utils.Utils; import pl.droidsonroids.gif.GifImageView; +import retrofit2.Call; import retrofit2.Retrofit; public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter implements CacheManager { @@ -130,6 +135,7 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter fetchGfycatOrStreamableVideoCall; @BindView(R.id.icon_gif_image_view_item_post_detail_video_autoplay) AspectRatioGifImageView mIconGifImageView; @BindView(R.id.subreddit_text_view_item_post_detail_video_autoplay) @@ -1613,14 +1642,14 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter fetchGfycatOrStreamableVideoCall; + //public FetchGfycatOrRedgifsVideoLinks fetchGfycatOrRedgifsVideoLinks; PostVideoAutoplayViewHolder(View itemView) { super(itemView); @@ -2616,14 +2676,14 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter fetchGfycatOrStreamableVideoCall; PostCard2VideoAutoplayViewHolder(View itemView) { super(itemView); @@ -3900,14 +3960,14 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter EventBus.getDefault().post(new PostUpdateEventToPostList(mPost, postListPosition))); @@ -1190,7 +1192,7 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic mPostAdapter = new PostDetailRecyclerViewAdapter(activity, ViewPostDetailFragment.this, mExecutor, mCustomThemeWrapper, mRetrofit, mOauthRetrofit, mGfycatRetrofit, mRedgifsRetrofit, - mRedditDataRoomDatabase, mGlide, mSeparatePostAndComments, + mStreamableRetrofit, mRedditDataRoomDatabase, mGlide, mSeparatePostAndComments, mAccessToken, mAccountName, mPost, mLocale, mSharedPreferences, mNsfwAndSpoilerSharedPreferences, mPostDetailsSharedPreferences, mExoCreator, diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/post/Post.java b/app/src/main/java/ml/docilealligator/infinityforreddit/post/Post.java index 69c93030..c20df496 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/post/Post.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/post/Post.java @@ -55,7 +55,7 @@ public class Post implements Parcelable { private boolean isGfycat; private boolean isRedgifs; private boolean isStreamable; - private boolean loadGfyOrRedgifsVideoSuccess; + private boolean loadGfyOrStreamableVideoSuccess; private String permalink; private String flair; private String awards; @@ -178,7 +178,7 @@ public class Post implements Parcelable { isGfycat = in.readByte() != 0; isRedgifs = in.readByte() != 0; isStreamable = in.readByte() != 0; - loadGfyOrRedgifsVideoSuccess = in.readByte() != 0; + loadGfyOrStreamableVideoSuccess = in.readByte() != 0; permalink = in.readString(); flair = in.readString(); awards = in.readString(); @@ -357,12 +357,12 @@ public class Post implements Parcelable { this.isStreamable = isStreamable; } - public boolean isLoadGfycatOrRedgifsVideoSuccess() { - return loadGfyOrRedgifsVideoSuccess; + public boolean isLoadGfycatOrStreamableVideoSuccess() { + return loadGfyOrStreamableVideoSuccess; } - public void setLoadGfyOrRedgifsVideoSuccess(boolean loadGfyOrRedgifsVideoSuccess) { - this.loadGfyOrRedgifsVideoSuccess = loadGfyOrRedgifsVideoSuccess; + public void setLoadGfyOrStreamableVideoSuccess(boolean loadGfyOrStreamableVideoSuccess) { + this.loadGfyOrStreamableVideoSuccess = loadGfyOrStreamableVideoSuccess; } public String getPermalink() { @@ -556,7 +556,7 @@ public class Post implements Parcelable { parcel.writeByte((byte) (isGfycat ? 1 : 0)); parcel.writeByte((byte) (isRedgifs ? 1 : 0)); parcel.writeByte((byte) (isStreamable ? 1 : 0)); - parcel.writeByte((byte) (loadGfyOrRedgifsVideoSuccess ? 1 : 0)); + parcel.writeByte((byte) (loadGfyOrStreamableVideoSuccess ? 1 : 0)); parcel.writeString(permalink); parcel.writeString(flair); parcel.writeString(awards); -- cgit v1.2.3