From dce5777fbbaa7d8611c819fe3a75ab7f85a9a09d Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Sat, 20 Jun 2020 22:02:18 +0800 Subject: Add an option to automatically try loading removed gfycat videos on Redgifs. --- .../Activity/ViewVideoActivity.java | 71 +++++++++++-------- .../FetchGfycatOrRedgifsVideoLinks.java | 79 ++++++++++++++++++++++ .../infinityforreddit/FetchGfycatVideoLinks.java | 79 ---------------------- .../Settings/AutoplayPreferenceFragment.java | 46 ------------- .../Settings/VideoPreferenceFragment.java | 46 +++++++++++++ .../Utils/SharedPreferencesUtils.java | 1 + app/src/main/res/layout/activity_view_video.xml | 7 +- app/src/main/res/values/strings.xml | 6 +- app/src/main/res/xml/autoplay_preferences.xml | 24 ------- app/src/main/res/xml/main_preferences.xml | 11 +-- app/src/main/res/xml/video_preferences.xml | 38 +++++++++++ 11 files changed, 219 insertions(+), 189 deletions(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/FetchGfycatOrRedgifsVideoLinks.java delete mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/FetchGfycatVideoLinks.java delete mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/Settings/AutoplayPreferenceFragment.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/Settings/VideoPreferenceFragment.java delete mode 100644 app/src/main/res/xml/autoplay_preferences.xml create mode 100644 app/src/main/res/xml/video_preferences.xml diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewVideoActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewVideoActivity.java index 601920d4..3db53108 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewVideoActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewVideoActivity.java @@ -24,6 +24,7 @@ import android.widget.Toast; import androidx.annotation.NonNull; import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.AppCompatActivity; +import androidx.coordinatorlayout.widget.CoordinatorLayout; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; @@ -44,6 +45,7 @@ import com.google.android.exoplayer2.upstream.DataSource; import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory; import com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory; import com.google.android.exoplayer2.util.Util; +import com.google.android.material.snackbar.Snackbar; import com.thefuntasty.hauler.DragDirection; import com.thefuntasty.hauler.HaulerView; @@ -52,7 +54,7 @@ import javax.inject.Named; import butterknife.BindView; import butterknife.ButterKnife; -import ml.docilealligator.infinityforreddit.FetchGfycatVideoLinks; +import ml.docilealligator.infinityforreddit.FetchGfycatOrRedgifsVideoLinks; import ml.docilealligator.infinityforreddit.Font.ContentFontFamily; import ml.docilealligator.infinityforreddit.Font.FontFamily; import ml.docilealligator.infinityforreddit.Font.TitleFontFamily; @@ -83,6 +85,8 @@ public class ViewVideoActivity extends AppCompatActivity { private static final String VIDEO_URI_STATE = "VUS"; @BindView(R.id.hauler_view_view_video_activity) HaulerView haulerView; + @BindView(R.id.coordinator_layout_view_video_activity) + CoordinatorLayout coordinatorLayout; @BindView(R.id.progress_bar_view_video_activity) ProgressBar progressBar; @BindView(R.id.player_view_view_video_activity) @@ -214,42 +218,17 @@ public class ViewVideoActivity extends AppCompatActivity { } if (mVideoUri == null) { - progressBar.setVisibility(View.VISIBLE); String gfycatId = intent.getStringExtra(EXTRA_GFYCAT_ID); if (gfycatId != null && gfycatId.contains("-")) { gfycatId = gfycatId.substring(0, gfycatId.indexOf('-')); } - Retrofit retrofit; if (videoType == VIDEO_TYPE_GFYCAT) { - retrofit = gfycatRetrofit; videoFileName = "Gfycat-" + gfycatId + ".mp4"; + loadGfycatOrRedgifsVideo(gfycatRetrofit, gfycatId, savedInstanceState, true); } else { - retrofit = redgifsRetrofit; videoFileName = "Redgifs-" + gfycatId + ".mp4"; + loadGfycatOrRedgifsVideo(redgifsRetrofit, gfycatId, savedInstanceState, false); } - FetchGfycatVideoLinks.fetchGfycatVideoLinks(retrofit, gfycatId, - new FetchGfycatVideoLinks.FetchGfycatVideoLinksListener() { - @Override - public void success(String webm, String mp4) { - progressBar.setVisibility(View.GONE); - mVideoUri = Uri.parse(webm); - videoDownloadUrl = mp4; - dataSourceFactory = new DefaultDataSourceFactory(ViewVideoActivity.this, - Util.getUserAgent(ViewVideoActivity.this, "Infinity")); - player.prepare(new ProgressiveMediaSource.Factory(dataSourceFactory).createMediaSource(mVideoUri)); - preparePlayer(savedInstanceState); - } - - @Override - public void failed() { - progressBar.setVisibility(View.GONE); - if (videoType == VIDEO_TYPE_GFYCAT) { - Toast.makeText(ViewVideoActivity.this, R.string.fetch_gfycat_video_failed, Toast.LENGTH_SHORT).show(); - } else { - Toast.makeText(ViewVideoActivity.this, R.string.fetch_redgifs_video_failed, Toast.LENGTH_SHORT).show(); - } - } - }); } else { dataSourceFactory = new DefaultDataSourceFactory(ViewVideoActivity.this, Util.getUserAgent(ViewVideoActivity.this, "Infinity")); @@ -333,6 +312,42 @@ public class ViewVideoActivity extends AppCompatActivity { }); } + private void loadGfycatOrRedgifsVideo(Retrofit retrofit, String gfycatId, Bundle savedInstanceState, boolean needErrorHandling) { + progressBar.setVisibility(View.VISIBLE); + FetchGfycatOrRedgifsVideoLinks.fetchGfycatOrRedgifsVideoLinks(retrofit, gfycatId, + new FetchGfycatOrRedgifsVideoLinks.FetchGfycatOrRedgifsVideoLinksListener() { + @Override + public void success(String webm, String mp4) { + progressBar.setVisibility(View.GONE); + mVideoUri = Uri.parse(webm); + videoDownloadUrl = mp4; + dataSourceFactory = new DefaultDataSourceFactory(ViewVideoActivity.this, + Util.getUserAgent(ViewVideoActivity.this, "Infinity")); + player.prepare(new ProgressiveMediaSource.Factory(dataSourceFactory).createMediaSource(mVideoUri)); + preparePlayer(savedInstanceState); + } + + @Override + public void failed(int errorCode) { + progressBar.setVisibility(View.GONE); + if (videoType == VIDEO_TYPE_GFYCAT) { + if (errorCode == 404 && needErrorHandling) { + if (mSharedPreferences.getBoolean(SharedPreferencesUtils.AUTOMATICALLY_TRY_REDGIFS, true)) { + loadGfycatOrRedgifsVideo(redgifsRetrofit, gfycatId, savedInstanceState, false); + } else { + Snackbar.make(coordinatorLayout, R.string.load_video_in_redgifs, Snackbar.LENGTH_INDEFINITE).setAction(R.string.yes, + view -> loadGfycatOrRedgifsVideo(redgifsRetrofit, gfycatId, savedInstanceState, false)).show(); + } + } else { + Toast.makeText(ViewVideoActivity.this, R.string.fetch_gfycat_video_failed, Toast.LENGTH_SHORT).show(); + } + } else { + Toast.makeText(ViewVideoActivity.this, R.string.fetch_redgifs_video_failed, Toast.LENGTH_SHORT).show(); + } + } + }); + } + @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.view_video, menu); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchGfycatOrRedgifsVideoLinks.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchGfycatOrRedgifsVideoLinks.java new file mode 100644 index 00000000..9d51cb1e --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchGfycatOrRedgifsVideoLinks.java @@ -0,0 +1,79 @@ +package ml.docilealligator.infinityforreddit; + +import android.os.AsyncTask; + +import androidx.annotation.NonNull; + +import org.json.JSONException; +import org.json.JSONObject; + +import ml.docilealligator.infinityforreddit.API.GfycatAPI; +import ml.docilealligator.infinityforreddit.Utils.JSONUtils; +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Response; +import retrofit2.Retrofit; + +public class FetchGfycatOrRedgifsVideoLinks { + public interface FetchGfycatOrRedgifsVideoLinksListener { + void success(String webm, String mp4); + void failed(int errorCode); + } + + public static void fetchGfycatOrRedgifsVideoLinks(Retrofit gfycatRetrofit, String gfycatId, + FetchGfycatOrRedgifsVideoLinksListener fetchGfycatOrRedgifsVideoLinksListener) { + gfycatRetrofit.create(GfycatAPI.class).getGfycatData(gfycatId).enqueue(new Callback() { + @Override + public void onResponse(@NonNull Call call, @NonNull Response response) { + if (response.isSuccessful()) { + new ParseGfycatVideoLinksAsyncTask(response.body(), fetchGfycatOrRedgifsVideoLinksListener).execute(); + } else { + fetchGfycatOrRedgifsVideoLinksListener.failed(response.code()); + } + } + + @Override + public void onFailure(@NonNull Call call, @NonNull Throwable t) { + fetchGfycatOrRedgifsVideoLinksListener.failed(-1); + } + }); + } + + private static class ParseGfycatVideoLinksAsyncTask extends AsyncTask { + + private String response; + private String webm; + private String mp4; + private boolean parseFailed = false; + private FetchGfycatOrRedgifsVideoLinksListener fetchGfycatOrRedgifsVideoLinksListener; + + ParseGfycatVideoLinksAsyncTask(String response, FetchGfycatOrRedgifsVideoLinksListener fetchGfycatOrRedgifsVideoLinksListener) { + this.response = response; + this.fetchGfycatOrRedgifsVideoLinksListener = fetchGfycatOrRedgifsVideoLinksListener; + } + + @Override + protected Void doInBackground(Void... voids) { + try { + JSONObject jsonObject = new JSONObject(response); + webm = jsonObject.getJSONObject(JSONUtils.GFY_ITEM_KEY).getString(JSONUtils.WEBM_URL_KEY); + mp4 = jsonObject.getJSONObject(JSONUtils.GFY_ITEM_KEY).getString(JSONUtils.MP4_URL_KEY); + } catch (JSONException e) { + e.printStackTrace(); + parseFailed = true; + } + + return null; + } + + @Override + protected void onPostExecute(Void aVoid) { + super.onPostExecute(aVoid); + if (parseFailed) { + fetchGfycatOrRedgifsVideoLinksListener.failed(-1); + } else { + fetchGfycatOrRedgifsVideoLinksListener.success(webm, mp4); + } + } + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchGfycatVideoLinks.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchGfycatVideoLinks.java deleted file mode 100644 index b9799484..00000000 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchGfycatVideoLinks.java +++ /dev/null @@ -1,79 +0,0 @@ -package ml.docilealligator.infinityforreddit; - -import android.os.AsyncTask; - -import androidx.annotation.NonNull; - -import org.json.JSONException; -import org.json.JSONObject; - -import ml.docilealligator.infinityforreddit.API.GfycatAPI; -import ml.docilealligator.infinityforreddit.Utils.JSONUtils; -import retrofit2.Call; -import retrofit2.Callback; -import retrofit2.Response; -import retrofit2.Retrofit; - -public class FetchGfycatVideoLinks { - public interface FetchGfycatVideoLinksListener { - void success(String webm, String mp4); - void failed(); - } - - public static void fetchGfycatVideoLinks(Retrofit gfycatRetrofit, String gfycatId, - FetchGfycatVideoLinksListener fetchGfycatVideoLinksListener) { - gfycatRetrofit.create(GfycatAPI.class).getGfycatData(gfycatId).enqueue(new Callback() { - @Override - public void onResponse(@NonNull Call call, @NonNull Response response) { - if (response.isSuccessful()) { - new ParseGfycatVideoLinksAsyncTask(response.body(), fetchGfycatVideoLinksListener).execute(); - } else { - fetchGfycatVideoLinksListener.failed(); - } - } - - @Override - public void onFailure(@NonNull Call call, @NonNull Throwable t) { - fetchGfycatVideoLinksListener.failed(); - } - }); - } - - private static class ParseGfycatVideoLinksAsyncTask extends AsyncTask { - - private String response; - private String webm; - private String mp4; - private boolean parseFailed = false; - private FetchGfycatVideoLinksListener fetchGfycatVideoLinksListener; - - ParseGfycatVideoLinksAsyncTask(String response, FetchGfycatVideoLinksListener fetchGfycatVideoLinksListener) { - this.response = response; - this.fetchGfycatVideoLinksListener = fetchGfycatVideoLinksListener; - } - - @Override - protected Void doInBackground(Void... voids) { - try { - JSONObject jsonObject = new JSONObject(response); - webm = jsonObject.getJSONObject(JSONUtils.GFY_ITEM_KEY).getString(JSONUtils.WEBM_URL_KEY); - mp4 = jsonObject.getJSONObject(JSONUtils.GFY_ITEM_KEY).getString(JSONUtils.MP4_URL_KEY); - } catch (JSONException e) { - e.printStackTrace(); - parseFailed = true; - } - - return null; - } - - @Override - protected void onPostExecute(Void aVoid) { - super.onPostExecute(aVoid); - if (parseFailed) { - fetchGfycatVideoLinksListener.failed(); - } else { - fetchGfycatVideoLinksListener.success(webm, mp4); - } - } - } -} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/AutoplayPreferenceFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/AutoplayPreferenceFragment.java deleted file mode 100644 index 173b5934..00000000 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/AutoplayPreferenceFragment.java +++ /dev/null @@ -1,46 +0,0 @@ -package ml.docilealligator.infinityforreddit.Settings; - -import android.os.Bundle; - -import androidx.preference.ListPreference; -import androidx.preference.PreferenceFragmentCompat; -import androidx.preference.SwitchPreference; - -import org.greenrobot.eventbus.EventBus; - -import ml.docilealligator.infinityforreddit.Event.ChangeAutoplayNsfwVideosEvent; -import ml.docilealligator.infinityforreddit.Event.ChangeMuteAutoplayingVideosEvent; -import ml.docilealligator.infinityforreddit.Event.ChangeVideoAutoplayEvent; -import ml.docilealligator.infinityforreddit.R; -import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; - -public class AutoplayPreferenceFragment extends PreferenceFragmentCompat { - - @Override - public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { - setPreferencesFromResource(R.xml.autoplay_preferences, rootKey); - - ListPreference videoAutoplayListPreference = findPreference(SharedPreferencesUtils.VIDEO_AUTOPLAY); - SwitchPreference muteAutoplayingVideosSwitchPreference = findPreference(SharedPreferencesUtils.MUTE_AUTOPLAYING_VIDEOS); - SwitchPreference autoplayNsfwVideosSwitchPreference = findPreference(SharedPreferencesUtils.AUTOPLAY_NSFW_VIDEOS); - - if (videoAutoplayListPreference != null && autoplayNsfwVideosSwitchPreference != null) { - videoAutoplayListPreference.setOnPreferenceChangeListener((preference, newValue) -> { - EventBus.getDefault().post(new ChangeVideoAutoplayEvent((String) newValue)); - return true; - }); - - autoplayNsfwVideosSwitchPreference.setOnPreferenceChangeListener((preference, newValue) -> { - EventBus.getDefault().post(new ChangeAutoplayNsfwVideosEvent((Boolean) newValue)); - return true; - }); - } - - if (muteAutoplayingVideosSwitchPreference != null) { - muteAutoplayingVideosSwitchPreference.setOnPreferenceChangeListener((preference, newValue) -> { - EventBus.getDefault().post(new ChangeMuteAutoplayingVideosEvent((Boolean) newValue)); - return true; - }); - } - } -} \ No newline at end of file diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/VideoPreferenceFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/VideoPreferenceFragment.java new file mode 100644 index 00000000..e7cd2432 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/VideoPreferenceFragment.java @@ -0,0 +1,46 @@ +package ml.docilealligator.infinityforreddit.Settings; + +import android.os.Bundle; + +import androidx.preference.ListPreference; +import androidx.preference.PreferenceFragmentCompat; +import androidx.preference.SwitchPreference; + +import org.greenrobot.eventbus.EventBus; + +import ml.docilealligator.infinityforreddit.Event.ChangeAutoplayNsfwVideosEvent; +import ml.docilealligator.infinityforreddit.Event.ChangeMuteAutoplayingVideosEvent; +import ml.docilealligator.infinityforreddit.Event.ChangeVideoAutoplayEvent; +import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; + +public class VideoPreferenceFragment extends PreferenceFragmentCompat { + + @Override + public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { + setPreferencesFromResource(R.xml.video_preferences, rootKey); + + ListPreference videoAutoplayListPreference = findPreference(SharedPreferencesUtils.VIDEO_AUTOPLAY); + SwitchPreference muteAutoplayingVideosSwitchPreference = findPreference(SharedPreferencesUtils.MUTE_AUTOPLAYING_VIDEOS); + SwitchPreference autoplayNsfwVideosSwitchPreference = findPreference(SharedPreferencesUtils.AUTOPLAY_NSFW_VIDEOS); + + if (videoAutoplayListPreference != null && autoplayNsfwVideosSwitchPreference != null) { + videoAutoplayListPreference.setOnPreferenceChangeListener((preference, newValue) -> { + EventBus.getDefault().post(new ChangeVideoAutoplayEvent((String) newValue)); + return true; + }); + + autoplayNsfwVideosSwitchPreference.setOnPreferenceChangeListener((preference, newValue) -> { + EventBus.getDefault().post(new ChangeAutoplayNsfwVideosEvent((Boolean) newValue)); + return true; + }); + } + + if (muteAutoplayingVideosSwitchPreference != null) { + muteAutoplayingVideosSwitchPreference.setOnPreferenceChangeListener((preference, newValue) -> { + EventBus.getDefault().post(new ChangeMuteAutoplayingVideosEvent((Boolean) newValue)); + return true; + }); + } + } +} \ No newline at end of file diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java index 0b40cc1d..ee25195d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java @@ -88,6 +88,7 @@ public class SharedPreferencesUtils { public static final String VIDEO_AUTOPLAY_VALUE_NEVER = "0"; public static final String MUTE_AUTOPLAYING_VIDEOS = "mute_autoplaying_videos"; public static final String AUTOPLAY_NSFW_VIDEOS = "autoplay_nsfw_videos"; + public static final String AUTOMATICALLY_TRY_REDGIFS = "automatically_try_redgifs"; public static final String LOCK_JUMP_TO_NEXT_TOP_LEVEL_COMMENT_BUTTON = "lock_jump_to_next_top_level_comment_button"; public static final String SWAP_TAP_AND_LONG_COMMENTS = "swap_tap_and_long_in_comments"; public static final String SWIPE_UP_TO_HIDE_JUMP_TO_NEXT_TOP_LEVEL_COMMENT_BUTTON = "swipe_up_to_hide_jump_to_next_top_level_comments_button"; diff --git a/app/src/main/res/layout/activity_view_video.xml b/app/src/main/res/layout/activity_view_video.xml index 2907fd4d..1d18f2ab 100644 --- a/app/src/main/res/layout/activity_view_video.xml +++ b/app/src/main/res/layout/activity_view_video.xml @@ -15,7 +15,8 @@ android:layout_height="match_parent" android:fillViewport="true"> - @@ -29,10 +30,10 @@ android:id="@+id/progress_bar_view_video_activity" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:layout_centerInParent="true" + android:layout_gravity="center" android:visibility="gone" /> - + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 34dc43cb..1eecbe16 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -329,6 +329,7 @@ Interface Gestures & Buttons Open Link In App + Video Video Autoplay Mute Autoplaying Videos Autoplay NSFW Videos @@ -343,6 +344,7 @@ Use Volume Keys to Navigate Comments in Posts Use Volume Keys to Navigate Posts Mute Video + Automatically Try Accessing Redgifs if Videos on Gfycat are Removed. Confirm to Exit Show Top-level Comments First Show Comment Divider @@ -394,7 +396,7 @@ Material Icons Open Source Star it on Github if you like this app - Rate in Google Play + Rate on Google Play Give me a 5-star rating and I will be really happy F-Droid It\'s available in IzzyOnDroid, an F-Droid Repository @@ -788,4 +790,6 @@ Default + Try loading the video on Redgifs + diff --git a/app/src/main/res/xml/autoplay_preferences.xml b/app/src/main/res/xml/autoplay_preferences.xml deleted file mode 100644 index 5cc344aa..00000000 --- a/app/src/main/res/xml/autoplay_preferences.xml +++ /dev/null @@ -1,24 +0,0 @@ - - - - - - - - - - \ No newline at end of file diff --git a/app/src/main/res/xml/main_preferences.xml b/app/src/main/res/xml/main_preferences.xml index f332a49c..c15bfee4 100644 --- a/app/src/main/res/xml/main_preferences.xml +++ b/app/src/main/res/xml/main_preferences.xml @@ -29,14 +29,9 @@ app:title="@string/settings_open_link_in_app_title" /> - - + app:title="@string/settigns_video_title" + android:icon="@drawable/ic_outline_video_24dp" + app:fragment="ml.docilealligator.infinityforreddit.Settings.VideoPreferenceFragment" /> + + + + + + + + + + + + + + + \ No newline at end of file -- cgit v1.2.3