diff options
6 files changed, 108 insertions, 39 deletions
diff --git a/app/src/main/java/de/danoeh/antennapod/activity/AudioplayerActivity.java b/app/src/main/java/de/danoeh/antennapod/activity/AudioplayerActivity.java index 50d557735..12bae2f51 100644 --- a/app/src/main/java/de/danoeh/antennapod/activity/AudioplayerActivity.java +++ b/app/src/main/java/de/danoeh/antennapod/activity/AudioplayerActivity.java @@ -6,11 +6,11 @@ import android.content.Intent; import android.content.SharedPreferences; import android.content.res.Configuration; import android.os.Build; +import android.support.annotation.Nullable; import android.support.design.widget.AppBarLayout; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentStatePagerAdapter; -import android.support.v4.app.ListFragment; import android.support.v4.view.ViewPager; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBarDrawerToggle; @@ -28,6 +28,7 @@ import android.widget.ListView; import com.viewpagerindicator.CirclePageIndicator; import java.util.List; +import java.util.concurrent.atomic.AtomicBoolean; import de.danoeh.antennapod.R; import de.danoeh.antennapod.adapter.ChaptersListAdapter; @@ -83,6 +84,8 @@ public class AudioplayerActivity extends MediaplayerActivity implements NavDrawe AddFeedFragment.TAG }; + private AtomicBoolean isSetup = new AtomicBoolean(false); + private DrawerLayout drawerLayout; private NavListAdapter navAdapter; private ListView navList; @@ -108,6 +111,16 @@ public class AudioplayerActivity extends MediaplayerActivity implements NavDrawe } @Override + public void onDestroy() { + super.onDestroy(); + // don't risk creating memory leaks + navAdapter = null; + drawerToggle = null; + mPager = null; + mPagerAdapter = null; + } + + @Override protected void chooseTheme() { setTheme(UserPreferences.getNoTitleTheme()); } @@ -116,7 +129,6 @@ public class AudioplayerActivity extends MediaplayerActivity implements NavDrawe if(mPager == null) { return; } - Log.d(TAG, "Saving preferences"); SharedPreferences prefs = getSharedPreferences(PREFS, MODE_PRIVATE); prefs.edit() @@ -156,10 +168,9 @@ public class AudioplayerActivity extends MediaplayerActivity implements NavDrawe } if(mPagerAdapter != null && controller != null && controller.getMedia() != media) { media = controller.getMedia(); - mPagerAdapter.notifyDataSetChanged(); + mPagerAdapter.onMediaChanged(media); } - EventDistributor.getInstance().register(contentUpdate); loadData(); } @@ -193,6 +204,9 @@ public class AudioplayerActivity extends MediaplayerActivity implements NavDrawe @Override protected void setupGUI() { + if(isSetup.getAndSet(true)) { + return; + } super.setupGUI(); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); @@ -242,13 +256,11 @@ public class AudioplayerActivity extends MediaplayerActivity implements NavDrawe }); mPager = (ViewPager) findViewById(R.id.pager); - if(mPager.getAdapter() == null) { - mPagerAdapter = new AudioplayerPagerAdapter(getSupportFragmentManager()); - mPager.setAdapter(mPagerAdapter); - CirclePageIndicator pageIndicator = (CirclePageIndicator) findViewById(R.id.page_indicator); - pageIndicator.setViewPager(mPager); - loadLastFragment(); - } + mPagerAdapter = new AudioplayerPagerAdapter(getSupportFragmentManager()); + mPager.setAdapter(mPagerAdapter); + CirclePageIndicator pageIndicator = (CirclePageIndicator) findViewById(R.id.page_indicator); + pageIndicator.setViewPager(mPager); + loadLastFragment(); mPager.onSaveInstanceState(); } @@ -265,16 +277,18 @@ public class AudioplayerActivity extends MediaplayerActivity implements NavDrawe } if(controller.getMedia() != media) { media = controller.getMedia(); - mPagerAdapter.notifyDataSetChanged(); + mPagerAdapter.onMediaChanged(media); } return true; } public void notifyMediaPositionChanged() { - ListFragment chapterFragment = (ListFragment) mPagerAdapter.getItem(POS_CHAPTERS); - ChaptersListAdapter adapter = (ChaptersListAdapter) chapterFragment.getListAdapter(); - if(adapter != null) { - adapter.notifyDataSetChanged(); + ChaptersFragment chaptersFragment = mPagerAdapter.getChaptersFragment(); + if(chaptersFragment != null) { + ChaptersListAdapter adapter = (ChaptersListAdapter) chaptersFragment.getListAdapter(); + if (adapter != null) { + adapter.notifyDataSetChanged(); + } } } @@ -510,7 +524,7 @@ public class AudioplayerActivity extends MediaplayerActivity implements NavDrawe }; public interface AudioplayerContentFragment { - void onDataSetChanged(Playable media); + void onMediaChanged(Playable media); } private class AudioplayerPagerAdapter extends FragmentStatePagerAdapter { @@ -519,22 +533,51 @@ public class AudioplayerActivity extends MediaplayerActivity implements NavDrawe super(fm); } + private CoverFragment coverFragment; + private ItemDescriptionFragment itemDescriptionFragment; + private ChaptersFragment chaptersFragment; + + public void onMediaChanged(Playable media) { + if(coverFragment != null) { + coverFragment.onMediaChanged(media); + } + if(itemDescriptionFragment != null) { + itemDescriptionFragment.onMediaChanged(media); + } + if(chaptersFragment != null) { + chaptersFragment.onMediaChanged(media); + } + } + + @Nullable + public ChaptersFragment getChaptersFragment() { + return chaptersFragment; + } + @Override public Fragment getItem(int position) { Log.d(TAG, "getItem(" + position + ")"); switch (position) { case POS_COVER: - return CoverFragment.newInstance(media); + if(coverFragment == null) { + coverFragment = CoverFragment.newInstance(media); + } + return coverFragment; case POS_DESCR: - return ItemDescriptionFragment.newInstance(media, true, true); + if(itemDescriptionFragment == null) { + itemDescriptionFragment = ItemDescriptionFragment.newInstance(media, true, true); + } + return itemDescriptionFragment; case POS_CHAPTERS: - return ChaptersFragment.newInstance(media, controller); + if(chaptersFragment == null) { + chaptersFragment = ChaptersFragment.newInstance(media, controller); + } + return chaptersFragment; default: return null; } } - @Override public int getCount() { return NUM_CONTENT_FRAGMENTS; diff --git a/app/src/main/java/de/danoeh/antennapod/activity/VideoplayerActivity.java b/app/src/main/java/de/danoeh/antennapod/activity/VideoplayerActivity.java index fbea102eb..ee459dbc6 100644 --- a/app/src/main/java/de/danoeh/antennapod/activity/VideoplayerActivity.java +++ b/app/src/main/java/de/danoeh/antennapod/activity/VideoplayerActivity.java @@ -20,6 +20,7 @@ import android.widget.ProgressBar; import android.widget.SeekBar; import java.lang.ref.WeakReference; +import java.util.concurrent.atomic.AtomicBoolean; import de.danoeh.antennapod.R; import de.danoeh.antennapod.core.feed.MediaType; @@ -43,6 +44,8 @@ public class VideoplayerActivity extends MediaplayerActivity { private VideoControlsHider videoControlsHider = new VideoControlsHider(this); + private AtomicBoolean isSetup = new AtomicBoolean(false); + private LinearLayout controls; private LinearLayout videoOverlay; private AspectRatioVideoView videoview; @@ -115,6 +118,9 @@ public class VideoplayerActivity extends MediaplayerActivity { @Override protected void setupGUI() { + if(isSetup.getAndSet(true)) { + return; + } super.setupGUI(); getSupportActionBar().setDisplayHomeAsUpEnabled(true); controls = (LinearLayout) findViewById(R.id.controls); 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 abc9f3d22..ce1d753e8 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/ChaptersFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/ChaptersFragment.java @@ -55,8 +55,17 @@ public class ChaptersFragment extends ListFragment implements AudioplayerContent } } + public void onDestroy() { + super.onDestroy(); + adapter = null; + } + @Override - public void onDataSetChanged(Playable media) { + public void onMediaChanged(Playable media) { + if(this.media == media || adapter == null) { + return; + } + this.media = media; adapter.setMedia(media); adapter.notifyDataSetChanged(); if(media.getChapters() == null) { 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 a3e9723ea..931d14924 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/CoverFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/CoverFragment.java @@ -19,8 +19,8 @@ import de.danoeh.antennapod.core.util.playback.Playable; /** * Displays the cover and the title of a FeedItem. */ -public class CoverFragment extends Fragment implements - AudioplayerContentFragment { +public class CoverFragment extends Fragment implements AudioplayerContentFragment { + private static final String TAG = "CoverFragment"; private static final String ARG_PLAYABLE = "arg.playable"; @@ -71,14 +71,12 @@ public class CoverFragment extends Fragment implements Log.d(TAG, "episode title: " + media.getEpisodeTitle()); txtvPodcastTitle.setText(media.getFeedTitle()); txtvEpisodeTitle.setText(media.getEpisodeTitle()); - imgvCover.post(() -> { - Glide.with(this) - .load(media.getImageUri()) - .diskCacheStrategy(ApGlideSettings.AP_DISK_CACHE_STRATEGY) - .dontAnimate() - .fitCenter() - .into(imgvCover); - }); + Glide.with(this) + .load(media.getImageUri()) + .diskCacheStrategy(ApGlideSettings.AP_DISK_CACHE_STRATEGY) + .dontAnimate() + .fitCenter() + .into(imgvCover); } else { Log.w(TAG, "loadMediaInfo was called while media was null"); } @@ -97,7 +95,17 @@ public class CoverFragment extends Fragment implements } @Override - public void onDataSetChanged(Playable media) { + public void onDestroy() { + super.onDestroy(); + // prevent memory leaks + root = null; + } + + @Override + public void onMediaChanged(Playable media) { + if(this.media == media) { + return; + } this.media = media; loadMediaInfo(); } diff --git a/app/src/main/java/de/danoeh/antennapod/fragment/ItemDescriptionFragment.java b/app/src/main/java/de/danoeh/antennapod/fragment/ItemDescriptionFragment.java index 9790e66b3..4c723e5ff 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/ItemDescriptionFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/ItemDescriptionFragment.java @@ -21,7 +21,7 @@ import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; -import android.webkit.WebSettings.LayoutAlgorithm; +import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.Toast; @@ -67,7 +67,6 @@ public class ItemDescriptionFragment extends Fragment implements AudioplayerCont private ShownotesProvider shownotesProvider; private Playable media; - private Subscription webViewLoader; /** @@ -124,7 +123,7 @@ public class ItemDescriptionFragment extends Fragment implements AudioplayerCont ta.recycle(); webvDescription.setBackgroundColor(backgroundColor); webvDescription.getSettings().setUseWideViewPort(false); - webvDescription.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN); + webvDescription.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN); webvDescription.getSettings().setLoadWithOverviewMode(true); webvDescription.setOnLongClickListener(webViewLongClickListener); webvDescription.setWebViewClient(new WebViewClient() { @@ -384,7 +383,10 @@ public class ItemDescriptionFragment extends Fragment implements AudioplayerCont } @Override - public void onDataSetChanged(Playable media) { + public void onMediaChanged(Playable media) { + if(this.media == media) { + return; + } this.media = media; this.shownotesProvider = media; load(); diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackServiceMediaPlayer.java b/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackServiceMediaPlayer.java index aa73b17a7..a82e82506 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackServiceMediaPlayer.java +++ b/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackServiceMediaPlayer.java @@ -24,7 +24,6 @@ import android.view.SurfaceHolder; import android.view.WindowManager; import com.bumptech.glide.Glide; -import com.bumptech.glide.request.target.Target; import java.io.IOException; import java.util.concurrent.CountDownLatch; @@ -982,6 +981,8 @@ public class PlaybackServiceMediaPlayer implements SharedPreferences.OnSharedPre playerLock.lock(); releaseWifiLockIfNecessary(); + boolean isPlaying = playerStatus == PlayerStatus.PLAYING; + if (playerStatus != PlayerStatus.INDETERMINATE) { setPlayerStatus(PlayerStatus.INDETERMINATE, media); } @@ -990,7 +991,7 @@ public class PlaybackServiceMediaPlayer implements SharedPreferences.OnSharedPre } audioManager.abandonAudioFocus(audioFocusChangeListener); - callback.endPlayback(true, wasSkipped); + callback.endPlayback(isPlaying, wasSkipped); playerLock.unlock(); }); |