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

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