Commit 3feab0cb4c25d7359c26cdfc09f19078e1e33dd8

Adding Shuffle and Repeat support
  
22 <item android:id="@+id/pl_clear" android:title="Clear" android:icon="@android:drawable/ic_menu_delete" />
33 <item android:id="@+id/pl_save" android:title="Save" android:icon="@android:drawable/ic_menu_save" />
44 <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" />
57</menu>
  
1414 <string name="summary_password_preference"></string>
1515 <string name="dialog_password_preference">password</string>
1616 <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>
1721</resources>
  
5050import java.io.ObjectOutputStream;
5151import java.io.FileOutputStream;
5252import java.util.ArrayList;
53import java.util.Random;
5354
5455public final class playlistActivity extends Activity implements MediaPlayerControl, OnBufferingUpdateListener, OnCompletionListener, OnItemClickListener
5556{
107107
108108 }
109109
110 // List for keeping track of Shuffle/Random
111 private ArrayList shuffleHistory = new ArrayList();
110112
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
111178 /* on pause and on resume make sure that we don't attempt to display the MediaController when
112179 * we can't see it */
113180 @Override
232232 }
233233 pla.refresh();
234234 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;
235272 }
236273 return true;
237274 }
370370
371371 public void onCompletion(MediaPlayer media) {
372372 turnOffPlayingView();
373 amdroid.playingIndex++;
373 nextInPlaylist();
374374 play();
375375 }
376376
388388 {
389389 public void onClick(View v) {
390390 turnOffPlayingView();
391 amdroid.playingIndex--;
391 prevInPlaylist();
392392 play();
393393 }
394394 }
397397 {
398398 public void onClick(View v) {
399399 turnOffPlayingView();
400 amdroid.playingIndex++;
400 nextInPlaylist();
401401 play();
402402 }
403403 }