diff options
Diffstat (limited to 'app/src/main/java')
-rw-r--r-- | app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewVideoActivity.java | 71 | ||||
-rw-r--r-- | app/src/main/java/ml/docilealligator/infinityforreddit/FetchGfycatOrRedgifsVideoLinks.java (renamed from app/src/main/java/ml/docilealligator/infinityforreddit/FetchGfycatVideoLinks.java) | 26 | ||||
-rw-r--r-- | app/src/main/java/ml/docilealligator/infinityforreddit/Settings/VideoPreferenceFragment.java (renamed from app/src/main/java/ml/docilealligator/infinityforreddit/Settings/AutoplayPreferenceFragment.java) | 4 | ||||
-rw-r--r-- | app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java | 1 |
4 files changed, 59 insertions, 43 deletions
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/FetchGfycatVideoLinks.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchGfycatOrRedgifsVideoLinks.java index b9799484..9d51cb1e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchGfycatVideoLinks.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchGfycatOrRedgifsVideoLinks.java @@ -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); } } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/AutoplayPreferenceFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/VideoPreferenceFragment.java index 173b5934..e7cd2432 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/AutoplayPreferenceFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/VideoPreferenceFragment.java @@ -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); 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"; |