Let PlaybackController handle listening for service start

This commit is contained in:
ByteHamster 2019-04-12 11:39:38 +02:00
parent 7d20baf4f9
commit 09f4ebf08a
6 changed files with 21 additions and 70 deletions

View File

@ -38,7 +38,6 @@ import com.joanzapata.iconify.fonts.FontAwesomeIcons;
import java.util.Locale; import java.util.Locale;
import de.danoeh.antennapod.R; import de.danoeh.antennapod.R;
import de.danoeh.antennapod.core.event.ServiceEvent;
import de.danoeh.antennapod.core.feed.FeedItem; import de.danoeh.antennapod.core.feed.FeedItem;
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;
@ -68,9 +67,6 @@ import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable; import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers; import io.reactivex.schedulers.Schedulers;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
/** /**
@ -288,7 +284,6 @@ public abstract class MediaplayerActivity extends CastEnabledActivity implements
controller.init(); controller.init();
loadMediaInfo(); loadMediaInfo();
onPositionObserverUpdate(); onPositionObserverUpdate();
EventBus.getDefault().register(this);
} }
@Override @Override
@ -301,7 +296,6 @@ public abstract class MediaplayerActivity extends CastEnabledActivity implements
if (disposable != null) { if (disposable != null) {
disposable.dispose(); disposable.dispose();
} }
EventBus.getDefault().unregister(this);
super.onStop(); super.onStop();
} }
@ -669,16 +663,6 @@ public abstract class MediaplayerActivity extends CastEnabledActivity implements
StorageUtils.checkStorageAvailability(this); StorageUtils.checkStorageAvailability(this);
} }
@Subscribe(threadMode = ThreadMode.MAIN)
public void onEventMainThread(ServiceEvent event) {
Log.d(TAG, "onEvent(" + event + ")");
if (event.action == ServiceEvent.Action.SERVICE_STARTED) {
if (controller != null) {
controller.init();
}
}
}
/** /**
* Called by 'handleStatus()' when the PlaybackService is waiting for * Called by 'handleStatus()' when the PlaybackService is waiting for
* a video surface. * a video surface.

View File

@ -7,13 +7,9 @@ import android.widget.ListView;
import de.danoeh.antennapod.R; import de.danoeh.antennapod.R;
import de.danoeh.antennapod.adapter.ChaptersListAdapter; import de.danoeh.antennapod.adapter.ChaptersListAdapter;
import de.danoeh.antennapod.core.event.ServiceEvent;
import de.danoeh.antennapod.core.feed.Chapter; import de.danoeh.antennapod.core.feed.Chapter;
import de.danoeh.antennapod.core.util.playback.Playable; import de.danoeh.antennapod.core.util.playback.Playable;
import de.danoeh.antennapod.core.util.playback.PlaybackController; import de.danoeh.antennapod.core.util.playback.PlaybackController;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
public class ChaptersFragment extends ListFragment { public class ChaptersFragment extends ListFragment {
@ -58,24 +54,15 @@ public class ChaptersFragment extends ListFragment {
}; };
controller.init(); controller.init();
onMediaChanged(controller.getMedia()); onMediaChanged(controller.getMedia());
EventBus.getDefault().register(this);
} }
@Override @Override
public void onStop() { public void onStop() {
super.onStop(); super.onStop();
EventBus.getDefault().unregister(this);
controller.release(); controller.release();
controller = null; controller = null;
} }
@Subscribe(threadMode = ThreadMode.MAIN)
public void onEventMainThread(ServiceEvent event) {
if (event.action == ServiceEvent.Action.SERVICE_STARTED && controller != null) {
controller.init();
}
}
private void onMediaChanged(Playable media) { private void onMediaChanged(Playable media) {
if (adapter != null) { if (adapter != null) {
adapter.setMedia(media); adapter.setMedia(media);

View File

@ -13,13 +13,9 @@ import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions; import com.bumptech.glide.request.RequestOptions;
import de.danoeh.antennapod.R; import de.danoeh.antennapod.R;
import de.danoeh.antennapod.core.event.ServiceEvent;
import de.danoeh.antennapod.core.glide.ApGlideSettings; import de.danoeh.antennapod.core.glide.ApGlideSettings;
import de.danoeh.antennapod.core.util.playback.Playable; import de.danoeh.antennapod.core.util.playback.Playable;
import de.danoeh.antennapod.core.util.playback.PlaybackController; import de.danoeh.antennapod.core.util.playback.PlaybackController;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
/** /**
* Displays the cover and the title of a FeedItem. * Displays the cover and the title of a FeedItem.
@ -85,20 +81,11 @@ public class CoverFragment extends Fragment {
}; };
controller.init(); controller.init();
loadMediaInfo(); loadMediaInfo();
EventBus.getDefault().register(this);
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void onEventMainThread(ServiceEvent event) {
if (event.action == ServiceEvent.Action.SERVICE_STARTED && controller != null) {
controller.init();
}
} }
@Override @Override
public void onStop() { public void onStop() {
super.onStop(); super.onStop();
EventBus.getDefault().unregister(this);
controller.release(); controller.release();
controller = null; controller = null;
} }

View File

@ -18,7 +18,6 @@ import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions; import com.bumptech.glide.request.RequestOptions;
import de.danoeh.antennapod.R; import de.danoeh.antennapod.R;
import de.danoeh.antennapod.core.event.ServiceEvent;
import de.danoeh.antennapod.core.feed.MediaType; import de.danoeh.antennapod.core.feed.MediaType;
import de.danoeh.antennapod.core.glide.ApGlideSettings; import de.danoeh.antennapod.core.glide.ApGlideSettings;
import de.danoeh.antennapod.core.service.playback.PlaybackService; import de.danoeh.antennapod.core.service.playback.PlaybackService;
@ -28,9 +27,6 @@ import io.reactivex.Maybe;
import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable; import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers; import io.reactivex.schedulers.Schedulers;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
/** /**
* Fragment which is supposed to be displayed outside of the MediaplayerActivity * Fragment which is supposed to be displayed outside of the MediaplayerActivity
@ -94,14 +90,6 @@ public class ExternalPlayerFragment extends Fragment {
loadMediaInfo(); loadMediaInfo();
} }
@Subscribe(threadMode = ThreadMode.MAIN)
public void onEventMainThread(ServiceEvent event) {
Log.d(TAG, "onEvent(" + event + ")");
if (event.action == ServiceEvent.Action.SERVICE_STARTED) {
controller.init();
}
}
private PlaybackController setupPlaybackController() { private PlaybackController setupPlaybackController() {
return new PlaybackController(getActivity(), true) { return new PlaybackController(getActivity(), true) {
@ -147,7 +135,6 @@ public class ExternalPlayerFragment extends Fragment {
public void onStart() { public void onStart() {
super.onStart(); super.onStart();
controller.init(); controller.init();
EventBus.getDefault().register(this);
} }
@Override @Override
@ -156,7 +143,6 @@ public class ExternalPlayerFragment extends Fragment {
if (controller != null) { if (controller != null) {
controller.release(); controller.release();
} }
EventBus.getDefault().unregister(this);
} }
@Override @Override

View File

@ -28,7 +28,6 @@ import android.widget.Toast;
import de.danoeh.antennapod.R; import de.danoeh.antennapod.R;
import de.danoeh.antennapod.activity.MediaplayerInfoActivity; import de.danoeh.antennapod.activity.MediaplayerInfoActivity;
import de.danoeh.antennapod.core.event.ServiceEvent;
import de.danoeh.antennapod.core.preferences.UserPreferences; import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.util.Converter; import de.danoeh.antennapod.core.util.Converter;
import de.danoeh.antennapod.core.util.IntentUtils; import de.danoeh.antennapod.core.util.IntentUtils;
@ -40,9 +39,6 @@ import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable; import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers; import io.reactivex.schedulers.Schedulers;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
/** /**
* Displays the description of a Playable object in a Webview. * Displays the description of a Playable object in a Webview.
@ -312,20 +308,11 @@ public class ItemDescriptionFragment extends Fragment {
}; };
controller.init(); controller.init();
load(); load();
EventBus.getDefault().register(this);
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void onEventMainThread(ServiceEvent event) {
if (event.action == ServiceEvent.Action.SERVICE_STARTED && controller != null) {
controller.init();
}
} }
@Override @Override
public void onStop() { public void onStop() {
super.onStop(); super.onStop();
EventBus.getDefault().unregister(this);
controller.release(); controller.release();
controller = null; controller = null;
} }

View File

@ -25,6 +25,7 @@ import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import de.danoeh.antennapod.core.R; import de.danoeh.antennapod.core.R;
import de.danoeh.antennapod.core.event.ServiceEvent;
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;
@ -43,6 +44,9 @@ import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable; import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers; import io.reactivex.schedulers.Schedulers;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
/** /**
* Communicates with the playback service. GUI classes should use this class to * Communicates with the playback service. GUI classes should use this class to
@ -69,6 +73,7 @@ public abstract class PlaybackController {
private boolean mediaInfoLoaded = false; private boolean mediaInfoLoaded = false;
private boolean released = false; private boolean released = false;
private boolean initialized = false; private boolean initialized = false;
private boolean eventsRegistered = false;
private Disposable serviceBinder; private Disposable serviceBinder;
private Disposable mediaLoader; private Disposable mediaLoader;
@ -95,7 +100,11 @@ public abstract class PlaybackController {
/** /**
* Creates a new connection to the playbackService. * Creates a new connection to the playbackService.
*/ */
public void init() { public synchronized void init() {
if (!eventsRegistered) {
EventBus.getDefault().register(this);
eventsRegistered = true;
}
if (PlaybackService.isRunning) { if (PlaybackService.isRunning) {
initServiceRunning(); initServiceRunning();
} else { } else {
@ -103,6 +112,13 @@ public abstract class PlaybackController {
} }
} }
@Subscribe(threadMode = ThreadMode.MAIN)
public void onEventMainThread(ServiceEvent event) {
if (event.action == ServiceEvent.Action.SERVICE_STARTED) {
init();
}
}
private synchronized void initServiceRunning() { private synchronized void initServiceRunning() {
Log.v(TAG, "initServiceRunning()"); Log.v(TAG, "initServiceRunning()");
if (initialized) { if (initialized) {
@ -165,6 +181,10 @@ public abstract class PlaybackController {
media = null; media = null;
released = true; released = true;
if (eventsRegistered) {
EventBus.getDefault().unregister(this);
eventsRegistered = false;
}
} }
/** /**