Fixed exit and restart

This commit is contained in:
Nite 2020-06-30 15:16:45 +02:00
parent 7dad738fce
commit decca7103f
No known key found for this signature in database
GPG Key ID: 1D1AD59B1C6386C1
4 changed files with 24 additions and 3 deletions

View File

@ -160,7 +160,7 @@ public class SubsonicTabActivity extends ResultActivity implements OnClickListen
Util.registerMediaButtonEventReceiver(this);
// Lifecycle support's constructor registers some event receivers so it should be created early
lifecycleSupport.getValue();
lifecycleSupport.getValue().onCreate();
// Make sure to update theme
if (theme != null && !theme.equals(Util.getTheme(this)))

View File

@ -54,6 +54,7 @@ public class MediaPlayerControllerImpl implements MediaPlayerController
{
private static final String TAG = MediaPlayerControllerImpl.class.getSimpleName();
private boolean created = false;
private String suggestedPlaylistName;
private boolean keepScreenOn;
@ -79,6 +80,12 @@ public class MediaPlayerControllerImpl implements MediaPlayerController
this.shufflePlayBuffer = shufflePlayBuffer;
this.localMediaPlayer = localMediaPlayer;
Log.i(TAG, "MediaPlayerControllerImpl constructed");
}
public void onCreate()
{
if (created) return;
this.externalStorageMonitor.onCreate(new Runnable() {
@Override
public void run() {
@ -88,15 +95,18 @@ public class MediaPlayerControllerImpl implements MediaPlayerController
int instance = Util.getActiveServer(context);
setJukeboxEnabled(Util.getJukeboxEnabled(context, instance));
created = true;
Log.i(TAG, "MediaPlayerControllerImpl created");
}
public void onDestroy()
{
if (!created) return;
externalStorageMonitor.onDestroy();
context.stopService(new Intent(context, MediaPlayerService.class));
downloader.onDestroy();
created = false;
Log.i(TAG, "MediaPlayerControllerImpl destroyed");
}

View File

@ -44,6 +44,7 @@ public class MediaPlayerLifecycleSupport
{
private static final String TAG = MediaPlayerLifecycleSupport.class.getSimpleName();
private boolean created = false;
private DownloadQueueSerializer downloadQueueSerializer; // From DI
private final MediaPlayerControllerImpl mediaPlayerController; // From DI
private final Downloader downloader; // From DI
@ -59,6 +60,12 @@ public class MediaPlayerLifecycleSupport
this.context = context;
this.downloader = downloader;
Log.i(TAG, "LifecycleSupport constructed");
}
public void onCreate()
{
if (created) return;
registerHeadsetReceiver();
// React to media buttons.
@ -75,6 +82,7 @@ public class MediaPlayerLifecycleSupport
commandFilter.addAction(Constants.CMD_PROCESS_KEYCODE);
context.registerReceiver(intentReceiver, commandFilter);
mediaPlayerController.onCreate();
this.downloadQueueSerializer.deserializeDownloadQueue(new Consumer<State>() {
@Override
public void accept(State state) {
@ -88,15 +96,20 @@ public class MediaPlayerLifecycleSupport
});
new CacheCleaner(context).clean();
created = true;
Log.i(TAG, "LifecycleSupport created");
}
public void onDestroy()
{
if (!created) return;
downloadQueueSerializer.serializeDownloadQueueNow(downloader.downloadList,
downloader.getCurrentPlayingIndex(), mediaPlayerController.getPlayerPosition());
mediaPlayerController.clear(false);
context.unregisterReceiver(headsetEventReceiver);
context.unregisterReceiver(intentReceiver);
mediaPlayerController.onDestroy();
created = false;
Log.i(TAG, "LifecycleSupport destroyed");
}

View File

@ -191,8 +191,6 @@ public class MediaPlayerService extends Service
instance = null;
try {
downloadQueueSerializer.serializeDownloadQueueNow(downloader.downloadList,
downloader.getCurrentPlayingIndex(), getPlayerPosition());
localMediaPlayer.onDestroy();
downloader.stop();
shufflePlayBuffer.onDestroy();