New option: Settings->Data Saving Mode->Reddit Video Default Resolution.

This commit is contained in:
Docile-Alligator 2022-03-25 21:43:38 +08:00
parent 7422098734
commit ac749fe0f0
6 changed files with 70 additions and 12 deletions

View File

@ -44,7 +44,9 @@ import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.core.app.ActivityCompat; import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat; import androidx.core.content.ContextCompat;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.ExoPlayerFactory; import com.google.android.exoplayer2.ExoPlayerFactory;
import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.PlaybackParameters; import com.google.android.exoplayer2.PlaybackParameters;
import com.google.android.exoplayer2.Player; import com.google.android.exoplayer2.Player;
import com.google.android.exoplayer2.SimpleExoPlayer; import com.google.android.exoplayer2.SimpleExoPlayer;
@ -62,6 +64,7 @@ import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
import com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory; import com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory;
import com.google.android.exoplayer2.upstream.cache.CacheDataSourceFactory; import com.google.android.exoplayer2.upstream.cache.CacheDataSourceFactory;
import com.google.android.exoplayer2.upstream.cache.SimpleCache; import com.google.android.exoplayer2.upstream.cache.SimpleCache;
import com.google.android.exoplayer2.util.MimeTypes;
import com.google.android.exoplayer2.util.Util; import com.google.android.exoplayer2.util.Util;
import com.google.android.exoplayer2.video.VideoListener; import com.google.android.exoplayer2.video.VideoListener;
import com.google.android.material.bottomappbar.BottomAppBar; import com.google.android.material.bottomappbar.BottomAppBar;
@ -188,7 +191,7 @@ public class ViewVideoActivity extends AppCompatActivity implements CustomFontRe
private long resumePosition = -1; private long resumePosition = -1;
private int videoType; private int videoType;
private boolean isDataSavingMode; private boolean isDataSavingMode;
private boolean isHd; private int dataSavingModeDefaultResolution;
private Integer originalOrientation; private Integer originalOrientation;
private int playbackSpeed = 100; private int playbackSpeed = 100;
private boolean useBottomAppBar; private boolean useBottomAppBar;
@ -333,7 +336,7 @@ public class ViewVideoActivity extends AppCompatActivity implements CustomFontRe
} else if (dataSavingModeString.equals(SharedPreferencesUtils.DATA_SAVING_MODE_ONLY_ON_CELLULAR_DATA)) { } else if (dataSavingModeString.equals(SharedPreferencesUtils.DATA_SAVING_MODE_ONLY_ON_CELLULAR_DATA)) {
isDataSavingMode = networkType == Utils.NETWORK_TYPE_CELLULAR; isDataSavingMode = networkType == Utils.NETWORK_TYPE_CELLULAR;
} }
isHd = !isDataSavingMode; dataSavingModeDefaultResolution = Integer.parseInt(mSharedPreferences.getString(SharedPreferencesUtils.REDDIT_VIDEO_DEFAULT_RESOLUTION, "360"));
if (!mSharedPreferences.getBoolean(SharedPreferencesUtils.VIDEO_PLAYER_IGNORE_NAV_BAR, false)) { if (!mSharedPreferences.getBoolean(SharedPreferencesUtils.VIDEO_PLAYER_IGNORE_NAV_BAR, false)) {
if (resources.getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT || resources.getBoolean(R.bool.isTablet)) { if (resources.getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT || resources.getBoolean(R.bool.isTablet)) {
@ -417,6 +420,11 @@ public class ViewVideoActivity extends AppCompatActivity implements CustomFontRe
TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(); TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory();
trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory); trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);
if (videoType == VIDEO_TYPE_NORMAL && isDataSavingMode && dataSavingModeDefaultResolution > 0) {
trackSelector.setParameters(
trackSelector.buildUponParameters()
.setMaxVideoSize(dataSavingModeDefaultResolution, dataSavingModeDefaultResolution));
}
player = ExoPlayerFactory.newSimpleInstance(this, trackSelector); player = ExoPlayerFactory.newSimpleInstance(this, trackSelector);
playerControlView.setPlayer(player); playerControlView.setPlayer(player);
@ -610,12 +618,6 @@ public class ViewVideoActivity extends AppCompatActivity implements CustomFontRe
public void onTracksChanged(TrackGroupArray trackGroups, TrackSelectionArray trackSelections) { public void onTracksChanged(TrackGroupArray trackGroups, TrackSelectionArray trackSelections) {
if (!trackGroups.isEmpty()) { if (!trackGroups.isEmpty()) {
if (videoType == VIDEO_TYPE_NORMAL) { if (videoType == VIDEO_TYPE_NORMAL) {
if (isDataSavingMode) {
trackSelector.setParameters(
trackSelector.buildUponParameters()
.setMaxVideoSize(720, 720));
}
hdButton.setVisibility(View.VISIBLE); hdButton.setVisibility(View.VISIBLE);
hdButton.setOnClickListener(view -> { hdButton.setOnClickListener(view -> {
TrackSelectionDialogBuilder builder = new TrackSelectionDialogBuilder(ViewVideoActivity.this, getString(R.string.select_video_quality), trackSelector, 0); TrackSelectionDialogBuilder builder = new TrackSelectionDialogBuilder(ViewVideoActivity.this, getString(R.string.select_video_quality), trackSelector, 0);
@ -653,6 +655,26 @@ public class ViewVideoActivity extends AppCompatActivity implements CustomFontRe
}); });
} }
private int inferPrimaryTrackType(Format format) {
int trackType = MimeTypes.getTrackType(format.sampleMimeType);
if (trackType != C.TRACK_TYPE_UNKNOWN) {
return trackType;
}
if (MimeTypes.getVideoMediaMimeType(format.codecs) != null) {
return C.TRACK_TYPE_VIDEO;
}
if (MimeTypes.getAudioMediaMimeType(format.codecs) != null) {
return C.TRACK_TYPE_AUDIO;
}
if (format.width != Format.NO_VALUE || format.height != Format.NO_VALUE) {
return C.TRACK_TYPE_VIDEO;
}
if (format.channelCount != Format.NO_VALUE || format.sampleRate != Format.NO_VALUE) {
return C.TRACK_TYPE_AUDIO;
}
return C.TRACK_TYPE_UNKNOWN;
}
private void loadGfycatOrRedgifsVideo(Retrofit retrofit, String gfycatId, Bundle savedInstanceState, boolean needErrorHandling) { private void loadGfycatOrRedgifsVideo(Retrofit retrofit, String gfycatId, Bundle savedInstanceState, boolean needErrorHandling) {
progressBar.setVisibility(View.VISIBLE); progressBar.setVisibility(View.VISIBLE);
FetchGfycatOrRedgifsVideoLinks.fetchGfycatOrRedgifsVideoLinks(mExecutor, new Handler(), retrofit, gfycatId, FetchGfycatOrRedgifsVideoLinks.fetchGfycatOrRedgifsVideoLinks(mExecutor, new Handler(), retrofit, gfycatId,

View File

@ -1,16 +1,13 @@
package ml.docilealligator.infinityforreddit.settings; package ml.docilealligator.infinityforreddit.settings;
import android.content.Context;
import android.os.Bundle; import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.preference.ListPreference; import androidx.preference.ListPreference;
import androidx.preference.SwitchPreference; import androidx.preference.SwitchPreference;
import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.EventBus;
import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.activities.SettingsActivity;
import ml.docilealligator.infinityforreddit.customviews.CustomFontPreferenceFragmentCompat; import ml.docilealligator.infinityforreddit.customviews.CustomFontPreferenceFragmentCompat;
import ml.docilealligator.infinityforreddit.events.ChangeDataSavingModeEvent; import ml.docilealligator.infinityforreddit.events.ChangeDataSavingModeEvent;
import ml.docilealligator.infinityforreddit.events.ChangeDisableImagePreviewEvent; import ml.docilealligator.infinityforreddit.events.ChangeDisableImagePreviewEvent;
@ -30,7 +27,7 @@ public class DataSavingModePreferenceFragment extends CustomFontPreferenceFragme
ListPreference dataSavingModeListPreference = findPreference(SharedPreferencesUtils.DATA_SAVING_MODE); ListPreference dataSavingModeListPreference = findPreference(SharedPreferencesUtils.DATA_SAVING_MODE);
SwitchPreference disableImagePreviewPreference = findPreference(SharedPreferencesUtils.DISABLE_IMAGE_PREVIEW); SwitchPreference disableImagePreviewPreference = findPreference(SharedPreferencesUtils.DISABLE_IMAGE_PREVIEW);
SwitchPreference onlyDisablePreviewInVideoAndGifPostsPreference = findPreference(SharedPreferencesUtils.ONLY_DISABLE_PREVIEW_IN_VIDEO_AND_GIF_POSTS); SwitchPreference onlyDisablePreviewInVideoAndGifPostsPreference = findPreference(SharedPreferencesUtils.ONLY_DISABLE_PREVIEW_IN_VIDEO_AND_GIF_POSTS);
ListPreference redditVideoDefaultResolutionListPreference = findPreference(SharedPreferencesUtils.REDDIT_VIDEO_DEFAULT_RESOLUTION);
if (dataSavingModeListPreference != null) { if (dataSavingModeListPreference != null) {
if (dataSavingModeListPreference.getValue().equals("0")) { if (dataSavingModeListPreference.getValue().equals("0")) {
@ -40,6 +37,9 @@ public class DataSavingModePreferenceFragment extends CustomFontPreferenceFragme
if (disableImagePreviewPreference != null) { if (disableImagePreviewPreference != null) {
disableImagePreviewPreference.setVisible(false); disableImagePreviewPreference.setVisible(false);
} }
if (redditVideoDefaultResolutionListPreference != null) {
redditVideoDefaultResolutionListPreference.setVisible(false);
}
} }
dataSavingModeListPreference.setOnPreferenceChangeListener((preference, newValue) -> { dataSavingModeListPreference.setOnPreferenceChangeListener((preference, newValue) -> {
EventBus.getDefault().post(new ChangeDataSavingModeEvent((String) newValue)); EventBus.getDefault().post(new ChangeDataSavingModeEvent((String) newValue));
@ -50,6 +50,9 @@ public class DataSavingModePreferenceFragment extends CustomFontPreferenceFragme
if (disableImagePreviewPreference != null) { if (disableImagePreviewPreference != null) {
disableImagePreviewPreference.setVisible(false); disableImagePreviewPreference.setVisible(false);
} }
if (redditVideoDefaultResolutionListPreference != null) {
redditVideoDefaultResolutionListPreference.setVisible(false);
}
} else { } else {
if (onlyDisablePreviewInVideoAndGifPostsPreference != null) { if (onlyDisablePreviewInVideoAndGifPostsPreference != null) {
onlyDisablePreviewInVideoAndGifPostsPreference.setVisible(true); onlyDisablePreviewInVideoAndGifPostsPreference.setVisible(true);
@ -57,6 +60,9 @@ public class DataSavingModePreferenceFragment extends CustomFontPreferenceFragme
if (disableImagePreviewPreference != null) { if (disableImagePreviewPreference != null) {
disableImagePreviewPreference.setVisible(true); disableImagePreviewPreference.setVisible(true);
} }
if (redditVideoDefaultResolutionListPreference != null) {
redditVideoDefaultResolutionListPreference.setVisible(true);
}
} }
return true; return true;
}); });

View File

@ -209,6 +209,7 @@ public class SharedPreferencesUtils {
public static final String ALWAYS_SHOW_CHILD_COMMENT_COUNT = "always_show_child_comment_count"; public static final String ALWAYS_SHOW_CHILD_COMMENT_COUNT = "always_show_child_comment_count";
public static final String HIDE_UPVOTE_RATIO = "hide_upvote_ratio"; public static final String HIDE_UPVOTE_RATIO = "hide_upvote_ratio";
public static final String POST_FEED_MAX_RESOLUTION = "post_feed_max_resolution"; public static final String POST_FEED_MAX_RESOLUTION = "post_feed_max_resolution";
public static final String REDDIT_VIDEO_DEFAULT_RESOLUTION = "reddit_video_default_resolution";
public static final String DEFAULT_PREFERENCES_FILE = "ml.docilealligator.infinityforreddit_preferences"; public static final String DEFAULT_PREFERENCES_FILE = "ml.docilealligator.infinityforreddit_preferences";
public static final String MAIN_PAGE_TABS_SHARED_PREFERENCES_FILE = "ml.docilealligator.infinityforreddit.main_page_tabs"; public static final String MAIN_PAGE_TABS_SHARED_PREFERENCES_FILE = "ml.docilealligator.infinityforreddit.main_page_tabs";

View File

@ -643,4 +643,24 @@
<item>2</item> <item>2</item>
</string-array> </string-array>
<string-array name="settings_reddit_video_default_resolution">
<item>Auto</item>
<item>1080p</item>
<item>720p</item>
<item>480p</item>
<item>360p</item>
<item>240p</item>
<item>144p</item>
</string-array>
<string-array name="settings_reddit_video_default_resolution_values">
<item>0</item>
<item>1080</item>
<item>720</item>
<item>480</item>
<item>360</item>
<item>240</item>
<item>144</item>
</string-array>
</resources> </resources>

View File

@ -640,6 +640,7 @@
<string name="settings_miscellaneous_dangerous_group_title">Dangerous</string> <string name="settings_miscellaneous_dangerous_group_title">Dangerous</string>
<string name="settings_post_feed_max_resolution_warning_title">Increase the value to show previews in higher resolution, but the app may crash unexpectedly.</string> <string name="settings_post_feed_max_resolution_warning_title">Increase the value to show previews in higher resolution, but the app may crash unexpectedly.</string>
<string name="settings_post_feed_max_resolution_title">Post Feed Preview Max Resolution (Width * Height)</string> <string name="settings_post_feed_max_resolution_title">Post Feed Preview Max Resolution (Width * Height)</string>
<string name="settings_reddit_video_default_resolution">Reddit Video Default Resolution</string>
<string name="no_link_available">Cannot get the link</string> <string name="no_link_available">Cannot get the link</string>

View File

@ -25,4 +25,12 @@
app:key="only_disable_preview_in_video_and_gif_posts" app:key="only_disable_preview_in_video_and_gif_posts"
android:title="@string/settings_only_disable_preview_in_video_and_gif_posts_title" /> android:title="@string/settings_only_disable_preview_in_video_and_gif_posts_title" />
<ml.docilealligator.infinityforreddit.customviews.CustomFontListPreference
app:defaultValue="360"
app:key="reddit_video_default_resolution"
android:title="@string/settings_reddit_video_default_resolution"
app:entries="@array/settings_reddit_video_default_resolution"
app:entryValues="@array/settings_reddit_video_default_resolution_values"
app:useSimpleSummaryProvider="true" />
</PreferenceScreen> </PreferenceScreen>