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 177c8af5a..9ccf833c9 100644 --- a/app/src/main/java/de/danoeh/antennapod/activity/AudioplayerActivity.java +++ b/app/src/main/java/de/danoeh/antennapod/activity/AudioplayerActivity.java @@ -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); + } + } } diff --git a/app/src/main/java/de/danoeh/antennapod/activity/CastplayerActivity.java b/app/src/main/java/de/danoeh/antennapod/activity/CastplayerActivity.java index ac24e421b..d6fe6de4a 100644 --- a/app/src/main/java/de/danoeh/antennapod/activity/CastplayerActivity.java +++ b/app/src/main/java/de/danoeh/antennapod/activity/CastplayerActivity.java @@ -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); + } + } } 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 7f70aa3ab..89e046ce7 100644 --- a/app/src/main/java/de/danoeh/antennapod/activity/MediaplayerActivity.java +++ b/app/src/main/java/de/danoeh/antennapod/activity/MediaplayerActivity.java @@ -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,28 +657,29 @@ 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); - txtvLength.setOnClickListener(v -> { - showTimeLeft = !showTimeLeft; - Playable media = controller.getMedia(); - if (media == null) { - return; - } + if (txtvLength != null) { + txtvLength.setOnClickListener(v -> { + showTimeLeft = !showTimeLeft; + Playable media = controller.getMedia(); + if (media == null) { + return; + } - String length; - if (showTimeLeft) { - length = "-" + Converter.getDurationStringLong(media.getDuration() - media.getPosition()); - } else { - length = Converter.getDurationStringLong(media.getDuration()); - } - txtvLength.setText(length); + String length; + if (showTimeLeft) { + length = "-" + Converter.getDurationStringLong(media.getDuration() - media.getPosition()); + } else { + length = Converter.getDurationStringLong(media.getDuration()); + } + txtvLength.setText(length); - SharedPreferences.Editor editor = prefs.edit(); - editor.putBoolean(PREF_SHOW_TIME_LEFT, showTimeLeft); - editor.apply(); - Log.d("timeleft on click", showTimeLeft ? "true" : "false"); - }); + SharedPreferences.Editor editor = prefs.edit(); + editor.putBoolean(PREF_SHOW_TIME_LEFT, showTimeLeft); + 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() { diff --git a/app/src/main/java/de/danoeh/antennapod/activity/MediaplayerInfoActivity.java b/app/src/main/java/de/danoeh/antennapod/activity/MediaplayerInfoActivity.java index 7a7897454..7e7bf1992 100644 --- a/app/src/main/java/de/danoeh/antennapod/activity/MediaplayerInfoActivity.java +++ b/app/src/main/java/de/danoeh/antennapod/activity/MediaplayerInfoActivity.java @@ -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);