From 570dded8d601ea86ec92038283a65774fed8fb92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Jakse?= Date: Thu, 16 Jan 2020 20:44:08 +0100 Subject: [PATCH 1/3] Add field START_PAUSED to the Player Intent This allows fixing spurious playback resume when minimizing to the background player. --- .../org/schabi/newpipe/player/BackgroundPlayerActivity.java | 5 ++++- app/src/main/java/org/schabi/newpipe/player/BasePlayer.java | 4 +++- .../java/org/schabi/newpipe/player/MainVideoPlayer.java | 6 ++++-- .../java/org/schabi/newpipe/player/PopupVideoPlayer.java | 5 +++-- .../org/schabi/newpipe/player/PopupVideoPlayerActivity.java | 5 ++++- .../org/schabi/newpipe/player/ServicePlayerActivity.java | 6 +++++- .../main/java/org/schabi/newpipe/util/NavigationHelper.java | 6 ++++-- 7 files changed, 27 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/player/BackgroundPlayerActivity.java b/app/src/main/java/org/schabi/newpipe/player/BackgroundPlayerActivity.java index 761b50d85..59f6e1e6d 100644 --- a/app/src/main/java/org/schabi/newpipe/player/BackgroundPlayerActivity.java +++ b/app/src/main/java/org/schabi/newpipe/player/BackgroundPlayerActivity.java @@ -57,7 +57,10 @@ public final class BackgroundPlayerActivity extends ServicePlayerActivity { this.player.setRecovery(); getApplicationContext().sendBroadcast(getPlayerShutdownIntent()); - getApplicationContext().startService(getSwitchIntent(PopupVideoPlayer.class)); + getApplicationContext().startService( + getSwitchIntent(PopupVideoPlayer.class) + .putExtra(BasePlayer.START_PAUSED, !this.player.isPlaying()) + ); return true; } return false; 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 6452a9850..cd1ec07f9 100644 --- a/app/src/main/java/org/schabi/newpipe/player/BasePlayer.java +++ b/app/src/main/java/org/schabi/newpipe/player/BasePlayer.java @@ -150,6 +150,8 @@ public abstract class BasePlayer implements @NonNull public static final String RESUME_PLAYBACK = "resume_playback"; @NonNull + public static final String START_PAUSED = "start_paused"; + @NonNull public static final String SELECT_ON_APPEND = "select_on_append"; /*////////////////////////////////////////////////////////////////////////// @@ -304,7 +306,7 @@ public abstract class BasePlayer implements } // Good to go... initPlayback(queue, repeatMode, playbackSpeed, playbackPitch, playbackSkipSilence, - /*playOnInit=*/true); + /*playOnInit=*/!intent.getBooleanExtra(START_PAUSED, false)); } protected void initPlayback(@NonNull final PlayQueue queue, diff --git a/app/src/main/java/org/schabi/newpipe/player/MainVideoPlayer.java b/app/src/main/java/org/schabi/newpipe/player/MainVideoPlayer.java index 7a3e60c66..9ccf5b9d3 100644 --- a/app/src/main/java/org/schabi/newpipe/player/MainVideoPlayer.java +++ b/app/src/main/java/org/schabi/newpipe/player/MainVideoPlayer.java @@ -614,7 +614,8 @@ public final class MainVideoPlayer extends AppCompatActivity this.getPlaybackPitch(), this.getPlaybackSkipSilence(), this.getPlaybackQuality(), - false + false, + !isPlaying() ); context.startService(intent); @@ -637,7 +638,8 @@ public final class MainVideoPlayer extends AppCompatActivity this.getPlaybackPitch(), this.getPlaybackSkipSilence(), this.getPlaybackQuality(), - false + false, + !isPlaying() ); context.startService(intent); diff --git a/app/src/main/java/org/schabi/newpipe/player/PopupVideoPlayer.java b/app/src/main/java/org/schabi/newpipe/player/PopupVideoPlayer.java index 969c47990..70fb77060 100644 --- a/app/src/main/java/org/schabi/newpipe/player/PopupVideoPlayer.java +++ b/app/src/main/java/org/schabi/newpipe/player/PopupVideoPlayer.java @@ -567,7 +567,8 @@ public final class PopupVideoPlayer extends Service { this.getPlaybackPitch(), this.getPlaybackSkipSilence(), this.getPlaybackQuality(), - false + false, + !isPlaying() ); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent); @@ -1123,4 +1124,4 @@ public final class PopupVideoPlayer extends Service { return distanceFromCloseButton(popupMotionEvent) <= getClosingRadius(); } } -} \ No newline at end of file +} diff --git a/app/src/main/java/org/schabi/newpipe/player/PopupVideoPlayerActivity.java b/app/src/main/java/org/schabi/newpipe/player/PopupVideoPlayerActivity.java index 44fcdb8dd..5000d07e2 100644 --- a/app/src/main/java/org/schabi/newpipe/player/PopupVideoPlayerActivity.java +++ b/app/src/main/java/org/schabi/newpipe/player/PopupVideoPlayerActivity.java @@ -50,7 +50,10 @@ public final class PopupVideoPlayerActivity extends ServicePlayerActivity { if (item.getItemId() == R.id.action_switch_background) { this.player.setRecovery(); getApplicationContext().sendBroadcast(getPlayerShutdownIntent()); - getApplicationContext().startService(getSwitchIntent(BackgroundPlayer.class)); + getApplicationContext().startService( + getSwitchIntent(BackgroundPlayer.class) + .putExtra(BasePlayer.START_PAUSED, !this.player.isPlaying()) + ); return true; } return false; diff --git a/app/src/main/java/org/schabi/newpipe/player/ServicePlayerActivity.java b/app/src/main/java/org/schabi/newpipe/player/ServicePlayerActivity.java index 2207808ac..a7b3006a1 100644 --- a/app/src/main/java/org/schabi/newpipe/player/ServicePlayerActivity.java +++ b/app/src/main/java/org/schabi/newpipe/player/ServicePlayerActivity.java @@ -167,7 +167,10 @@ public abstract class ServicePlayerActivity extends AppCompatActivity case R.id.action_switch_main: this.player.setRecovery(); getApplicationContext().sendBroadcast(getPlayerShutdownIntent()); - getApplicationContext().startActivity(getSwitchIntent(MainVideoPlayer.class)); + getApplicationContext().startActivity( + getSwitchIntent(MainVideoPlayer.class) + .putExtra(BasePlayer.START_PAUSED, !this.player.isPlaying()) + ); return true; } return onPlayerOptionSelected(item) || super.onOptionsItemSelected(item); @@ -189,6 +192,7 @@ public abstract class ServicePlayerActivity extends AppCompatActivity this.player.getPlaybackPitch(), this.player.getPlaybackSkipSilence(), null, + false, false ).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); } diff --git a/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java b/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java index e2b03c8e8..a19aa92ae 100644 --- a/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java +++ b/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java @@ -109,12 +109,14 @@ public class NavigationHelper { final float playbackPitch, final boolean playbackSkipSilence, @Nullable final String playbackQuality, - final boolean resumePlayback) { + final boolean resumePlayback, + final boolean startPaused) { return getPlayerIntent(context, targetClazz, playQueue, playbackQuality, resumePlayback) .putExtra(BasePlayer.REPEAT_MODE, repeatMode) .putExtra(BasePlayer.PLAYBACK_SPEED, playbackSpeed) .putExtra(BasePlayer.PLAYBACK_PITCH, playbackPitch) - .putExtra(BasePlayer.PLAYBACK_SKIP_SILENCE, playbackSkipSilence); + .putExtra(BasePlayer.PLAYBACK_SKIP_SILENCE, playbackSkipSilence) + .putExtra(BasePlayer.START_PAUSED, startPaused); } public static void playOnMainPlayer(final Context context, final PlayQueue queue, final boolean resumePlayback) { From ef90493c27af898d8e79adbb5e8d449ef5d0ae35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Jakse?= Date: Thu, 16 Jan 2020 20:46:11 +0100 Subject: [PATCH 2/3] Deduplicate code switching to another player into a function --- .../player/BackgroundPlayerActivity.java | 8 +------- .../player/PopupVideoPlayerActivity.java | 8 +------- .../newpipe/player/ServicePlayerActivity.java | 18 ++++++++++-------- 3 files changed, 12 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/player/BackgroundPlayerActivity.java b/app/src/main/java/org/schabi/newpipe/player/BackgroundPlayerActivity.java index 59f6e1e6d..1b5b5d07c 100644 --- a/app/src/main/java/org/schabi/newpipe/player/BackgroundPlayerActivity.java +++ b/app/src/main/java/org/schabi/newpipe/player/BackgroundPlayerActivity.java @@ -55,13 +55,7 @@ public final class BackgroundPlayerActivity extends ServicePlayerActivity { return true; } - this.player.setRecovery(); - getApplicationContext().sendBroadcast(getPlayerShutdownIntent()); - getApplicationContext().startService( - getSwitchIntent(PopupVideoPlayer.class) - .putExtra(BasePlayer.START_PAUSED, !this.player.isPlaying()) - ); - return true; + return switchTo(PopupVideoPlayer.class); } return false; } diff --git a/app/src/main/java/org/schabi/newpipe/player/PopupVideoPlayerActivity.java b/app/src/main/java/org/schabi/newpipe/player/PopupVideoPlayerActivity.java index 5000d07e2..b2af6d9d8 100644 --- a/app/src/main/java/org/schabi/newpipe/player/PopupVideoPlayerActivity.java +++ b/app/src/main/java/org/schabi/newpipe/player/PopupVideoPlayerActivity.java @@ -48,13 +48,7 @@ public final class PopupVideoPlayerActivity extends ServicePlayerActivity { @Override public boolean onPlayerOptionSelected(MenuItem item) { if (item.getItemId() == R.id.action_switch_background) { - this.player.setRecovery(); - getApplicationContext().sendBroadcast(getPlayerShutdownIntent()); - getApplicationContext().startService( - getSwitchIntent(BackgroundPlayer.class) - .putExtra(BasePlayer.START_PAUSED, !this.player.isPlaying()) - ); - return true; + return switchTo(BackgroundPlayer.class); } return false; } diff --git a/app/src/main/java/org/schabi/newpipe/player/ServicePlayerActivity.java b/app/src/main/java/org/schabi/newpipe/player/ServicePlayerActivity.java index a7b3006a1..b5a697d05 100644 --- a/app/src/main/java/org/schabi/newpipe/player/ServicePlayerActivity.java +++ b/app/src/main/java/org/schabi/newpipe/player/ServicePlayerActivity.java @@ -165,13 +165,7 @@ public abstract class ServicePlayerActivity extends AppCompatActivity startActivity(new Intent(Settings.ACTION_SOUND_SETTINGS)); return true; case R.id.action_switch_main: - this.player.setRecovery(); - getApplicationContext().sendBroadcast(getPlayerShutdownIntent()); - getApplicationContext().startActivity( - getSwitchIntent(MainVideoPlayer.class) - .putExtra(BasePlayer.START_PAUSED, !this.player.isPlaying()) - ); - return true; + return switchTo(MainVideoPlayer.class); } return onPlayerOptionSelected(item) || super.onOptionsItemSelected(item); } @@ -194,7 +188,15 @@ public abstract class ServicePlayerActivity extends AppCompatActivity null, false, false - ).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + ).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + .putExtra(BasePlayer.START_PAUSED, !this.player.isPlaying()); + } + + protected boolean switchTo(final Class clazz) { + this.player.setRecovery(); + getApplicationContext().sendBroadcast(getPlayerShutdownIntent()); + getApplicationContext().startActivity(getSwitchIntent(clazz)); + return true; } //////////////////////////////////////////////////////////////////////////// From 7dbb2b206c438078676f668a9a779537730143fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Jakse?= Date: Thu, 16 Jan 2020 20:46:52 +0100 Subject: [PATCH 3/3] Simplify an if expression --- app/src/main/java/org/schabi/newpipe/player/BasePlayer.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 cd1ec07f9..46ca3921d 100644 --- a/app/src/main/java/org/schabi/newpipe/player/BasePlayer.java +++ b/app/src/main/java/org/schabi/newpipe/player/BasePlayer.java @@ -946,10 +946,10 @@ public abstract class BasePlayer implements public void onPlayPause() { if (DEBUG) Log.d(TAG, "onPlayPause() called"); - if (!isPlaying()) { - onPlay(); - } else { + if (isPlaying()) { onPause(); + } else { + onPlay(); } }