Improved cold start time from Doze

This commit is contained in:
Nite 2020-07-16 14:25:58 +02:00
parent 07553464e8
commit 5f3861b1ac
No known key found for this signature in database
GPG Key ID: 1D1AD59B1C6386C1
2 changed files with 70 additions and 53 deletions

View File

@ -143,6 +143,11 @@ public class MediaPlayerControllerImpl implements MediaPlayerController
} }
} }
public synchronized void preload()
{
MediaPlayerService.getInstance(context);
}
@Override @Override
public synchronized void play(final int index) public synchronized void play(final int index)
{ {

View File

@ -65,12 +65,17 @@ public class MediaPlayerLifecycleSupport
public void onCreate() public void onCreate()
{ {
onCreate(false); onCreate(false, null);
} }
private void onCreate(final boolean autoPlay) private void onCreate(final boolean autoPlay, final Runnable afterCreated)
{ {
if (created) return; if (created)
{
if (afterCreated != null) afterCreated.run();
return;
}
registerHeadsetReceiver(); registerHeadsetReceiver();
// React to media buttons. // React to media buttons.
@ -88,6 +93,8 @@ public class MediaPlayerLifecycleSupport
context.registerReceiver(intentReceiver, commandFilter); context.registerReceiver(intentReceiver, commandFilter);
mediaPlayerController.onCreate(); mediaPlayerController.onCreate();
if (autoPlay) mediaPlayerController.preload();
this.downloadQueueSerializer.deserializeDownloadQueue(new Consumer<State>() { this.downloadQueueSerializer.deserializeDownloadQueue(new Consumer<State>() {
@Override @Override
public void accept(State state) { public void accept(State state) {
@ -96,6 +103,8 @@ public class MediaPlayerLifecycleSupport
// Work-around: Serialize again, as the restore() method creates a serialization without current playing info. // Work-around: Serialize again, as the restore() method creates a serialization without current playing info.
MediaPlayerLifecycleSupport.this.downloadQueueSerializer.serializeDownloadQueue(downloader.downloadList, MediaPlayerLifecycleSupport.this.downloadQueueSerializer.serializeDownloadQueue(downloader.downloadList,
downloader.getCurrentPlayingIndex(), mediaPlayerController.getPlayerPosition()); downloader.getCurrentPlayingIndex(), mediaPlayerController.getPlayerPosition());
if (afterCreated != null) afterCreated.run();
} }
}); });
@ -181,7 +190,7 @@ public class MediaPlayerLifecycleSupport
return; return;
} }
int keyCode = event.getKeyCode(); final int keyCode = event.getKeyCode();
boolean autoStart = (keyCode == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE || boolean autoStart = (keyCode == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE ||
keyCode == KeyEvent.KEYCODE_MEDIA_PLAY || keyCode == KeyEvent.KEYCODE_MEDIA_PLAY ||
keyCode == KeyEvent.KEYCODE_HEADSETHOOK || keyCode == KeyEvent.KEYCODE_HEADSETHOOK ||
@ -189,8 +198,9 @@ public class MediaPlayerLifecycleSupport
keyCode == KeyEvent.KEYCODE_MEDIA_NEXT); keyCode == KeyEvent.KEYCODE_MEDIA_NEXT);
// We can receive intents (e.g. MediaButton) when everything is stopped, so we need to start // We can receive intents (e.g. MediaButton) when everything is stopped, so we need to start
if (!created) onCreate(autoStart); onCreate(autoStart, new Runnable() {
@Override
public void run() {
switch (keyCode) switch (keyCode)
{ {
case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE: case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
@ -241,6 +251,8 @@ public class MediaPlayerLifecycleSupport
break; break;
} }
} }
});
}
/** /**
* This receiver manages the intent that could come from other applications. * This receiver manages the intent that could come from other applications.