Commit 3feab0cb4c25d7359c26cdfc09f19078e1e33dd8
- Diff rendering mode:
- inline
- side by side
res/menu/playlist_menu.xml
(2 / 0)
|   | |||
| 2 | 2 | <item android:id="@+id/pl_clear" android:title="Clear" android:icon="@android:drawable/ic_menu_delete" /> | |
| 3 | 3 | <item android:id="@+id/pl_save" android:title="Save" android:icon="@android:drawable/ic_menu_save" /> | |
| 4 | 4 | <item android:id="@+id/pl_load" android:title="Load" android:icon="@android:drawable/ic_menu_revert" /> | |
| 5 | <item android:id="@+id/pl_shuffle" android:title="Shuffle" android:checkable="true" android:checked="false" /> | ||
| 6 | <item android:id="@+id/pl_repeat" android:title="Repeat" android:checkable="true" android:checked="false" /> | ||
| 5 | 7 | </menu> |
res/values/strings.xml
(4 / 0)
|   | |||
| 14 | 14 | <string name="summary_password_preference"></string> | |
| 15 | 15 | <string name="dialog_password_preference">password</string> | |
| 16 | 16 | <string name="search_label">Songs</string> | |
| 17 | <string name="shuffle">Shuffle</string> | ||
| 18 | <string name="shuffle2">|Shuffle|</string> | ||
| 19 | <string name="repeat">Repeat</string> | ||
| 20 | <string name="repeat2">|Repeat|</string> | ||
| 17 | 21 | </resources> |
|   | |||
| 50 | 50 | import java.io.ObjectOutputStream; | |
| 51 | 51 | import java.io.FileOutputStream; | |
| 52 | 52 | import java.util.ArrayList; | |
| 53 | import java.util.Random; | ||
| 53 | 54 | ||
| 54 | 55 | public final class playlistActivity extends Activity implements MediaPlayerControl, OnBufferingUpdateListener, OnCompletionListener, OnItemClickListener | |
| 55 | 56 | { | |
| … | … | ||
| 107 | 107 | ||
| 108 | 108 | } | |
| 109 | 109 | ||
| 110 | // List for keeping track of Shuffle/Random | ||
| 111 | private ArrayList shuffleHistory = new ArrayList(); | ||
| 110 | 112 | ||
| 113 | private boolean shuffleEnabled = false; | ||
| 114 | private boolean repeatEnabled = false; | ||
| 115 | |||
| 116 | private void nextInPlaylist() { | ||
| 117 | if ( shuffleEnabled ) { | ||
| 118 | // So we don't play a song more than once | ||
| 119 | if ( !shuffleHistory.contains( amdroid.playingIndex ) ) | ||
| 120 | shuffleHistory.add( amdroid.playingIndex ); | ||
| 121 | |||
| 122 | // Just played the last song, repeat if repeat is enabled, stop otherwise | ||
| 123 | if ( shuffleHistory.size() >= amdroid.playlistCurrent.size() && repeatEnabled ) | ||
| 124 | shuffleHistory.clear(); | ||
| 125 | else | ||
| 126 | amdroid.playingIndex = amdroid.playlistCurrent.size(); | ||
| 127 | |||
| 128 | int next = 0; | ||
| 129 | Random rand = new Random(); | ||
| 130 | |||
| 131 | // Try random numbers until finding one that is not used | ||
| 132 | do { | ||
| 133 | next = rand.nextInt( amdroid.playlistCurrent.size() ); | ||
| 134 | } while ( shuffleHistory.contains( next ) ); | ||
| 135 | |||
| 136 | // Set next playing index | ||
| 137 | amdroid.playingIndex = next; | ||
| 138 | } else { | ||
| 139 | amdroid.playingIndex++; | ||
| 140 | |||
| 141 | // Reset playlist to beginning if repeat is enabled | ||
| 142 | if ( amdroid.playingIndex >= amdroid.playlistCurrent.size() && repeatEnabled ) | ||
| 143 | amdroid.playingIndex = 0; | ||
| 144 | } | ||
| 145 | } | ||
| 146 | |||
| 147 | private void prevInPlaylist() { | ||
| 148 | if ( shuffleEnabled ) { | ||
| 149 | int currIndex = shuffleHistory.indexOf( amdroid.playingIndex ); | ||
| 150 | |||
| 151 | // Call a random next song if this is the first song | ||
| 152 | if ( shuffleHistory.size() < 1 ) { | ||
| 153 | nextInPlaylist(); | ||
| 154 | |||
| 155 | return; | ||
| 156 | } | ||
| 157 | |||
| 158 | // Previous (Current item is not in the shuffle history) | ||
| 159 | if ( currIndex == -1 ) { | ||
| 160 | // Set previous song | ||
| 161 | amdroid.playingIndex = (Integer)shuffleHistory.get( shuffleHistory.size() - 1 ); | ||
| 162 | |||
| 163 | // Remove item, I consider Previous like an undo | ||
| 164 | shuffleHistory.remove( shuffleHistory.size() - 1 ); | ||
| 165 | } | ||
| 166 | // This shouldn't be possible, but... | ||
| 167 | else if ( currIndex > 0 ) { | ||
| 168 | amdroid.playingIndex = (Integer)shuffleHistory.get( currIndex - 1 ); | ||
| 169 | |||
| 170 | shuffleHistory.remove( currIndex ); | ||
| 171 | } | ||
| 172 | } | ||
| 173 | // Do not call previous if it is the first song | ||
| 174 | else if ( amdroid.playingIndex > 0 ) | ||
| 175 | amdroid.playingIndex--; | ||
| 176 | } | ||
| 177 | |||
| 111 | 178 | /* on pause and on resume make sure that we don't attempt to display the MediaController when | |
| 112 | 179 | * we can't see it */ | |
| 113 | 180 | @Override | |
| … | … | ||
| 232 | 232 | } | |
| 233 | 233 | pla.refresh(); | |
| 234 | 234 | break; | |
| 235 | |||
| 236 | case R.id.pl_shuffle: | ||
| 237 | if ( item.isChecked() ) { | ||
| 238 | // Clean Shuffle History | ||
| 239 | shuffleHistory.clear(); | ||
| 240 | |||
| 241 | item.setChecked( false ); | ||
| 242 | item.setTitle( R.string.shuffle ); | ||
| 243 | |||
| 244 | // Disable Shuffle | ||
| 245 | shuffleEnabled = false; | ||
| 246 | } else { | ||
| 247 | item.setChecked( true ); | ||
| 248 | item.setTitle( R.string.shuffle2 ); | ||
| 249 | |||
| 250 | // Enable Shuffle | ||
| 251 | shuffleEnabled = true; | ||
| 252 | } | ||
| 253 | |||
| 254 | break; | ||
| 255 | |||
| 256 | case R.id.pl_repeat: | ||
| 257 | if ( item.isChecked() ) { | ||
| 258 | item.setChecked( false ); | ||
| 259 | item.setTitle( R.string.repeat ); | ||
| 260 | |||
| 261 | // Disable Repeat | ||
| 262 | repeatEnabled = false; | ||
| 263 | } else { | ||
| 264 | item.setChecked( true ); | ||
| 265 | item.setTitle( R.string.repeat2 ); | ||
| 266 | |||
| 267 | // Enable Repeat | ||
| 268 | repeatEnabled = true; | ||
| 269 | } | ||
| 270 | |||
| 271 | break; | ||
| 235 | 272 | } | |
| 236 | 273 | return true; | |
| 237 | 274 | } | |
| … | … | ||
| 370 | 370 | ||
| 371 | 371 | public void onCompletion(MediaPlayer media) { | |
| 372 | 372 | turnOffPlayingView(); | |
| 373 | amdroid.playingIndex++; | ||
| 373 | nextInPlaylist(); | ||
| 374 | 374 | play(); | |
| 375 | 375 | } | |
| 376 | 376 | ||
| … | … | ||
| 388 | 388 | { | |
| 389 | 389 | public void onClick(View v) { | |
| 390 | 390 | turnOffPlayingView(); | |
| 391 | amdroid.playingIndex--; | ||
| 391 | prevInPlaylist(); | ||
| 392 | 392 | play(); | |
| 393 | 393 | } | |
| 394 | 394 | } | |
| … | … | ||
| 397 | 397 | { | |
| 398 | 398 | public void onClick(View v) { | |
| 399 | 399 | turnOffPlayingView(); | |
| 400 | amdroid.playingIndex++; | ||
| 400 | nextInPlaylist(); | ||
| 401 | 401 | play(); | |
| 402 | 402 | } | |
| 403 | 403 | } |

