Open mini player if player running on app open

This commit is contained in:
Stypox 2020-11-01 21:45:39 +01:00
parent 00a178f7d3
commit 5a87cfc25d
No known key found for this signature in database
GPG Key ID: 4BDF1B40A49FDD23
2 changed files with 37 additions and 27 deletions

View File

@ -69,6 +69,7 @@ import org.schabi.newpipe.fragments.detail.VideoDetailFragment;
import org.schabi.newpipe.fragments.list.search.SearchFragment; import org.schabi.newpipe.fragments.list.search.SearchFragment;
import org.schabi.newpipe.player.VideoPlayer; import org.schabi.newpipe.player.VideoPlayer;
import org.schabi.newpipe.player.event.OnKeyDownListener; import org.schabi.newpipe.player.event.OnKeyDownListener;
import org.schabi.newpipe.player.helper.PlayerHolder;
import org.schabi.newpipe.player.playqueue.PlayQueue; import org.schabi.newpipe.player.playqueue.PlayQueue;
import org.schabi.newpipe.report.ErrorActivity; import org.schabi.newpipe.report.ErrorActivity;
import org.schabi.newpipe.util.Constants; import org.schabi.newpipe.util.Constants;
@ -152,7 +153,7 @@ public class MainActivity extends AppCompatActivity {
if (DeviceUtils.isTv(this)) { if (DeviceUtils.isTv(this)) {
FocusOverlayView.setupFocusObserver(this); FocusOverlayView.setupFocusObserver(this);
} }
setupBroadcastReceiver(); openMiniPlayerUponPlayerStarted();
} }
private void setupDrawer() throws Exception { private void setupDrawer() throws Exception {
@ -809,34 +810,39 @@ public class MainActivity extends AppCompatActivity {
} }
} }
private void setupBroadcastReceiver() { private void openMiniPlayerIfMissing() {
broadcastReceiver = new BroadcastReceiver() { final Fragment fragmentPlayer = getSupportFragmentManager()
@Override .findFragmentById(R.id.fragment_player_holder);
public void onReceive(final Context context, final Intent intent) { if (fragmentPlayer == null) {
if (intent.getAction().equals(VideoDetailFragment.ACTION_PLAYER_STARTED)) { // We still don't have a fragment attached to the activity. It can happen when a user
final Fragment fragmentPlayer = getSupportFragmentManager() // started popup or background players without opening a stream inside the fragment.
.findFragmentById(R.id.fragment_player_holder); // Adding it in a collapsed state (only mini player will be visible).
if (fragmentPlayer == null) { NavigationHelper.showMiniPlayer(getSupportFragmentManager());
/* }
* We still don't have a fragment attached to the activity. }
* It can happen when a user started popup or background players
* without opening a stream inside the fragment. private void openMiniPlayerUponPlayerStarted() {
* Adding it in a collapsed state (only mini player will be visible) if (PlayerHolder.isPlayerOpen()) {
* */ // no need for a broadcast receiver if the player is already open
NavigationHelper.showMiniPlayer(getSupportFragmentManager()); openMiniPlayerIfMissing();
} else {
// listen for player intents being sent around
broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(final Context context, final Intent intent) {
if (intent.getAction().equals(VideoDetailFragment.ACTION_PLAYER_STARTED)) {
openMiniPlayerIfMissing();
// At this point the player is added 100%, we can unregister. Other actions
// are useless since the fragment will not be removed after that.
unregisterReceiver(broadcastReceiver);
broadcastReceiver = null;
} }
/*
* At this point the player is added 100%, we can unregister.
* Other actions are useless since the fragment will not be removed after that
* */
unregisterReceiver(broadcastReceiver);
broadcastReceiver = null;
} }
} };
}; final IntentFilter intentFilter = new IntentFilter();
final IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(VideoDetailFragment.ACTION_PLAYER_STARTED);
intentFilter.addAction(VideoDetailFragment.ACTION_PLAYER_STARTED); registerReceiver(broadcastReceiver, intentFilter);
registerReceiver(broadcastReceiver, intentFilter); }
} }
private boolean bottomSheetHiddenOrCollapsed() { private boolean bottomSheetHiddenOrCollapsed() {

View File

@ -56,6 +56,10 @@ public final class PlayerHolder {
return player.isPlaying(); return player.isPlaying();
} }
public static boolean isPlayerOpen() {
return player != null;
}
public static void setListener(final PlayerServiceExtendedEventListener newListener) { public static void setListener(final PlayerServiceExtendedEventListener newListener) {
listener = newListener; listener = newListener;
// Force reload data from service // Force reload data from service