Skip to content

Commit 2a2c82e

Browse files
committed
More fixes with opening VideoDetailFragment
1 parent bb882ad commit 2a2c82e

13 files changed

+87
-170
lines changed

app/src/main/java/org/schabi/newpipe/RouterActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,7 @@ public Consumer<Info> getResultHandler(final Choice choice) {
685685
}
686686

687687
if (choice.playerChoice.equals(videoPlayerKey)) {
688-
NavigationHelper.playOnMainPlayer(this, playQueue);
688+
NavigationHelper.playOnMainPlayer(this, playQueue, false);
689689
} else if (choice.playerChoice.equals(backgroundPlayerKey)) {
690690
NavigationHelper.playOnBackgroundPlayer(this, playQueue, true);
691691
} else if (choice.playerChoice.equals(popupPlayerKey)) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,7 +1097,7 @@ private void openNormalBackgroundPlayer(final boolean append) {
10971097

10981098
private void openMainPlayer() {
10991099
if (playerService == null) {
1100-
PlayerHolder.startService(App.getApp(), true, this);
1100+
PlayerHolder.startService(App.getApp(), autoPlayEnabled, this);
11011101
return;
11021102
}
11031103
if (currentInfo == null) {
@@ -1112,7 +1112,7 @@ private void openMainPlayer() {
11121112
addVideoPlayerView();
11131113

11141114
final Intent playerIntent = NavigationHelper
1115-
.getPlayerIntent(requireContext(), MainPlayer.class, queue, null, true);
1115+
.getPlayerIntent(requireContext(), MainPlayer.class, queue, true, autoPlayEnabled);
11161116
activity.startService(playerIntent);
11171117
}
11181118

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

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,8 @@
22

33
import android.content.Intent;
44
import android.view.Menu;
5-
import android.view.MenuItem;
65

76
import org.schabi.newpipe.R;
8-
import org.schabi.newpipe.util.NavigationHelper;
9-
import org.schabi.newpipe.util.PermissionHelper;
107

118
public final class BackgroundPlayerActivity extends ServicePlayerActivity {
129

@@ -46,31 +43,6 @@ public int getPlayerOptionMenuResource() {
4643
return R.menu.menu_play_queue_bg;
4744
}
4845

49-
@Override
50-
public boolean onPlayerOptionSelected(final MenuItem item) {
51-
if (item.getItemId() == R.id.action_switch_popup) {
52-
53-
if (!PermissionHelper.isPopupEnabled(this)) {
54-
PermissionHelper.showPopupEnablementToast(this);
55-
return true;
56-
}
57-
58-
this.player.setRecovery();
59-
NavigationHelper.playOnPopupPlayer(
60-
getApplicationContext(), player.playQueue, this.player.isPlaying());
61-
return true;
62-
}
63-
64-
if (item.getItemId() == R.id.action_switch_background) {
65-
this.player.setRecovery();
66-
NavigationHelper.playOnBackgroundPlayer(
67-
getApplicationContext(), player.playQueue, this.player.isPlaying());
68-
return true;
69-
}
70-
71-
return false;
72-
}
73-
7446
@Override
7547
public void setupMenu(final Menu menu) {
7648
if (player == null) {

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

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public abstract class BasePlayer implements
125125
@NonNull
126126
public static final String RESUME_PLAYBACK = "resume_playback";
127127
@NonNull
128-
public static final String START_PAUSED = "start_paused";
128+
public static final String PLAY_WHEN_READY = "play_when_ready";
129129
@NonNull
130130
public static final String SELECT_ON_APPEND = "select_on_append";
131131
@NonNull
@@ -224,7 +224,7 @@ public void onReceive(final Context ctx, final Intent intent) {
224224
this.dataSource = new PlayerDataSource(context, userAgent, bandwidthMeter);
225225

226226
final TrackSelection.Factory trackSelectionFactory = PlayerHelper
227-
.getQualitySelector(context);
227+
.getQualitySelector();
228228
this.trackSelector = new CustomTrackSelector(context, trackSelectionFactory);
229229

230230
this.loadControl = new LoadController();
@@ -302,6 +302,7 @@ public void handleIntent(final Intent intent) {
302302
final boolean samePlayQueue = playQueue != null && playQueue.equals(queue);
303303

304304
final int repeatMode = intent.getIntExtra(REPEAT_MODE, getRepeatMode());
305+
final boolean playWhenReady = intent.getBooleanExtra(PLAY_WHEN_READY, true);
305306
final boolean isMuted = intent
306307
.getBooleanExtra(IS_MUTED, simpleExoPlayer != null && isMuted());
307308

@@ -327,16 +328,20 @@ public void handleIntent(final Intent intent) {
327328
simpleExoPlayer.retry();
328329
}
329330
simpleExoPlayer.seekTo(playQueue.getIndex(), queue.getItem().getRecoveryPosition());
330-
return;
331+
simpleExoPlayer.setPlayWhenReady(playWhenReady);
331332

332-
} else if (samePlayQueue && !playQueue.isDisposed() && simpleExoPlayer != null) {
333+
} else if (simpleExoPlayer != null
334+
&& samePlayQueue
335+
&& playQueue != null
336+
&& !playQueue.isDisposed()) {
333337
// Do not re-init the same PlayQueue. Save time
334338
// Player can have state = IDLE when playback is stopped or failed
335339
// and we should retry() in this case
336340
if (simpleExoPlayer.getPlaybackState() == Player.STATE_IDLE) {
337341
simpleExoPlayer.retry();
338342
}
339-
return;
343+
simpleExoPlayer.setPlayWhenReady(playWhenReady);
344+
340345
} else if (intent.getBooleanExtra(RESUME_PLAYBACK, false)
341346
&& isPlaybackResumeEnabled()
342347
&& !samePlayQueue) {
@@ -351,32 +356,30 @@ && isPlaybackResumeEnabled()
351356
state -> {
352357
queue.setRecovery(queue.getIndex(), state.getProgressTime());
353358
initPlayback(queue, repeatMode, playbackSpeed, playbackPitch,
354-
playbackSkipSilence, true, isMuted);
359+
playbackSkipSilence, playWhenReady, isMuted);
355360
},
356361
error -> {
357362
if (DEBUG) {
358363
error.printStackTrace();
359364
}
360365
// In case any error we can start playback without history
361366
initPlayback(queue, repeatMode, playbackSpeed, playbackPitch,
362-
playbackSkipSilence, true, isMuted);
367+
playbackSkipSilence, playWhenReady, isMuted);
363368
},
364369
() -> {
365370
// Completed but not found in history
366371
initPlayback(queue, repeatMode, playbackSpeed, playbackPitch,
367-
playbackSkipSilence, true, isMuted);
372+
playbackSkipSilence, playWhenReady, isMuted);
368373
}
369374
);
370375
databaseUpdateReactor.add(stateLoader);
371-
return;
372376
}
377+
} else {
378+
// Good to go...
379+
// In a case of equal PlayQueues we can re-init old one but only when it is disposed
380+
initPlayback(samePlayQueue ? playQueue : queue, repeatMode, playbackSpeed,
381+
playbackPitch, playbackSkipSilence, playWhenReady, isMuted);
373382
}
374-
// Good to go...
375-
// In a case of equal PlayQueues we can re-init old one but only when it is disposed
376-
initPlayback(samePlayQueue ? playQueue : queue, repeatMode,
377-
playbackSpeed, playbackPitch, playbackSkipSilence,
378-
!intent.getBooleanExtra(START_PAUSED, false),
379-
isMuted);
380383
}
381384

382385
private PlaybackParameters retrievePlaybackParametersFromPreferences() {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ public View getView() {
240240
}
241241

242242
public void removeViewFromParent() {
243-
if (getView().getParent() != null) {
243+
if (getView() != null && getView().getParent() != null) {
244244
if (playerImpl.getParentActivity() != null) {
245245
// This means view was added to fragment
246246
final ViewGroup parent = (ViewGroup) getView().getParent();

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

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@
2727
import com.google.android.exoplayer2.PlaybackParameters;
2828
import com.google.android.exoplayer2.Player;
2929

30-
import org.schabi.newpipe.MainActivity;
3130
import org.schabi.newpipe.R;
32-
import org.schabi.newpipe.extractor.StreamingService;
3331
import org.schabi.newpipe.extractor.stream.StreamInfo;
3432
import org.schabi.newpipe.fragments.OnScrollBelowItemsListener;
3533
import org.schabi.newpipe.local.dialog.PlaylistAppendDialog;
@@ -42,9 +40,9 @@
4240
import org.schabi.newpipe.player.playqueue.PlayQueueItemBuilder;
4341
import org.schabi.newpipe.player.playqueue.PlayQueueItemHolder;
4442
import org.schabi.newpipe.player.playqueue.PlayQueueItemTouchCallback;
45-
import org.schabi.newpipe.util.Constants;
4643
import org.schabi.newpipe.util.Localization;
4744
import org.schabi.newpipe.util.NavigationHelper;
45+
import org.schabi.newpipe.util.PermissionHelper;
4846
import org.schabi.newpipe.util.ThemeHelper;
4947

5048
import java.util.Collections;
@@ -113,9 +111,8 @@ public abstract class ServicePlayerActivity extends AppCompatActivity
113111

114112
public abstract int getPlayerOptionMenuResource();
115113

116-
public abstract boolean onPlayerOptionSelected(MenuItem item);
117-
118114
public abstract void setupMenu(Menu m);
115+
119116
////////////////////////////////////////////////////////////////////////////
120117
// Activity Lifecycle
121118
////////////////////////////////////////////////////////////////////////////
@@ -187,12 +184,22 @@ public boolean onOptionsItemSelected(final MenuItem item) {
187184
return true;
188185
case R.id.action_switch_main:
189186
this.player.setRecovery();
190-
getApplicationContext().startActivity(
191-
getSwitchIntent(MainActivity.class, MainPlayer.PlayerType.VIDEO)
192-
.putExtra(BasePlayer.START_PAUSED, !this.player.isPlaying()));
187+
NavigationHelper.playOnMainPlayer(this, player.getPlayQueue(), true);
188+
return true;
189+
case R.id.action_switch_popup:
190+
if (PermissionHelper.isPopupEnabled(this)) {
191+
this.player.setRecovery();
192+
NavigationHelper.playOnPopupPlayer(this, player.playQueue, true);
193+
} else {
194+
PermissionHelper.showPopupEnablementToast(this);
195+
}
196+
return true;
197+
case R.id.action_switch_background:
198+
this.player.setRecovery();
199+
NavigationHelper.playOnBackgroundPlayer(this, player.playQueue, true);
193200
return true;
194201
}
195-
return onPlayerOptionSelected(item) || super.onOptionsItemSelected(item);
202+
return super.onOptionsItemSelected(item);
196203
}
197204

198205
@Override
@@ -201,24 +208,6 @@ protected void onDestroy() {
201208
unbind();
202209
}
203210

204-
protected Intent getSwitchIntent(final Class clazz, final MainPlayer.PlayerType playerType) {
205-
return NavigationHelper.getPlayerIntent(getApplicationContext(), clazz,
206-
this.player.getPlayQueue(), this.player.getRepeatMode(),
207-
this.player.getPlaybackSpeed(), this.player.getPlaybackPitch(),
208-
this.player.getPlaybackSkipSilence(),
209-
null,
210-
true,
211-
!this.player.isPlaying(),
212-
this.player.isMuted())
213-
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
214-
.putExtra(Constants.KEY_LINK_TYPE, StreamingService.LinkType.STREAM)
215-
.putExtra(Constants.KEY_URL, this.player.getVideoUrl())
216-
.putExtra(Constants.KEY_TITLE, this.player.getVideoTitle())
217-
.putExtra(Constants.KEY_SERVICE_ID,
218-
this.player.getCurrentMetadata().getMetadata().getServiceId())
219-
.putExtra(VideoPlayer.PLAYER_TYPE, playerType);
220-
}
221-
222211
////////////////////////////////////////////////////////////////////////////
223212
// Service Connection
224213
////////////////////////////////////////////////////////////////////////////
@@ -368,8 +357,8 @@ private void buildItemPopupMenu(final PlayQueueItem item, final View view) {
368357
Menu.NONE, R.string.play_queue_stream_detail);
369358
detail.setOnMenuItemClickListener(menuItem -> {
370359
// playQueue is null since we don't want any queue change
371-
NavigationHelper.openVideoDetail(
372-
this, item.getServiceId(), item.getUrl(), item.getTitle(), null);
360+
NavigationHelper.openVideoDetail(this, item.getServiceId(), item.getUrl(),
361+
item.getTitle(), null, false);
373362
return true;
374363
});
375364

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

Lines changed: 9 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,7 @@
7676
import com.google.android.material.floatingactionbutton.FloatingActionButton;
7777
import com.nostra13.universalimageloader.core.assist.FailReason;
7878

79-
import org.schabi.newpipe.MainActivity;
8079
import org.schabi.newpipe.R;
81-
import org.schabi.newpipe.extractor.StreamingService;
8280
import org.schabi.newpipe.extractor.stream.StreamInfo;
8381
import org.schabi.newpipe.extractor.stream.VideoStream;
8482
import org.schabi.newpipe.fragments.OnScrollBelowItemsListener;
@@ -97,7 +95,6 @@
9795
import org.schabi.newpipe.player.resolver.MediaSourceTag;
9896
import org.schabi.newpipe.player.resolver.VideoPlaybackResolver;
9997
import org.schabi.newpipe.util.AnimationUtils;
100-
import org.schabi.newpipe.util.Constants;
10198
import org.schabi.newpipe.util.DeviceUtils;
10299
import org.schabi.newpipe.util.KoreUtil;
103100
import org.schabi.newpipe.util.ListHelper;
@@ -260,7 +257,12 @@ public void handleIntent(final Intent intent) {
260257
onQueueClosed();
261258
// Android TV: without it focus will frame the whole player
262259
playPauseButton.requestFocus();
263-
onPlay();
260+
261+
if (simpleExoPlayer.getPlayWhenReady()) {
262+
onPlay();
263+
} else {
264+
onPause();
265+
}
264266
}
265267
NavigationHelper.sendPlayerStartedEvent(service);
266268
}
@@ -756,40 +758,6 @@ public void toggleFullscreen() {
756758
setupScreenRotationButton();
757759
}
758760

759-
public void switchFromPopupToMain() {
760-
if (DEBUG) {
761-
Log.d(TAG, "switchFromPopupToMain() called");
762-
}
763-
if (!popupPlayerSelected() || simpleExoPlayer == null || getCurrentMetadata() == null) {
764-
return;
765-
}
766-
767-
setRecovery();
768-
service.removeViewFromParent();
769-
final Intent intent = NavigationHelper.getPlayerIntent(
770-
service,
771-
MainActivity.class,
772-
this.getPlayQueue(),
773-
this.getRepeatMode(),
774-
this.getPlaybackSpeed(),
775-
this.getPlaybackPitch(),
776-
this.getPlaybackSkipSilence(),
777-
null,
778-
true,
779-
!isPlaying(),
780-
isMuted()
781-
);
782-
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
783-
intent.putExtra(Constants.KEY_SERVICE_ID,
784-
getCurrentMetadata().getMetadata().getServiceId());
785-
intent.putExtra(Constants.KEY_LINK_TYPE, StreamingService.LinkType.STREAM);
786-
intent.putExtra(Constants.KEY_URL, getVideoUrl());
787-
intent.putExtra(Constants.KEY_TITLE, getVideoTitle());
788-
intent.putExtra(VideoDetailFragment.KEY_SWITCHING_PLAYERS, true);
789-
service.onDestroy();
790-
context.startActivity(intent);
791-
}
792-
793761
@Override
794762
public void onClick(final View v) {
795763
super.onClick(v);
@@ -817,7 +785,9 @@ public void onClick(final View v) {
817785
} else if (v.getId() == openInBrowser.getId()) {
818786
onOpenInBrowserClicked();
819787
} else if (v.getId() == fullscreenButton.getId()) {
820-
switchFromPopupToMain();
788+
setRecovery();
789+
NavigationHelper.playOnMainPlayer(context, getPlayQueue(), true);
790+
return;
821791
} else if (v.getId() == screenRotationButton.getId()) {
822792
// Only if it's not a vertical video or vertical video but in landscape with locked
823793
// orientation a screen orientation can be changed automatically

app/src/main/java/org/schabi/newpipe/player/event/BasePlayerGestureListener.kt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ abstract class BasePlayerGestureListener(
6363
private var isMovingInPopup = false
6464
private var isResizing = false
6565

66-
private val tossFlingVelocity = PlayerHelper.getTossFlingVelocity(service)
66+
private val tossFlingVelocity = PlayerHelper.getTossFlingVelocity()
6767

6868
// [popup] initial coordinates and distance between fingers
6969
private var initPointerDistance = -1.0
@@ -104,9 +104,6 @@ abstract class BasePlayerGestureListener(
104104
}
105105

106106
private fun onTouchInPopup(v: View, event: MotionEvent): Boolean {
107-
if (playerImpl == null) {
108-
return false
109-
}
110107
playerImpl.gestureDetector.onTouchEvent(event)
111108
if (event.pointerCount == 2 && !isMovingInPopup && !isResizing) {
112109
if (DEBUG) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public void onAnimationEnd(final Animator animation) {
164164

165165
@Override
166166
public void onAudioSessionId(final EventTime eventTime, final int audioSessionId) {
167-
if (!PlayerHelper.isUsingDSP(context)) {
167+
if (!PlayerHelper.isUsingDSP()) {
168168
return;
169169
}
170170

0 commit comments

Comments
 (0)