diff --git a/app/src/androidTest/java/de/test/antennapod/playback/PlaybackTest.java b/app/src/androidTest/java/de/test/antennapod/playback/PlaybackTest.java index 78cf59907..65e28c572 100644 --- a/app/src/androidTest/java/de/test/antennapod/playback/PlaybackTest.java +++ b/app/src/androidTest/java/de/test/antennapod/playback/PlaybackTest.java @@ -3,6 +3,7 @@ package de.test.antennapod.playback; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; +import android.view.KeyEvent; import androidx.preference.PreferenceManager; import android.view.View; @@ -10,6 +11,7 @@ import androidx.test.filters.LargeTest; import androidx.test.platform.app.InstrumentationRegistry; import androidx.test.rule.ActivityTestRule; +import de.danoeh.antennapod.core.receiver.MediaButtonReceiver; import de.danoeh.antennapod.model.feed.FeedItemFilter; import de.danoeh.antennapod.playback.base.PlayerStatus; import org.awaitility.Awaitility; @@ -32,10 +34,8 @@ import de.danoeh.antennapod.model.feed.FeedItem; import de.danoeh.antennapod.model.feed.FeedMedia; import de.danoeh.antennapod.core.preferences.PlaybackPreferences; import de.danoeh.antennapod.core.preferences.UserPreferences; -import de.danoeh.antennapod.core.service.playback.PlaybackService; import de.danoeh.antennapod.core.storage.DBReader; import de.danoeh.antennapod.core.storage.DBWriter; -import de.danoeh.antennapod.core.util.IntentUtils; import de.danoeh.antennapod.core.util.LongList; import de.danoeh.antennapod.core.util.playback.PlaybackController; import de.test.antennapod.EspressoTestUtils; @@ -241,11 +241,11 @@ public class PlaybackTest { } private void skipEpisode() { - IntentUtils.sendLocalBroadcast(context, PlaybackService.ACTION_SKIP_CURRENT_EPISODE); + context.sendBroadcast(MediaButtonReceiver.createIntent(context, KeyEvent.KEYCODE_MEDIA_NEXT)); } protected void pauseEpisode() { - IntentUtils.sendLocalBroadcast(context, PlaybackService.ACTION_PAUSE_PLAY_CURRENT_EPISODE); + context.sendBroadcast(MediaButtonReceiver.createIntent(context, KeyEvent.KEYCODE_MEDIA_PAUSE)); } protected void startLocalPlayback() { diff --git a/app/src/main/java/de/danoeh/antennapod/activity/MainActivity.java b/app/src/main/java/de/danoeh/antennapod/activity/MainActivity.java index 450a99873..2b2d3de15 100644 --- a/app/src/main/java/de/danoeh/antennapod/activity/MainActivity.java +++ b/app/src/main/java/de/danoeh/antennapod/activity/MainActivity.java @@ -1,6 +1,5 @@ package de.danoeh.antennapod.activity; -import android.annotation.TargetApi; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; @@ -23,7 +22,6 @@ import androidx.core.view.ViewCompat; import androidx.core.view.WindowCompat; import androidx.core.view.WindowInsetsCompat; import com.google.android.material.appbar.MaterialToolbar; -import androidx.core.content.ContextCompat; import androidx.drawerlayout.widget.DrawerLayout; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentContainerView; @@ -44,7 +42,6 @@ import org.greenrobot.eventbus.ThreadMode; import de.danoeh.antennapod.R; import de.danoeh.antennapod.core.preferences.UserPreferences; import de.danoeh.antennapod.core.receiver.MediaButtonReceiver; -import de.danoeh.antennapod.core.service.playback.PlaybackService; import de.danoeh.antennapod.core.util.download.AutoUpdateManager; import de.danoeh.antennapod.dialog.RatingDialog; import de.danoeh.antennapod.event.MessageEvent; @@ -448,7 +445,6 @@ public class MainActivity extends CastEnabledActivity { EventBus.getDefault().unregister(this); } - @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) @Override public void onTrimMemory(int level) { super.onTrimMemory(level); @@ -662,9 +658,7 @@ public class MainActivity extends CastEnabledActivity { } if (customKeyCode != null) { - Intent intent = new Intent(this, PlaybackService.class); - intent.putExtra(MediaButtonReceiver.EXTRA_KEYCODE, customKeyCode); - ContextCompat.startForegroundService(this, intent); + sendBroadcast(MediaButtonReceiver.createIntent(this, customKeyCode)); return true; } return super.onKeyUp(keyCode, event); diff --git a/app/src/main/java/de/danoeh/antennapod/adapter/actionbutton/PauseActionButton.java b/app/src/main/java/de/danoeh/antennapod/adapter/actionbutton/PauseActionButton.java index de4dae6a0..5e9a2e9f3 100644 --- a/app/src/main/java/de/danoeh/antennapod/adapter/actionbutton/PauseActionButton.java +++ b/app/src/main/java/de/danoeh/antennapod/adapter/actionbutton/PauseActionButton.java @@ -1,15 +1,14 @@ package de.danoeh.antennapod.adapter.actionbutton; import android.content.Context; +import android.view.KeyEvent; import androidx.annotation.DrawableRes; import androidx.annotation.StringRes; import de.danoeh.antennapod.R; +import de.danoeh.antennapod.core.receiver.MediaButtonReceiver; import de.danoeh.antennapod.model.feed.FeedItem; import de.danoeh.antennapod.model.feed.FeedMedia; import de.danoeh.antennapod.core.util.FeedItemUtil; -import de.danoeh.antennapod.core.util.IntentUtils; - -import static de.danoeh.antennapod.core.service.playback.PlaybackService.ACTION_PAUSE_PLAY_CURRENT_EPISODE; public class PauseActionButton extends ItemActionButton { @@ -37,7 +36,7 @@ public class PauseActionButton extends ItemActionButton { } if (FeedItemUtil.isCurrentlyPlaying(media)) { - IntentUtils.sendLocalBroadcast(context, ACTION_PAUSE_PLAY_CURRENT_EPISODE); + context.sendBroadcast(MediaButtonReceiver.createIntent(context, KeyEvent.KEYCODE_MEDIA_PAUSE)); } } } diff --git a/app/src/main/java/de/danoeh/antennapod/fragment/AudioPlayerFragment.java b/app/src/main/java/de/danoeh/antennapod/fragment/AudioPlayerFragment.java index 5a00e0e96..3a92a8fda 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/AudioPlayerFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/AudioPlayerFragment.java @@ -3,6 +3,7 @@ package de.danoeh.antennapod.fragment; import android.content.Intent; import android.os.Bundle; import android.util.Log; +import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.MenuItem; import android.view.View; @@ -26,6 +27,7 @@ import com.google.android.material.bottomsheet.BottomSheetBehavior; import com.google.android.material.elevation.SurfaceColors; import com.google.android.material.snackbar.Snackbar; +import de.danoeh.antennapod.core.receiver.MediaButtonReceiver; import de.danoeh.antennapod.core.service.playback.PlaybackService; import de.danoeh.antennapod.core.util.playback.PlaybackController; import de.danoeh.antennapod.event.playback.BufferUpdateEvent; @@ -54,7 +56,6 @@ import de.danoeh.antennapod.core.feed.util.PlaybackSpeedUtils; import de.danoeh.antennapod.core.preferences.UserPreferences; import de.danoeh.antennapod.core.util.ChapterUtils; import de.danoeh.antennapod.core.util.Converter; -import de.danoeh.antennapod.core.util.IntentUtils; import de.danoeh.antennapod.core.util.TimeSpeedConverter; import de.danoeh.antennapod.model.playback.Playable; import de.danoeh.antennapod.dialog.PlaybackControlsDialog; @@ -215,8 +216,8 @@ public class AudioPlayerFragment extends Fragment implements SkipPreferenceDialog.SkipDirection.SKIP_FORWARD, txtvFF); return false; }); - butSkip.setOnClickListener(v -> - IntentUtils.sendLocalBroadcast(getActivity(), PlaybackService.ACTION_SKIP_CURRENT_EPISODE)); + butSkip.setOnClickListener(v -> getActivity().sendBroadcast( + MediaButtonReceiver.createIntent(getContext(), KeyEvent.KEYCODE_MEDIA_NEXT))); } @Subscribe(threadMode = ThreadMode.MAIN) diff --git a/app/src/main/java/de/danoeh/antennapod/menuhandler/FeedItemMenuHandler.java b/app/src/main/java/de/danoeh/antennapod/menuhandler/FeedItemMenuHandler.java index 5bbda2980..c52db0500 100644 --- a/app/src/main/java/de/danoeh/antennapod/menuhandler/FeedItemMenuHandler.java +++ b/app/src/main/java/de/danoeh/antennapod/menuhandler/FeedItemMenuHandler.java @@ -3,6 +3,7 @@ package de.danoeh.antennapod.menuhandler; import android.content.Context; import android.os.Handler; import android.util.Log; +import android.view.KeyEvent; import android.view.Menu; import android.view.MenuItem; @@ -15,6 +16,7 @@ import de.danoeh.antennapod.R; import de.danoeh.antennapod.activity.MainActivity; import de.danoeh.antennapod.core.preferences.PlaybackPreferences; import de.danoeh.antennapod.core.preferences.UserPreferences; +import de.danoeh.antennapod.core.receiver.MediaButtonReceiver; import de.danoeh.antennapod.core.service.playback.PlaybackService; import de.danoeh.antennapod.core.storage.DBWriter; import de.danoeh.antennapod.core.sync.SynchronizationSettings; @@ -143,7 +145,7 @@ public class FeedItemMenuHandler { @NonNull Context context = fragment.requireContext(); if (menuItemId == R.id.skip_episode_item) { - IntentUtils.sendLocalBroadcast(context, PlaybackService.ACTION_SKIP_CURRENT_EPISODE); + context.sendBroadcast(MediaButtonReceiver.createIntent(context, KeyEvent.KEYCODE_MEDIA_NEXT)); } else if (menuItemId == R.id.remove_item) { DBWriter.deleteFeedMediaOfItem(context, selectedItem.getMedia().getId()); } else if (menuItemId == R.id.remove_inbox_item) { diff --git a/core/src/main/java/de/danoeh/antennapod/core/receiver/MediaButtonReceiver.java b/core/src/main/java/de/danoeh/antennapod/core/receiver/MediaButtonReceiver.java index abee9d8d3..9a96379bc 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/receiver/MediaButtonReceiver.java +++ b/core/src/main/java/de/danoeh/antennapod/core/receiver/MediaButtonReceiver.java @@ -1,8 +1,10 @@ package de.danoeh.antennapod.core.receiver; +import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; +import android.os.Build; import androidx.core.content.ContextCompat; import android.util.Log; import android.view.KeyEvent; @@ -10,36 +12,44 @@ import android.view.KeyEvent; import de.danoeh.antennapod.core.ClientConfig; import de.danoeh.antennapod.core.service.playback.PlaybackService; -/** Receives media button events. */ +/** + * Receives media button events. + */ public class MediaButtonReceiver extends BroadcastReceiver { - private static final String TAG = "MediaButtonReceiver"; - public static final String EXTRA_KEYCODE = "de.danoeh.antennapod.core.service.extra.MediaButtonReceiver.KEYCODE"; - public static final String EXTRA_SOURCE = "de.danoeh.antennapod.core.service.extra.MediaButtonReceiver.SOURCE"; - public static final String EXTRA_HARDWAREBUTTON = "de.danoeh.antennapod.core.service.extra.MediaButtonReceiver.HARDWAREBUTTON"; + private static final String TAG = "MediaButtonReceiver"; + public static final String EXTRA_KEYCODE = "de.danoeh.antennapod.core.service.extra.MediaButtonReceiver.KEYCODE"; + public static final String EXTRA_SOURCE = "de.danoeh.antennapod.core.service.extra.MediaButtonReceiver.SOURCE"; + public static final String EXTRA_HARDWAREBUTTON + = "de.danoeh.antennapod.core.service.extra.MediaButtonReceiver.HARDWAREBUTTON"; + public static final String NOTIFY_BUTTON_RECEIVER = "de.danoeh.antennapod.NOTIFY_BUTTON_RECEIVER"; - public static final String NOTIFY_BUTTON_RECEIVER = "de.danoeh.antennapod.NOTIFY_BUTTON_RECEIVER"; + @Override + public void onReceive(Context context, Intent intent) { + Log.d(TAG, "Received intent"); + if (intent == null || intent.getExtras() == null) { + return; + } + KeyEvent event = (KeyEvent) intent.getExtras().get(Intent.EXTRA_KEY_EVENT); + if (event != null && event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) { + ClientConfig.initialize(context); + Intent serviceIntent = new Intent(context, PlaybackService.class); + serviceIntent.putExtra(EXTRA_KEYCODE, event.getKeyCode()); + serviceIntent.putExtra(EXTRA_SOURCE, event.getSource()); + serviceIntent.putExtra(EXTRA_HARDWAREBUTTON, event.getEventTime() > 0 || event.getDownTime() > 0); + ContextCompat.startForegroundService(context, serviceIntent); + } + } - @Override - public void onReceive(Context context, Intent intent) { - Log.d(TAG, "Received intent"); - if (intent == null || intent.getExtras() == null) { - return; - } - KeyEvent event = (KeyEvent) intent.getExtras().get(Intent.EXTRA_KEY_EVENT); - if (event != null && event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount()==0) { - ClientConfig.initialize(context); - Intent serviceIntent = new Intent(context, PlaybackService.class); - serviceIntent.putExtra(EXTRA_KEYCODE, event.getKeyCode()); - serviceIntent.putExtra(EXTRA_SOURCE, event.getSource()); - //detect if this is a hardware button press - if (event.getEventTime() > 0 || event.getDownTime() > 0) { - serviceIntent.putExtra(EXTRA_HARDWAREBUTTON, true); - } else { - serviceIntent.putExtra(EXTRA_HARDWAREBUTTON, false); - } - ContextCompat.startForegroundService(context, serviceIntent); - } - - } + public static Intent createIntent(Context context, int eventCode) { + KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, eventCode); + Intent startingIntent = new Intent(context, MediaButtonReceiver.class); + startingIntent.setAction(MediaButtonReceiver.NOTIFY_BUTTON_RECEIVER); + startingIntent.putExtra(Intent.EXTRA_KEY_EVENT, event); + return startingIntent; + } + public static PendingIntent createPendingIntent(Context context, int eventCode) { + return PendingIntent.getBroadcast(context, eventCode, createIntent(context, eventCode), + (Build.VERSION.SDK_INT >= 23 ? PendingIntent.FLAG_IMMUTABLE : 0)); + } } diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java b/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java index 391c2b75d..f39466fc4 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java +++ b/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java @@ -127,17 +127,6 @@ public class PlaybackService extends MediaBrowserServiceCompat { */ public static final String ACTION_SHUTDOWN_PLAYBACK_SERVICE = "action.de.danoeh.antennapod.core.service.actionShutdownPlaybackService"; - /** - * If the PlaybackService receives this action, it will end playback of the - * current episode and load the next episode if there is one available. - */ - public static final String ACTION_SKIP_CURRENT_EPISODE = "action.de.danoeh.antennapod.core.service.skipCurrentEpisode"; - - /** - * If the PlaybackService receives this action, it will pause playback. - */ - public static final String ACTION_PAUSE_PLAY_CURRENT_EPISODE = "action.de.danoeh.antennapod.core.service.pausePlayCurrentEpisode"; - /** * Custom action used by Android Wear, Android Auto */ @@ -263,8 +252,6 @@ public class PlaybackService extends MediaBrowserServiceCompat { registerReceiver(shutdownReceiver, new IntentFilter(ACTION_SHUTDOWN_PLAYBACK_SERVICE)); registerReceiver(bluetoothStateUpdated, new IntentFilter(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED)); registerReceiver(audioBecomingNoisy, new IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY)); - registerReceiver(skipCurrentEpisodeReceiver, new IntentFilter(ACTION_SKIP_CURRENT_EPISODE)); - registerReceiver(pausePlayCurrentEpisodeReceiver, new IntentFilter(ACTION_PAUSE_PLAY_CURRENT_EPISODE)); EventBus.getDefault().register(this); taskManager = new PlaybackServiceTaskManager(this, taskManagerCallback); @@ -347,8 +334,6 @@ public class PlaybackService extends MediaBrowserServiceCompat { unregisterReceiver(shutdownReceiver); unregisterReceiver(bluetoothStateUpdated); unregisterReceiver(audioBecomingNoisy); - unregisterReceiver(skipCurrentEpisodeReceiver); - unregisterReceiver(pausePlayCurrentEpisodeReceiver); mediaPlayer.shutdown(); taskManager.shutdown(); } @@ -1551,26 +1536,6 @@ public class PlaybackService extends MediaBrowserServiceCompat { }; - private final BroadcastReceiver skipCurrentEpisodeReceiver = new BroadcastReceiver() { - @Override - public void onReceive(Context context, Intent intent) { - if (TextUtils.equals(intent.getAction(), ACTION_SKIP_CURRENT_EPISODE)) { - Log.d(TAG, "Received SKIP_CURRENT_EPISODE intent"); - mediaPlayer.skip(); - } - } - }; - - private final BroadcastReceiver pausePlayCurrentEpisodeReceiver = new BroadcastReceiver() { - @Override - public void onReceive(Context context, Intent intent) { - if (TextUtils.equals(intent.getAction(), ACTION_PAUSE_PLAY_CURRENT_EPISODE)) { - Log.d(TAG, "Received PAUSE_PLAY_CURRENT_EPISODE intent"); - mediaPlayer.pause(false, false); - } - } - }; - @Subscribe(threadMode = ThreadMode.MAIN) @SuppressWarnings("unused") public void volumeAdaptionChanged(VolumeAdaptionChangedEvent event) { @@ -1630,10 +1595,6 @@ public class PlaybackService extends MediaBrowserServiceCompat { mediaPlayer.pause(abandonAudioFocus, reinit); } - public void reinit() { - mediaPlayer.reinit(); - } - public PlaybackServiceMediaPlayer.PSMPInfo getPSMPInfo() { return mediaPlayer.getPSMPInfo(); } diff --git a/core/src/main/java/de/danoeh/antennapod/core/widget/WidgetUpdater.java b/core/src/main/java/de/danoeh/antennapod/core/widget/WidgetUpdater.java index 11b0a29ca..1d1017bdc 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/widget/WidgetUpdater.java +++ b/core/src/main/java/de/danoeh/antennapod/core/widget/WidgetUpdater.java @@ -4,10 +4,8 @@ import android.app.PendingIntent; import android.appwidget.AppWidgetManager; import android.content.ComponentName; import android.content.Context; -import android.content.Intent; import android.content.SharedPreferences; import android.graphics.Bitmap; -import android.os.Build; import android.os.Bundle; import android.util.Log; import android.view.KeyEvent; @@ -133,21 +131,21 @@ public abstract class WidgetUpdater { views.setContentDescription(R.id.butPlayExtended, context.getString(R.string.play_label)); } views.setOnClickPendingIntent(R.id.butPlay, - createMediaButtonIntent(context, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE)); + MediaButtonReceiver.createPendingIntent(context, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE)); views.setOnClickPendingIntent(R.id.butPlayExtended, - createMediaButtonIntent(context, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE)); + MediaButtonReceiver.createPendingIntent(context, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE)); views.setOnClickPendingIntent(R.id.butRew, - createMediaButtonIntent(context, KeyEvent.KEYCODE_MEDIA_REWIND)); + MediaButtonReceiver.createPendingIntent(context, KeyEvent.KEYCODE_MEDIA_REWIND)); views.setOnClickPendingIntent(R.id.butFastForward, - createMediaButtonIntent(context, KeyEvent.KEYCODE_MEDIA_FAST_FORWARD)); + MediaButtonReceiver.createPendingIntent(context, KeyEvent.KEYCODE_MEDIA_FAST_FORWARD)); views.setOnClickPendingIntent(R.id.butSkip, - createMediaButtonIntent(context, KeyEvent.KEYCODE_MEDIA_NEXT)); + MediaButtonReceiver.createPendingIntent(context, KeyEvent.KEYCODE_MEDIA_NEXT)); } else { // start the app if they click anything views.setOnClickPendingIntent(R.id.layout_left, startMediaPlayer); views.setOnClickPendingIntent(R.id.butPlay, startMediaPlayer); views.setOnClickPendingIntent(R.id.butPlayExtended, - createMediaButtonIntent(context, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE)); + MediaButtonReceiver.createPendingIntent(context, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE)); views.setViewVisibility(R.id.txtvProgress, View.GONE); views.setViewVisibility(R.id.txtvTitle, View.GONE); views.setViewVisibility(R.id.txtNoPlaying, View.VISIBLE); @@ -205,19 +203,6 @@ public abstract class WidgetUpdater { return n - 1; } - /** - * Creates an intent which fakes a mediabutton press. - */ - private static PendingIntent createMediaButtonIntent(Context context, int eventCode) { - KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, eventCode); - Intent startingIntent = new Intent(context, MediaButtonReceiver.class); - startingIntent.setAction(MediaButtonReceiver.NOTIFY_BUTTON_RECEIVER); - startingIntent.putExtra(Intent.EXTRA_KEY_EVENT, event); - - return PendingIntent.getBroadcast(context, eventCode, startingIntent, - (Build.VERSION.SDK_INT >= 23 ? PendingIntent.FLAG_IMMUTABLE : 0)); - } - private static String getProgressString(int position, int duration, float speed) { if (position < 0 || duration <= 0) { return null;