PageRenderTime 32ms CodeModel.GetById 14ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

/documentation/RockLockTutorial/StepByStep/src/com/marvin/rocklock/DirectoryStructuredSongPicker.java

http://eyes-free.googlecode.com/
Java | 271 lines | 219 code | 30 blank | 22 comment | 52 complexity | 6e0d4ec14cf508657c5aafc1c54284ba 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.os.Environment;
 23import android.preference.PreferenceManager;
 24
 25import java.io.File;
 26import java.io.FileFilter;
 27
 28/**
 29 * SongPicker implementation that uses the directory structure to organize the
 30 * music instead of using the tags in the music files. For music to be picked
 31 * up, it has to be put in the /sdcard/RockLock/.music directory.
 32 * 
 33 * @author clchen@google.com (Charles L. Chen)
 34 */
 35public class DirectoryStructuredSongPicker implements SongPicker {
 36    private static final String PREF_ARTIST = "DIR_ARTIST";
 37
 38    private static final String PREF_ALBUM = "DIR_ALBUM";
 39
 40    private static final String PREF_TRACK = "DIR_TRACK";
 41
 42    private static final File musicDir = new File(Environment.getExternalStorageDirectory()
 43            + "/RockLock/.music");
 44
 45    private class DirectoryFilter implements FileFilter {
 46        @Override
 47        public boolean accept(File pathname) {
 48            if (pathname.isDirectory()) {
 49                return true;
 50            }
 51            return false;
 52        }
 53    }
 54
 55    private class MP3Filter implements FileFilter {
 56        @Override
 57        public boolean accept(File pathname) {
 58            if (pathname.isFile() && pathname.toString().endsWith(".mp3")) {
 59                return true;
 60            }
 61            return false;
 62        }
 63    }
 64
 65    private Editor editor;
 66
 67    private String currentArtistFullPath;
 68
 69    private String currentAlbumFullPath;
 70
 71    private String currentTrackFullPath;
 72
 73    public DirectoryStructuredSongPicker(Activity parentActivity) {
 74        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(parentActivity);
 75        editor = prefs.edit();
 76        if (!musicDir.exists()) {
 77            musicDir.mkdirs();
 78        }
 79        if (!restoreFromPrefs(prefs)) {
 80            currentArtistFullPath = "";
 81            currentAlbumFullPath = "";
 82            currentTrackFullPath = "";
 83            goNextArtist();
 84        }
 85    }
 86
 87    private boolean restoreFromPrefs(SharedPreferences prefs) {
 88        currentArtistFullPath = prefs.getString(PREF_ARTIST, "");
 89        currentAlbumFullPath = prefs.getString(PREF_ALBUM, "");
 90        currentTrackFullPath = prefs.getString(PREF_TRACK, "");
 91        if (new File(currentArtistFullPath).exists() && new File(currentAlbumFullPath).exists()
 92                && new File(currentTrackFullPath).exists()) {
 93            return true;
 94        }
 95        return false;
 96    }
 97
 98    private String filePathToName(final String filePath) {
 99        String name = filePath;
100        int startIndex = name.lastIndexOf("/") + 1;
101        int endIndex = name.lastIndexOf(".");
102        if (endIndex > startIndex) {
103            return name.substring(startIndex, endIndex);
104        } else {
105            return name.substring(startIndex);
106        }
107    }
108
109    public String peekNextArtist() {
110        String artist = "";
111        File[] artists = musicDir.listFiles(new DirectoryFilter());
112        if ((artists == null) || (artists.length == 0)) {
113            return "";
114        }
115        java.util.Arrays.sort(artists);
116        if (currentArtistFullPath.length() > 1) {
117            boolean moved = false;
118            for (int i = 0; i < artists.length - 1; i++) {
119                if (currentArtistFullPath.equals(artists[i].getAbsolutePath())) {
120                    return filePathToName(artists[i + 1].getAbsolutePath());
121                }
122            }
123        }
124        return filePathToName(artists[0].getAbsolutePath());
125    }
126
127    public String peekPrevArtist() {
128        String artist = "";
129        File[] artists = musicDir.listFiles(new DirectoryFilter());
130        if ((artists == null) || (artists.length == 0)) {
131            return "";
132        }
133        java.util.Arrays.sort(artists);
134        if (currentArtistFullPath.length() > 1) {
135            boolean moved = false;
136            for (int i = artists.length - 1; i > 0; i--) {
137                if (currentArtistFullPath.equals(artists[i].getAbsolutePath())) {
138                    return filePathToName(artists[i - 1].getAbsolutePath());
139                }
140            }
141        }
142        return filePathToName(artists[artists.length - 1].getAbsolutePath());
143    }
144
145    public String goNextArtist() {
146        currentArtistFullPath = musicDir + "/" + peekNextArtist();
147        currentAlbumFullPath = "";
148        goNextAlbum();
149        currentTrackFullPath = "";
150        goNextTrack();
151        return filePathToName(currentArtistFullPath);
152    }
153
154    public String goPrevArtist() {
155        currentArtistFullPath = musicDir + "/" + peekPrevArtist();
156        currentAlbumFullPath = "";
157        goNextAlbum();
158        currentTrackFullPath = "";
159        goNextTrack();
160        return filePathToName(currentArtistFullPath);
161    }
162
163    public String peekNextAlbum() {
164        File artistDir = new File(currentArtistFullPath);
165        File[] albums = artistDir.listFiles(new DirectoryFilter());
166        if ((albums == null) || (albums.length == 0)) {
167            return "";
168        }
169        java.util.Arrays.sort(albums);
170        if (currentAlbumFullPath.length() > 1) {
171            boolean moved = false;
172            for (int i = 0; i < albums.length - 1; i++) {
173                if (currentAlbumFullPath.equals(albums[i].getAbsolutePath())) {
174                    return filePathToName(albums[i + 1].getAbsolutePath());
175                }
176            }
177        }
178        return filePathToName(albums[0].getAbsolutePath());
179    }
180
181    public String goNextAlbum() {
182        currentAlbumFullPath = currentArtistFullPath + "/" + peekNextAlbum();
183        currentTrackFullPath = "";
184        goNextTrack();
185        return filePathToName(currentAlbumFullPath);
186    }
187
188    public String peekPrevAlbum() {
189        File artistDir = new File(currentArtistFullPath);
190        File[] albums = artistDir.listFiles(new DirectoryFilter());
191        if ((albums == null) || (albums.length == 0)) {
192            return "";
193        }
194        java.util.Arrays.sort(albums);
195        if (currentAlbumFullPath.length() > 1) {
196            boolean moved = false;
197            for (int i = albums.length - 1; i > 0; i--) {
198                if (currentAlbumFullPath.equals(albums[i].getAbsolutePath())) {
199                    return filePathToName(albums[i - 1].getAbsolutePath());
200                }
201            }
202        }
203        return filePathToName(albums[albums.length - 1].getAbsolutePath());
204    }
205
206    public String goPrevAlbum() {
207        currentAlbumFullPath = currentArtistFullPath + "/" + peekPrevAlbum();
208        currentTrackFullPath = "";
209        goNextTrack();
210        return filePathToName(currentAlbumFullPath);
211    }
212
213    public String peekNextTrack() {
214        File trackDir = new File(currentAlbumFullPath);
215        File[] tracks = trackDir.listFiles(new MP3Filter());
216        if ((tracks == null) || (tracks.length == 0)) {
217            return "";
218        }
219        java.util.Arrays.sort(tracks);
220        if (currentTrackFullPath.length() > 1) {
221            boolean moved = false;
222            for (int i = 0; i < tracks.length - 1; i++) {
223                if (currentTrackFullPath.equals(tracks[i].getAbsolutePath())) {
224                    return filePathToName(tracks[i + 1].getAbsolutePath());
225                }
226            }
227        }
228        return filePathToName(tracks[0].getAbsolutePath());
229    }
230
231    public String goNextTrack() {
232        currentTrackFullPath = currentAlbumFullPath + "/" + peekNextTrack() + ".mp3";
233        return filePathToName(currentTrackFullPath);
234    }
235
236    public String peekPrevTrack() {
237        File trackDir = new File(currentAlbumFullPath);
238        File[] tracks = trackDir.listFiles(new MP3Filter());
239        if ((tracks == null) || (tracks.length == 0)) {
240            return "";
241        }
242        java.util.Arrays.sort(tracks);
243        if (currentTrackFullPath.length() > 1) {
244            boolean moved = false;
245            for (int i = tracks.length - 1; i > 0; i--) {
246                if (currentTrackFullPath.equals(tracks[i].getAbsolutePath())) {
247                    return filePathToName(tracks[i - 1].getAbsolutePath());
248                }
249            }
250        }
251        return filePathToName(tracks[tracks.length - 1].getAbsolutePath());
252    }
253
254    public String goPrevTrack() {
255        currentTrackFullPath = currentAlbumFullPath + "/" + peekPrevTrack() + ".mp3";
256        return filePathToName(currentTrackFullPath);
257    }
258
259    public String getCurrentSongFile() {
260        editor.putString(PREF_ARTIST, currentArtistFullPath);
261        editor.putString(PREF_ALBUM, currentAlbumFullPath);
262        editor.putString(PREF_TRACK, currentTrackFullPath);
263        editor.commit();
264        return currentTrackFullPath;
265    }
266
267    public String getCurrentSongInfo() {
268        return filePathToName(currentArtistFullPath) + "\n" + filePathToName(currentAlbumFullPath)
269                + "\n" + filePathToName(currentTrackFullPath);
270    }
271}