Delegate all handling of the Intent receiver registering to our Service Class.
This commit is contained in:
parent
80036034e4
commit
a3b97fef47
|
@ -492,13 +492,8 @@ public class SettingsFragment extends PreferenceFragmentCompat
|
|||
}
|
||||
|
||||
private void setMediaButtonsEnabled(boolean enabled) {
|
||||
if (enabled) {
|
||||
lockScreenEnabled.setEnabled(true);
|
||||
Util.registerMediaButtonEventReceiver(getActivity(), false);
|
||||
} else {
|
||||
lockScreenEnabled.setEnabled(false);
|
||||
Util.unregisterMediaButtonEventReceiver(getActivity(), false);
|
||||
}
|
||||
lockScreenEnabled.setEnabled(enabled);
|
||||
Util.updateMediaButtonEventReceiver();
|
||||
}
|
||||
|
||||
private void setBluetoothPreferences(boolean enabled) {
|
||||
|
|
|
@ -29,7 +29,7 @@ import org.moire.ultrasonic.util.Constants;
|
|||
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
|
||||
*/
|
||||
|
@ -63,7 +63,6 @@ public class BluetoothIntentReceiver extends BroadcastReceiver
|
|||
if (state == android.bluetooth.BluetoothA2dp.STATE_CONNECTED) actionA2dpConnected = true;
|
||||
else if (state == android.bluetooth.BluetoothA2dp.STATE_DISCONNECTED) actionA2dpDisconnected = true;
|
||||
|
||||
boolean connected = actionA2dpConnected || actionBluetoothDeviceConnected;
|
||||
boolean resume = false;
|
||||
boolean pause = false;
|
||||
|
||||
|
@ -83,12 +82,6 @@ public class BluetoothIntentReceiver extends BroadcastReceiver
|
|||
break;
|
||||
}
|
||||
|
||||
if (connected)
|
||||
{
|
||||
Timber.i("Connected to Bluetooth device %s address %s, requesting media button focus.", name, address);
|
||||
Util.registerMediaButtonEventReceiver(context, false);
|
||||
}
|
||||
|
||||
if (resume)
|
||||
{
|
||||
Timber.i("Connected to Bluetooth device %s address %s, resuming playback.", name, address);
|
||||
|
|
|
@ -76,9 +76,6 @@ public class MediaPlayerLifecycleSupport
|
|||
|
||||
registerHeadsetReceiver();
|
||||
|
||||
// React to media buttons.
|
||||
Util.registerMediaButtonEventReceiver(context, true);
|
||||
|
||||
mediaPlayerController.onCreate();
|
||||
if (autoPlay) mediaPlayerController.preload();
|
||||
|
||||
|
|
|
@ -677,39 +677,14 @@ public class Util
|
|||
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))
|
||||
{
|
||||
if (isService) mediaButtonsRegisteredForService = true;
|
||||
else mediaButtonsRegisteredForUI = true;
|
||||
|
||||
MediaPlayerService.executeOnStartedMediaPlayerService(context, (mediaPlayerService) ->
|
||||
{
|
||||
mediaPlayerService.registerMediaButtonEventReceiver();
|
||||
return null;
|
||||
MediaPlayerService mediaPlayerService = MediaPlayerService.getRunningInstance();
|
||||
if (mediaPlayerService != null) {
|
||||
mediaPlayerService.updateMediaButtonReceiver();
|
||||
}
|
||||
);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
|
|
|
@ -178,7 +178,7 @@ class NavigationActivity : AppCompatActivity() {
|
|||
super.onResume()
|
||||
|
||||
setMenuForServerSetting()
|
||||
Util.registerMediaButtonEventReceiver(this, false)
|
||||
|
||||
// Lifecycle support's constructor registers some event receivers so it should be created early
|
||||
lifecycleSupport.onCreate()
|
||||
|
||||
|
@ -188,7 +188,6 @@ class NavigationActivity : AppCompatActivity() {
|
|||
|
||||
override fun onDestroy() {
|
||||
super.onDestroy()
|
||||
Util.unregisterMediaButtonEventReceiver(this, false)
|
||||
nowPlayingEventDistributor.unsubscribe(nowPlayingEventListener)
|
||||
themeChangedEventDistributor.unsubscribe(themeChangedEventListener)
|
||||
imageLoaderProvider.clearImageLoader()
|
||||
|
@ -310,7 +309,6 @@ class NavigationActivity : AppCompatActivity() {
|
|||
|
||||
private fun exit() {
|
||||
lifecycleSupport.onDestroy()
|
||||
Util.unregisterMediaButtonEventReceiver(this, false)
|
||||
finish()
|
||||
}
|
||||
|
||||
|
|
|
@ -119,7 +119,6 @@ class LocalMediaPlayer(
|
|||
}.start()
|
||||
|
||||
wakeLock.setReferenceCounted(false)
|
||||
Util.registerMediaButtonEventReceiver(context, true)
|
||||
Timber.i("LocalMediaPlayer created")
|
||||
}
|
||||
|
||||
|
@ -146,7 +145,7 @@ class LocalMediaPlayer(
|
|||
if (nextPlayingTask != null) {
|
||||
nextPlayingTask!!.cancel()
|
||||
}
|
||||
Util.unregisterMediaButtonEventReceiver(context, true)
|
||||
|
||||
wakeLock.release()
|
||||
} catch (exception: Throwable) {
|
||||
Timber.w(exception, "LocalMediaPlayer onDestroy exception: ")
|
||||
|
|
|
@ -793,7 +793,8 @@ class MediaPlayerService : Service() {
|
|||
|
||||
mediaSession = MediaSessionCompat(applicationContext, "UltrasonicService")
|
||||
mediaSessionToken = mediaSession!!.sessionToken
|
||||
// mediaController = new MediaControllerCompat(getApplicationContext(), mediaSessionToken);
|
||||
|
||||
updateMediaButtonReceiver()
|
||||
|
||||
mediaSession!!.setCallback(object : MediaSessionCompat.Callback() {
|
||||
override fun onPlay() {
|
||||
|
@ -839,6 +840,14 @@ class MediaPlayerService : Service() {
|
|||
)
|
||||
}
|
||||
|
||||
fun updateMediaButtonReceiver() {
|
||||
if (Util.getMediaButtonsEnabled(applicationContext)) {
|
||||
registerMediaButtonEventReceiver()
|
||||
} else {
|
||||
unregisterMediaButtonEventReceiver()
|
||||
}
|
||||
}
|
||||
|
||||
fun registerMediaButtonEventReceiver() {
|
||||
val component = ComponentName(packageName, MediaButtonIntentReceiver::class.java.name)
|
||||
val mediaButtonIntent = Intent(Intent.ACTION_MEDIA_BUTTON)
|
||||
|
|
Loading…
Reference in New Issue