From 9e66a8656b87408bce66eda0c485c98d1205ac4b Mon Sep 17 00:00:00 2001 From: polymorphicshade Date: Thu, 6 Aug 2020 17:06:44 -0600 Subject: [PATCH] SponsorBlock: Added button to player that enables/disables segment skipping for the current video --- .../org/schabi/newpipe/player/BasePlayer.java | 19 +++++++++++++- .../newpipe/player/VideoPlayerImpl.java | 25 +++++++++++++++++++ .../drawable/ic_sponsor_block_white_96dp.xml | 10 -------- .../ic_sponsorblock_disable_white_24dp.xml | 6 +++++ .../ic_sponsorblock_enable_white_24dp.xml | 6 +++++ app/src/main/res/layout/player.xml | 14 +++++++++++ app/src/main/res/values/strings.xml | 1 + 7 files changed, 70 insertions(+), 11 deletions(-) delete mode 100644 app/src/main/res/drawable/ic_sponsor_block_white_96dp.xml create mode 100644 app/src/main/res/drawable/ic_sponsorblock_disable_white_24dp.xml create mode 100644 app/src/main/res/drawable/ic_sponsorblock_enable_white_24dp.xml diff --git a/app/src/main/java/org/schabi/newpipe/player/BasePlayer.java b/app/src/main/java/org/schabi/newpipe/player/BasePlayer.java index 7939f4acc..c08e8ce46 100644 --- a/app/src/main/java/org/schabi/newpipe/player/BasePlayer.java +++ b/app/src/main/java/org/schabi/newpipe/player/BasePlayer.java @@ -205,6 +205,7 @@ public abstract class BasePlayer implements private Disposable stateLoader; protected int currentState = STATE_PREFLIGHT; + private boolean isBlockingSponsors; public BasePlayer(@NonNull final Context context) { this.context = context; @@ -236,6 +237,9 @@ public abstract class BasePlayer implements this.renderFactory = new DefaultRenderersFactory(context); this.mPrefs = PreferenceManager.getDefaultSharedPreferences(App.getApp()); + + isBlockingSponsors = mPrefs.getBoolean(context.getString(R.string.sponsorblock_enable_key), + false); } public void setup() { @@ -691,6 +695,17 @@ public abstract class BasePlayer implements return simpleExoPlayer.getVolume() == 0; } + public void onBlockingSponsorsButtonClicked() { + if (DEBUG) { + Log.d(TAG, "onBlockingSponsorsButtonClicked() called"); + } + isBlockingSponsors = !isBlockingSponsors; + } + + public boolean isBlockingSponsors() { + return isBlockingSponsors; + } + /*////////////////////////////////////////////////////////////////////////// // Progress Updates //////////////////////////////////////////////////////////////////////////*/ @@ -716,7 +731,9 @@ public abstract class BasePlayer implements simpleExoPlayer.getBufferedPercentage() ); - if (mPrefs.getBoolean(context.getString(R.string.sponsorblock_enable_key), false)) { + if (isBlockingSponsors + && mPrefs.getBoolean( + context.getString(R.string.sponsorblock_enable_key), false)) { VideoSegment segment = getSkippableSegment(currentProgress); if (segment == null) { return; diff --git a/app/src/main/java/org/schabi/newpipe/player/VideoPlayerImpl.java b/app/src/main/java/org/schabi/newpipe/player/VideoPlayerImpl.java index 0f98b2296..680c35dfe 100644 --- a/app/src/main/java/org/schabi/newpipe/player/VideoPlayerImpl.java +++ b/app/src/main/java/org/schabi/newpipe/player/VideoPlayerImpl.java @@ -163,6 +163,7 @@ public class VideoPlayerImpl extends VideoPlayer private ImageButton playerCloseButton; private ImageButton screenRotationButton; private ImageButton muteButton; + private ImageButton blockSponsorsButton; private ImageButton playPauseButton; private ImageButton playPreviousButton; @@ -295,6 +296,7 @@ public class VideoPlayerImpl extends VideoPlayer this.screenRotationButton = view.findViewById(R.id.screenRotationButton); this.playerCloseButton = view.findViewById(R.id.playerCloseButton); this.muteButton = view.findViewById(R.id.switchMute); + this.blockSponsorsButton = view.findViewById(R.id.switchSponsorBlocking); this.playPauseButton = view.findViewById(R.id.playPauseButton); this.playPreviousButton = view.findViewById(R.id.playPreviousButton); @@ -358,6 +360,7 @@ public class VideoPlayerImpl extends VideoPlayer playWithKodi.setVisibility(View.GONE); openInBrowser.setVisibility(View.GONE); muteButton.setVisibility(View.GONE); + blockSponsorsButton.setVisibility(View.GONE); playerCloseButton.setVisibility(View.GONE); getTopControlsRoot().bringToFront(); getTopControlsRoot().setClickable(false); @@ -379,6 +382,11 @@ public class VideoPlayerImpl extends VideoPlayer showHideKodiButton(); openInBrowser.setVisibility(View.VISIBLE); muteButton.setVisibility(View.VISIBLE); + + boolean isSponsorBlockEnabled = mPrefs.getBoolean( + context.getString(R.string.sponsorblock_enable_key), false); + blockSponsorsButton.setVisibility(isSponsorBlockEnabled ? View.VISIBLE : View.GONE); + playerCloseButton.setVisibility(isFullscreen ? View.GONE : View.VISIBLE); // Top controls have a large minHeight which is allows to drag the player // down in fullscreen mode (just larger area to make easy to locate by finger) @@ -393,6 +401,7 @@ public class VideoPlayerImpl extends VideoPlayer channelTextView.setVisibility(View.VISIBLE); } setMuteButton(muteButton, isMuted()); + setBlockSponsorsButton(blockSponsorsButton, isBlockingSponsors()); animateRotation(moreOptionsButton, DEFAULT_CONTROLS_DURATION, 0); } @@ -463,6 +472,7 @@ public class VideoPlayerImpl extends VideoPlayer openInBrowser.setOnClickListener(this); playerCloseButton.setOnClickListener(this); muteButton.setOnClickListener(this); + blockSponsorsButton.setOnClickListener(this); settingsContentObserver = new ContentObserver(new Handler()) { @Override @@ -632,6 +642,12 @@ public class VideoPlayerImpl extends VideoPlayer setMuteButton(muteButton, isMuted()); } + @Override + public void onBlockingSponsorsButtonClicked() { + super.onBlockingSponsorsButtonClicked(); + setBlockSponsorsButton(blockSponsorsButton, isBlockingSponsors()); + } + @Override public void onUpdateProgress(final int currentProgress, final int duration, final int bufferPercent) { @@ -798,6 +814,8 @@ public class VideoPlayerImpl extends VideoPlayer } } else if (v.getId() == muteButton.getId()) { onMuteUnmuteButtonClicked(); + } else if (v.getId() == blockSponsorsButton.getId()) { + onBlockingSponsorsButtonClicked(); } else if (v.getId() == playerCloseButton.getId()) { service.sendBroadcast(new Intent(VideoDetailFragment.ACTION_HIDE_MAIN_PLAYER)); } @@ -1568,6 +1586,13 @@ public class VideoPlayerImpl extends VideoPlayer ? R.drawable.ic_volume_off_white_24dp : R.drawable.ic_volume_up_white_24dp)); } + protected void setBlockSponsorsButton(final ImageButton button, + final boolean isBlockingSponsors) { + button.setImageDrawable(AppCompatResources.getDrawable(service, isBlockingSponsors + ? R.drawable.ic_sponsorblock_disable_white_24dp + : R.drawable.ic_sponsorblock_enable_white_24dp)); + } + /** * @return true if main player is attached to activity and activity inside multiWindow mode */ diff --git a/app/src/main/res/drawable/ic_sponsor_block_white_96dp.xml b/app/src/main/res/drawable/ic_sponsor_block_white_96dp.xml deleted file mode 100644 index 45e2337c0..000000000 --- a/app/src/main/res/drawable/ic_sponsor_block_white_96dp.xml +++ /dev/null @@ -1,10 +0,0 @@ - - - diff --git a/app/src/main/res/drawable/ic_sponsorblock_disable_white_24dp.xml b/app/src/main/res/drawable/ic_sponsorblock_disable_white_24dp.xml new file mode 100644 index 000000000..2d7730aaa --- /dev/null +++ b/app/src/main/res/drawable/ic_sponsorblock_disable_white_24dp.xml @@ -0,0 +1,6 @@ + + + diff --git a/app/src/main/res/drawable/ic_sponsorblock_enable_white_24dp.xml b/app/src/main/res/drawable/ic_sponsorblock_enable_white_24dp.xml new file mode 100644 index 000000000..58448ed0c --- /dev/null +++ b/app/src/main/res/drawable/ic_sponsorblock_enable_white_24dp.xml @@ -0,0 +1,6 @@ + + + diff --git a/app/src/main/res/layout/player.xml b/app/src/main/res/layout/player.xml index ed79d9f86..4858161dc 100644 --- a/app/src/main/res/layout/player.xml +++ b/app/src/main/res/layout/player.xml @@ -298,6 +298,7 @@ android:layout_width="wrap_content" android:layout_height="37dp" android:padding="@dimen/player_main_buttons_padding" + android:layout_marginEnd="8dp" android:clickable="true" android:focusable="true" android:scaleType="fitXY" @@ -306,6 +307,19 @@ android:contentDescription="@string/mute" tools:ignore="RtlHardcoded" /> + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 444f47c4b..e1e3340e7 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -705,4 +705,5 @@ Skipped interaction reminder segment Skipped unpaid/self promo segment Skipped non-music segment + Toggle skipping sponsors