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