From b1df272797ec82ae1e697f996c146ec9cde0d9ef Mon Sep 17 00:00:00 2001 From: Martin Fietz Date: Tue, 23 Feb 2016 08:26:56 +0100 Subject: [PATCH] Refactor --- .../activity/AudioplayerActivity.java | 34 ++++----- .../activity/MediaplayerActivity.java | 71 ++++++++----------- 2 files changed, 45 insertions(+), 60 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 8af6f3552..2dacb5121 100644 --- a/app/src/main/java/de/danoeh/antennapod/activity/AudioplayerActivity.java +++ b/app/src/main/java/de/danoeh/antennapod/activity/AudioplayerActivity.java @@ -94,8 +94,8 @@ public class AudioplayerActivity extends MediaplayerActivity implements NavDrawe private int mPosition = -1; private Playable media; - private ViewPager mPager; - private AudioplayerPagerAdapter mPagerAdapter; + private ViewPager pager; + private AudioplayerPagerAdapter pagerAdapter; private Subscription subscription; @@ -116,8 +116,8 @@ public class AudioplayerActivity extends MediaplayerActivity implements NavDrawe // don't risk creating memory leaks navAdapter = null; drawerToggle = null; - mPager = null; - mPagerAdapter = null; + pager = null; + pagerAdapter = null; } @Override @@ -126,13 +126,13 @@ public class AudioplayerActivity extends MediaplayerActivity implements NavDrawe } private void saveCurrentFragment() { - if(mPager == null) { + if(pager == null) { return; } Log.d(TAG, "Saving preferences"); SharedPreferences prefs = getSharedPreferences(PREFS, MODE_PRIVATE); prefs.edit() - .putInt(PREF_KEY_SELECTED_FRAGMENT_POSITION, mPager.getCurrentItem()) + .putInt(PREF_KEY_SELECTED_FRAGMENT_POSITION, pager.getCurrentItem()) .commit(); } @@ -146,7 +146,7 @@ public class AudioplayerActivity extends MediaplayerActivity implements NavDrawe Log.d(TAG, "Restoring instance state"); SharedPreferences prefs = getSharedPreferences(PREFS, MODE_PRIVATE); int lastPosition = prefs.getInt(PREF_KEY_SELECTED_FRAGMENT_POSITION, -1); - mPager.setCurrentItem(lastPosition); + pager.setCurrentItem(lastPosition); } @Override @@ -166,9 +166,9 @@ public class AudioplayerActivity extends MediaplayerActivity implements NavDrawe true); startService(launchIntent); } - if(mPagerAdapter != null && controller != null && controller.getMedia() != media) { + if(pagerAdapter != null && controller != null && controller.getMedia() != media) { media = controller.getMedia(); - mPagerAdapter.onMediaChanged(media); + pagerAdapter.onMediaChanged(media); } EventDistributor.getInstance().register(contentUpdate); @@ -255,13 +255,13 @@ public class AudioplayerActivity extends MediaplayerActivity implements NavDrawe startActivity(new Intent(AudioplayerActivity.this, PreferenceController.getPreferenceActivity())); }); - mPager = (ViewPager) findViewById(R.id.pager); - mPagerAdapter = new AudioplayerPagerAdapter(getSupportFragmentManager()); - mPager.setAdapter(mPagerAdapter); + pager = (ViewPager) findViewById(R.id.pager); + pagerAdapter = new AudioplayerPagerAdapter(getSupportFragmentManager()); + pager.setAdapter(pagerAdapter); CirclePageIndicator pageIndicator = (CirclePageIndicator) findViewById(R.id.page_indicator); - pageIndicator.setViewPager(mPager); + pageIndicator.setViewPager(pager); loadLastFragment(); - mPager.onSaveInstanceState(); + pager.onSaveInstanceState(); } @Override @@ -277,7 +277,7 @@ public class AudioplayerActivity extends MediaplayerActivity implements NavDrawe } if(controller.getMedia() != media) { media = controller.getMedia(); - mPagerAdapter.onMediaChanged(media); + pagerAdapter.onMediaChanged(media); } return true; } @@ -411,13 +411,13 @@ public class AudioplayerActivity extends MediaplayerActivity implements NavDrawe public void onBackPressed() { if(isDrawerOpen()) { drawerLayout.closeDrawer(navDrawer); - } else if (mPager.getCurrentItem() == 0) { + } else if (pager == null || pager.getCurrentItem() == 0) { // If the user is currently looking at the first step, allow the system to handle the // Back button. This calls finish() on this activity and pops the back stack. super.onBackPressed(); } else { // Otherwise, select the previous step. - mPager.setCurrentItem(mPager.getCurrentItem() - 1); + pager.setCurrentItem(pager.getCurrentItem() - 1); } } diff --git a/app/src/main/java/de/danoeh/antennapod/activity/MediaplayerActivity.java b/app/src/main/java/de/danoeh/antennapod/activity/MediaplayerActivity.java index e890f03b7..27212acd6 100644 --- a/app/src/main/java/de/danoeh/antennapod/activity/MediaplayerActivity.java +++ b/app/src/main/java/de/danoeh/antennapod/activity/MediaplayerActivity.java @@ -229,8 +229,7 @@ public abstract class MediaplayerActivity extends AppCompatActivity implements O protected void onBufferUpdate(float progress) { if (sbPosition != null) { - sbPosition.setSecondaryProgress((int) progress - * sbPosition.getMax()); + sbPosition.setSecondaryProgress((int) progress * sbPosition.getMax()); } } @@ -597,28 +596,24 @@ public abstract class MediaplayerActivity extends AppCompatActivity implements O protected abstract void clearStatusMsg(); protected void onPositionObserverUpdate() { - if (controller != null) { - int currentPosition = controller.getPosition(); - int duration = controller.getDuration(); - Log.d(TAG, "currentPosition " + Converter - .getDurationStringLong(currentPosition)); - if (currentPosition != PlaybackService.INVALID_TIME - && duration != PlaybackService.INVALID_TIME - && controller.getMedia() != null) { - txtvPosition.setText(Converter - .getDurationStringLong(currentPosition)); - if (showTimeLeft) { - txtvLength.setText("-" + Converter - .getDurationStringLong(duration - currentPosition)); - } else { - txtvLength.setText(Converter - .getDurationStringLong(duration)); - } - updateProgressbarPosition(currentPosition, duration); - } else { - Log.w(TAG, "Could not react to position observer update because of invalid time"); - } + if (controller == null || txtvPosition == null || txtvLength == null) { + return; } + int currentPosition = controller.getPosition(); + int duration = controller.getDuration(); + Log.d(TAG, "currentPosition " + Converter.getDurationStringLong(currentPosition)); + if (currentPosition == PlaybackService.INVALID_TIME || + duration == PlaybackService.INVALID_TIME) { + Log.w(TAG, "Could not react to position observer update because of invalid time"); + return; + } + txtvPosition.setText(Converter.getDurationStringLong(currentPosition)); + if (showTimeLeft) { + txtvLength.setText("-" + Converter.getDurationStringLong(duration - currentPosition)); + } else { + txtvLength.setText(Converter.getDurationStringLong(duration)); + } + updateProgressbarPosition(currentPosition, duration); } private void updateProgressbarPosition(int position, int duration) { @@ -639,17 +634,7 @@ public abstract class MediaplayerActivity extends AppCompatActivity implements O SharedPreferences prefs = getSharedPreferences(PREFS, MODE_PRIVATE); showTimeLeft = prefs.getBoolean(PREF_SHOW_TIME_LEFT, false); if (media != null) { - txtvPosition.setText(Converter.getDurationStringLong((media.getPosition()))); - - if (media.getDuration() != 0) { - txtvLength.setText(Converter.getDurationStringLong(media.getDuration())); - float progress = ((float) media.getPosition()) / media.getDuration(); - sbPosition.setProgress((int) (progress * sbPosition.getMax())); - if (showTimeLeft) { - int timeLeft = media.getDuration() - media.getPosition(); - txtvLength.setText("-" + Converter.getDurationStringLong(timeLeft)); - } - } + onPositionObserverUpdate(); checkFavorite(); if(butPlaybackSpeed != null) { if (controller == null) { @@ -857,8 +842,7 @@ public abstract class MediaplayerActivity extends AppCompatActivity implements O void handleError(int errorCode) { final AlertDialog.Builder errorDialog = new AlertDialog.Builder(this); errorDialog.setTitle(R.string.error_label); - errorDialog - .setMessage(MediaPlayerError.getErrorString(this, errorCode)); + errorDialog.setMessage(MediaPlayerError.getErrorString(this, errorCode)); errorDialog.setNeutralButton("OK", (dialog, which) -> { dialog.dismiss(); @@ -872,13 +856,14 @@ public abstract class MediaplayerActivity extends AppCompatActivity implements O @Override public void onProgressChanged (SeekBar seekBar,int progress, boolean fromUser) { - if (controller != null) { - prog = controller.onSeekBarProgressChanged(seekBar, progress, fromUser, txtvPosition); - if (showTimeLeft && prog != 0) { - int duration = controller.getDuration(); - String length = "-" + Converter.getDurationStringLong(duration - (int) (prog * duration)); - txtvLength.setText(length); - } + if (controller == null || txtvLength == null) { + return; + } + prog = controller.onSeekBarProgressChanged(seekBar, progress, fromUser, txtvPosition); + if (showTimeLeft && prog != 0) { + int duration = controller.getDuration(); + String length = "-" + Converter.getDurationStringLong(duration - (int) (prog * duration)); + txtvLength.setText(length); } }