PageRenderTime 36ms CodeModel.GetById 8ms app.highlight 24ms RepoModel.GetById 1ms app.codeStats 1ms

/RockLock/src/com/marvin/rocklock/TagStructuredSongPicker.java

http://eyes-free.googlecode.com/
Java | 432 lines | 379 code | 32 blank | 21 comment | 96 complexity | 92a5bf747e1d272c7ec02112c1dbe706 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.Audio.AudioColumns;
 26import android.provider.MediaStore.MediaColumns;
 27
 28/**
 29 * SongPicker implementation that uses the tags in the music files to organize
 30 * the music.
 31 * 
 32 * @author clchen@google.com (Charles L. Chen)
 33 */
 34public class TagStructuredSongPicker implements SongPicker {
 35    private static final String PREF_ARTIST = "TAG_ARTIST";
 36
 37    private static final String PREF_ALBUM = "TAG_ALBUM";
 38
 39    private static final String PREF_TRACK = "TAG_TRACK";
 40
 41    private static final int ARTIST = 0;
 42
 43    private static final int ALBUM = 1;
 44
 45    private static final int TRACK = 2;
 46
 47    private static final int FILEPATH = 3;
 48
 49    private Cursor musicCursor;
 50
 51    private String currentArtist = "";
 52
 53    private String currentAlbum = "";
 54
 55    private String currentTrack = "";
 56
 57    private Editor editor;
 58
 59    private boolean taggedMusicAvailable = true;
 60
 61    public TagStructuredSongPicker(Activity parentActivity) {
 62        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(parentActivity);
 63        editor = prefs.edit();
 64
 65        String[] proj = {
 66                AudioColumns.ARTIST, AudioColumns.ALBUM, MediaColumns.TITLE, MediaColumns.DATA
 67        };
 68        taggedMusicAvailable = true;
 69        musicCursor = parentActivity.managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
 70                proj, null, null, null);
 71        if (musicCursor == null) {
 72            taggedMusicAvailable = false;
 73            return;
 74        }
 75        taggedMusicAvailable = musicCursor.moveToFirst();
 76        if (!taggedMusicAvailable) {
 77            return;
 78        }
 79        if (!restoreFromPrefs(prefs)) {
 80            musicCursor.moveToFirst();
 81            currentArtist = musicCursor.getString(ARTIST);
 82            currentAlbum = musicCursor.getString(ALBUM);
 83            currentTrack = musicCursor.getString(TRACK);
 84        }
 85    }
 86
 87    private boolean restoreFromPrefs(SharedPreferences prefs) {
 88        musicCursor.moveToFirst();
 89        currentArtist = prefs.getString(PREF_ARTIST, "");
 90        currentAlbum = prefs.getString(PREF_ALBUM, "");
 91        currentTrack = prefs.getString(PREF_TRACK, "");
 92        while (musicCursor.moveToNext()) {
 93            if ((musicCursor.getString(ARTIST) != null)
 94                    && 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                return track;
311            }
312        }
313        musicCursor.moveToFirst();
314        while (musicCursor.getPosition() < initialPosition) {
315            if (musicCursor.getString(ARTIST).equals(currentArtist)
316                    && musicCursor.getString(ALBUM).equals(currentAlbum)
317                    && !musicCursor.getString(TRACK).equals(currentTrack)) {
318                String track = musicCursor.getString(TRACK);
319                musicCursor.moveToPosition(initialPosition);
320                return track;
321            }
322            musicCursor.moveToNext();
323        }
324        return musicCursor.getString(TRACK);
325    }
326
327    public String goNextTrack() {
328        if (!taggedMusicAvailable) {
329            return "";
330        }
331        int initialPosition = musicCursor.getPosition();
332        while (musicCursor.moveToNext()) {
333            if (musicCursor.getString(ARTIST).equals(currentArtist)
334                    && musicCursor.getString(ALBUM).equals(currentAlbum)
335                    && !musicCursor.getString(TRACK).equals(currentTrack)) {
336                currentTrack = musicCursor.getString(TRACK);
337                return currentTrack;
338            }
339        }
340        musicCursor.moveToFirst();
341        while (musicCursor.getPosition() < initialPosition) {
342            if (musicCursor.getString(ARTIST).equals(currentArtist)
343                    && musicCursor.getString(ALBUM).equals(currentAlbum)
344                    && !musicCursor.getString(TRACK).equals(currentTrack)) {
345                currentTrack = musicCursor.getString(TRACK);
346                return currentTrack;
347            }
348            musicCursor.moveToNext();
349        }
350        currentTrack = musicCursor.getString(TRACK);
351        return currentTrack;
352    }
353
354    public String peekPrevTrack() {
355        if (!taggedMusicAvailable) {
356            return "";
357        }
358        int initialPosition = musicCursor.getPosition();
359        while (musicCursor.moveToPrevious()) {
360            if (musicCursor.getString(ARTIST).equals(currentArtist)
361                    && musicCursor.getString(ALBUM).equals(currentAlbum)
362                    && !musicCursor.getString(TRACK).equals(currentTrack)) {
363                String track = musicCursor.getString(TRACK);
364                musicCursor.moveToPosition(initialPosition);
365                return track;
366            }
367        }
368        musicCursor.moveToLast();
369        while (musicCursor.getPosition() > initialPosition) {
370            if (musicCursor.getString(ARTIST).equals(currentArtist)
371                    && musicCursor.getString(ALBUM).equals(currentAlbum)
372                    && !musicCursor.getString(TRACK).equals(currentTrack)) {
373                String track = musicCursor.getString(TRACK);
374                musicCursor.moveToPosition(initialPosition);
375                return track;
376            }
377            musicCursor.moveToPrevious();
378        }
379        return musicCursor.getString(TRACK);
380    }
381
382    public String goPrevTrack() {
383        if (!taggedMusicAvailable) {
384            return "";
385        }
386        int initialPosition = musicCursor.getPosition();
387        while (musicCursor.moveToPrevious()) {
388            if (musicCursor.getString(ARTIST).equals(currentArtist)
389                    && musicCursor.getString(ALBUM).equals(currentAlbum)
390                    && !musicCursor.getString(TRACK).equals(currentTrack)) {
391                currentTrack = musicCursor.getString(TRACK);
392                return currentTrack;
393            }
394        }
395        musicCursor.moveToLast();
396        while (musicCursor.getPosition() > initialPosition) {
397            if (musicCursor.getString(ARTIST).equals(currentArtist)
398                    && musicCursor.getString(ALBUM).equals(currentAlbum)
399                    && !musicCursor.getString(TRACK).equals(currentTrack)) {
400                currentTrack = musicCursor.getString(TRACK);
401                return currentTrack;
402            }
403            musicCursor.moveToPrevious();
404        }
405        currentTrack = musicCursor.getString(TRACK);
406        return currentTrack;
407    }
408
409    public String getCurrentSongFile() {
410        if (!taggedMusicAvailable) {
411            return "";
412        }
413        currentArtist = musicCursor.getString(ARTIST);
414        currentAlbum = musicCursor.getString(ALBUM);
415        currentTrack = musicCursor.getString(TRACK);
416        editor.putString(PREF_ARTIST, currentArtist);
417        editor.putString(PREF_ALBUM, currentAlbum);
418        editor.putString(PREF_TRACK, currentTrack);
419        editor.commit();
420        return musicCursor.getString(FILEPATH);
421    }
422
423    public String getCurrentSongInfo() {
424        if (!taggedMusicAvailable) {
425            return "";
426        }
427        currentArtist = musicCursor.getString(ARTIST);
428        currentAlbum = musicCursor.getString(ALBUM);
429        currentTrack = musicCursor.getString(TRACK);
430        return currentArtist + "\n" + currentAlbum + "\n" + currentTrack;
431    }
432}