From bb221e09ffe719ecd2d1c41aeba492ac2adf6151 Mon Sep 17 00:00:00 2001 From: Marcus Date: Wed, 12 Apr 2017 09:19:26 -0700 Subject: Enhanced Playback speed UI using shehabic/Droppy library and persistence of speed settings between songs and non-songs. --- .../dsub/fragments/NowPlayingFragment.java | 137 ++++++++------------- .../daneren2005/dsub/service/DownloadService.java | 7 +- .../github/daneren2005/dsub/util/Constants.java | 2 +- .../daneren2005/dsub/util/DroppySpeedControl.java | 82 ++++++++++++ app/src/main/res/layout/set_playback_speed.xml | 62 ++++++++-- app/src/main/res/menu/playback_speed_options.xml | 26 ---- 6 files changed, 194 insertions(+), 122 deletions(-) create mode 100644 app/src/main/java/github/daneren2005/dsub/util/DroppySpeedControl.java delete mode 100644 app/src/main/res/menu/playback_speed_options.xml (limited to 'app/src') diff --git a/app/src/main/java/github/daneren2005/dsub/fragments/NowPlayingFragment.java b/app/src/main/java/github/daneren2005/dsub/fragments/NowPlayingFragment.java index bf91bd1a..19df9bfb 100644 --- a/app/src/main/java/github/daneren2005/dsub/fragments/NowPlayingFragment.java +++ b/app/src/main/java/github/daneren2005/dsub/fragments/NowPlayingFragment.java @@ -55,6 +55,9 @@ import android.widget.PopupMenu; import android.widget.SeekBar; import android.widget.TextView; import android.widget.ViewFlipper; +import com.shehabic.droppy.DroppyClickCallbackInterface; +import com.shehabic.droppy.DroppyMenuPopup; +import com.shehabic.droppy.animations.DroppyFadeInAnimation; import github.daneren2005.dsub.R; import github.daneren2005.dsub.activity.SubsonicFragmentActivity; import github.daneren2005.dsub.adapter.SectionAdapter; @@ -132,6 +135,7 @@ public class NowPlayingFragment extends SubsonicFragment implements OnGestureLis private int lastY = 0; private int currentPlayingSize = 0; private MenuItem timerMenu; + private DroppySpeedControl speed; /** * Called when the activity is first created. @@ -391,41 +395,7 @@ public class NowPlayingFragment extends SubsonicFragment implements OnGestureLis }); if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - playbackSpeedButton.setOnClickListener(new OnClickListener() { - @Override - public void onClick(View v) { - PopupMenu popup = new PopupMenu(context, v); - popup.getMenuInflater().inflate(R.menu.playback_speed_options, popup.getMenu()); - - popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { - @Override - public boolean onMenuItemClick(MenuItem menuItem) { - float playbackSpeed = 1.0f; - switch (menuItem.getItemId()) { - case R.id.playback_speed_half: - playbackSpeed = 0.5f; - break; - case R.id.playback_speed_one_half: - playbackSpeed = 1.5f; - break; - case R.id.playback_speed_double: - playbackSpeed = 2.0f; - break; - case R.id.playback_speed_tripple: - playbackSpeed = 3.0f; - break; - case R.id.playback_speed_custom: - setPlaybackSpeed(); - return true; - } - - setPlaybackSpeed(playbackSpeed); - return true; - } - }); - popup.show(); - } - }); + setPlaybackSpeed(); } else { playbackSpeedButton.setVisibility(View.GONE); } @@ -1277,6 +1247,7 @@ public class NowPlayingFragment extends SubsonicFragment implements OnGestureLis } } updateTitle(); + setPlaybackSpeed(); } private void setupSubtitle(int currentPlayingIndex) { @@ -1345,6 +1316,7 @@ public class NowPlayingFragment extends SubsonicFragment implements OnGestureLis toggleListButton.setVisibility(View.VISIBLE); repeatButton.setVisibility(View.VISIBLE); } + setPlaybackSpeed(); } @Override @@ -1546,55 +1518,52 @@ public class NowPlayingFragment extends SubsonicFragment implements OnGestureLis } private void setPlaybackSpeed() { - View dialogView = context.getLayoutInflater().inflate(R.layout.set_playback_speed, null); - - // Setup playbackSpeed label - final TextView playbackSpeedBox = (TextView) dialogView.findViewById(R.id.playback_speed_label); - final SharedPreferences prefs = Util.getPreferences(context); - String playbackSpeedString = prefs.getString(Constants.PREFERENCES_KEY_CUSTOM_PLAYBACK_SPEED, "17"); - int playbackSpeed = Integer.parseInt(playbackSpeedString); - playbackSpeedBox.setText(new Float(playbackSpeed / 10.0).toString()); - - // Setup playbackSpeed slider - final SeekBar playbackSpeedBar = (SeekBar) dialogView.findViewById(R.id.playback_speed_bar); - playbackSpeedBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { - @Override - public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { - if (fromUser) { - playbackSpeedBox.setText(new Float((progress + 5) / 10.0).toString()); - seekBar.setProgress(progress); - } - } - - @Override - public void onStartTrackingTouch(SeekBar seekBar) { - } - - @Override - public void onStopTrackingTouch(SeekBar seekBar) { - } - }); - playbackSpeedBar.setProgress(playbackSpeed - 5); - - AlertDialog.Builder builder = new AlertDialog.Builder(context); - builder.setTitle(R.string.download_playback_speed_custom) - .setView(dialogView) - .setPositiveButton(R.string.common_ok, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int id) { - int playbackSpeed = playbackSpeedBar.getProgress() + 5; - - SharedPreferences.Editor editor = prefs.edit(); - editor.putString(Constants.PREFERENCES_KEY_CUSTOM_PLAYBACK_SPEED, Integer.toString(playbackSpeed)); - editor.commit(); - - setPlaybackSpeed(new Float(playbackSpeed / 10.0)); - } - }) - .setNegativeButton(R.string.common_cancel, null); - AlertDialog dialog = builder.create(); - dialog.show(); - } + if (playbackSpeedButton.getVisibility() == View.GONE) + return; + speed = new DroppySpeedControl(R.layout.set_playback_speed); + DroppyMenuPopup.Builder builder = new DroppyMenuPopup.Builder(context,playbackSpeedButton); + speed.setClickable(true); + float playbackSpeed; + + playbackSpeed = getDownloadService() != null ? getDownloadService().getPlaybackSpeed() : 1.0f; + + final DroppyMenuPopup popup = builder.triggerOnAnchorClick(true).addMenuItem(speed).setPopupAnimation(new DroppyFadeInAnimation()).build(); + speed.setOnSeekBarChangeListener(context, new DroppyClickCallbackInterface() { + @Override + public void call(View v, int id) { + SeekBar playbackSpeedBar = (SeekBar) v; + int playbackSpeed = playbackSpeedBar.getProgress() +5 ; + setPlaybackSpeed(playbackSpeed/10f); + } + },R.id.playback_speed_bar,R.id.playback_speed_label,playbackSpeed); + speed.setOnClicks(context, + new DroppyClickCallbackInterface() { + @Override + public void call(View v, int id) { + float playbackSpeed = 1.0f; + switch (id) { + case R.id.playback_speed_one_half: + playbackSpeed = 1.5f; + break; + case R.id.playback_speed_double: + playbackSpeed = 2.0f; + break; + case R.id.playback_speed_triple: + playbackSpeed = 3.0f; + break; + default: + break; + } + setPlaybackSpeed(playbackSpeed); + speed.updateSeekBar(playbackSpeed); + popup.dismiss(true); + } + } + ,R.id.playback_speed_normal,R.id.playback_speed_one_half,R.id.playback_speed_double, + R.id.playback_speed_triple); + speed.updateSeekBar(playbackSpeed); + + } private void setPlaybackSpeed(float playbackSpeed) { DownloadService downloadService = getDownloadService(); if (downloadService == null) { diff --git a/app/src/main/java/github/daneren2005/dsub/service/DownloadService.java b/app/src/main/java/github/daneren2005/dsub/service/DownloadService.java index 6d85d128..77ef971c 100644 --- a/app/src/main/java/github/daneren2005/dsub/service/DownloadService.java +++ b/app/src/main/java/github/daneren2005/dsub/service/DownloadService.java @@ -873,7 +873,7 @@ public class DownloadService extends Service { this.currentPlaying.setPlaying(false); } if(delayUpdateProgress != DEFAULT_DELAY_UPDATE_PROGRESS && !isNextPlayingSameAlbum(currentPlaying, this.currentPlaying)) { - resetPlaybackSpeed(); +// resetPlaybackSpeed(); } this.currentPlaying = currentPlaying; if(currentPlaying == null) { @@ -2657,7 +2657,10 @@ public class DownloadService extends Service { } public void setPlaybackSpeed(float playbackSpeed) { - Util.getPreferences(this).edit().putFloat(Constants.PREFERENCES_KEY_PLAYBACK_SPEED, playbackSpeed).commit(); + if(currentPlaying.isSong()) + Util.getPreferences(this).edit().putFloat(Constants.PREFERENCES_KEY_SONG_PLAYBACK_SPEED, playbackSpeed).commit(); + else + Util.getPreferences(this).edit().putFloat(Constants.PREFERENCES_KEY_PLAYBACK_SPEED, playbackSpeed).commit(); if(mediaPlayer != null && (playerState == PREPARED || playerState == STARTED || playerState == PAUSED || playerState == PAUSED_TEMP)) { applyPlaybackParamsMain(); } diff --git a/app/src/main/java/github/daneren2005/dsub/util/Constants.java b/app/src/main/java/github/daneren2005/dsub/util/Constants.java index b62729ab..d692f4d2 100644 --- a/app/src/main/java/github/daneren2005/dsub/util/Constants.java +++ b/app/src/main/java/github/daneren2005/dsub/util/Constants.java @@ -177,7 +177,7 @@ public final class Constants { public static final String PREFERENCES_KEY_HEADS_UP_NOTIFICATION = "headsUpNotification"; public static final String PREFERENCES_KEY_CAST_CACHE = "castCache"; public static final String PREFERENCES_KEY_PLAYBACK_SPEED = "playbackSpeed"; - public static final String PREFERENCES_KEY_CUSTOM_PLAYBACK_SPEED = "customPlaybackSpeed"; + public static final String PREFERENCES_KEY_SONG_PLAYBACK_SPEED = "songPlaybackSpeed"; public static final String OFFLINE_SCROBBLE_COUNT = "scrobbleCount"; public static final String OFFLINE_SCROBBLE_ID = "scrobbleID"; diff --git a/app/src/main/java/github/daneren2005/dsub/util/DroppySpeedControl.java b/app/src/main/java/github/daneren2005/dsub/util/DroppySpeedControl.java new file mode 100644 index 00000000..8f58e60e --- /dev/null +++ b/app/src/main/java/github/daneren2005/dsub/util/DroppySpeedControl.java @@ -0,0 +1,82 @@ +package github.daneren2005.dsub.util; + +import android.content.Context; +import android.content.res.Resources; +import android.view.View; +import android.widget.SeekBar; +import android.widget.TextView; +import com.shehabic.droppy.DroppyClickCallbackInterface; +import com.shehabic.droppy.DroppyMenuCustomItem; + +/** + * Created by marcus on 2/14/2017. + */ +public class DroppySpeedControl extends DroppyMenuCustomItem { + + private Context context; + private SeekBar seekBar; + private DroppyClickCallbackInterface updateBarCallback; + public DroppySpeedControl(int customResourceId) { + super(customResourceId); + + } + + @Override + public View render(Context context) { + return super.render(context); + + + } + + public DroppySpeedControl setOnClicks(Context context, final DroppyClickCallbackInterface callback, int ... elementsByID){ + render(context); + View.OnClickListener listener = new View.OnClickListener() { + @Override + public void onClick(View v) { + callback.call(v, v.getId()); + } + }; + for (Integer element : elementsByID) { + renderedView.findViewById(element).setOnClickListener(listener); + } + return this; + } + + + public void updateSeekBar(float playbackSpeed){ + TextView tv = (TextView)seekBar.getTag(); + tv.setText(Float.toString(playbackSpeed)); + seekBar.setProgress((int)(playbackSpeed*10)-5); + } + + public DroppySpeedControl setOnSeekBarChangeListener(Context context, final DroppyClickCallbackInterface callback, int seekBarByID, int textViewByID, float playbackSpeed) { + updateBarCallback = callback; + render(context); + final TextView textBox = (TextView) renderedView.findViewById(textViewByID); + textBox.setText(Float.toString(playbackSpeed)); + SeekBar seekBar = ((SeekBar) renderedView.findViewById(seekBarByID)); + this.seekBar = seekBar; + seekBar.setTag(textBox); + seekBar.setProgress((int)(playbackSpeed*10)-5); + seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { + @Override + public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { + if (fromUser) { + textBox.setText(new Float((progress + 5) / 10.0).toString()); + seekBar.setProgress(progress); + callback.call(seekBar,seekBar.getId()); + } + } + + @Override + public void onStartTrackingTouch(SeekBar seekBar) { + } + + @Override + public void onStopTrackingTouch(SeekBar seekBar) { + } + }); + seekBar.setProgress((int)((playbackSpeed/10.0) - 5)); + return this; + } +} diff --git a/app/src/main/res/layout/set_playback_speed.xml b/app/src/main/res/layout/set_playback_speed.xml index 42f23a35..c85719bb 100644 --- a/app/src/main/res/layout/set_playback_speed.xml +++ b/app/src/main/res/layout/set_playback_speed.xml @@ -1,8 +1,48 @@ - + + + + + +