Switched widget to JobIntentService

This commit is contained in:
ByteHamster 2018-05-05 13:39:57 +02:00
parent ca3d6b9a3d
commit 0b54d97a0a
8 changed files with 214 additions and 308 deletions

View File

@ -118,7 +118,8 @@
</activity> </activity>
<service <service
android:name=".service.PlayerWidgetService" android:name=".service.PlayerWidgetJobService"
android:permission="android.permission.BIND_JOB_SERVICE"
android:enabled="true" android:enabled="true"
android:exported="false"> android:exported="false">
</service> </service>

View File

@ -31,7 +31,6 @@ import de.danoeh.antennapod.core.service.playback.PlayerStatus;
import de.danoeh.antennapod.core.util.gui.PictureInPictureUtil; import de.danoeh.antennapod.core.util.gui.PictureInPictureUtil;
import de.danoeh.antennapod.core.util.playback.ExternalMedia; import de.danoeh.antennapod.core.util.playback.ExternalMedia;
import de.danoeh.antennapod.core.util.playback.Playable; import de.danoeh.antennapod.core.util.playback.Playable;
import de.danoeh.antennapod.service.PlayerWidgetService;
import de.danoeh.antennapod.view.AspectRatioVideoView; import de.danoeh.antennapod.view.AspectRatioVideoView;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;

View File

@ -5,14 +5,13 @@ import android.appwidget.AppWidgetProvider;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import java.util.Arrays; import java.util.Arrays;
import de.danoeh.antennapod.core.service.playback.PlaybackService; import de.danoeh.antennapod.core.service.playback.PlaybackService;
import de.danoeh.antennapod.service.PlayerWidgetService; import de.danoeh.antennapod.service.PlayerWidgetJobService;
public class PlayerWidget extends AppWidgetProvider { public class PlayerWidget extends AppWidgetProvider {
private static final String TAG = "PlayerWidget"; private static final String TAG = "PlayerWidget";
@ -23,17 +22,7 @@ public class PlayerWidget extends AppWidgetProvider {
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
Log.d(TAG, "onReceive"); Log.d(TAG, "onReceive");
super.onReceive(context, intent); super.onReceive(context, intent);
// don't do anything if we're not enabled PlayerWidgetJobService.updateWidget(context);
if (!isEnabled(context)) {
return;
}
// these come from the PlaybackService when things should get updated
if (TextUtils.equals(intent.getAction(), PlaybackService.FORCE_WIDGET_UPDATE)) {
startUpdate(context);
} else if (TextUtils.equals(intent.getAction(), PlaybackService.STOP_WIDGET_UPDATE)) {
stopUpdate(context);
}
} }
@Override @Override
@ -41,14 +30,13 @@ public class PlayerWidget extends AppWidgetProvider {
super.onEnabled(context); super.onEnabled(context);
Log.d(TAG, "Widget enabled"); Log.d(TAG, "Widget enabled");
setEnabled(context, true); setEnabled(context, true);
startUpdate(context); PlayerWidgetJobService.updateWidget(context);
} }
@Override @Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
int[] appWidgetIds) {
Log.d(TAG, "onUpdate() called with: " + "context = [" + context + "], appWidgetManager = [" + appWidgetManager + "], appWidgetIds = [" + Arrays.toString(appWidgetIds) + "]"); Log.d(TAG, "onUpdate() called with: " + "context = [" + context + "], appWidgetManager = [" + appWidgetManager + "], appWidgetIds = [" + Arrays.toString(appWidgetIds) + "]");
startUpdate(context); PlayerWidgetJobService.updateWidget(context);
} }
@Override @Override
@ -56,20 +44,9 @@ public class PlayerWidget extends AppWidgetProvider {
super.onDisabled(context); super.onDisabled(context);
Log.d(TAG, "Widget disabled"); Log.d(TAG, "Widget disabled");
setEnabled(context, false); setEnabled(context, false);
stopUpdate(context);
} }
private void startUpdate(Context context) { public static boolean isEnabled(Context context) {
Log.d(TAG, "startUpdate() called with: " + "context = [" + context + "]");
ContextCompat.startForegroundService(context, new Intent(context, PlayerWidgetService.class));
}
private void stopUpdate(Context context) {
Log.d(TAG, "stopUpdate() called with: " + "context = [" + context + "]");
context.stopService(new Intent(context, PlayerWidgetService.class));
}
private boolean isEnabled(Context context) {
SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
return prefs.getBoolean(KEY_ENABLED, false); return prefs.getBoolean(KEY_ENABLED, false);
} }

View File

@ -0,0 +1,182 @@
package de.danoeh.antennapod.service;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Build;
import android.os.IBinder;
import android.support.annotation.NonNull;
import android.support.v4.app.JobIntentService;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.RemoteViews;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.activity.MainActivity;
import de.danoeh.antennapod.core.receiver.MediaButtonReceiver;
import de.danoeh.antennapod.core.service.playback.PlaybackService;
import de.danoeh.antennapod.core.service.playback.PlayerStatus;
import de.danoeh.antennapod.core.util.Converter;
import de.danoeh.antennapod.core.util.playback.Playable;
import de.danoeh.antennapod.fragment.QueueFragment;
import de.danoeh.antennapod.receiver.PlayerWidget;
/**
* Updates the state of the player widget
*/
public class PlayerWidgetJobService extends JobIntentService {
private static final String TAG = "PlayerWidgetJobService";
private PlaybackService playbackService;
private final Object waitForService = new Object();
public PlayerWidgetJobService() {
}
public static void updateWidget(Context context) {
enqueueWork(context, PlayerWidgetJobService.class, 0, new Intent(context, PlayerWidgetJobService.class));
}
@Override
protected void onHandleWork(@NonNull Intent intent) {
if (!PlayerWidget.isEnabled(getApplicationContext())) {
return;
}
if (PlaybackService.isRunning && playbackService == null) {
synchronized (waitForService) {
bindService(new Intent(this, PlaybackService.class), mConnection, 0);
while (playbackService == null) {
try {
waitForService.wait();
} catch (InterruptedException e) {
return;
}
}
}
}
updateViews();
if (playbackService != null) {
try {
unbindService(mConnection);
} catch (IllegalArgumentException e) {
Log.w(TAG, "IllegalArgumentException when trying to unbind service");
}
}
}
private void updateViews() {
ComponentName playerWidget = new ComponentName(this, PlayerWidget.class);
AppWidgetManager manager = AppWidgetManager.getInstance(this);
RemoteViews views = new RemoteViews(getPackageName(), R.layout.player_widget);
PendingIntent startMediaplayer = PendingIntent.getActivity(this, 0,
PlaybackService.getPlayerActivityIntent(this), 0);
Intent startApp = new Intent(getBaseContext(), MainActivity.class);
startApp.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startApp.putExtra(MainActivity.EXTRA_FRAGMENT_TAG, QueueFragment.TAG);
PendingIntent startAppPending = PendingIntent.getActivity(getBaseContext(), 0, startApp, PendingIntent.FLAG_UPDATE_CURRENT);
boolean nothingPlaying = false;
Playable media;
PlayerStatus status;
if (playbackService != null) {
media = playbackService.getPlayable();
status = playbackService.getStatus();
} else {
media = Playable.PlayableUtils.createInstanceFromPreferences(getApplicationContext());
status = PlayerStatus.STOPPED;
}
if (media != null) {
views.setOnClickPendingIntent(R.id.layout_left, startMediaplayer);
views.setTextViewText(R.id.txtvTitle, media.getEpisodeTitle());
String progressString;
if (playbackService != null) {
progressString = getProgressString(playbackService.getCurrentPosition(), playbackService.getDuration());
} else {
progressString = getProgressString(media.getPosition(), media.getDuration());
}
if (progressString != null) {
views.setViewVisibility(R.id.txtvProgress, View.VISIBLE);
views.setTextViewText(R.id.txtvProgress, progressString);
}
if (status == PlayerStatus.PLAYING) {
views.setImageViewResource(R.id.butPlay, R.drawable.ic_pause_white_24dp);
if (Build.VERSION.SDK_INT >= 15) {
views.setContentDescription(R.id.butPlay, getString(R.string.pause_label));
}
} else {
views.setImageViewResource(R.id.butPlay, R.drawable.ic_play_arrow_white_24dp);
if (Build.VERSION.SDK_INT >= 15) {
views.setContentDescription(R.id.butPlay, getString(R.string.play_label));
}
}
views.setOnClickPendingIntent(R.id.butPlay, createMediaButtonIntent());
} else {
nothingPlaying = true;
}
if (nothingPlaying) {
// start the app if they click anything
views.setOnClickPendingIntent(R.id.layout_left, startAppPending);
views.setOnClickPendingIntent(R.id.butPlay, startAppPending);
views.setViewVisibility(R.id.txtvProgress, View.INVISIBLE);
views.setTextViewText(R.id.txtvTitle,
this.getString(R.string.no_media_playing_label));
views.setImageViewResource(R.id.butPlay, R.drawable.ic_play_arrow_white_24dp);
}
manager.updateAppWidget(playerWidget, views);
}
/**
* Creates an intent which fakes a mediabutton press
*/
private PendingIntent createMediaButtonIntent() {
KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE);
Intent startingIntent = new Intent(getBaseContext(), MediaButtonReceiver.class);
startingIntent.setAction(MediaButtonReceiver.NOTIFY_BUTTON_RECEIVER);
startingIntent.putExtra(Intent.EXTRA_KEY_EVENT, event);
return PendingIntent.getBroadcast(this, 0, startingIntent, 0);
}
private String getProgressString(int position, int duration) {
if (position > 0 && duration > 0) {
return Converter.getDurationStringLong(position) + " / "
+ Converter.getDurationStringLong(duration);
} else {
return null;
}
}
private final ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
Log.d(TAG, "Connection to service established");
if (service instanceof PlaybackService.LocalBinder) {
synchronized (waitForService) {
playbackService = ((PlaybackService.LocalBinder) service).getService();
waitForService.notifyAll();
}
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
playbackService = null;
Log.d(TAG, "Disconnected from service");
}
};
}

View File

@ -1,244 +0,0 @@
package de.danoeh.antennapod.service;
import android.app.PendingIntent;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.RemoteViews;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.activity.MainActivity;
import de.danoeh.antennapod.core.feed.FeedItem;
import de.danoeh.antennapod.core.feed.FeedMedia;
import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.receiver.MediaButtonReceiver;
import de.danoeh.antennapod.core.service.playback.PlaybackService;
import de.danoeh.antennapod.core.service.playback.PlayerStatus;
import de.danoeh.antennapod.core.storage.DBWriter;
import de.danoeh.antennapod.core.util.Converter;
import de.danoeh.antennapod.core.util.playback.Playable;
import de.danoeh.antennapod.fragment.QueueFragment;
import de.danoeh.antennapod.receiver.PlayerWidget;
/**
* Updates the state of the player widget
*/
public class PlayerWidgetService extends Service {
private static final String TAG = "PlayerWidgetService";
private PlaybackService playbackService;
/**
* Controls write access to playbackservice reference
*/
private final Object psLock = new Object();
/**
* True while service is updating the widget
*/
private volatile boolean isUpdating;
public PlayerWidgetService() {
}
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "Service created");
isUpdating = false;
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "Service is about to be destroyed");
if (playbackService != null) {
Playable playable = playbackService.getPlayable();
if (playable != null && playable instanceof FeedMedia) {
FeedMedia media = (FeedMedia) playable;
if (media.hasAlmostEnded()) {
Log.d(TAG, "smart mark as read");
FeedItem item = media.getItem();
DBWriter.markItemPlayed(item, FeedItem.PLAYED, false);
DBWriter.removeQueueItem(this, item, false);
DBWriter.addItemToPlaybackHistory(media);
if (item.getFeed().getPreferences().getCurrentAutoDelete() &&
(!item.isTagged(FeedItem.TAG_FAVORITE) || !UserPreferences.shouldFavoriteKeepEpisode())) {
Log.d(TAG, "Delete " + media.toString());
DBWriter.deleteFeedMediaOfItem(this, media.getId());
}
}
}
}
try {
unbindService(mConnection);
} catch (IllegalArgumentException e) {
Log.w(TAG, "IllegalArgumentException when trying to unbind service");
}
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (!isUpdating) {
if (playbackService == null && PlaybackService.isRunning) {
bindService(new Intent(this, PlaybackService.class),
mConnection, 0);
} else {
startViewUpdaterIfNotRunning();
}
} else {
Log.d(TAG, "Service was called while updating. Ignoring update request");
}
return Service.START_NOT_STICKY;
}
private void updateViews() {
isUpdating = true;
ComponentName playerWidget = new ComponentName(this, PlayerWidget.class);
AppWidgetManager manager = AppWidgetManager.getInstance(this);
RemoteViews views = new RemoteViews(getPackageName(),
R.layout.player_widget);
PendingIntent startMediaplayer = PendingIntent.getActivity(this, 0,
PlaybackService.getPlayerActivityIntent(this), 0);
Intent startApp = new Intent(getBaseContext(), MainActivity.class);
startApp.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startApp.putExtra(MainActivity.EXTRA_FRAGMENT_TAG, QueueFragment.TAG);
PendingIntent startAppPending = PendingIntent.getActivity(getBaseContext(), 0, startApp, PendingIntent.FLAG_UPDATE_CURRENT);
boolean nothingPlaying = false;
if (playbackService != null) {
final Playable media = playbackService.getPlayable();
if (media != null) {
PlayerStatus status = playbackService.getStatus();
views.setOnClickPendingIntent(R.id.layout_left, startMediaplayer);
views.setTextViewText(R.id.txtvTitle, media.getEpisodeTitle());
String progressString = getProgressString();
if (progressString != null) {
views.setViewVisibility(R.id.txtvProgress, View.VISIBLE);
views.setTextViewText(R.id.txtvProgress, progressString);
}
if (status == PlayerStatus.PLAYING) {
views.setImageViewResource(R.id.butPlay, R.drawable.ic_pause_white_24dp);
if (Build.VERSION.SDK_INT >= 15) {
views.setContentDescription(R.id.butPlay, getString(R.string.pause_label));
}
} else {
views.setImageViewResource(R.id.butPlay, R.drawable.ic_play_arrow_white_24dp);
if (Build.VERSION.SDK_INT >= 15) {
views.setContentDescription(R.id.butPlay, getString(R.string.play_label));
}
}
views.setOnClickPendingIntent(R.id.butPlay,
createMediaButtonIntent());
} else {
nothingPlaying = true;
}
} else {
nothingPlaying = true;
}
if (nothingPlaying) {
// start the app if they click anything
views.setOnClickPendingIntent(R.id.layout_left, startAppPending);
views.setOnClickPendingIntent(R.id.butPlay, startAppPending);
views.setViewVisibility(R.id.txtvProgress, View.INVISIBLE);
views.setTextViewText(R.id.txtvTitle,
this.getString(R.string.no_media_playing_label));
views.setImageViewResource(R.id.butPlay, R.drawable.ic_play_arrow_white_24dp);
}
manager.updateAppWidget(playerWidget, views);
isUpdating = false;
}
/**
* Creates an intent which fakes a mediabutton press
*/
private PendingIntent createMediaButtonIntent() {
KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN,
KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE);
Intent startingIntent = new Intent(
MediaButtonReceiver.NOTIFY_BUTTON_RECEIVER);
startingIntent.putExtra(Intent.EXTRA_KEY_EVENT, event);
return PendingIntent.getBroadcast(this, 0, startingIntent, 0);
}
private String getProgressString() {
int position = playbackService.getCurrentPosition();
int duration = playbackService.getDuration();
if (position > 0 && duration > 0) {
return Converter.getDurationStringLong(position) + " / "
+ Converter.getDurationStringLong(duration);
} else {
return null;
}
}
private final ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
Log.d(TAG, "Connection to service established");
synchronized (psLock) {
if(service instanceof PlaybackService.LocalBinder) {
playbackService = ((PlaybackService.LocalBinder) service).getService();
startViewUpdaterIfNotRunning();
}
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
synchronized (psLock) {
playbackService = null;
Log.d(TAG, "Disconnected from service");
}
}
};
private void startViewUpdaterIfNotRunning() {
if (!isUpdating) {
ViewUpdater updateThread = new ViewUpdater(this);
updateThread.start();
}
}
class ViewUpdater extends Thread {
private static final String THREAD_NAME = "ViewUpdater";
private final PlayerWidgetService service;
public ViewUpdater(PlayerWidgetService service) {
super();
setName(THREAD_NAME);
this.service = service;
}
@Override
public void run() {
synchronized (psLock) {
service.updateViews();
}
}
}
}

View File

@ -77,8 +77,6 @@ import de.greenrobot.event.EventBus;
* Controls the MediaPlayer that plays a FeedMedia-file * Controls the MediaPlayer that plays a FeedMedia-file
*/ */
public class PlaybackService extends MediaBrowserServiceCompat { public class PlaybackService extends MediaBrowserServiceCompat {
public static final String FORCE_WIDGET_UPDATE = "de.danoeh.antennapod.FORCE_WIDGET_UPDATE";
public static final String STOP_WIDGET_UPDATE = "de.danoeh.antennapod.STOP_WIDGET_UPDATE";
/** /**
* Logging tag * Logging tag
*/ */
@ -321,11 +319,8 @@ public class PlaybackService extends MediaBrowserServiceCompat {
startForeground(NOTIFICATION_ID, notificationBuilder.build()); startForeground(NOTIFICATION_ID, notificationBuilder.build());
EventBus.getDefault().post(new ServiceEvent(ServiceEvent.Action.SERVICE_STARTED)); EventBus.getDefault().post(new ServiceEvent(ServiceEvent.Action.SERVICE_STARTED));
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
long currentlyPlayingMedia = PlaybackPreferences.getCurrentlyPlayingMedia(); setupNotification(Playable.PlayableUtils.createInstanceFromPreferences(getApplicationContext()));
Playable lastPlayable = Playable.PlayableUtils.createInstanceFromPreferences(
getApplicationContext(), (int) currentlyPlayingMedia, prefs);
setupNotification(lastPlayable);
} }
private NotificationCompat.Builder createBasicNotification() { private NotificationCompat.Builder createBasicNotification() {
@ -635,7 +630,7 @@ public class PlaybackService extends MediaBrowserServiceCompat {
@Override @Override
public void onWidgetUpdaterTick() { public void onWidgetUpdaterTick() {
updateWidget(); //PlayerWidgetJobService.updateWidget(getBaseContext()); // TODO: Not accessible from core module
} }
@Override @Override
@ -697,7 +692,7 @@ public class PlaybackService extends MediaBrowserServiceCompat {
Intent statusUpdate = new Intent(ACTION_PLAYER_STATUS_CHANGED); Intent statusUpdate = new Intent(ACTION_PLAYER_STATUS_CHANGED);
// statusUpdate.putExtra(EXTRA_NEW_PLAYER_STATUS, newInfo.playerStatus.ordinal()); // statusUpdate.putExtra(EXTRA_NEW_PLAYER_STATUS, newInfo.playerStatus.ordinal());
sendBroadcast(statusUpdate); sendBroadcast(statusUpdate);
updateWidget(); //PlayerWidgetJobService.updateWidget(getBaseContext()); // TODO: Not accessible from core module
bluetoothNotifyChange(newInfo, AVRCP_ACTION_PLAYER_STATUS_CHANGED); bluetoothNotifyChange(newInfo, AVRCP_ACTION_PLAYER_STATUS_CHANGED);
bluetoothNotifyChange(newInfo, AVRCP_ACTION_META_CHANGED); bluetoothNotifyChange(newInfo, AVRCP_ACTION_META_CHANGED);
} }
@ -855,7 +850,6 @@ public class PlaybackService extends MediaBrowserServiceCompat {
if (!isCasting) { if (!isCasting) {
stopForeground(true); stopForeground(true);
} }
stopWidgetUpdater();
} }
if (mediaType == null) { if (mediaType == null) {
sendNotificationBroadcast(NOTIFICATION_TYPE_PLAYBACK_END, 0); sendNotificationBroadcast(NOTIFICATION_TYPE_PLAYBACK_END, 0);
@ -1402,16 +1396,6 @@ public class PlaybackService extends MediaBrowserServiceCompat {
} }
} }
private void stopWidgetUpdater() {
taskManager.cancelWidgetUpdater();
sendBroadcast(new Intent(STOP_WIDGET_UPDATE));
}
private void updateWidget() {
PlaybackService.this.sendBroadcast(new Intent(
FORCE_WIDGET_UPDATE));
}
public boolean sleepTimerActive() { public boolean sleepTimerActive() {
return taskManager.isSleepTimerActive(); return taskManager.isSleepTimerActive();
} }

View File

@ -3,6 +3,7 @@ package de.danoeh.antennapod.core.util.playback;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.Parcelable; import android.os.Parcelable;
import android.preference.PreferenceManager;
import android.util.Log; import android.util.Log;
import java.util.List; import java.util.List;
@ -11,6 +12,7 @@ import de.danoeh.antennapod.core.asynctask.ImageResource;
import de.danoeh.antennapod.core.feed.Chapter; import de.danoeh.antennapod.core.feed.Chapter;
import de.danoeh.antennapod.core.feed.FeedMedia; import de.danoeh.antennapod.core.feed.FeedMedia;
import de.danoeh.antennapod.core.feed.MediaType; import de.danoeh.antennapod.core.feed.MediaType;
import de.danoeh.antennapod.core.preferences.PlaybackPreferences;
import de.danoeh.antennapod.core.storage.DBReader; import de.danoeh.antennapod.core.storage.DBReader;
import de.danoeh.antennapod.core.util.ShownotesProvider; import de.danoeh.antennapod.core.util.ShownotesProvider;
@ -175,6 +177,22 @@ public interface Playable extends Parcelable,
class PlayableUtils { class PlayableUtils {
private static final String TAG = "PlayableUtils"; private static final String TAG = "PlayableUtils";
/**
* Restores a playable object from a sharedPreferences file. This method might load data from the database,
* depending on the type of playable that was restored.
*
* @return The restored Playable object
*/
public static Playable createInstanceFromPreferences(Context context) {
long currentlyPlayingMedia = PlaybackPreferences.getCurrentlyPlayingMedia();
if (currentlyPlayingMedia != PlaybackPreferences.NO_MEDIA_PLAYING) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
return PlayableUtils.createInstanceFromPreferences(context,
(int) currentlyPlayingMedia, prefs);
}
return null;
}
/** /**
* Restores a playable object from a sharedPreferences file. This method might load data from the database, * Restores a playable object from a sharedPreferences file. This method might load data from the database,
* depending on the type of playable that was restored. * depending on the type of playable that was restored.

View File

@ -208,24 +208,13 @@ public abstract class PlaybackController {
}, error -> Log.e(TAG, Log.getStackTraceString(error))); }, error -> Log.e(TAG, Log.getStackTraceString(error)));
} }
private Playable getMediaFromPreferences() {
long currentlyPlayingMedia = PlaybackPreferences.getCurrentlyPlayingMedia();
if (currentlyPlayingMedia != PlaybackPreferences.NO_MEDIA_PLAYING) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(
activity.getApplicationContext());
return PlayableUtils.createInstanceFromPreferences(activity,
(int) currentlyPlayingMedia, prefs);
}
return null;
}
/** /**
* Returns an intent that starts the PlaybackService and plays the last * Returns an intent that starts the PlaybackService and plays the last
* played media or null if no last played media could be found. * played media or null if no last played media could be found.
*/ */
private Intent getPlayLastPlayedMediaIntent() { private Intent getPlayLastPlayedMediaIntent() {
Log.d(TAG, "Trying to restore last played media"); Log.d(TAG, "Trying to restore last played media");
Playable media = getMediaFromPreferences(); Playable media = PlayableUtils.createInstanceFromPreferences(activity);
if (media != null) { if (media != null) {
Intent serviceIntent = new Intent(activity, PlaybackService.class); Intent serviceIntent = new Intent(activity, PlaybackService.class);
serviceIntent.putExtra(PlaybackService.EXTRA_PLAYABLE, media); serviceIntent.putExtra(PlaybackService.EXTRA_PLAYABLE, media);
@ -648,7 +637,7 @@ public abstract class PlaybackController {
public Playable getMedia() { public Playable getMedia() {
if (media == null) { if (media == null) {
media = getMediaFromPreferences(); media = PlayableUtils.createInstanceFromPreferences(activity);
} }
return media; return media;
} }