This commit is contained in:
Martin Fietz 2016-02-23 08:26:56 +01:00
parent edc3c4c08d
commit b1df272797
2 changed files with 45 additions and 60 deletions

View File

@ -94,8 +94,8 @@ public class AudioplayerActivity extends MediaplayerActivity implements NavDrawe
private int mPosition = -1; private int mPosition = -1;
private Playable media; private Playable media;
private ViewPager mPager; private ViewPager pager;
private AudioplayerPagerAdapter mPagerAdapter; private AudioplayerPagerAdapter pagerAdapter;
private Subscription subscription; private Subscription subscription;
@ -116,8 +116,8 @@ public class AudioplayerActivity extends MediaplayerActivity implements NavDrawe
// don't risk creating memory leaks // don't risk creating memory leaks
navAdapter = null; navAdapter = null;
drawerToggle = null; drawerToggle = null;
mPager = null; pager = null;
mPagerAdapter = null; pagerAdapter = null;
} }
@Override @Override
@ -126,13 +126,13 @@ public class AudioplayerActivity extends MediaplayerActivity implements NavDrawe
} }
private void saveCurrentFragment() { private void saveCurrentFragment() {
if(mPager == null) { if(pager == null) {
return; return;
} }
Log.d(TAG, "Saving preferences"); Log.d(TAG, "Saving preferences");
SharedPreferences prefs = getSharedPreferences(PREFS, MODE_PRIVATE); SharedPreferences prefs = getSharedPreferences(PREFS, MODE_PRIVATE);
prefs.edit() prefs.edit()
.putInt(PREF_KEY_SELECTED_FRAGMENT_POSITION, mPager.getCurrentItem()) .putInt(PREF_KEY_SELECTED_FRAGMENT_POSITION, pager.getCurrentItem())
.commit(); .commit();
} }
@ -146,7 +146,7 @@ public class AudioplayerActivity extends MediaplayerActivity implements NavDrawe
Log.d(TAG, "Restoring instance state"); Log.d(TAG, "Restoring instance state");
SharedPreferences prefs = getSharedPreferences(PREFS, MODE_PRIVATE); SharedPreferences prefs = getSharedPreferences(PREFS, MODE_PRIVATE);
int lastPosition = prefs.getInt(PREF_KEY_SELECTED_FRAGMENT_POSITION, -1); int lastPosition = prefs.getInt(PREF_KEY_SELECTED_FRAGMENT_POSITION, -1);
mPager.setCurrentItem(lastPosition); pager.setCurrentItem(lastPosition);
} }
@Override @Override
@ -166,9 +166,9 @@ public class AudioplayerActivity extends MediaplayerActivity implements NavDrawe
true); true);
startService(launchIntent); startService(launchIntent);
} }
if(mPagerAdapter != null && controller != null && controller.getMedia() != media) { if(pagerAdapter != null && controller != null && controller.getMedia() != media) {
media = controller.getMedia(); media = controller.getMedia();
mPagerAdapter.onMediaChanged(media); pagerAdapter.onMediaChanged(media);
} }
EventDistributor.getInstance().register(contentUpdate); EventDistributor.getInstance().register(contentUpdate);
@ -255,13 +255,13 @@ public class AudioplayerActivity extends MediaplayerActivity implements NavDrawe
startActivity(new Intent(AudioplayerActivity.this, PreferenceController.getPreferenceActivity())); startActivity(new Intent(AudioplayerActivity.this, PreferenceController.getPreferenceActivity()));
}); });
mPager = (ViewPager) findViewById(R.id.pager); pager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new AudioplayerPagerAdapter(getSupportFragmentManager()); pagerAdapter = new AudioplayerPagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter); pager.setAdapter(pagerAdapter);
CirclePageIndicator pageIndicator = (CirclePageIndicator) findViewById(R.id.page_indicator); CirclePageIndicator pageIndicator = (CirclePageIndicator) findViewById(R.id.page_indicator);
pageIndicator.setViewPager(mPager); pageIndicator.setViewPager(pager);
loadLastFragment(); loadLastFragment();
mPager.onSaveInstanceState(); pager.onSaveInstanceState();
} }
@Override @Override
@ -277,7 +277,7 @@ public class AudioplayerActivity extends MediaplayerActivity implements NavDrawe
} }
if(controller.getMedia() != media) { if(controller.getMedia() != media) {
media = controller.getMedia(); media = controller.getMedia();
mPagerAdapter.onMediaChanged(media); pagerAdapter.onMediaChanged(media);
} }
return true; return true;
} }
@ -411,13 +411,13 @@ public class AudioplayerActivity extends MediaplayerActivity implements NavDrawe
public void onBackPressed() { public void onBackPressed() {
if(isDrawerOpen()) { if(isDrawerOpen()) {
drawerLayout.closeDrawer(navDrawer); 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 // 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. // Back button. This calls finish() on this activity and pops the back stack.
super.onBackPressed(); super.onBackPressed();
} else { } else {
// Otherwise, select the previous step. // Otherwise, select the previous step.
mPager.setCurrentItem(mPager.getCurrentItem() - 1); pager.setCurrentItem(pager.getCurrentItem() - 1);
} }
} }

View File

@ -229,8 +229,7 @@ public abstract class MediaplayerActivity extends AppCompatActivity implements O
protected void onBufferUpdate(float progress) { protected void onBufferUpdate(float progress) {
if (sbPosition != null) { if (sbPosition != null) {
sbPosition.setSecondaryProgress((int) progress sbPosition.setSecondaryProgress((int) progress * sbPosition.getMax());
* sbPosition.getMax());
} }
} }
@ -597,28 +596,24 @@ public abstract class MediaplayerActivity extends AppCompatActivity implements O
protected abstract void clearStatusMsg(); protected abstract void clearStatusMsg();
protected void onPositionObserverUpdate() { protected void onPositionObserverUpdate() {
if (controller != null) { if (controller == null || txtvPosition == null || txtvLength == null) {
int currentPosition = controller.getPosition(); return;
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");
}
} }
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) { 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); SharedPreferences prefs = getSharedPreferences(PREFS, MODE_PRIVATE);
showTimeLeft = prefs.getBoolean(PREF_SHOW_TIME_LEFT, false); showTimeLeft = prefs.getBoolean(PREF_SHOW_TIME_LEFT, false);
if (media != null) { if (media != null) {
txtvPosition.setText(Converter.getDurationStringLong((media.getPosition()))); onPositionObserverUpdate();
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));
}
}
checkFavorite(); checkFavorite();
if(butPlaybackSpeed != null) { if(butPlaybackSpeed != null) {
if (controller == null) { if (controller == null) {
@ -857,8 +842,7 @@ public abstract class MediaplayerActivity extends AppCompatActivity implements O
void handleError(int errorCode) { void handleError(int errorCode) {
final AlertDialog.Builder errorDialog = new AlertDialog.Builder(this); final AlertDialog.Builder errorDialog = new AlertDialog.Builder(this);
errorDialog.setTitle(R.string.error_label); errorDialog.setTitle(R.string.error_label);
errorDialog errorDialog.setMessage(MediaPlayerError.getErrorString(this, errorCode));
.setMessage(MediaPlayerError.getErrorString(this, errorCode));
errorDialog.setNeutralButton("OK", errorDialog.setNeutralButton("OK",
(dialog, which) -> { (dialog, which) -> {
dialog.dismiss(); dialog.dismiss();
@ -872,13 +856,14 @@ public abstract class MediaplayerActivity extends AppCompatActivity implements O
@Override @Override
public void onProgressChanged (SeekBar seekBar,int progress, boolean fromUser) { public void onProgressChanged (SeekBar seekBar,int progress, boolean fromUser) {
if (controller != null) { if (controller == null || txtvLength == null) {
prog = controller.onSeekBarProgressChanged(seekBar, progress, fromUser, txtvPosition); return;
if (showTimeLeft && prog != 0) { }
int duration = controller.getDuration(); prog = controller.onSeekBarProgressChanged(seekBar, progress, fromUser, txtvPosition);
String length = "-" + Converter.getDurationStringLong(duration - (int) (prog * duration)); if (showTimeLeft && prog != 0) {
txtvLength.setText(length); int duration = controller.getDuration();
} String length = "-" + Converter.getDurationStringLong(duration - (int) (prog * duration));
txtvLength.setText(length);
} }
} }