From b0d38681e05a6fea987b7fe30290cc574b235a67 Mon Sep 17 00:00:00 2001 From: Scott Jackson Date: Sat, 4 Aug 2012 18:55:45 -0700 Subject: Added offline search --- .../subphonic/service/OfflineMusicService.java | 49 +++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) (limited to 'subsonic-android/src/github') diff --git a/subsonic-android/src/github/daneren2005/subphonic/service/OfflineMusicService.java b/subsonic-android/src/github/daneren2005/subphonic/service/OfflineMusicService.java index 0b79e02d..52955ca4 100644 --- a/subsonic-android/src/github/daneren2005/subphonic/service/OfflineMusicService.java +++ b/subsonic-android/src/github/daneren2005/subphonic/service/OfflineMusicService.java @@ -148,8 +148,55 @@ public class OfflineMusicService extends RESTMusicService { @Override public SearchResult search(SearchCritera criteria, Context context, ProgressListener progressListener) throws Exception { - throw new OfflineException("Search not available in offline mode"); + List artists = new ArrayList(); + List albums = new ArrayList(); + List songs = new ArrayList(); + File root = FileUtil.getMusicDirectory(context); + for (File artistFile : FileUtil.listFiles(root)) { + String artistName = artistFile.getName(); + if (artistFile.isDirectory()) { + if(matchCriteria(criteria, artistName)) { + Artist artist = new Artist(); + artist.setId(artistFile.getPath()); + artist.setIndex(artistFile.getName().substring(0, 1)); + artist.setName(artistFile.getName()); + artists.add(artist); + } + + for(File albumFile : FileUtil.listMusicFiles(artistFile)) { + if(albumFile.isDirectory()) { + String albumName = getName(albumFile); + if(matchCriteria(criteria, albumName)) { + albums.add(createEntry(context, albumFile, albumName)); + } + + for(File songFile : FileUtil.listMusicFiles(albumFile)) { + String songName = getName(songFile); + if(matchCriteria(criteria, songName)) { + songs.add(createEntry(context, albumFile, songName)); + } + } + } + } + } + } + + return new SearchResult(artists, albums, songs); } + + private boolean matchCriteria(SearchCritera criteria, String name) { + String query = criteria.getQuery().toLowerCase(); + String[] parts = query.split(" "); + name = name.toLowerCase(); + + for(String part : parts) { + if(name.indexOf(part) != -1) { + return true; + } + } + + return false; + } @Override public List getPlaylists(boolean refresh, Context context, ProgressListener progressListener) throws Exception { -- cgit v1.2.3