Use EventBus instead of broadcast for service shutdown

This commit is contained in:
ByteHamster 2021-05-14 20:26:57 +02:00
parent fb6bd0cbaa
commit 390fb1a15b
6 changed files with 27 additions and 40 deletions

View File

@ -17,6 +17,7 @@ import android.widget.TextView;
import com.bumptech.glide.Glide;
import de.danoeh.antennapod.core.event.ServiceEvent;
import de.danoeh.antennapod.view.PlayButton;
import org.apache.commons.lang3.StringUtils;
import org.greenrobot.eventbus.EventBus;
@ -138,11 +139,6 @@ public abstract class MediaplayerActivity extends CastEnabledActivity implements
MediaplayerActivity.this.onAwaitingVideoSurface();
}
@Override
public void onShutdownNotification() {
finish();
}
@Override
public void onPlaybackEnd() {
finish();
@ -171,6 +167,13 @@ public abstract class MediaplayerActivity extends CastEnabledActivity implements
onPositionObserverUpdate();
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void onPlaybackServiceChanged(ServiceEvent event) {
if (event.action == ServiceEvent.Action.SERVICE_SHUT_DOWN) {
finish();
}
}
private void onSetSpeedAbilityChanged() {
Log.d(TAG, "onSetSpeedAbilityChanged()");
updatePlaybackSpeedButton();

View File

@ -29,6 +29,7 @@ import de.danoeh.antennapod.activity.CastEnabledActivity;
import de.danoeh.antennapod.activity.MainActivity;
import de.danoeh.antennapod.core.event.FavoritesEvent;
import de.danoeh.antennapod.core.event.PlaybackPositionEvent;
import de.danoeh.antennapod.core.event.ServiceEvent;
import de.danoeh.antennapod.model.feed.Chapter;
import de.danoeh.antennapod.core.event.UnreadItemsUpdateEvent;
import de.danoeh.antennapod.model.feed.FeedItem;
@ -251,6 +252,13 @@ public class AudioPlayerFragment extends Fragment implements
controller.getDuration()));
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void onPlaybackServiceChanged(ServiceEvent event) {
if (event.action == ServiceEvent.Action.SERVICE_SHUT_DOWN) {
((MainActivity) getActivity()).getBottomSheet().setState(BottomSheetBehavior.STATE_COLLAPSED);
}
}
private void setupLengthTextView() {
showTimeLeft = UserPreferences.shouldShowRemainingTime();
txtvLength.setOnClickListener(v -> {
@ -391,11 +399,6 @@ public class AudioPlayerFragment extends Fragment implements
AudioPlayerFragment.this.loadMediaInfo();
}
@Override
public void onShutdownNotification() {
((MainActivity) getActivity()).getBottomSheet().setState(BottomSheetBehavior.STATE_COLLAPSED);
}
@Override
public void onPlaybackEnd() {
((MainActivity) getActivity()).getBottomSheet().setState(BottomSheetBehavior.STATE_COLLAPSED);

View File

@ -16,6 +16,7 @@ import com.google.android.material.bottomsheet.BottomSheetBehavior;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.activity.MainActivity;
import de.danoeh.antennapod.core.event.PlaybackPositionEvent;
import de.danoeh.antennapod.core.event.ServiceEvent;
import de.danoeh.antennapod.model.playback.MediaType;
import de.danoeh.antennapod.core.feed.util.ImageResourceUtils;
import de.danoeh.antennapod.core.glide.ApGlideSettings;
@ -112,11 +113,6 @@ public class ExternalPlayerFragment extends Fragment {
ExternalPlayerFragment.this.loadMediaInfo();
}
@Override
public void onShutdownNotification() {
((MainActivity) getActivity()).setPlayerVisible(false);
}
@Override
public void onPlaybackEnd() {
((MainActivity) getActivity()).setPlayerVisible(false);
@ -148,6 +144,13 @@ public class ExternalPlayerFragment extends Fragment {
onPositionObserverUpdate();
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void onPlaybackServiceChanged(ServiceEvent event) {
if (event.action == ServiceEvent.Action.SERVICE_SHUT_DOWN) {
((MainActivity) getActivity()).setPlayerVisible(false);
}
}
@Override
public void onDestroy() {
super.onDestroy();

View File

@ -2,7 +2,8 @@ package de.danoeh.antennapod.core.event;
public class ServiceEvent {
public enum Action {
SERVICE_STARTED
SERVICE_STARTED,
SERVICE_SHUT_DOWN
}
public final Action action;

View File

@ -1604,6 +1604,7 @@ public class PlaybackService extends MediaBrowserServiceCompat {
@Override
public void onReceive(Context context, Intent intent) {
if (TextUtils.equals(intent.getAction(), ACTION_SHUTDOWN_PLAYBACK_SERVICE)) {
EventBus.getDefault().post(new ServiceEvent(ServiceEvent.Action.SERVICE_SHUT_DOWN));
stateManager.stopService();
}
}

View File

@ -10,7 +10,6 @@ import android.content.ServiceConnection;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.IBinder;
import android.text.TextUtils;
import android.util.Log;
import android.util.Pair;
import android.view.SurfaceHolder;
@ -91,9 +90,6 @@ public abstract class PlaybackController {
activity.registerReceiver(notificationReceiver, new IntentFilter(
PlaybackService.ACTION_PLAYER_NOTIFICATION));
activity.registerReceiver(shutdownReceiver, new IntentFilter(
PlaybackService.ACTION_SHUTDOWN_PLAYBACK_SERVICE));
if (!released) {
bindToService();
} else {
@ -121,12 +117,6 @@ public abstract class PlaybackController {
// ignore
}
unbind();
try {
activity.unregisterReceiver(shutdownReceiver);
} catch (IllegalArgumentException e) {
// ignore
}
media = null;
released = true;
@ -260,18 +250,6 @@ public abstract class PlaybackController {
};
private final BroadcastReceiver shutdownReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (playbackService != null) {
if (TextUtils.equals(intent.getAction(), PlaybackService.ACTION_SHUTDOWN_PLAYBACK_SERVICE)) {
unbind();
onShutdownNotification();
}
}
}
};
public void onPositionObserverUpdate() {}
@ -279,8 +257,6 @@ public abstract class PlaybackController {
public void onSetSpeedAbilityChanged() {}
public void onShutdownNotification() {}
/**
* Called when the currently displayed information should be refreshed.
*/