Skip to content

Commit 097c236

Browse files
authored
Merge pull request #8180 from Trust04zh/fix-4053-8176
Make UI behavior for playback information display more consistent
2 parents 80e0c6a + e947e86 commit 097c236

8 files changed

Lines changed: 98 additions & 63 deletions

File tree

app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java

Lines changed: 21 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import static org.schabi.newpipe.ktx.ViewUtils.animateRotation;
88
import static org.schabi.newpipe.player.helper.PlayerHelper.globalScreenOrientationLocked;
99
import static org.schabi.newpipe.player.helper.PlayerHelper.isClearingQueueConfirmationRequired;
10-
import static org.schabi.newpipe.player.playqueue.PlayQueueItem.RECOVERY_UNSET;
10+
import static org.schabi.newpipe.util.DependentPreferenceHelper.getResumePlaybackEnabled;
1111
import static org.schabi.newpipe.util.ExtractorHelper.showMetaInfoInTextView;
1212
import static org.schabi.newpipe.util.ListHelper.getUrlAndNonTorrentStreams;
1313
import static org.schabi.newpipe.util.NavigationHelper.openPlayQueue;
@@ -1448,8 +1448,8 @@ public void showLoading() {
14481448

14491449
animate(binding.detailThumbnailPlayButton, false, 50);
14501450
animate(binding.detailDurationView, false, 100);
1451-
animate(binding.detailPositionView, false, 100);
1452-
animate(binding.positionView, false, 50);
1451+
binding.detailPositionView.setVisibility(View.GONE);
1452+
binding.positionView.setVisibility(View.GONE);
14531453

14541454
binding.detailVideoTitleView.setText(title);
14551455
binding.detailVideoTitleView.setMaxLines(1);
@@ -1566,7 +1566,7 @@ public void handleResult(@NonNull final StreamInfo info) {
15661566
binding.detailToggleSecondaryControlsView.setVisibility(View.VISIBLE);
15671567
binding.detailSecondaryControlPanel.setVisibility(View.GONE);
15681568

1569-
updateProgressInfo(info);
1569+
checkUpdateProgressInfo(info);
15701570
initThumbnailViews(info);
15711571
showMetaInfoInTextView(info.getMetaInfo(), binding.detailMetaInfoTextView,
15721572
binding.detailMetaInfoSeparator, disposables);
@@ -1665,67 +1665,43 @@ public void openDownloadDialog() {
16651665
// Stream Results
16661666
//////////////////////////////////////////////////////////////////////////*/
16671667

1668-
private void updateProgressInfo(@NonNull final StreamInfo info) {
1668+
private void checkUpdateProgressInfo(@NonNull final StreamInfo info) {
16691669
if (positionSubscriber != null) {
16701670
positionSubscriber.dispose();
16711671
}
1672-
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
1673-
final boolean playbackResumeEnabled = prefs
1674-
.getBoolean(activity.getString(R.string.enable_watch_history_key), true)
1675-
&& prefs.getBoolean(activity.getString(R.string.enable_playback_resume_key), true);
1676-
final boolean showPlaybackPosition = prefs.getBoolean(
1677-
activity.getString(R.string.enable_playback_state_lists_key), true);
1678-
if (!playbackResumeEnabled) {
1679-
if (playQueue == null || playQueue.getStreams().isEmpty()
1680-
|| playQueue.getItem().getRecoveryPosition() == RECOVERY_UNSET
1681-
|| !showPlaybackPosition) {
1682-
binding.positionView.setVisibility(View.INVISIBLE);
1683-
binding.detailPositionView.setVisibility(View.GONE);
1684-
// TODO: Remove this check when separation of concerns is done.
1685-
// (live streams weren't getting updated because they are mixed)
1686-
if (!StreamTypeUtil.isLiveStream(info.getStreamType())) {
1687-
return;
1688-
}
1689-
} else {
1690-
// Show saved position from backStack if user allows it
1691-
showPlaybackProgress(playQueue.getItem().getRecoveryPosition(),
1692-
playQueue.getItem().getDuration() * 1000);
1693-
animate(binding.positionView, true, 500);
1694-
animate(binding.detailPositionView, true, 500);
1695-
}
1672+
if (!getResumePlaybackEnabled(activity)) {
1673+
binding.positionView.setVisibility(View.GONE);
1674+
binding.detailPositionView.setVisibility(View.GONE);
16961675
return;
16971676
}
16981677
final HistoryRecordManager recordManager = new HistoryRecordManager(requireContext());
1699-
1700-
// TODO: Separate concerns when updating database data.
1701-
// (move the updating part to when the loading happens)
17021678
positionSubscriber = recordManager.loadStreamState(info)
17031679
.subscribeOn(Schedulers.io())
17041680
.onErrorComplete()
17051681
.observeOn(AndroidSchedulers.mainThread())
17061682
.subscribe(state -> {
1707-
showPlaybackProgress(state.getProgressMillis(), info.getDuration() * 1000);
1708-
animate(binding.positionView, true, 500);
1709-
animate(binding.detailPositionView, true, 500);
1683+
updatePlaybackProgress(
1684+
state.getProgressMillis(), info.getDuration() * 1000);
17101685
}, e -> {
1711-
if (DEBUG) {
1712-
e.printStackTrace();
1713-
}
1686+
// impossible since the onErrorComplete()
17141687
}, () -> {
17151688
binding.positionView.setVisibility(View.GONE);
17161689
binding.detailPositionView.setVisibility(View.GONE);
17171690
});
17181691
}
17191692

1720-
private void showPlaybackProgress(final long progress, final long duration) {
1693+
private void updatePlaybackProgress(final long progress, final long duration) {
1694+
if (!getResumePlaybackEnabled(activity)) {
1695+
return;
1696+
}
17211697
final int progressSeconds = (int) TimeUnit.MILLISECONDS.toSeconds(progress);
17221698
final int durationSeconds = (int) TimeUnit.MILLISECONDS.toSeconds(duration);
1723-
// If the old and the new progress values have a big difference then use
1724-
// animation. Otherwise don't because it affects CPU
1725-
final boolean shouldAnimate = Math.abs(binding.positionView.getProgress()
1726-
- progressSeconds) > 2;
1699+
// If the old and the new progress values have a big difference then use animation.
1700+
// Otherwise don't because it affects CPU
1701+
final int progressDifference = Math.abs(binding.positionView.getProgress()
1702+
- progressSeconds);
17271703
binding.positionView.setMax(durationSeconds);
1728-
if (shouldAnimate) {
1704+
if (progressDifference > 2) {
17291705
binding.positionView.setProgressAnimated(progressSeconds);
17301706
} else {
17311707
binding.positionView.setProgress(progressSeconds);
@@ -1820,7 +1796,7 @@ public void onProgressUpdate(final int currentProgress,
18201796
}
18211797

18221798
if (player.getPlayQueue().getItem().getUrl().equals(url)) {
1823-
showPlaybackProgress(currentProgress, duration);
1799+
updatePlaybackProgress(currentProgress, duration);
18241800
}
18251801
}
18261802

app/src/main/java/org/schabi/newpipe/info_list/holder/StreamMiniInfoItemHolder.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.schabi.newpipe.info_list.InfoItemBuilder;
1515
import org.schabi.newpipe.ktx.ViewUtils;
1616
import org.schabi.newpipe.local.history.HistoryRecordManager;
17+
import org.schabi.newpipe.util.DependentPreferenceHelper;
1718
import org.schabi.newpipe.util.Localization;
1819
import org.schabi.newpipe.util.PicassoHelper;
1920
import org.schabi.newpipe.util.StreamTypeUtil;
@@ -60,8 +61,12 @@ public void updateFromItem(final InfoItem infoItem,
6061
R.color.duration_background_color));
6162
itemDurationView.setVisibility(View.VISIBLE);
6263

63-
final StreamStateEntity state2 = historyRecordManager.loadStreamState(infoItem)
64-
.blockingGet()[0];
64+
StreamStateEntity state2 = null;
65+
if (DependentPreferenceHelper
66+
.getPositionsInListsEnabled(itemProgressView.getContext())) {
67+
state2 = historyRecordManager.loadStreamState(infoItem)
68+
.blockingGet()[0];
69+
}
6570
if (state2 != null) {
6671
itemProgressView.setVisibility(View.VISIBLE);
6772
itemProgressView.setMax((int) item.getDuration());
@@ -111,9 +116,12 @@ public void updateState(final InfoItem infoItem,
111116
final HistoryRecordManager historyRecordManager) {
112117
final StreamInfoItem item = (StreamInfoItem) infoItem;
113118

114-
final StreamStateEntity state = historyRecordManager
115-
.loadStreamState(infoItem)
116-
.blockingGet()[0];
119+
StreamStateEntity state = null;
120+
if (DependentPreferenceHelper.getPositionsInListsEnabled(itemProgressView.getContext())) {
121+
state = historyRecordManager
122+
.loadStreamState(infoItem)
123+
.blockingGet()[0];
124+
}
117125
if (state != null && item.getDuration() > 0
118126
&& !StreamTypeUtil.isLiveStream(item.getStreamType())) {
119127
itemProgressView.setMax((int) item.getDuration());

app/src/main/java/org/schabi/newpipe/local/holder/LocalPlaylistStreamItemHolder.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.schabi.newpipe.ktx.ViewUtils;
1515
import org.schabi.newpipe.local.LocalItemBuilder;
1616
import org.schabi.newpipe.local.history.HistoryRecordManager;
17+
import org.schabi.newpipe.util.DependentPreferenceHelper;
1718
import org.schabi.newpipe.util.Localization;
1819
import org.schabi.newpipe.util.PicassoHelper;
1920
import org.schabi.newpipe.util.ServiceHelper;
@@ -68,7 +69,8 @@ public void updateFromItem(final LocalItem localItem,
6869
R.color.duration_background_color));
6970
itemDurationView.setVisibility(View.VISIBLE);
7071

71-
if (item.getProgressMillis() > 0) {
72+
if (DependentPreferenceHelper.getPositionsInListsEnabled(itemProgressView.getContext())
73+
&& item.getProgressMillis() > 0) {
7274
itemProgressView.setVisibility(View.VISIBLE);
7375
itemProgressView.setMax((int) item.getStreamEntity().getDuration());
7476
itemProgressView.setProgress((int) TimeUnit.MILLISECONDS
@@ -109,7 +111,8 @@ public void updateState(final LocalItem localItem,
109111
}
110112
final PlaylistStreamEntry item = (PlaylistStreamEntry) localItem;
111113

112-
if (item.getProgressMillis() > 0 && item.getStreamEntity().getDuration() > 0) {
114+
if (DependentPreferenceHelper.getPositionsInListsEnabled(itemProgressView.getContext())
115+
&& item.getProgressMillis() > 0 && item.getStreamEntity().getDuration() > 0) {
113116
itemProgressView.setMax((int) item.getStreamEntity().getDuration());
114117
if (itemProgressView.getVisibility() == View.VISIBLE) {
115118
itemProgressView.setProgressAnimated((int) TimeUnit.MILLISECONDS

app/src/main/java/org/schabi/newpipe/local/holder/LocalStatisticStreamItemHolder.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.schabi.newpipe.ktx.ViewUtils;
1515
import org.schabi.newpipe.local.LocalItemBuilder;
1616
import org.schabi.newpipe.local.history.HistoryRecordManager;
17+
import org.schabi.newpipe.util.DependentPreferenceHelper;
1718
import org.schabi.newpipe.util.Localization;
1819
import org.schabi.newpipe.util.PicassoHelper;
1920
import org.schabi.newpipe.util.ServiceHelper;
@@ -97,7 +98,8 @@ public void updateFromItem(final LocalItem localItem,
9798
R.color.duration_background_color));
9899
itemDurationView.setVisibility(View.VISIBLE);
99100

100-
if (item.getProgressMillis() > 0) {
101+
if (DependentPreferenceHelper.getPositionsInListsEnabled(itemProgressView.getContext())
102+
&& item.getProgressMillis() > 0) {
101103
itemProgressView.setVisibility(View.VISIBLE);
102104
itemProgressView.setMax((int) item.getStreamEntity().getDuration());
103105
itemProgressView.setProgress((int) TimeUnit.MILLISECONDS
@@ -141,7 +143,8 @@ public void updateState(final LocalItem localItem,
141143
}
142144
final StreamStatisticsEntry item = (StreamStatisticsEntry) localItem;
143145

144-
if (item.getProgressMillis() > 0 && item.getStreamEntity().getDuration() > 0) {
146+
if (DependentPreferenceHelper.getPositionsInListsEnabled(itemProgressView.getContext())
147+
&& item.getProgressMillis() > 0 && item.getStreamEntity().getDuration() > 0) {
145148
itemProgressView.setMax((int) item.getStreamEntity().getDuration());
146149
if (itemProgressView.getVisibility() == View.VISIBLE) {
147150
itemProgressView.setProgressAnimated((int) TimeUnit.MILLISECONDS

app/src/main/java/org/schabi/newpipe/player/Player.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import static com.google.android.exoplayer2.Player.RepeatMode;
3030
import static org.schabi.newpipe.extractor.ServiceList.YouTube;
3131
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
32-
import static org.schabi.newpipe.player.helper.PlayerHelper.isPlaybackResumeEnabled;
3332
import static org.schabi.newpipe.player.helper.PlayerHelper.nextRepeatMode;
3433
import static org.schabi.newpipe.player.helper.PlayerHelper.retrievePlaybackParametersFromPrefs;
3534
import static org.schabi.newpipe.player.helper.PlayerHelper.retrieveSeekDurationFromPreferences;
@@ -115,6 +114,7 @@
115114
import org.schabi.newpipe.player.ui.PlayerUiList;
116115
import org.schabi.newpipe.player.ui.PopupPlayerUi;
117116
import org.schabi.newpipe.player.ui.VideoPlayerUi;
117+
import org.schabi.newpipe.util.DependentPreferenceHelper;
118118
import org.schabi.newpipe.util.DeviceUtils;
119119
import org.schabi.newpipe.util.ListHelper;
120120
import org.schabi.newpipe.util.NavigationHelper;
@@ -391,7 +391,7 @@ public void handleIntent(@NonNull final Intent intent) {
391391
simpleExoPlayer.setPlayWhenReady(playWhenReady);
392392

393393
} else if (intent.getBooleanExtra(RESUME_PLAYBACK, false)
394-
&& isPlaybackResumeEnabled(this)
394+
&& DependentPreferenceHelper.getResumePlaybackEnabled(context)
395395
&& !samePlayQueue
396396
&& !newQueue.isEmpty()
397397
&& newQueue.getItem() != null

app/src/main/java/org/schabi/newpipe/player/helper/PlayerHelper.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -429,13 +429,6 @@ private static SinglePlayQueue getAutoQueuedSinglePlayQueue(
429429
// Utils used by player
430430
////////////////////////////////////////////////////////////////////////////
431431

432-
public static boolean isPlaybackResumeEnabled(final Player player) {
433-
return player.getPrefs().getBoolean(
434-
player.getContext().getString(R.string.enable_watch_history_key), true)
435-
&& player.getPrefs().getBoolean(
436-
player.getContext().getString(R.string.enable_playback_resume_key), true);
437-
}
438-
439432
@RepeatMode
440433
public static int nextRepeatMode(@RepeatMode final int repeatMode) {
441434
switch (repeatMode) {
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package org.schabi.newpipe.util;
2+
3+
import android.content.Context;
4+
import android.content.SharedPreferences;
5+
6+
import androidx.preference.PreferenceManager;
7+
8+
import org.schabi.newpipe.R;
9+
10+
/**
11+
* For preferences with dependencies and multiple use case,
12+
* this class can be used to reduce the lines of code.
13+
*/
14+
public final class DependentPreferenceHelper {
15+
16+
private DependentPreferenceHelper() {
17+
// no instance
18+
}
19+
20+
/**
21+
* Option `Resume playback` depends on `Watch history`, this method can be used to retrieve if
22+
* `Resume playback` and its dependencies are all enabled.
23+
*
24+
* @param context the Android context
25+
* @return returns true if `Resume playback` and `Watch history` are both enabled
26+
*/
27+
public static boolean getResumePlaybackEnabled(final Context context) {
28+
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
29+
30+
return prefs.getBoolean(context.getString(
31+
R.string.enable_watch_history_key), true)
32+
&& prefs.getBoolean(context.getString(
33+
R.string.enable_playback_resume_key), true);
34+
}
35+
36+
/**
37+
* Option `Position in lists` depends on `Watch history`, this method can be used to retrieve if
38+
* `Position in lists` and its dependencies are all enabled.
39+
*
40+
* @param context the Android context
41+
* @return returns true if `Positions in lists` and `Watch history` are both enabled
42+
*/
43+
public static boolean getPositionsInListsEnabled(final Context context) {
44+
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
45+
46+
return prefs.getBoolean(context.getString(
47+
R.string.enable_watch_history_key), true)
48+
&& prefs.getBoolean(context.getString(
49+
R.string.enable_playback_state_lists_key), true);
50+
}
51+
}

app/src/main/res/xml/history_settings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
<SwitchPreferenceCompat
2525
android:defaultValue="true"
26+
android:dependency="@string/enable_watch_history_key"
2627
android:key="@string/enable_playback_state_lists_key"
2728
android:summary="@string/enable_playback_state_lists_summary"
2829
android:title="@string/enable_playback_state_lists_title"

0 commit comments

Comments
 (0)