diff options
author | Alex Ning <chineseperson5@gmail.com> | 2020-09-10 13:42:37 +0000 |
---|---|---|
committer | Alex Ning <chineseperson5@gmail.com> | 2020-09-10 13:42:37 +0000 |
commit | fc1b1ae58546861edd8f745fee9e807a13d8629a (patch) | |
tree | bbbf50d9a93e25ca9cef8e061ee8bb4bb4945919 /app | |
parent | 24261df3dbc4cfd0843c871b8570587344a4001a (diff) | |
download | infinity-for-reddit-fc1b1ae58546861edd8f745fee9e807a13d8629a.tar infinity-for-reddit-fc1b1ae58546861edd8f745fee9e807a13d8629a.tar.gz infinity-for-reddit-fc1b1ae58546861edd8f745fee9e807a13d8629a.tar.bz2 infinity-for-reddit-fc1b1ae58546861edd8f745fee9e807a13d8629a.tar.lz infinity-for-reddit-fc1b1ae58546861edd8f745fee9e807a13d8629a.tar.xz infinity-for-reddit-fc1b1ae58546861edd8f745fee9e807a13d8629a.tar.zst infinity-for-reddit-fc1b1ae58546861edd8f745fee9e807a13d8629a.zip |
Barebone gfycat and redgifs video autoplay in PostRecyclerViewAdapter.
Diffstat (limited to 'app')
5 files changed, 176 insertions, 17 deletions
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 cde3919c..9315f153 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java @@ -81,6 +81,7 @@ import ml.docilealligator.infinityforreddit.BottomSheetFragment.ShareLinkBottomS import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.CustomView.AspectRatioGifImageView; import ml.docilealligator.infinityforreddit.Event.PostUpdateEventToDetailActivity; +import ml.docilealligator.infinityforreddit.FetchGfycatOrRedgifsVideoLinks; import ml.docilealligator.infinityforreddit.NetworkState; import ml.docilealligator.infinityforreddit.Post.Post; import ml.docilealligator.infinityforreddit.Post.PostDataSource; @@ -124,6 +125,8 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView private AppCompatActivity mActivity; private Retrofit mOauthRetrofit; private Retrofit mRetrofit; + private Retrofit mGfycatRetrofit; + private Retrofit mRedgifsRetrofit; private int mImageViewWidth; private String mAccessToken; private RequestManager mGlide; @@ -178,6 +181,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView private boolean mShowThumbnailOnTheRightInCompactLayout; private double mStartAutoplayVisibleAreaOffset; private boolean mMuteNSFWVideo; + private boolean mAutomaticallyTryRedgifs; private Drawable mCommentIcon; private NetworkState networkState; private ExoCreator mExoCreator; @@ -185,6 +189,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView private ShareLinkBottomSheetFragment mShareLinkBottomSheetFragment; public PostRecyclerViewAdapter(AppCompatActivity activity, Retrofit oauthRetrofit, Retrofit retrofit, + Retrofit gfycatRetrofit, Retrofit redgifsRetrofit, RedditDataRoomDatabase redditDataRoomDatabase, CustomThemeWrapper customThemeWrapper, Locale locale, int imageViewWidth, String accessToken, int postType, int postLayout, boolean displaySubredditName, @@ -194,6 +199,8 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView mActivity = activity; mOauthRetrofit = oauthRetrofit; mRetrofit = retrofit; + mGfycatRetrofit = gfycatRetrofit; + mRedgifsRetrofit = redgifsRetrofit; mImageViewWidth = imageViewWidth; mAccessToken = accessToken; mPostType = postType; @@ -222,6 +229,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView sharedPreferences.getInt(SharedPreferencesUtils.START_AUTOPLAY_VISIBLE_AREA_OFFSET_LANDSCAPE, 50) / 100.0; mMuteNSFWVideo = sharedPreferences.getBoolean(SharedPreferencesUtils.MUTE_NSFW_VIDEO, false); + mAutomaticallyTryRedgifs = sharedPreferences.getBoolean(SharedPreferencesUtils.AUTOMATICALLY_TRY_REDGIFS, true); mPostLayout = postLayout; @@ -555,7 +563,30 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView mGlide.load(post.getPreviewUrl()).into(((PostVideoAutoplayViewHolder) holder).previewImageView); } ((PostVideoAutoplayViewHolder) holder).setVolume(mMuteAutoplayingVideos || (post.isNSFW() && mMuteNSFWVideo) ? 0f : 1f); - ((PostVideoAutoplayViewHolder) holder).bindVideoUri(Uri.parse(post.getVideoUrl())); + + if (post.isGfycat() || post.isRedgifs() && !post.isLoadGfyOrRedgifsVideoSuccess()) { + ((PostVideoAutoplayViewHolder) holder).typeTextView.setText("GFYCAT"); + ((PostVideoAutoplayViewHolder) holder).fetchGfycatOrRedgifsVideoLinks = new FetchGfycatOrRedgifsVideoLinks(new FetchGfycatOrRedgifsVideoLinks.FetchGfycatOrRedgifsVideoLinksListener() { + @Override + public void success(String webm, String mp4) { + post.setVideoDownloadUrl(mp4); + post.setVideoUrl(webm); + post.setLoadGfyOrRedgifsVideoSuccess(true); + ((PostVideoAutoplayViewHolder) holder).bindVideoUri(Uri.parse(post.getVideoUrl())); + } + + @Override + public void failed(int errorCode) { + Toast.makeText(mActivity, "asdfasdfadf", Toast.LENGTH_SHORT).show(); + } + }); + ((PostVideoAutoplayViewHolder) holder).fetchGfycatOrRedgifsVideoLinks + .fetchGfycatOrRedgifsVideoLinksInRecyclerViewAdapter(mGfycatRetrofit, mRedgifsRetrofit, + post.getGfycatId(), post.isGfycat(), mAutomaticallyTryRedgifs); + } else { + ((PostVideoAutoplayViewHolder) holder).typeTextView.setText("VIDEO"); + ((PostVideoAutoplayViewHolder) holder).bindVideoUri(Uri.parse(post.getVideoUrl())); + } } else if (holder instanceof PostVideoAndGifPreviewViewHolder) { if (post.getPostType() == Post.VIDEO_TYPE) { ((PostVideoAndGifPreviewViewHolder) holder).typeTextView.setText(mActivity.getString(R.string.video)); @@ -1158,6 +1189,10 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView super.onViewRecycled(holder); if (holder instanceof PostBaseViewHolder) { if (holder instanceof PostVideoAutoplayViewHolder) { + ((PostVideoAutoplayViewHolder) holder).mediaUri = null; + if (((PostVideoAutoplayViewHolder) holder).fetchGfycatOrRedgifsVideoLinks != null) { + ((PostVideoAutoplayViewHolder) holder).fetchGfycatOrRedgifsVideoLinks.cancel(); + } ((PostVideoAutoplayViewHolder) holder).muteButton.setVisibility(View.GONE); ((PostVideoAutoplayViewHolder) holder).resetVolume(); mGlide.clear(((PostVideoAutoplayViewHolder) holder).previewImageView); @@ -1781,6 +1816,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView ExoPlayerViewHelper helper; private Uri mediaUri; private float volume; + public FetchGfycatOrRedgifsVideoLinks fetchGfycatOrRedgifsVideoLinks; PostVideoAutoplayViewHolder(View itemView) { super(itemView); @@ -1865,11 +1901,14 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView @NonNull @Override public PlaybackInfo getCurrentPlaybackInfo() { - return helper != null ? helper.getLatestPlaybackInfo() : new PlaybackInfo(); + return helper != null && mediaUri != null ? helper.getLatestPlaybackInfo() : new PlaybackInfo(); } @Override public void initialize(@NonNull Container container, @NonNull PlaybackInfo playbackInfo) { + if (mediaUri == null) { + return; + } if (helper == null) { helper = new ExoPlayerViewHelper(this, mediaUri, null, mExoCreator); helper.addEventListener(new Playable.EventListener() { @@ -1916,7 +1955,9 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView @Override public void play() { - if (helper != null) helper.play(); + if (helper != null && mediaUri != null) { + helper.play(); + } } @Override @@ -1939,7 +1980,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView @Override public boolean wantsToPlay() { - return ToroUtil.visibleAreaOffset(this, itemView.getParent()) >= mStartAutoplayVisibleAreaOffset; + return mediaUri != null && ToroUtil.visibleAreaOffset(this, itemView.getParent()) >= mStartAutoplayVisibleAreaOffset; } @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchGfycatOrRedgifsVideoLinks.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchGfycatOrRedgifsVideoLinks.java index 9d51cb1e..4567fe85 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchGfycatOrRedgifsVideoLinks.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchGfycatOrRedgifsVideoLinks.java @@ -15,11 +15,20 @@ import retrofit2.Response; import retrofit2.Retrofit; public class FetchGfycatOrRedgifsVideoLinks { + private FetchGfycatOrRedgifsVideoLinksListener fetchGfycatOrRedgifsVideoLinksListener; + private ParseGfycatVideoLinksAsyncTask parseGfycatVideoLinksAsyncTask; + Retrofit gfycatRetrofit; + Call<String> 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(Retrofit gfycatRetrofit, String gfycatId, FetchGfycatOrRedgifsVideoLinksListener fetchGfycatOrRedgifsVideoLinksListener) { gfycatRetrofit.create(GfycatAPI.class).getGfycatData(gfycatId).enqueue(new Callback<String>() { @@ -39,6 +48,41 @@ public class FetchGfycatOrRedgifsVideoLinks { }); } + public void fetchGfycatOrRedgifsVideoLinksInRecyclerViewAdapter(Retrofit gfycatRetrofit, Retrofit redgifsRetrofit, + String gfycatId, boolean isGfycatVideo, + boolean automaticallyTryRedgifs) { + gfycatCall = (isGfycatVideo ? gfycatRetrofit : redgifsRetrofit).create(GfycatAPI.class).getGfycatData(gfycatId); + gfycatCall.enqueue(new Callback<String>() { + @Override + public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { + if (response.isSuccessful()) { + parseGfycatVideoLinksAsyncTask = new ParseGfycatVideoLinksAsyncTask(response.body(), fetchGfycatOrRedgifsVideoLinksListener); + parseGfycatVideoLinksAsyncTask.execute(); + } else { + if (response.code() == 404 && isGfycatVideo && automaticallyTryRedgifs) { + fetchGfycatOrRedgifsVideoLinksInRecyclerViewAdapter(gfycatRetrofit, redgifsRetrofit, gfycatId, false, false); + } else { + fetchGfycatOrRedgifsVideoLinksListener.failed(response.code()); + } + } + } + + @Override + public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) { + fetchGfycatOrRedgifsVideoLinksListener.failed(-1); + } + }); + } + + public void cancel() { + if (gfycatCall != null && !gfycatCall.isCanceled()) { + gfycatCall.cancel(); + } + if (parseGfycatVideoLinksAsyncTask != null && !parseGfycatVideoLinksAsyncTask.isCancelled()) { + parseGfycatVideoLinksAsyncTask.cancel(true); + } + } + private static class ParseGfycatVideoLinksAsyncTask extends AsyncTask<Void, Void, Void> { private String response; 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 7edfa0c4..f334b7fa 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java @@ -138,6 +138,12 @@ public class PostFragment extends Fragment implements FragmentCommunicator { @Named("oauth") Retrofit mOauthRetrofit; @Inject + @Named("gfycat") + Retrofit mGfycatRetrofit; + @Inject + @Named("redgifs") + Retrofit mRedgifsRetrofit; + @Inject RedditDataRoomDatabase mRedditDataRoomDatabase; @Inject @Named("default") @@ -401,8 +407,9 @@ public class PostFragment extends Fragment implements FragmentCommunicator { sortType = new SortType(SortType.Type.valueOf(sort), SortType.Time.valueOf(sortTime)); postLayout = mPostLayoutSharedPreferences.getInt(SharedPreferencesUtils.POST_LAYOUT_SEARCH_POST, defaultPostLayout); - mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mRedditDataRoomDatabase, - customThemeWrapper, locale, windowWidth, accessToken, postType, postLayout, true, + mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mGfycatRetrofit, + mRedgifsRetrofit, mRedditDataRoomDatabase, customThemeWrapper, locale, + windowWidth, accessToken, postType, postLayout, true, mSharedPreferences, exoCreator, new PostRecyclerViewAdapter.Callback() { @Override public void retryLoadingMore() { @@ -456,8 +463,9 @@ public class PostFragment extends Fragment implements FragmentCommunicator { sortType = new SortType(SortType.Type.valueOf(sort)); } - mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mRedditDataRoomDatabase, - customThemeWrapper, locale, windowWidth, accessToken, postType, postLayout, displaySubredditName, + mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mGfycatRetrofit, + mRedgifsRetrofit, mRedditDataRoomDatabase, customThemeWrapper, locale, + windowWidth, accessToken, postType, postLayout, displaySubredditName, mSharedPreferences, exoCreator, new PostRecyclerViewAdapter.Callback() { @Override public void retryLoadingMore() { @@ -504,8 +512,9 @@ public class PostFragment extends Fragment implements FragmentCommunicator { sortType = new SortType(SortType.Type.valueOf(sort)); } - mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mRedditDataRoomDatabase, - customThemeWrapper, locale, windowWidth, accessToken, postType, postLayout, true, + mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mGfycatRetrofit, + mRedgifsRetrofit, mRedditDataRoomDatabase, customThemeWrapper, locale, + windowWidth, accessToken, postType, postLayout, true, mSharedPreferences, exoCreator, new PostRecyclerViewAdapter.Callback() { @Override public void retryLoadingMore() { @@ -550,8 +559,9 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } postLayout = mPostLayoutSharedPreferences.getInt(SharedPreferencesUtils.POST_LAYOUT_USER_POST_BASE + username, defaultPostLayout); - mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mRedditDataRoomDatabase, - customThemeWrapper, locale, windowWidth, accessToken, postType, postLayout, true, + mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mGfycatRetrofit, + mRedgifsRetrofit, mRedditDataRoomDatabase, customThemeWrapper, locale, + windowWidth, accessToken, postType, postLayout, true, mSharedPreferences, exoCreator, new PostRecyclerViewAdapter.Callback() { @Override public void retryLoadingMore() { @@ -589,8 +599,9 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } postLayout = mPostLayoutSharedPreferences.getInt(SharedPreferencesUtils.POST_LAYOUT_FRONT_PAGE_POST, defaultPostLayout); - mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mRedditDataRoomDatabase, - customThemeWrapper, locale, windowWidth, accessToken, postType, postLayout, true, + mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mGfycatRetrofit, + mRedgifsRetrofit, mRedditDataRoomDatabase, customThemeWrapper, locale, + windowWidth, accessToken, postType, postLayout, true, mSharedPreferences, exoCreator, new PostRecyclerViewAdapter.Callback() { @Override public void retryLoadingMore() { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Post/ParsePost.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/ParsePost.java index 477cb697..11a08868 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Post/ParsePost.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/ParsePost.java @@ -384,9 +384,14 @@ public class ParsePost { try { Uri uri = Uri.parse(url); String authority = uri.getAuthority(); - if (authority != null && (authority.contains("gfycat.com") || authority.contains("redgifs.com"))) { - post.setPostType(Post.LINK_TYPE); - post.setUrl(url); + if (authority != null && (authority.contains("gfycat.com"))) { + post.setIsGfycat(true); + post.setVideoUrl(url); + post.setGfycatId(url.substring(url.lastIndexOf("/") + 1)); + } else if (authority != null && authority.contains("redgifs.com")) { + post.setIsRedgifs(true); + post.setVideoUrl(url); + post.setGfycatId(url.substring(url.lastIndexOf("/") + 1)); } } catch (IllegalArgumentException ignore) { } } else if (post.getPostType() == Post.LINK_TYPE || post.getPostType() == Post.NO_PREVIEW_LINK_TYPE) { @@ -422,6 +427,20 @@ public class ParsePost { post.setPostType(Post.GALLERY_TYPE); post.setGallery(gallery); } + } else if (post.getPostType() == Post.LINK_TYPE) { + Uri uri = Uri.parse(url); + String authority = uri.getAuthority(); + if (authority != null && (authority.contains("gfycat.com"))) { + post.setPostType(Post.VIDEO_TYPE); + post.setIsGfycat(true); + post.setVideoUrl(url); + post.setGfycatId(url.substring(url.lastIndexOf("/") + 1)); + } else if (authority != null && authority.contains("redgifs.com")) { + post.setPostType(Post.VIDEO_TYPE); + post.setIsRedgifs(true); + post.setVideoUrl(url); + post.setGfycatId(url.substring(url.lastIndexOf("/") + 1)); + } } } 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 a1a8c9c7..82c550d5 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Post/Post.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/Post.java @@ -52,6 +52,10 @@ public class Post implements Parcelable { private String url; private String videoUrl; private String videoDownloadUrl; + private String gfycatId; + private boolean isGfycat; + private boolean isRedgifs; + private boolean loadGfyOrRedgifsVideoSuccess; private String permalink; private String flair; private String awards; @@ -202,6 +206,10 @@ public class Post implements Parcelable { url = in.readString(); videoUrl = in.readString(); videoDownloadUrl = in.readString(); + gfycatId = in.readString(); + isGfycat = in.readByte() != 0; + isRedgifs = in.readByte() != 0; + loadGfyOrRedgifsVideoSuccess = in.readByte() != 0; permalink = in.readString(); flair = in.readString(); awards = in.readString(); @@ -353,6 +361,38 @@ public class Post implements Parcelable { this.videoDownloadUrl = videoDownloadUrl; } + public String getGfycatId() { + return gfycatId; + } + + public void setGfycatId(String gfycatId) { + this.gfycatId = gfycatId; + } + + public boolean isGfycat() { + return isGfycat; + } + + public void setIsGfycat(boolean isGfycat) { + this.isGfycat = isGfycat; + } + + public boolean isRedgifs() { + return isRedgifs; + } + + public void setIsRedgifs(boolean isRedgifs) { + this.isRedgifs = isRedgifs; + } + + public boolean isLoadGfyOrRedgifsVideoSuccess() { + return loadGfyOrRedgifsVideoSuccess; + } + + public void setLoadGfyOrRedgifsVideoSuccess(boolean loadGfyOrRedgifsVideoSuccess) { + this.loadGfyOrRedgifsVideoSuccess = loadGfyOrRedgifsVideoSuccess; + } + public String getPermalink() { return permalink; } @@ -512,6 +552,10 @@ public class Post implements Parcelable { parcel.writeString(url); parcel.writeString(videoUrl); parcel.writeString(videoDownloadUrl); + parcel.writeString(gfycatId); + parcel.writeByte((byte) (isGfycat ? 1 : 0)); + parcel.writeByte((byte) (isRedgifs ? 1 : 0)); + parcel.writeByte((byte) (loadGfyOrRedgifsVideoSuccess ? 1 : 0)); parcel.writeString(permalink); parcel.writeString(flair); parcel.writeString(awards); |