/MusicLockTutorial/src/com/marvin/rocklock/DirectoryStructuredSongPicker.java

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