From e99ce0a33b83f4f127c9deb5ef5bba5855e726c1 Mon Sep 17 00:00:00 2001 From: ByteHamster Date: Sun, 12 May 2019 00:20:30 +0200 Subject: [PATCH] Do not query database on main thread --- .../antennapod/fragment/ChaptersFragment.java | 31 ++++++++++++++++--- .../antennapod/fragment/CoverFragment.java | 30 +++++++++++++----- 2 files changed, 49 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/de/danoeh/antennapod/fragment/ChaptersFragment.java b/app/src/main/java/de/danoeh/antennapod/fragment/ChaptersFragment.java index 8f2073ed0..998ff698e 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/ChaptersFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/ChaptersFragment.java @@ -2,6 +2,7 @@ package de.danoeh.antennapod.fragment; import android.os.Bundle; import android.support.v4.app.ListFragment; +import android.util.Log; import android.view.View; import android.widget.ListView; @@ -12,12 +13,17 @@ import de.danoeh.antennapod.core.feed.Chapter; import de.danoeh.antennapod.core.util.playback.Playable; import de.danoeh.antennapod.core.util.playback.PlaybackController; import de.greenrobot.event.EventBus; +import io.reactivex.Maybe; +import io.reactivex.android.schedulers.AndroidSchedulers; +import io.reactivex.disposables.Disposable; +import io.reactivex.schedulers.Schedulers; public class ChaptersFragment extends ListFragment { private static final String TAG = "ChaptersFragment"; private ChaptersListAdapter adapter; private PlaybackController controller; + private Disposable disposable; @Override @@ -42,10 +48,7 @@ public class ChaptersFragment extends ListFragment { controller = new PlaybackController(getActivity(), false) { @Override public boolean loadMediaInfo() { - if (getMedia() == null) { - return false; - } - onMediaChanged(getMedia()); + ChaptersFragment.this.loadMediaInfo(); return true; } @@ -55,7 +58,7 @@ public class ChaptersFragment extends ListFragment { } }; controller.init(); - onMediaChanged(controller.getMedia()); + loadMediaInfo(); EventBus.getDefault().register(this); } @@ -73,6 +76,24 @@ public class ChaptersFragment extends ListFragment { } } + private void loadMediaInfo() { + if (disposable != null) { + disposable.dispose(); + } + disposable = Maybe.create(emitter -> { + Playable media = controller.getMedia(); + if (media != null) { + emitter.onSuccess(media); + } else { + emitter.onComplete(); + } + }) + .subscribeOn(Schedulers.io()) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(media -> onMediaChanged((Playable) media), + error -> Log.e(TAG, Log.getStackTraceString(error))); + } + private void onMediaChanged(Playable media) { if (adapter != null) { adapter.setMedia(media); diff --git a/app/src/main/java/de/danoeh/antennapod/fragment/CoverFragment.java b/app/src/main/java/de/danoeh/antennapod/fragment/CoverFragment.java index 88d748c5a..87d58b1ff 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/CoverFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/CoverFragment.java @@ -1,6 +1,7 @@ package de.danoeh.antennapod.fragment; import android.os.Bundle; +import android.support.annotation.NonNull; import android.support.v4.app.Fragment; import android.util.Log; import android.view.LayoutInflater; @@ -18,6 +19,10 @@ import de.danoeh.antennapod.core.glide.ApGlideSettings; import de.danoeh.antennapod.core.util.playback.Playable; import de.danoeh.antennapod.core.util.playback.PlaybackController; import de.greenrobot.event.EventBus; +import io.reactivex.Maybe; +import io.reactivex.android.schedulers.AndroidSchedulers; +import io.reactivex.disposables.Disposable; +import io.reactivex.schedulers.Schedulers; /** * Displays the cover and the title of a FeedItem. @@ -31,6 +36,7 @@ public class CoverFragment extends Fragment { private TextView txtvEpisodeTitle; private ImageView imgvCover; private PlaybackController controller; + private Disposable disposable; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, @@ -44,11 +50,24 @@ public class CoverFragment extends Fragment { } private void loadMediaInfo() { - Playable media = controller.getMedia(); - if (media == null) { - Log.w(TAG, "loadMediaInfo was called while media was null"); - return; + if (disposable != null) { + disposable.dispose(); } + disposable = Maybe.create(emitter -> { + Playable media = controller.getMedia(); + if (media != null) { + emitter.onSuccess(media); + } else { + emitter.onComplete(); + } + }) + .subscribeOn(Schedulers.io()) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(media -> displayMediaInfo((Playable) media), + error -> Log.e(TAG, Log.getStackTraceString(error))); + } + + private void displayMediaInfo(@NonNull Playable media) { txtvPodcastTitle.setText(media.getFeedTitle()); txtvEpisodeTitle.setText(media.getEpisodeTitle()); Glide.with(this) @@ -73,9 +92,6 @@ public class CoverFragment extends Fragment { controller = new PlaybackController(getActivity(), false) { @Override public boolean loadMediaInfo() { - if (getMedia() == null) { - return false; - } CoverFragment.this.loadMediaInfo(); return true; }