Delegate all handling of the Intent receiver registering to our Service Class.

This commit is contained in:
tzugen 2021-05-04 16:29:27 +02:00
parent 80036034e4
commit a3b97fef47
No known key found for this signature in database
GPG Key ID: 61E9C34BC10EC930
7 changed files with 21 additions and 55 deletions

View File

@ -492,13 +492,8 @@ public class SettingsFragment extends PreferenceFragmentCompat
} }
private void setMediaButtonsEnabled(boolean enabled) { private void setMediaButtonsEnabled(boolean enabled) {
if (enabled) { lockScreenEnabled.setEnabled(enabled);
lockScreenEnabled.setEnabled(true); Util.updateMediaButtonEventReceiver();
Util.registerMediaButtonEventReceiver(getActivity(), false);
} else {
lockScreenEnabled.setEnabled(false);
Util.unregisterMediaButtonEventReceiver(getActivity(), false);
}
} }
private void setBluetoothPreferences(boolean enabled) { private void setBluetoothPreferences(boolean enabled) {

View File

@ -29,7 +29,7 @@ import org.moire.ultrasonic.util.Constants;
import org.moire.ultrasonic.util.Util; import org.moire.ultrasonic.util.Util;
/** /**
* Request media button focus when connected to Bluetooth A2DP. * Resume or pause playback on Bluetooth A2DP connect/disconnect.
* *
* @author Sindre Mehus * @author Sindre Mehus
*/ */
@ -63,7 +63,6 @@ public class BluetoothIntentReceiver extends BroadcastReceiver
if (state == android.bluetooth.BluetoothA2dp.STATE_CONNECTED) actionA2dpConnected = true; if (state == android.bluetooth.BluetoothA2dp.STATE_CONNECTED) actionA2dpConnected = true;
else if (state == android.bluetooth.BluetoothA2dp.STATE_DISCONNECTED) actionA2dpDisconnected = true; else if (state == android.bluetooth.BluetoothA2dp.STATE_DISCONNECTED) actionA2dpDisconnected = true;
boolean connected = actionA2dpConnected || actionBluetoothDeviceConnected;
boolean resume = false; boolean resume = false;
boolean pause = false; boolean pause = false;
@ -83,12 +82,6 @@ public class BluetoothIntentReceiver extends BroadcastReceiver
break; break;
} }
if (connected)
{
Timber.i("Connected to Bluetooth device %s address %s, requesting media button focus.", name, address);
Util.registerMediaButtonEventReceiver(context, false);
}
if (resume) if (resume)
{ {
Timber.i("Connected to Bluetooth device %s address %s, resuming playback.", name, address); Timber.i("Connected to Bluetooth device %s address %s, resuming playback.", name, address);

View File

@ -76,9 +76,6 @@ public class MediaPlayerLifecycleSupport
registerHeadsetReceiver(); registerHeadsetReceiver();
// React to media buttons.
Util.registerMediaButtonEventReceiver(context, true);
mediaPlayerController.onCreate(); mediaPlayerController.onCreate();
if (autoPlay) mediaPlayerController.preload(); if (autoPlay) mediaPlayerController.preload();

View File

@ -677,41 +677,16 @@ public class Util
return Bitmap.createScaledBitmap(bitmap, size, getScaledHeight(bitmap, size), true); return Bitmap.createScaledBitmap(bitmap, size, getScaledHeight(bitmap, size), true);
} }
public static void registerMediaButtonEventReceiver(Context context, boolean isService) // Trigger an update on the MediaSession. Depending on the preference it will register
// or deregister the MediaButtonReceiver.
public static void updateMediaButtonEventReceiver()
{ {
if (getMediaButtonsEnabled(context)) MediaPlayerService mediaPlayerService = MediaPlayerService.getRunningInstance();
{ if (mediaPlayerService != null) {
if (isService) mediaButtonsRegisteredForService = true; mediaPlayerService.updateMediaButtonReceiver();
else mediaButtonsRegisteredForUI = true;
MediaPlayerService.executeOnStartedMediaPlayerService(context, (mediaPlayerService) ->
{
mediaPlayerService.registerMediaButtonEventReceiver();
return null;
}
);
} }
} }
public static void unregisterMediaButtonEventReceiver(Context context, boolean isService)
{
if (isService) mediaButtonsRegisteredForService = false;
else mediaButtonsRegisteredForUI = false;
// Do not unregister while there is an active part of the app which needs the control
if (mediaButtonsRegisteredForService || mediaButtonsRegisteredForUI) return;
MediaPlayerService.executeOnStartedMediaPlayerService(context, (mediaPlayerService) ->
{
mediaPlayerService.unregisterMediaButtonEventReceiver();
return null;
}
);
Timber.i("MediaButtonEventReceiver unregistered.");
}
public static MusicDirectory getSongsFromSearchResult(SearchResult searchResult) public static MusicDirectory getSongsFromSearchResult(SearchResult searchResult)
{ {
MusicDirectory musicDirectory = new MusicDirectory(); MusicDirectory musicDirectory = new MusicDirectory();

View File

@ -178,7 +178,7 @@ class NavigationActivity : AppCompatActivity() {
super.onResume() super.onResume()
setMenuForServerSetting() setMenuForServerSetting()
Util.registerMediaButtonEventReceiver(this, false)
// Lifecycle support's constructor registers some event receivers so it should be created early // Lifecycle support's constructor registers some event receivers so it should be created early
lifecycleSupport.onCreate() lifecycleSupport.onCreate()
@ -188,7 +188,6 @@ class NavigationActivity : AppCompatActivity() {
override fun onDestroy() { override fun onDestroy() {
super.onDestroy() super.onDestroy()
Util.unregisterMediaButtonEventReceiver(this, false)
nowPlayingEventDistributor.unsubscribe(nowPlayingEventListener) nowPlayingEventDistributor.unsubscribe(nowPlayingEventListener)
themeChangedEventDistributor.unsubscribe(themeChangedEventListener) themeChangedEventDistributor.unsubscribe(themeChangedEventListener)
imageLoaderProvider.clearImageLoader() imageLoaderProvider.clearImageLoader()
@ -310,7 +309,6 @@ class NavigationActivity : AppCompatActivity() {
private fun exit() { private fun exit() {
lifecycleSupport.onDestroy() lifecycleSupport.onDestroy()
Util.unregisterMediaButtonEventReceiver(this, false)
finish() finish()
} }

View File

@ -119,7 +119,6 @@ class LocalMediaPlayer(
}.start() }.start()
wakeLock.setReferenceCounted(false) wakeLock.setReferenceCounted(false)
Util.registerMediaButtonEventReceiver(context, true)
Timber.i("LocalMediaPlayer created") Timber.i("LocalMediaPlayer created")
} }
@ -146,7 +145,7 @@ class LocalMediaPlayer(
if (nextPlayingTask != null) { if (nextPlayingTask != null) {
nextPlayingTask!!.cancel() nextPlayingTask!!.cancel()
} }
Util.unregisterMediaButtonEventReceiver(context, true)
wakeLock.release() wakeLock.release()
} catch (exception: Throwable) { } catch (exception: Throwable) {
Timber.w(exception, "LocalMediaPlayer onDestroy exception: ") Timber.w(exception, "LocalMediaPlayer onDestroy exception: ")

View File

@ -793,7 +793,8 @@ class MediaPlayerService : Service() {
mediaSession = MediaSessionCompat(applicationContext, "UltrasonicService") mediaSession = MediaSessionCompat(applicationContext, "UltrasonicService")
mediaSessionToken = mediaSession!!.sessionToken mediaSessionToken = mediaSession!!.sessionToken
// mediaController = new MediaControllerCompat(getApplicationContext(), mediaSessionToken);
updateMediaButtonReceiver()
mediaSession!!.setCallback(object : MediaSessionCompat.Callback() { mediaSession!!.setCallback(object : MediaSessionCompat.Callback() {
override fun onPlay() { override fun onPlay() {
@ -839,6 +840,14 @@ class MediaPlayerService : Service() {
) )
} }
fun updateMediaButtonReceiver() {
if (Util.getMediaButtonsEnabled(applicationContext)) {
registerMediaButtonEventReceiver()
} else {
unregisterMediaButtonEventReceiver()
}
}
fun registerMediaButtonEventReceiver() { fun registerMediaButtonEventReceiver() {
val component = ComponentName(packageName, MediaButtonIntentReceiver::class.java.name) val component = ComponentName(packageName, MediaButtonIntentReceiver::class.java.name)
val mediaButtonIntent = Intent(Intent.ACTION_MEDIA_BUTTON) val mediaButtonIntent = Intent(Intent.ACTION_MEDIA_BUTTON)