Minor fixes

This commit is contained in:
Nite 2020-06-21 09:37:12 +02:00
parent 02da3e79ba
commit f6a41206b8
No known key found for this signature in database
GPG Key ID: 1D1AD59B1C6386C1
3 changed files with 33 additions and 5 deletions

View File

@ -25,6 +25,7 @@ import android.os.Build;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;
import android.view.KeyEvent;
import org.moire.ultrasonic.service.DownloadServiceImpl;
import org.moire.ultrasonic.util.Util;
@ -62,16 +63,36 @@ public class MediaButtonIntentReceiver extends BroadcastReceiver
try
{
context.startService(serviceIntent);
}
catch (IllegalStateException exception)
{
Log.i(TAG, "MediaButtonIntentReceiver couldn't start DownloadServiceImpl because the application was in the background.");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
KeyEvent keyEvent = (KeyEvent) event;
if (keyEvent.getAction() == KeyEvent.ACTION_DOWN && keyEvent.getRepeatCount() == 0)
{
int keyCode = keyEvent.getKeyCode();
if (keyCode == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE ||
keyCode == KeyEvent.KEYCODE_HEADSETHOOK ||
keyCode == KeyEvent.KEYCODE_MEDIA_PLAY)
{
// TODO: The only time it is OK to start DownloadServiceImpl as a foreground service is when we now it will display its notification.
// When DownloadServiceImpl is refactored to a proper foreground service, this can be removed.
context.startForegroundService(serviceIntent);
Log.i(TAG, "MediaButtonIntentReceiver started DownloadServiceImpl as foreground service");
}
}
}
}
try
{
if (isOrderedBroadcast())
{
abortBroadcast();
}
}
catch (IllegalStateException exception)
{
Log.w(TAG, "MediaButtonIntentReceiver couldn't start DownloadServiceImpl because the application was in the background.");
}
catch (Exception x)
{
// Ignored.

View File

@ -752,6 +752,7 @@ public class DownloadServiceImpl extends Service implements DownloadService
if (tabInstance != null)
{
stopForeground(true);
clearRemoteControl();
isInForeground = false;
tabInstance.hideNowPlaying();
}
@ -1277,6 +1278,7 @@ public class DownloadServiceImpl extends Service implements DownloadService
if (tabInstance != null)
{
stopForeground(true);
clearRemoteControl();
isInForeground = false;
tabInstance.hideNowPlaying();
}

View File

@ -289,6 +289,7 @@ public class DownloadServiceLifecycleSupport
return;
}
Log.i(TAG, "Deserialized currentPlayingIndex: " + state.currentPlayingIndex + ", currentPlayingPosition: " + state.currentPlayingPosition);
// TODO: here the autoPlay = false creates problems when Ultrasonic is started by a Play MediaButton as the player won't start this way.
downloadService.restore(state.songs, state.currentPlayingIndex, state.currentPlayingPosition, false, false);
// Work-around: Serialize again, as the restore() method creates a serialization without current playing info.
@ -321,7 +322,11 @@ public class DownloadServiceLifecycleSupport
downloadService.stop();
break;
case KeyEvent.KEYCODE_MEDIA_PLAY:
if (downloadService.getPlayerState() != PlayerState.STARTED)
if (downloadService.getPlayerState() == PlayerState.IDLE)
{
downloadService.play();
}
else if (downloadService.getPlayerState() != PlayerState.STARTED)
{
downloadService.start();
}