PageRenderTime 38ms CodeModel.GetById 8ms app.highlight 25ms RepoModel.GetById 1ms app.codeStats 0ms

/documentation/RockLockTutorial/RockLock_02/src/com/marvin/rocklock/TagStructuredSongPicker.java

http://eyes-free.googlecode.com/
Java | 435 lines | 382 code | 32 blank | 21 comment | 94 complexity | 794ae78d14fc0457d86e8d47e83b57a5 MD5 | raw file
  1/*
  2 * Copyright (C) 2010 Google Inc.
  3 * 
  4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5 * use this file except in compliance with the License. You may obtain a copy of
  6 * the License at
  7 * 
  8 * http://www.apache.org/licenses/LICENSE-2.0
  9 * 
 10 * Unless required by applicable law or agreed to in writing, software
 11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 13 * License for the specific language governing permissions and limitations under
 14 * the License.
 15 */
 16
 17package com.marvin.rocklock;
 18
 19import android.app.Activity;
 20import android.content.SharedPreferences;
 21import android.content.SharedPreferences.Editor;
 22import android.database.Cursor;
 23import android.preference.PreferenceManager;
 24import android.provider.MediaStore;
 25import android.provider.MediaStore.MediaColumns;
 26import android.provider.MediaStore.Audio.AudioColumns;
 27import android.util.Log;
 28
 29/**
 30 * SongPicker implementation that uses the tags in the music files to organize
 31 * the music.
 32 * 
 33 * @author clchen@google.com (Charles L. Chen)
 34 */
 35public class TagStructuredSongPicker implements SongPicker {
 36    private static final String PREF_ARTIST = "TAG_ARTIST";
 37
 38    private static final String PREF_ALBUM = "TAG_ALBUM";
 39
 40    private static final String PREF_TRACK = "TAG_TRACK";
 41
 42    private static final int ARTIST = 0;
 43
 44    private static final int ALBUM = 1;
 45
 46    private static final int TRACK = 2;
 47
 48    private static final int FILEPATH = 3;
 49
 50    private Cursor musicCursor;
 51
 52    private String currentArtist = "";
 53
 54    private String currentAlbum = "";
 55
 56    private String currentTrack = "";
 57
 58    private Editor editor;
 59
 60    private boolean taggedMusicAvailable = true;
 61
 62    public TagStructuredSongPicker(Activity parentActivity) {
 63        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(parentActivity);
 64        editor = prefs.edit();
 65
 66        String[] proj = {
 67                AudioColumns.ARTIST, AudioColumns.ALBUM, MediaColumns.TITLE, MediaColumns.DATA
 68        };
 69        taggedMusicAvailable = true;
 70        musicCursor = parentActivity.managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
 71                proj, null, null, null);
 72        if (musicCursor == null) {
 73            taggedMusicAvailable = false;
 74            return;
 75        }
 76        taggedMusicAvailable = musicCursor.moveToFirst();
 77        if (!taggedMusicAvailable) {
 78            return;
 79        }
 80        if (!restoreFromPrefs(prefs)) {
 81            musicCursor.moveToFirst();
 82            currentArtist = musicCursor.getString(ARTIST);
 83            currentAlbum = musicCursor.getString(ALBUM);
 84            currentTrack = musicCursor.getString(TRACK);
 85        }
 86    }
 87
 88    private boolean restoreFromPrefs(SharedPreferences prefs) {
 89        musicCursor.moveToFirst();
 90        currentArtist = prefs.getString(PREF_ARTIST, "");
 91        currentAlbum = prefs.getString(PREF_ALBUM, "");
 92        currentTrack = prefs.getString(PREF_TRACK, "");
 93        while (musicCursor.moveToNext()) {
 94            if (musicCursor.getString(ARTIST).equals(currentArtist)
 95                    && musicCursor.getString(ALBUM).equals(currentAlbum)
 96                    && musicCursor.getString(TRACK).equals(currentTrack)) {
 97                return true;
 98            }
 99        }
100        return false;
101    }
102
103    public String peekNextArtist() {
104        if (!taggedMusicAvailable) {
105            return "";
106        }
107        int initialPosition = musicCursor.getPosition();
108        while (musicCursor.moveToNext()) {
109            if (!musicCursor.getString(ARTIST).equals(currentArtist)) {
110                String artist = musicCursor.getString(ARTIST);
111                musicCursor.moveToPosition(initialPosition);
112                return artist;
113            }
114        }
115        musicCursor.moveToFirst();
116        while (musicCursor.getPosition() < initialPosition) {
117            if (!musicCursor.getString(ARTIST).equals(currentArtist)) {
118                String artist = musicCursor.getString(ARTIST);
119                musicCursor.moveToPosition(initialPosition);
120                return artist;
121            }
122            musicCursor.moveToNext();
123        }
124        return musicCursor.getString(ARTIST);
125    }
126
127    public String goNextArtist() {
128        if (!taggedMusicAvailable) {
129            return "";
130        }
131        int initialPosition = musicCursor.getPosition();
132        while (musicCursor.moveToNext()) {
133            if (!musicCursor.getString(ARTIST).equals(currentArtist)) {
134                currentArtist = musicCursor.getString(ARTIST);
135                return currentArtist;
136            }
137        }
138        musicCursor.moveToFirst();
139        while (musicCursor.getPosition() < initialPosition) {
140            if (!musicCursor.getString(ARTIST).equals(currentArtist)) {
141                currentArtist = musicCursor.getString(ARTIST);
142                return currentArtist;
143            }
144            musicCursor.moveToNext();
145        }
146        currentArtist = musicCursor.getString(ARTIST);
147        return currentArtist;
148    }
149
150    public String peekPrevArtist() {
151        if (!taggedMusicAvailable) {
152            return "";
153        }
154        int initialPosition = musicCursor.getPosition();
155        while (musicCursor.moveToPrevious()) {
156            if (!musicCursor.getString(ARTIST).equals(currentArtist)) {
157                String artist = musicCursor.getString(ARTIST);
158                musicCursor.moveToPosition(initialPosition);
159                return artist;
160            }
161        }
162        musicCursor.moveToLast();
163        while (musicCursor.getPosition() > initialPosition) {
164            if (!musicCursor.getString(ARTIST).equals(currentArtist)) {
165                String artist = musicCursor.getString(ARTIST);
166                musicCursor.moveToPosition(initialPosition);
167                return artist;
168            }
169            musicCursor.moveToPrevious();
170        }
171        return musicCursor.getString(ARTIST);
172    }
173
174    public String goPrevArtist() {
175        if (!taggedMusicAvailable) {
176            return "";
177        }
178        int initialPosition = musicCursor.getPosition();
179        while (musicCursor.moveToPrevious()) {
180            if (!musicCursor.getString(ARTIST).equals(currentArtist)) {
181                currentArtist = musicCursor.getString(ARTIST);
182                return currentArtist;
183            }
184        }
185        musicCursor.moveToLast();
186        while (musicCursor.getPosition() > initialPosition) {
187            if (!musicCursor.getString(ARTIST).equals(currentArtist)) {
188                currentArtist = musicCursor.getString(ARTIST);
189                return currentArtist;
190            }
191            musicCursor.moveToPrevious();
192        }
193        currentArtist = musicCursor.getString(ARTIST);
194        return currentArtist;
195    }
196
197    public String peekNextAlbum() {
198        if (!taggedMusicAvailable) {
199            return "";
200        }
201        int initialPosition = musicCursor.getPosition();
202        while (musicCursor.moveToNext()) {
203            if (musicCursor.getString(ARTIST).equals(currentArtist)
204                    && !musicCursor.getString(ALBUM).equals(currentAlbum)) {
205                String album = musicCursor.getString(ALBUM);
206                musicCursor.moveToPosition(initialPosition);
207                return album;
208            }
209        }
210        musicCursor.moveToFirst();
211        while (musicCursor.getPosition() < initialPosition) {
212            if (musicCursor.getString(ARTIST).equals(currentArtist)
213                    && !musicCursor.getString(ALBUM).equals(currentAlbum)) {
214                String album = musicCursor.getString(ALBUM);
215                musicCursor.moveToPosition(initialPosition);
216                return album;
217            }
218            musicCursor.moveToNext();
219        }
220        return musicCursor.getString(ALBUM);
221    }
222
223    public String goNextAlbum() {
224        if (!taggedMusicAvailable) {
225            return "";
226        }
227        int initialPosition = musicCursor.getPosition();
228        while (musicCursor.moveToNext()) {
229            if (musicCursor.getString(ARTIST).equals(currentArtist)
230                    && !musicCursor.getString(ALBUM).equals(currentAlbum)) {
231                currentAlbum = musicCursor.getString(ALBUM);
232                return currentAlbum;
233            }
234        }
235        musicCursor.moveToFirst();
236        while (musicCursor.getPosition() < initialPosition) {
237            if (musicCursor.getString(ARTIST).equals(currentArtist)
238                    && !musicCursor.getString(ALBUM).equals(currentAlbum)) {
239                currentAlbum = musicCursor.getString(ALBUM);
240                return currentAlbum;
241            }
242            musicCursor.moveToNext();
243        }
244        currentAlbum = musicCursor.getString(ALBUM);
245        return currentAlbum;
246    }
247
248    public String peekPrevAlbum() {
249        if (!taggedMusicAvailable) {
250            return "";
251        }
252        int initialPosition = musicCursor.getPosition();
253        while (musicCursor.moveToPrevious()) {
254            if (musicCursor.getString(ARTIST).equals(currentArtist)
255                    && !musicCursor.getString(ALBUM).equals(currentAlbum)) {
256                String album = musicCursor.getString(ALBUM);
257                musicCursor.moveToPosition(initialPosition);
258                return album;
259            }
260        }
261        musicCursor.moveToLast();
262        while (musicCursor.getPosition() > initialPosition) {
263            if (musicCursor.getString(ARTIST).equals(currentArtist)
264                    && !musicCursor.getString(ALBUM).equals(currentAlbum)) {
265                String album = musicCursor.getString(ALBUM);
266                musicCursor.moveToPosition(initialPosition);
267                return album;
268            }
269            musicCursor.moveToPrevious();
270        }
271        return musicCursor.getString(ALBUM);
272    }
273
274    public String goPrevAlbum() {
275        if (!taggedMusicAvailable) {
276            return "";
277        }
278        int initialPosition = musicCursor.getPosition();
279        while (musicCursor.moveToPrevious()) {
280            if (musicCursor.getString(ARTIST).equals(currentArtist)
281                    && !musicCursor.getString(ALBUM).equals(currentAlbum)) {
282                currentAlbum = musicCursor.getString(ALBUM);
283                return currentAlbum;
284            }
285        }
286        musicCursor.moveToLast();
287        while (musicCursor.getPosition() > initialPosition) {
288            if (musicCursor.getString(ARTIST).equals(currentArtist)
289                    && !musicCursor.getString(ALBUM).equals(currentAlbum)) {
290                currentAlbum = musicCursor.getString(ALBUM);
291                return currentAlbum;
292            }
293            musicCursor.moveToPrevious();
294        }
295        currentAlbum = musicCursor.getString(ALBUM);
296        return currentAlbum;
297    }
298
299    public String peekNextTrack() {
300        if (!taggedMusicAvailable) {
301            return "";
302        }
303        int initialPosition = musicCursor.getPosition();
304        while (musicCursor.moveToNext()) {
305            if (musicCursor.getString(ARTIST).equals(currentArtist)
306                    && musicCursor.getString(ALBUM).equals(currentAlbum)
307                    && !musicCursor.getString(TRACK).equals(currentTrack)) {
308                String track = musicCursor.getString(TRACK);
309                musicCursor.moveToPosition(initialPosition);
310                Log.e("peek end 0", musicCursor.getPosition() + " " + getCurrentSongInfo());
311                return track;
312            }
313        }
314        musicCursor.moveToFirst();
315        while (musicCursor.getPosition() < initialPosition) {
316            if (musicCursor.getString(ARTIST).equals(currentArtist)
317                    && musicCursor.getString(ALBUM).equals(currentAlbum)
318                    && !musicCursor.getString(TRACK).equals(currentTrack)) {
319                String track = musicCursor.getString(TRACK);
320                musicCursor.moveToPosition(initialPosition);
321                Log.e("peek end 1", musicCursor.getPosition() + " " + getCurrentSongInfo());
322                return track;
323            }
324            musicCursor.moveToNext();
325        }
326        Log.e("peek end 2", musicCursor.getPosition() + " " + getCurrentSongInfo());
327        return musicCursor.getString(TRACK);
328    }
329
330    public String goNextTrack() {
331        if (!taggedMusicAvailable) {
332            return "";
333        }
334        int initialPosition = musicCursor.getPosition();
335        while (musicCursor.moveToNext()) {
336            if (musicCursor.getString(ARTIST).equals(currentArtist)
337                    && musicCursor.getString(ALBUM).equals(currentAlbum)
338                    && !musicCursor.getString(TRACK).equals(currentTrack)) {
339                currentTrack = musicCursor.getString(TRACK);
340                return currentTrack;
341            }
342        }
343        musicCursor.moveToFirst();
344        while (musicCursor.getPosition() < initialPosition) {
345            if (musicCursor.getString(ARTIST).equals(currentArtist)
346                    && musicCursor.getString(ALBUM).equals(currentAlbum)
347                    && !musicCursor.getString(TRACK).equals(currentTrack)) {
348                currentTrack = musicCursor.getString(TRACK);
349                return currentTrack;
350            }
351            musicCursor.moveToNext();
352        }
353        currentTrack = musicCursor.getString(TRACK);
354        return currentTrack;
355    }
356
357    public String peekPrevTrack() {
358        if (!taggedMusicAvailable) {
359            return "";
360        }
361        int initialPosition = musicCursor.getPosition();
362        while (musicCursor.moveToPrevious()) {
363            if (musicCursor.getString(ARTIST).equals(currentArtist)
364                    && musicCursor.getString(ALBUM).equals(currentAlbum)
365                    && !musicCursor.getString(TRACK).equals(currentTrack)) {
366                String track = musicCursor.getString(TRACK);
367                musicCursor.moveToPosition(initialPosition);
368                return track;
369            }
370        }
371        musicCursor.moveToLast();
372        while (musicCursor.getPosition() > initialPosition) {
373            if (musicCursor.getString(ARTIST).equals(currentArtist)
374                    && musicCursor.getString(ALBUM).equals(currentAlbum)
375                    && !musicCursor.getString(TRACK).equals(currentTrack)) {
376                String track = musicCursor.getString(TRACK);
377                musicCursor.moveToPosition(initialPosition);
378                return track;
379            }
380            musicCursor.moveToPrevious();
381        }
382        return musicCursor.getString(TRACK);
383    }
384
385    public String goPrevTrack() {
386        if (!taggedMusicAvailable) {
387            return "";
388        }
389        int initialPosition = musicCursor.getPosition();
390        while (musicCursor.moveToPrevious()) {
391            if (musicCursor.getString(ARTIST).equals(currentArtist)
392                    && musicCursor.getString(ALBUM).equals(currentAlbum)
393                    && !musicCursor.getString(TRACK).equals(currentTrack)) {
394                currentTrack = musicCursor.getString(TRACK);
395                return currentTrack;
396            }
397        }
398        musicCursor.moveToLast();
399        while (musicCursor.getPosition() > initialPosition) {
400            if (musicCursor.getString(ARTIST).equals(currentArtist)
401                    && musicCursor.getString(ALBUM).equals(currentAlbum)
402                    && !musicCursor.getString(TRACK).equals(currentTrack)) {
403                currentTrack = musicCursor.getString(TRACK);
404                return currentTrack;
405            }
406            musicCursor.moveToPrevious();
407        }
408        currentTrack = musicCursor.getString(TRACK);
409        return currentTrack;
410    }
411
412    public String getCurrentSongFile() {
413        if (!taggedMusicAvailable) {
414            return "";
415        }
416        currentArtist = musicCursor.getString(ARTIST);
417        currentAlbum = musicCursor.getString(ALBUM);
418        currentTrack = musicCursor.getString(TRACK);
419        editor.putString(PREF_ARTIST, currentArtist);
420        editor.putString(PREF_ALBUM, currentAlbum);
421        editor.putString(PREF_TRACK, currentTrack);
422        editor.commit();
423        return musicCursor.getString(FILEPATH);
424    }
425
426    public String getCurrentSongInfo() {
427        if (!taggedMusicAvailable) {
428            return "";
429        }
430        currentArtist = musicCursor.getString(ARTIST);
431        currentAlbum = musicCursor.getString(ALBUM);
432        currentTrack = musicCursor.getString(TRACK);
433        return currentArtist + " - " + currentAlbum + " - " + currentTrack;
434    }
435}