From 117572eb7c08d359ccfd05513f9d2dc88c1998bb Mon Sep 17 00:00:00 2001 From: Antic1tizen One Date: Sun, 2 Jul 2017 15:24:11 +0300 Subject: [PATCH] Jump to song in queue if picked just one (#624) * Bump gradle versions needed for build Android Studio already refuses to build it as-is, so alter the versions required. * Rename mode -> action for better readability * Fix incorrect description on getByPos from timeline * Jump to song in queue if picked one. Implements #604 --- build.gradle | 4 +-- gradle/wrapper/gradle-wrapper.properties | 4 +-- res/values/translatable.xml | 1 + .../android/vanilla/LibraryActivity.java | 35 +++++++++++++------ .../android/vanilla/PlaybackService.java | 26 ++++++++------ .../android/vanilla/SongTimeline.java | 28 +++++++++++---- 6 files changed, 65 insertions(+), 33 deletions(-) diff --git a/build.gradle b/build.gradle index a678c574..c9eb505a 100644 --- a/build.gradle +++ b/build.gradle @@ -4,7 +4,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:2.1.2' + classpath 'com.android.tools.build:gradle:2.3.3' } } @@ -23,7 +23,7 @@ android { } compileSdkVersion 25 - buildToolsVersion "25.0.2" + buildToolsVersion "25.0.3" defaultConfig { applicationId "ch.blinkenlights.android.vanilla" diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index e417aa9f..4ccb224b 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,7 +1,7 @@ -#Wed Jun 15 08:19:46 MSK 2016 +#Sun May 28 17:31:34 MSK 2017 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-2.14-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip distributionSha256Sum=993b4f33b652c689e9721917d8e021cab6bbd3eae81b39ab2fd46fdb19a928d5 diff --git a/res/values/translatable.xml b/res/values/translatable.xml index 4698dd77..a8034710 100644 --- a/res/values/translatable.xml +++ b/res/values/translatable.xml @@ -80,6 +80,7 @@ THE SOFTWARE. Enqueue if playing; Play if paused Open Home directory + Song is already in queue, jumping… 1 track playing. diff --git a/src/ch/blinkenlights/android/vanilla/LibraryActivity.java b/src/ch/blinkenlights/android/vanilla/LibraryActivity.java index 2985a846..3f1b7081 100644 --- a/src/ch/blinkenlights/android/vanilla/LibraryActivity.java +++ b/src/ch/blinkenlights/android/vanilla/LibraryActivity.java @@ -52,6 +52,7 @@ import android.widget.LinearLayout; import android.widget.ListView; import android.widget.TextView; import android.widget.SearchView; +import android.widget.Toast; import java.io.File; @@ -63,8 +64,8 @@ import junit.framework.Assert; public class LibraryActivity extends SlidingPlaybackActivity implements DialogInterface.OnClickListener - , DialogInterface.OnDismissListener - , SearchView.OnQueryTextListener + , DialogInterface.OnDismissListener + , SearchView.OnQueryTextListener { @@ -360,25 +361,37 @@ public class LibraryActivity } /** - * Adds songs matching the data from the given intent to the song timelime. + * Adds songs matching the data from the given intent to the song timeline. * * @param intent An intent created with * {@link LibraryAdapter#createData(View)}. * @param action One of LibraryActivity.ACTION_* */ - private void pickSongs(Intent intent, int action) + private void pickSongs(Intent intent, final int action) { + int effectiveAction = action; // mutable copy long id = intent.getLongExtra("id", LibraryAdapter.INVALID_ID); + int type = mCurrentAdapter.getMediaType(); + + // special handling if we pick one song to be played that is already in queue + boolean songPicked = (id >= 0 && type == MediaUtils.TYPE_SONG); // not invalid, not play all + if (songPicked && effectiveAction == ACTION_PLAY) { + int songPosInQueue = PlaybackService.get(this).getQueuePositionForSong(id); + if (songPosInQueue > -1) { + // we picked for play one song that is already present in the queue, just jump to it + PlaybackService.get(this).jumpToQueuePosition(songPosInQueue); + Toast.makeText(this, R.string.jumping_to_song, Toast.LENGTH_SHORT).show(); + return; + } + } boolean all = false; - int mode = action; if (action == ACTION_PLAY_ALL || action == ACTION_ENQUEUE_ALL) { - int type = mCurrentAdapter.getMediaType(); boolean notPlayAllAdapter = type > MediaUtils.TYPE_SONG || id == LibraryAdapter.HEADER_ID; - if (mode == ACTION_ENQUEUE_ALL && notPlayAllAdapter) { - mode = ACTION_ENQUEUE; - } else if (mode == ACTION_PLAY_ALL && notPlayAllAdapter) { - mode = ACTION_PLAY; + if (effectiveAction == ACTION_ENQUEUE_ALL && notPlayAllAdapter) { + effectiveAction = ACTION_ENQUEUE; + } else if (effectiveAction == ACTION_PLAY_ALL && notPlayAllAdapter) { + effectiveAction = ACTION_PLAY; } else { all = true; } @@ -388,7 +401,7 @@ public class LibraryActivity all = true; // page header was clicked -> force all mode QueryTask query = buildQueryFromIntent(intent, false, (all ? mCurrentAdapter : null)); - query.mode = modeForAction[mode]; + query.mode = modeForAction[effectiveAction]; PlaybackService.get(this).addSongs(query); if (mDefaultAction == ACTION_LAST_USED && mLastAction != action) { diff --git a/src/ch/blinkenlights/android/vanilla/PlaybackService.java b/src/ch/blinkenlights/android/vanilla/PlaybackService.java index 046eeceb..d83a7df4 100644 --- a/src/ch/blinkenlights/android/vanilla/PlaybackService.java +++ b/src/ch/blinkenlights/android/vanilla/PlaybackService.java @@ -56,7 +56,6 @@ import android.os.PowerManager; import android.os.Process; import android.os.SystemClock; import android.preference.PreferenceManager; -import android.provider.MediaStore; import android.util.Log; import android.view.View; import android.widget.RemoteViews; @@ -1909,8 +1908,6 @@ public final class PlaybackService extends Service /** * Add an Activity to the registered PlaybackActivities. - * - * @param activity The Activity to be added */ public static void addTimelineCallback(TimelineCallback consumer) { @@ -1919,8 +1916,6 @@ public final class PlaybackService extends Service /** * Remove an Activity from the registered PlaybackActivities - * - * @param activity The Activity to be removed */ public static void removeTimelineCallback(TimelineCallback consumer) { @@ -2140,7 +2135,7 @@ public final class PlaybackService extends Service synchronized (mStateLock) { if((mState & FLAG_PLAYING) != 0) { mTransientAudioLoss = true; - + if(type == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) { setFlag(FLAG_DUCKING); } else { @@ -2318,17 +2313,26 @@ public final class PlaybackService extends Service } /** - * Returns 'Song' with given id from timeline + * Returns {@link Song} with given position from timeline or null if nothing found */ - public Song getSongByQueuePosition(int id) { - return mTimeline.getSongByQueuePosition(id); + public Song getSongByQueuePosition(int pos) { + return mTimeline.getSongByQueuePosition(pos); + } + + /** + * Retrieve song position in timeline + * @param id song id as defined in {@link Song#id} + * @return absolute position in song timeline or -1 if song currently not in timeline + */ + public int getQueuePositionForSong(long id) { + return mTimeline.getQueuePositionForSong(id); } /** * Do a 'hard' jump to given queue position */ - public void jumpToQueuePosition(int id) { - mTimeline.setCurrentQueuePosition(id); + public void jumpToQueuePosition(int pos) { + mTimeline.setCurrentQueuePosition(pos); play(); } diff --git a/src/ch/blinkenlights/android/vanilla/SongTimeline.java b/src/ch/blinkenlights/android/vanilla/SongTimeline.java index a7c5569e..7131b859 100644 --- a/src/ch/blinkenlights/android/vanilla/SongTimeline.java +++ b/src/ch/blinkenlights/android/vanilla/SongTimeline.java @@ -368,7 +368,7 @@ public final class SongTimeline { cursor.close(); - // The query may have returned zero results or we might + // The query may have returned zero results or we might // have failed to populate some songs: Get rid of all // uninitialized items Iterator it = songs.iterator(); @@ -631,7 +631,7 @@ public final class SongTimeline { mCurrentPos = pos; } - + /** * Hard-Jump to given queue position */ @@ -644,19 +644,33 @@ public final class SongTimeline { changed(); return getSong(0); } - + /** * Returns 'Song' at given position in queue */ - public Song getSongByQueuePosition(int id) { + public Song getSongByQueuePosition(int pos) { Song song = null; synchronized (this) { - if (mSongs.size() > id) - song = mSongs.get(id); + if (mSongs.size() > pos) + song = mSongs.get(pos); } return song; } - + + /** + * Returns song position for given {@link Song} + */ + public int getQueuePositionForSong(long id) { + synchronized (this) { + for (int pos = 0; pos < mSongs.size(); pos++) { + Song current = mSongs.get(pos); + if (current.id == id) + return pos; + } + } + return -1; + } + /** * Move to the next or previous song or album. *