move playback speed button methods inside AudioplayerActivity

This commit is contained in:
Domingos Lopes 2016-04-19 22:58:30 -04:00
parent 7164e328ef
commit 7caa840331
4 changed files with 139 additions and 107 deletions

View File

@ -1,13 +1,19 @@
package de.danoeh.antennapod.activity;
import android.content.Intent;
import android.support.v4.view.ViewCompat;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import java.util.concurrent.atomic.AtomicBoolean;
import de.danoeh.antennapod.core.cast.CastManager;
import de.danoeh.antennapod.core.feed.MediaType;
import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.service.playback.PlaybackService;
import de.danoeh.antennapod.core.util.playback.ExternalMedia;
import de.danoeh.antennapod.dialog.VariableSpeedDialog;
/**
* Activity for playing audio files.
@ -15,6 +21,8 @@ import de.danoeh.antennapod.core.util.playback.ExternalMedia;
public class AudioplayerActivity extends MediaplayerInfoActivity {
public static final String TAG = "AudioPlayerActivity";
private AtomicBoolean isSetup = new AtomicBoolean(false);
@Override
protected void onResume() {
super.onResume();
@ -50,4 +58,91 @@ public class AudioplayerActivity extends MediaplayerInfoActivity {
super.onReloadNotification(notificationCode);
}
}
@Override
protected void updatePlaybackSpeedButton() {
if(butPlaybackSpeed == null) {
return;
}
if (controller == null) {
butPlaybackSpeed.setVisibility(View.GONE);
return;
}
updatePlaybackSpeedButtonText();
ViewCompat.setAlpha(butPlaybackSpeed, controller.canSetPlaybackSpeed() ? 1.0f : 0.5f);
butPlaybackSpeed.setVisibility(View.VISIBLE);
}
@Override
protected void updatePlaybackSpeedButtonText() {
if(butPlaybackSpeed == null) {
return;
}
if (controller == null) {
butPlaybackSpeed.setVisibility(View.GONE);
return;
}
float speed = 1.0f;
if(controller.canSetPlaybackSpeed()) {
try {
// we can only retrieve the playback speed from the controller/playback service
// once mediaplayer has been initialized
speed = Float.parseFloat(UserPreferences.getPlaybackSpeed());
} catch (NumberFormatException e) {
Log.e(TAG, Log.getStackTraceString(e));
UserPreferences.setPlaybackSpeed(String.valueOf(speed));
}
}
String speedStr = String.format("%.2fx", speed);
butPlaybackSpeed.setText(speedStr);
}
@Override
protected void setupGUI() {
if(isSetup.getAndSet(true)) {
return;
}
super.setupGUI();
if(butPlaybackSpeed != null) {
butPlaybackSpeed.setOnClickListener(v -> {
if (controller == null) {
return;
}
if (controller.canSetPlaybackSpeed()) {
String[] availableSpeeds = UserPreferences.getPlaybackSpeedArray();
String currentSpeed = UserPreferences.getPlaybackSpeed();
// Provide initial value in case the speed list has changed
// out from under us
// and our current speed isn't in the new list
String newSpeed;
if (availableSpeeds.length > 0) {
newSpeed = availableSpeeds[0];
} else {
newSpeed = "1.00";
}
for (int i = 0; i < availableSpeeds.length; i++) {
if (availableSpeeds[i].equals(currentSpeed)) {
if (i == availableSpeeds.length - 1) {
newSpeed = availableSpeeds[0];
} else {
newSpeed = availableSpeeds[i + 1];
}
break;
}
}
UserPreferences.setPlaybackSpeed(newSpeed);
controller.setPlaybackSpeed(Float.parseFloat(newSpeed));
} else {
VariableSpeedDialog.showGetPluginDialog(this);
}
});
butPlaybackSpeed.setOnLongClickListener(v -> {
VariableSpeedDialog.showDialog(this);
return true;
});
butPlaybackSpeed.setVisibility(View.VISIBLE);
}
}
}

View File

@ -3,6 +3,9 @@ package de.danoeh.antennapod.activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import java.util.concurrent.atomic.AtomicBoolean;
import de.danoeh.antennapod.core.cast.CastManager;
import de.danoeh.antennapod.core.service.playback.PlaybackService;
@ -13,6 +16,8 @@ import de.danoeh.antennapod.core.service.playback.PlaybackService;
public class CastplayerActivity extends MediaplayerInfoActivity {
public static final String TAG = "CastPlayerActivity";
private AtomicBoolean isSetup = new AtomicBoolean(false);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -34,4 +39,15 @@ public class CastplayerActivity extends MediaplayerInfoActivity {
super.onReloadNotification(notificationCode);
}
}
@Override
protected void setupGUI() {
if(isSetup.getAndSet(true)) {
return;
}
super.setupGUI();
if (butPlaybackSpeed != null) {
butPlaybackSpeed.setVisibility(View.GONE);
}
}
}

View File

@ -9,7 +9,6 @@ import android.graphics.PixelFormat;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.view.ViewCompat;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.Menu;
@ -66,7 +65,6 @@ public abstract class MediaplayerActivity extends CastEnabledActivity implements
protected TextView txtvPosition;
protected TextView txtvLength;
protected SeekBar sbPosition;
protected Button butPlaybackSpeed;
protected ImageButton butRev;
protected TextView txtvRev;
protected ImageButton butPlay;
@ -641,41 +639,12 @@ public abstract class MediaplayerActivity extends CastEnabledActivity implements
}
}
private void updatePlaybackSpeedButton() {
if(butPlaybackSpeed == null) {
return;
}
// TODO this possibly needs to change if we introduce an alternative button
if (controller == null || !(this instanceof AudioplayerActivity)) {
butPlaybackSpeed.setVisibility(View.GONE);
return;
}
updatePlaybackSpeedButtonText();
ViewCompat.setAlpha(butPlaybackSpeed, controller.canSetPlaybackSpeed() ? 1.0f : 0.5f);
butPlaybackSpeed.setVisibility(View.VISIBLE);
protected void updatePlaybackSpeedButton() {
// Only meaningful on AudioplayerActivity, where it is overridden.
}
private void updatePlaybackSpeedButtonText() {
if(butPlaybackSpeed == null) {
return;
}
if (controller == null) {
butPlaybackSpeed.setVisibility(View.GONE);
return;
}
float speed = 1.0f;
if(controller.canSetPlaybackSpeed()) {
try {
// we can only retrieve the playback speed from the controller/playback service
// once mediaplayer has been initialized
speed = Float.parseFloat(UserPreferences.getPlaybackSpeed());
} catch (NumberFormatException e) {
Log.e(TAG, Log.getStackTraceString(e));
UserPreferences.setPlaybackSpeed(String.valueOf(speed));
}
}
String speedStr = String.format("%.2fx", speed);
butPlaybackSpeed.setText(speedStr);
protected void updatePlaybackSpeedButtonText() {
// Only meaningful on AudioplayerActivity, where it is overridden.
}
@ -688,6 +657,7 @@ public abstract class MediaplayerActivity extends CastEnabledActivity implements
showTimeLeft = prefs.getBoolean(PREF_SHOW_TIME_LEFT, false);
Log.d("timeleft", showTimeLeft ? "true" : "false");
txtvLength = (TextView) findViewById(R.id.txtvLength);
if (txtvLength != null) {
txtvLength.setOnClickListener(v -> {
showTimeLeft = !showTimeLeft;
Playable media = controller.getMedia();
@ -708,8 +678,8 @@ public abstract class MediaplayerActivity extends CastEnabledActivity implements
editor.apply();
Log.d("timeleft on click", showTimeLeft ? "true" : "false");
});
}
butPlaybackSpeed = (Button) findViewById(R.id.butPlaybackSpeed);
butRev = (ImageButton) findViewById(R.id.butRev);
txtvRev = (TextView) findViewById(R.id.txtvRev);
if (txtvRev != null) {
@ -729,53 +699,6 @@ public abstract class MediaplayerActivity extends CastEnabledActivity implements
// BUTTON SETUP
if(butPlaybackSpeed != null) {
if (this instanceof AudioplayerActivity) {
butPlaybackSpeed.setOnClickListener(v -> {
if (controller == null) {
return;
}
if (controller.canSetPlaybackSpeed()) {
String[] availableSpeeds = UserPreferences.getPlaybackSpeedArray();
String currentSpeed = UserPreferences.getPlaybackSpeed();
// Provide initial value in case the speed list has changed
// out from under us
// and our current speed isn't in the new list
String newSpeed;
if (availableSpeeds.length > 0) {
newSpeed = availableSpeeds[0];
} else {
newSpeed = "1.00";
}
for (int i = 0; i < availableSpeeds.length; i++) {
if (availableSpeeds[i].equals(currentSpeed)) {
if (i == availableSpeeds.length - 1) {
newSpeed = availableSpeeds[0];
} else {
newSpeed = availableSpeeds[i + 1];
}
break;
}
}
UserPreferences.setPlaybackSpeed(newSpeed);
controller.setPlaybackSpeed(Float.parseFloat(newSpeed));
} else {
VariableSpeedDialog.showGetPluginDialog(this);
}
});
butPlaybackSpeed.setOnLongClickListener(v -> {
VariableSpeedDialog.showDialog(this);
return true;
});
butPlaybackSpeed.setVisibility(View.VISIBLE);
} else {
// TODO in the future, perhaps replace it with some cast related button
butPlaybackSpeed.setVisibility(View.GONE);
}
}
if (butRev != null) {
butRev.setOnClickListener(v -> onRewind());
butRev.setOnLongClickListener(new View.OnLongClickListener() {

View File

@ -22,12 +22,12 @@ import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
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;
@ -84,8 +84,7 @@ public abstract class MediaplayerInfoActivity extends MediaplayerActivity implem
NavListAdapter.SUBSCRIPTION_LIST_TAG
};
private AtomicBoolean isSetup = new AtomicBoolean(false);
protected Button butPlaybackSpeed;
private DrawerLayout drawerLayout;
private NavListAdapter navAdapter;
private ListView navList;
@ -202,9 +201,6 @@ public abstract class MediaplayerInfoActivity extends MediaplayerActivity implem
@Override
protected void setupGUI() {
if(isSetup.getAndSet(true)) {
return;
}
super.setupGUI();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
@ -253,6 +249,8 @@ public abstract class MediaplayerInfoActivity extends MediaplayerActivity implem
startActivity(new Intent(MediaplayerInfoActivity.this, PreferenceController.getPreferenceActivity()));
});
butPlaybackSpeed = (Button) findViewById(R.id.butPlaybackSpeed);
pager = (ViewPager) findViewById(R.id.pager);
pagerAdapter = new MediaplayerInfoPagerAdapter(getSupportFragmentManager(), media);
pagerAdapter.setController(controller);