Add an option to automatically try loading removed gfycat videos on Redgifs.

This commit is contained in:
Alex Ning 2020-06-20 22:02:18 +08:00
parent 9f16b6c25a
commit dce5777fbb
8 changed files with 85 additions and 55 deletions

View File

@ -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);

View File

@ -14,27 +14,27 @@ import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
public class FetchGfycatVideoLinks {
public interface FetchGfycatVideoLinksListener {
public class FetchGfycatOrRedgifsVideoLinks {
public interface FetchGfycatOrRedgifsVideoLinksListener {
void success(String webm, String mp4);
void failed();
void failed(int errorCode);
}
public static void fetchGfycatVideoLinks(Retrofit gfycatRetrofit, String gfycatId,
FetchGfycatVideoLinksListener fetchGfycatVideoLinksListener) {
public static void fetchGfycatOrRedgifsVideoLinks(Retrofit gfycatRetrofit, String gfycatId,
FetchGfycatOrRedgifsVideoLinksListener fetchGfycatOrRedgifsVideoLinksListener) {
gfycatRetrofit.create(GfycatAPI.class).getGfycatData(gfycatId).enqueue(new Callback<String>() {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
if (response.isSuccessful()) {
new ParseGfycatVideoLinksAsyncTask(response.body(), fetchGfycatVideoLinksListener).execute();
new ParseGfycatVideoLinksAsyncTask(response.body(), fetchGfycatOrRedgifsVideoLinksListener).execute();
} else {
fetchGfycatVideoLinksListener.failed();
fetchGfycatOrRedgifsVideoLinksListener.failed(response.code());
}
}
@Override
public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
fetchGfycatVideoLinksListener.failed();
fetchGfycatOrRedgifsVideoLinksListener.failed(-1);
}
});
}
@ -45,11 +45,11 @@ public class FetchGfycatVideoLinks {
private String webm;
private String mp4;
private boolean parseFailed = false;
private FetchGfycatVideoLinksListener fetchGfycatVideoLinksListener;
private FetchGfycatOrRedgifsVideoLinksListener fetchGfycatOrRedgifsVideoLinksListener;
ParseGfycatVideoLinksAsyncTask(String response, FetchGfycatVideoLinksListener fetchGfycatVideoLinksListener) {
ParseGfycatVideoLinksAsyncTask(String response, FetchGfycatOrRedgifsVideoLinksListener fetchGfycatOrRedgifsVideoLinksListener) {
this.response = response;
this.fetchGfycatVideoLinksListener = fetchGfycatVideoLinksListener;
this.fetchGfycatOrRedgifsVideoLinksListener = fetchGfycatOrRedgifsVideoLinksListener;
}
@Override
@ -70,9 +70,9 @@ public class FetchGfycatVideoLinks {
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
if (parseFailed) {
fetchGfycatVideoLinksListener.failed();
fetchGfycatOrRedgifsVideoLinksListener.failed(-1);
} else {
fetchGfycatVideoLinksListener.success(webm, mp4);
fetchGfycatOrRedgifsVideoLinksListener.success(webm, mp4);
}
}
}

View File

@ -14,11 +14,11 @@ import ml.docilealligator.infinityforreddit.Event.ChangeVideoAutoplayEvent;
import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils;
public class AutoplayPreferenceFragment extends PreferenceFragmentCompat {
public class VideoPreferenceFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.autoplay_preferences, rootKey);
setPreferencesFromResource(R.xml.video_preferences, rootKey);
ListPreference videoAutoplayListPreference = findPreference(SharedPreferencesUtils.VIDEO_AUTOPLAY);
SwitchPreference muteAutoplayingVideosSwitchPreference = findPreference(SharedPreferencesUtils.MUTE_AUTOPLAYING_VIDEOS);

View File

@ -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";

View File

@ -15,7 +15,8 @@
android:layout_height="match_parent"
android:fillViewport="true">
<RelativeLayout
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:id="@+id/coordinator_layout_view_video_activity"
android:layout_width="match_parent"
android:layout_height="match_parent">
@ -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" />
</RelativeLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</androidx.core.widget.NestedScrollView>

View File

@ -329,6 +329,7 @@
<string name="settings_interface_title">Interface</string>
<string name="settings_gestures_and_buttons_title">Gestures &amp; Buttons</string>
<string name="settings_open_link_in_app_title">Open Link In App</string>
<string name="settigns_video_title">Video</string>
<string name="settings_video_autoplay_title">Video Autoplay</string>
<string name="settings_mute_autoplaying_videos_title">Mute Autoplaying Videos</string>
<string name="settings_autoplay_nsfw_videos_title">Autoplay NSFW Videos</string>
@ -343,6 +344,7 @@
<string name="settings_volume_keys_navigate_comments_title">Use Volume Keys to Navigate Comments in Posts</string>
<string name="settings_volume_keys_navigate_posts_title">Use Volume Keys to Navigate Posts</string>
<string name="settings_mute_video_title">Mute Video</string>
<string name="settings_automatically_try_redgifs_title">Automatically Try Accessing Redgifs if Videos on Gfycat are Removed.</string>
<string name="settings_confirm_to_exit">Confirm to Exit</string>
<string name="settings_show_top_level_comments_first_title">Show Top-level Comments First</string>
<string name="settings_show_comment_divider_title">Show Comment Divider</string>
@ -394,7 +396,7 @@
<string name="settings_credits_material_icons_title">Material Icons</string>
<string name="settings_open_source_title">Open Source</string>
<string name="settings_open_source_summary">Star it on Github if you like this app</string>
<string name="settings_rate_title">Rate in Google Play</string>
<string name="settings_rate_title">Rate on Google Play</string>
<string name="settings_rate_summary">Give me a 5-star rating and I will be really happy</string>
<string name="settings_f_droid_title">F-Droid</string>
<string name="settings_f_droid_summary">It\'s available in IzzyOnDroid, an F-Droid Repository</string>
@ -788,4 +790,6 @@
<string name="default_font_font_preview">Default</string>
<string name="load_video_in_redgifs">Try loading the video on Redgifs</string>
</resources>

View File

@ -29,14 +29,9 @@
app:title="@string/settings_open_link_in_app_title" />
<Preference
app:title="@string/settings_video_autoplay_title"
app:fragment="ml.docilealligator.infinityforreddit.Settings.AutoplayPreferenceFragment" />
<SwitchPreference
app:defaultValue="false"
app:key="mute_video"
app:icon="@drawable/ic_mute_preferences_24dp"
app:title="@string/settings_mute_video_title" />
app:title="@string/settigns_video_title"
android:icon="@drawable/ic_outline_video_24dp"
app:fragment="ml.docilealligator.infinityforreddit.Settings.VideoPreferenceFragment" />
<SwitchPreference
app:defaultValue="false"

View File

@ -2,6 +2,20 @@
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<SwitchPreference
app:defaultValue="false"
app:key="mute_video"
app:icon="@drawable/ic_mute_preferences_24dp"
app:title="@string/settings_mute_video_title" />
<SwitchPreference
app:defaultValue="true"
app:key="automatically_try_redgifs"
app:title="@string/settings_automatically_try_redgifs_title" />
<PreferenceCategory
app:title="@string/settings_video_autoplay_title" />
<ListPreference
app:defaultValue="0"
android:entries="@array/settings_video_autoplay"