Added basic cast button to AudioPlayerFragment

This commit is contained in:
ByteHamster 2020-03-23 19:16:18 +01:00
parent 11536361c5
commit 982ce61b3f
3 changed files with 26 additions and 4 deletions

View File

@ -429,6 +429,7 @@ public class AudioPlayerFragment extends Fragment implements
public void setupOptionsMenu() {
if (toolbar.getMenu().size() == 0) {
//toolbar.inflateMenu(R.menu.cast_enabled);
toolbar.inflateMenu(R.menu.mediaplayer);
}
if (controller == null) {
@ -443,6 +444,18 @@ public class AudioPlayerFragment extends Fragment implements
toolbar.getMenu().findItem(R.id.set_sleeptimer_item).setVisible(!controller.sleepTimerActive());
toolbar.getMenu().findItem(R.id.disable_sleeptimer_item).setVisible(controller.sleepTimerActive());
/*MenuItem mediaRouteButton = toolbar.getMenu().findItem(R.id.media_route_menu_item);
SwitchableMediaRouteActionProvider mediaRouteActionProvider =
CastManager.getInstance().addMediaRouterButton(mediaRouteButton);
CastEnabledActivity.CastButtonVisibilityManager castButtonVisibilityManager =
new CastEnabledActivity.CastButtonVisibilityManager(CastManager.getInstance());
castButtonVisibilityManager.setMenu(toolbar.getMenu());
castButtonVisibilityManager.setPrefEnabled(UserPreferences.isCastEnabled());
castButtonVisibilityManager.mediaRouteActionProvider = mediaRouteActionProvider;
castButtonVisibilityManager.setResumed(true);
castButtonVisibilityManager.requestCastButton(MenuItem.SHOW_AS_ACTION_ALWAYS);
mediaRouteActionProvider.setEnabled(castButtonVisibilityManager.shouldEnable());*/
}
@Override

View File

@ -31,9 +31,8 @@ public abstract class CastEnabledActivity extends AppCompatActivity
private CastConsumer castConsumer;
private CastManager castManager;
private SwitchableMediaRouteActionProvider mediaRouteActionProvider;
private final CastButtonVisibilityManager castButtonVisibilityManager = new CastButtonVisibilityManager();
private CastButtonVisibilityManager castButtonVisibilityManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -59,6 +58,7 @@ public abstract class CastEnabledActivity extends AppCompatActivity
};
castManager = CastManager.getInstance();
castManager.addCastConsumer(castConsumer);
castButtonVisibilityManager = new CastButtonVisibilityManager(castManager);
castButtonVisibilityManager.setPrefEnabled(UserPreferences.isCastEnabled());
onCastConnectionChanged(castManager.isConnected());
}
@ -103,6 +103,7 @@ public abstract class CastEnabledActivity extends AppCompatActivity
}
mediaRouteActionProvider = castManager.addMediaRouterButton(mediaRouteButton);
if (mediaRouteActionProvider != null) {
castButtonVisibilityManager.mediaRouteActionProvider = mediaRouteActionProvider;
mediaRouteActionProvider.setEnabled(castButtonVisibilityManager.shouldEnable());
}
return true;
@ -162,13 +163,19 @@ public abstract class CastEnabledActivity extends AppCompatActivity
castButtonVisibilityManager.requestCastButton(showAsAction);
}
private class CastButtonVisibilityManager {
public static class CastButtonVisibilityManager {
private final CastManager castManager;
private volatile boolean prefEnabled = false;
private volatile boolean viewRequested = false;
private volatile boolean resumed = false;
private volatile boolean connected = false;
private volatile int showAsAction = MenuItem.SHOW_AS_ACTION_IF_ROOM;
private Menu menu;
public SwitchableMediaRouteActionProvider mediaRouteActionProvider;
public CastButtonVisibilityManager(CastManager castManager) {
this.castManager = castManager;
}
public synchronized void setPrefEnabled(boolean newValue) {
if (prefEnabled != newValue && resumed && (viewRequested || connected)) {

View File

@ -1218,7 +1218,6 @@ public class PlaybackService extends MediaBrowserServiceCompat {
notificationBuilder.setCasting(isCasting);
notificationBuilder.updatePosition(getCurrentPosition(), getCurrentPlaybackSpeed());
Log.d(TAG, "setupNotification: startForeground" + playerStatus);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
startForegroundIfPlaying(playerStatus);
@ -1236,10 +1235,12 @@ public class PlaybackService extends MediaBrowserServiceCompat {
}
private void startForegroundIfPlaying(@NonNull PlayerStatus status) {
Log.d(TAG, "startForegroundIfPlaying: " + status);
if (stateManager.hasReceivedValidStartCommand()) {
if (isCasting || status == PlayerStatus.PLAYING || status == PlayerStatus.PREPARING
|| status == PlayerStatus.SEEKING) {
stateManager.startForeground(NOTIFICATION_ID, notificationBuilder.build());
Log.d(TAG, "foreground");
} else {
stateManager.stopForeground(false);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
@ -1781,6 +1782,7 @@ public class PlaybackService extends MediaBrowserServiceCompat {
@Override
public void setIsCasting(boolean isCasting) {
PlaybackService.isCasting = isCasting;
stateManager.validStartCommandWasReceived();
}
@Override