2013-02-08 10:09:55 +01:00
|
|
|
/*
|
|
|
|
This file is part of Subsonic.
|
|
|
|
|
|
|
|
Subsonic is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Subsonic is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Subsonic. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
Copyright 2009 (C) Sindre Mehus
|
|
|
|
*/
|
2013-04-06 21:47:24 +02:00
|
|
|
package com.thejoshwa.ultrasonic.androidapp.service;
|
2013-02-08 10:09:55 +01:00
|
|
|
|
2013-05-16 09:59:55 +02:00
|
|
|
import java.io.BufferedReader;
|
2013-02-08 10:09:55 +01:00
|
|
|
import java.io.File;
|
2013-05-16 09:59:55 +02:00
|
|
|
import java.io.FileReader;
|
|
|
|
import java.io.Reader;
|
2013-02-08 10:09:55 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collections;
|
2013-05-16 09:59:55 +02:00
|
|
|
import java.util.Comparator;
|
2013-02-08 10:09:55 +01:00
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Random;
|
|
|
|
import java.util.Set;
|
2013-07-17 10:59:58 +02:00
|
|
|
import java.util.SortedSet;
|
2013-04-07 04:14:26 +02:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2013-02-08 10:09:55 +01:00
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.graphics.Bitmap;
|
2013-04-07 04:14:26 +02:00
|
|
|
import android.media.MediaMetadataRetriever;
|
2013-02-08 10:09:55 +01:00
|
|
|
import android.util.Log;
|
2013-04-06 21:47:24 +02:00
|
|
|
import com.thejoshwa.ultrasonic.androidapp.domain.Artist;
|
2013-04-20 23:58:59 +02:00
|
|
|
import com.thejoshwa.ultrasonic.androidapp.domain.Genre;
|
2013-04-06 21:47:24 +02:00
|
|
|
import com.thejoshwa.ultrasonic.androidapp.domain.Indexes;
|
|
|
|
import com.thejoshwa.ultrasonic.androidapp.domain.JukeboxStatus;
|
|
|
|
import com.thejoshwa.ultrasonic.androidapp.domain.Lyrics;
|
|
|
|
import com.thejoshwa.ultrasonic.androidapp.domain.MusicDirectory;
|
|
|
|
import com.thejoshwa.ultrasonic.androidapp.domain.MusicFolder;
|
|
|
|
import com.thejoshwa.ultrasonic.androidapp.domain.Playlist;
|
2013-06-07 04:27:45 +02:00
|
|
|
import com.thejoshwa.ultrasonic.androidapp.domain.SearchCriteria;
|
2013-04-06 21:47:24 +02:00
|
|
|
import com.thejoshwa.ultrasonic.androidapp.domain.SearchResult;
|
|
|
|
import com.thejoshwa.ultrasonic.androidapp.util.Constants;
|
2013-07-17 10:59:58 +02:00
|
|
|
import com.thejoshwa.ultrasonic.androidapp.util.EntryByDiscAndTrackComparator;
|
2013-04-06 21:47:24 +02:00
|
|
|
import com.thejoshwa.ultrasonic.androidapp.util.FileUtil;
|
|
|
|
import com.thejoshwa.ultrasonic.androidapp.util.ProgressListener;
|
|
|
|
import com.thejoshwa.ultrasonic.androidapp.util.Util;
|
2013-02-08 10:09:55 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Sindre Mehus
|
|
|
|
*/
|
|
|
|
public class OfflineMusicService extends RESTMusicService {
|
2013-05-16 09:59:55 +02:00
|
|
|
private static final String TAG = OfflineMusicService.class.getSimpleName();
|
|
|
|
|
2013-02-08 10:09:55 +01:00
|
|
|
@Override
|
|
|
|
public boolean isLicenseValid(Context context, ProgressListener progressListener) throws Exception {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-07-17 10:59:58 +02:00
|
|
|
@Override
|
|
|
|
public Indexes getIndexes(String musicFolderId, boolean refresh, Context context, ProgressListener progressListener) throws Exception {
|
|
|
|
List<Artist> artists = new ArrayList<Artist>();
|
|
|
|
File root = FileUtil.getMusicDirectory(context);
|
|
|
|
for (File file : FileUtil.listFiles(root)) {
|
|
|
|
if (file.isDirectory()) {
|
|
|
|
Artist artist = new Artist();
|
|
|
|
artist.setId(file.getPath());
|
|
|
|
artist.setIndex(file.getName().substring(0, 1));
|
|
|
|
artist.setName(file.getName());
|
|
|
|
artists.add(artist);
|
|
|
|
}
|
|
|
|
}
|
2013-02-08 10:09:55 +01:00
|
|
|
|
2013-07-17 10:59:58 +02:00
|
|
|
String ignoredArticlesString = "The El La Los Las Le Les";
|
|
|
|
final String[] ignoredArticles = ignoredArticlesString.split(" ");
|
2013-02-08 10:09:55 +01:00
|
|
|
|
2013-07-17 10:59:58 +02:00
|
|
|
Collections.sort(artists, new Comparator<Artist>() {
|
|
|
|
public int compare(Artist lhsArtist, Artist rhsArtist) {
|
|
|
|
String lhs = lhsArtist.getName().toLowerCase();
|
|
|
|
String rhs = rhsArtist.getName().toLowerCase();
|
2013-02-08 10:09:55 +01:00
|
|
|
|
2013-07-17 10:59:58 +02:00
|
|
|
char lhs1 = lhs.charAt(0);
|
|
|
|
char rhs1 = rhs.charAt(0);
|
2013-02-08 10:09:55 +01:00
|
|
|
|
2013-07-17 10:59:58 +02:00
|
|
|
if(Character.isDigit(lhs1) && !Character.isDigit(rhs1)) {
|
|
|
|
return 1;
|
|
|
|
} else if(Character.isDigit(rhs1) && !Character.isDigit(lhs1)) {
|
|
|
|
return -1;
|
|
|
|
}
|
2013-02-08 10:09:55 +01:00
|
|
|
|
2013-07-17 10:59:58 +02:00
|
|
|
for(String article: ignoredArticles) {
|
|
|
|
int index = lhs.indexOf(article.toLowerCase() + " ");
|
|
|
|
if(index == 0) {
|
|
|
|
lhs = lhs.substring(article.length() + 1);
|
|
|
|
}
|
|
|
|
index = rhs.indexOf(article.toLowerCase() + " ");
|
|
|
|
if(index == 0) {
|
|
|
|
rhs = rhs.substring(article.length() + 1);
|
|
|
|
}
|
|
|
|
}
|
2013-02-08 10:09:55 +01:00
|
|
|
|
2013-07-17 10:59:58 +02:00
|
|
|
return lhs.compareTo(rhs);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return new Indexes(0L, Collections.<Artist>emptyList(), artists);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public MusicDirectory getMusicDirectory(String id, String artistName, boolean refresh, Context context, ProgressListener progressListener) throws Exception {
|
|
|
|
File dir = new File(id);
|
|
|
|
MusicDirectory result = new MusicDirectory();
|
|
|
|
result.setName(dir.getName());
|
|
|
|
|
|
|
|
Set<String> names = new HashSet<String>();
|
|
|
|
|
|
|
|
for (File file : FileUtil.listMediaFiles(dir)) {
|
|
|
|
String name = getName(file);
|
|
|
|
if (name != null & !names.contains(name)) {
|
|
|
|
names.add(name);
|
|
|
|
result.addChild(createEntry(context, file, name));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
private String getName(File file) {
|
|
|
|
String name = file.getName();
|
|
|
|
if (file.isDirectory()) {
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (name.endsWith(".partial") || name.contains(".partial.") || name.equals(Constants.ALBUM_ART_FILE)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
name = name.replace(".complete", "");
|
|
|
|
return FileUtil.getBaseName(name);
|
|
|
|
}
|
2013-02-08 10:09:55 +01:00
|
|
|
|
2013-07-17 10:59:58 +02:00
|
|
|
private MusicDirectory.Entry createEntry(Context context, File file, String name) {
|
2013-02-08 10:09:55 +01:00
|
|
|
MusicDirectory.Entry entry = new MusicDirectory.Entry();
|
2013-05-18 12:50:54 +02:00
|
|
|
entry.setIsDirectory(file.isDirectory());
|
2013-02-08 10:09:55 +01:00
|
|
|
entry.setId(file.getPath());
|
|
|
|
entry.setParent(file.getParent());
|
|
|
|
entry.setSize(file.length());
|
|
|
|
String root = FileUtil.getMusicDirectory(context).getPath();
|
|
|
|
entry.setPath(file.getPath().replaceFirst("^" + root + "/" , ""));
|
2013-04-07 04:14:26 +02:00
|
|
|
entry.setTitle(name);
|
|
|
|
|
2013-02-08 10:09:55 +01:00
|
|
|
if (file.isFile()) {
|
2013-04-07 04:14:26 +02:00
|
|
|
String artist = null;
|
|
|
|
String album = null;
|
|
|
|
String title = null;
|
|
|
|
String track = null;
|
|
|
|
String disc = null;
|
|
|
|
String year = null;
|
|
|
|
String genre = null;
|
2013-04-19 01:21:24 +02:00
|
|
|
//String bitrate = null;
|
2013-04-07 04:14:26 +02:00
|
|
|
String duration = null;
|
|
|
|
String hasVideo = null;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
MediaMetadataRetriever mmr = new MediaMetadataRetriever();
|
|
|
|
mmr.setDataSource(file.getPath());
|
|
|
|
artist = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST);
|
|
|
|
album = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM);
|
|
|
|
title = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE);
|
|
|
|
track = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_CD_TRACK_NUMBER);
|
|
|
|
disc = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DISC_NUMBER);
|
|
|
|
year = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_YEAR);
|
|
|
|
genre = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_GENRE);
|
|
|
|
//bitrate = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_BITRATE);
|
|
|
|
duration = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
|
|
|
|
hasVideo = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_HAS_VIDEO);
|
|
|
|
mmr.release();
|
2013-06-07 04:27:45 +02:00
|
|
|
} catch (Exception ignored) { }
|
2013-04-07 04:14:26 +02:00
|
|
|
|
|
|
|
entry.setArtist(artist != null ? artist : file.getParentFile().getParentFile().getName());
|
|
|
|
entry.setAlbum(album != null ? album : file.getParentFile().getName());
|
|
|
|
|
|
|
|
if (title != null) {
|
|
|
|
entry.setTitle(title);
|
|
|
|
}
|
|
|
|
|
2013-05-18 12:50:54 +02:00
|
|
|
entry.setIsVideo(hasVideo != null);
|
2013-04-07 04:14:26 +02:00
|
|
|
|
2013-04-19 01:21:24 +02:00
|
|
|
Log.i("OfflineMusicService", "Offline Stuff: " + track);
|
|
|
|
|
2013-04-07 04:14:26 +02:00
|
|
|
if (track != null) {
|
2013-04-19 01:21:24 +02:00
|
|
|
|
|
|
|
int trackValue = 0;
|
|
|
|
|
|
|
|
try {
|
|
|
|
int slashIndex = track.indexOf("/");
|
2013-04-07 04:14:26 +02:00
|
|
|
|
2013-04-19 01:21:24 +02:00
|
|
|
if (slashIndex > 0) {
|
|
|
|
track = track.substring(0, slashIndex);
|
|
|
|
}
|
2013-04-07 04:14:26 +02:00
|
|
|
|
|
|
|
trackValue = Integer.parseInt(track);
|
2013-04-19 01:21:24 +02:00
|
|
|
}
|
|
|
|
catch(Exception ex) { Log.e("OfflineMusicService", "Offline Stuff: " + ex); }
|
|
|
|
|
|
|
|
Log.i("OfflineMusicService", "Offline Stuff: Setting Track: " + trackValue);
|
|
|
|
|
2013-04-07 04:14:26 +02:00
|
|
|
entry.setTrack(trackValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (disc != null) {
|
2013-04-19 01:21:24 +02:00
|
|
|
int discValue = 0;
|
|
|
|
|
|
|
|
try {
|
|
|
|
int slashIndex = disc.indexOf("/");
|
2013-04-07 04:14:26 +02:00
|
|
|
|
2013-04-19 01:21:24 +02:00
|
|
|
if (slashIndex > 0) {
|
|
|
|
disc = disc.substring(0, slashIndex);
|
|
|
|
}
|
2013-04-07 04:14:26 +02:00
|
|
|
|
|
|
|
discValue = Integer.parseInt(disc);
|
|
|
|
}
|
2013-06-07 04:27:45 +02:00
|
|
|
catch(Exception ignored) { }
|
2013-04-19 01:21:24 +02:00
|
|
|
|
|
|
|
entry.setDiscNumber(discValue);
|
2013-04-07 04:14:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (year != null) {
|
|
|
|
int yearValue = 0;
|
2013-04-19 01:21:24 +02:00
|
|
|
|
2013-04-07 04:14:26 +02:00
|
|
|
try {
|
|
|
|
yearValue = Integer.parseInt(year);
|
2013-06-07 04:27:45 +02:00
|
|
|
} catch(Exception ignored) { }
|
2013-04-19 01:21:24 +02:00
|
|
|
|
|
|
|
entry.setYear(yearValue);
|
2013-04-07 04:14:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (genre != null) {
|
|
|
|
entry.setGenre(genre);
|
|
|
|
}
|
|
|
|
|
2013-04-19 01:21:24 +02:00
|
|
|
// if (bitrate != null) {
|
|
|
|
// int bitRateValue = 0;
|
|
|
|
//
|
|
|
|
// try {
|
|
|
|
// bitRateValue = Integer.parseInt(bitrate) / 1000;
|
|
|
|
// } catch(Exception ex) { }
|
|
|
|
//
|
|
|
|
// entry.setBitRate(bitRateValue);
|
|
|
|
// }
|
2013-04-07 04:14:26 +02:00
|
|
|
|
|
|
|
if (duration != null) {
|
|
|
|
long durationValue = 0;
|
2013-04-19 01:21:24 +02:00
|
|
|
|
2013-04-07 04:14:26 +02:00
|
|
|
try {
|
|
|
|
durationValue = Long.parseLong(duration);
|
2013-04-19 01:21:24 +02:00
|
|
|
durationValue = TimeUnit.MILLISECONDS.toSeconds(durationValue);
|
2013-06-07 04:27:45 +02:00
|
|
|
} catch(Exception ignored) { }
|
2013-04-19 01:21:24 +02:00
|
|
|
|
2013-04-07 04:14:26 +02:00
|
|
|
entry.setDuration(durationValue);
|
|
|
|
}
|
2013-02-08 10:09:55 +01:00
|
|
|
}
|
2013-04-07 04:14:26 +02:00
|
|
|
|
2013-02-08 10:09:55 +01:00
|
|
|
entry.setSuffix(FileUtil.getExtension(file.getName().replace(".complete", "")));
|
|
|
|
|
|
|
|
File albumArt = FileUtil.getAlbumArtFile(context, entry);
|
2013-04-19 01:21:24 +02:00
|
|
|
|
2013-02-08 10:09:55 +01:00
|
|
|
if (albumArt.exists()) {
|
|
|
|
entry.setCoverArt(albumArt.getPath());
|
|
|
|
}
|
2013-04-19 01:21:24 +02:00
|
|
|
|
2013-02-08 10:09:55 +01:00
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-05-20 08:33:40 +02:00
|
|
|
public Bitmap getCoverArt(Context context, MusicDirectory.Entry entry, int size, boolean saveToFile, boolean highQuality, ProgressListener progressListener) throws Exception {
|
2013-05-16 09:59:55 +02:00
|
|
|
try {
|
2013-05-20 08:33:40 +02:00
|
|
|
return FileUtil.getAlbumArtBitmap(context, entry, size, highQuality);
|
2013-05-16 09:59:55 +02:00
|
|
|
} catch(Exception e) {
|
|
|
|
return null;
|
|
|
|
}
|
2013-02-08 10:09:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-05-18 12:50:54 +02:00
|
|
|
public void star(String id, String albumId, String artistId, Context context, ProgressListener progressListener) throws Exception {
|
2013-02-08 10:09:55 +01:00
|
|
|
throw new OfflineException("Star not available in offline mode");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-05-18 12:50:54 +02:00
|
|
|
public void unstar(String id, String albumId, String artistId, Context context, ProgressListener progressListener) throws Exception {
|
2013-02-08 10:09:55 +01:00
|
|
|
throw new OfflineException("UnStar not available in offline mode");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-05-16 09:59:55 +02:00
|
|
|
public List<MusicFolder> getMusicFolders(boolean refresh, Context context, ProgressListener progressListener) throws Exception {
|
2013-02-08 10:09:55 +01:00
|
|
|
throw new OfflineException("Music folders not available in offline mode");
|
|
|
|
}
|
|
|
|
|
2013-07-17 10:59:58 +02:00
|
|
|
@Override
|
|
|
|
public SearchResult search(SearchCriteria criteria, Context context, ProgressListener progressListener) throws Exception {
|
2013-05-16 09:59:55 +02:00
|
|
|
List<Artist> artists = new ArrayList<Artist>();
|
|
|
|
List<MusicDirectory.Entry> albums = new ArrayList<MusicDirectory.Entry>();
|
|
|
|
List<MusicDirectory.Entry> songs = new ArrayList<MusicDirectory.Entry>();
|
2013-07-17 10:59:58 +02:00
|
|
|
File root = FileUtil.getMusicDirectory(context);
|
|
|
|
int closeness = 0;
|
|
|
|
for (File artistFile : FileUtil.listFiles(root)) {
|
2013-05-16 09:59:55 +02:00
|
|
|
String artistName = artistFile.getName();
|
2013-07-17 10:59:58 +02:00
|
|
|
if (artistFile.isDirectory()) {
|
|
|
|
if((closeness = matchCriteria(criteria, artistName)) > 0) {
|
2013-05-16 09:59:55 +02:00
|
|
|
Artist artist = new Artist();
|
|
|
|
artist.setId(artistFile.getPath());
|
|
|
|
artist.setIndex(artistFile.getName().substring(0, 1));
|
|
|
|
artist.setName(artistName);
|
|
|
|
artist.setCloseness(closeness);
|
|
|
|
artists.add(artist);
|
|
|
|
}
|
2013-07-17 10:59:58 +02:00
|
|
|
|
2013-05-16 09:59:55 +02:00
|
|
|
recursiveAlbumSearch(artistName, artistFile, criteria, context, albums, songs);
|
2013-07-17 10:59:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-16 09:59:55 +02:00
|
|
|
Collections.sort(artists, new Comparator<Artist>() {
|
|
|
|
public int compare(Artist lhs, Artist rhs) {
|
|
|
|
if(lhs.getCloseness() == rhs.getCloseness()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if(lhs.getCloseness() > rhs.getCloseness()) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
Collections.sort(albums, new Comparator<MusicDirectory.Entry>() {
|
|
|
|
public int compare(MusicDirectory.Entry lhs, MusicDirectory.Entry rhs) {
|
|
|
|
if(lhs.getCloseness() == rhs.getCloseness()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if(lhs.getCloseness() > rhs.getCloseness()) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
Collections.sort(songs, new Comparator<MusicDirectory.Entry>() {
|
|
|
|
public int compare(MusicDirectory.Entry lhs, MusicDirectory.Entry rhs) {
|
|
|
|
if(lhs.getCloseness() == rhs.getCloseness()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if(lhs.getCloseness() > rhs.getCloseness()) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2013-07-17 10:59:58 +02:00
|
|
|
|
2013-05-16 09:59:55 +02:00
|
|
|
return new SearchResult(artists, albums, songs);
|
2013-07-17 10:59:58 +02:00
|
|
|
}
|
|
|
|
|
2013-06-07 04:27:45 +02:00
|
|
|
private void recursiveAlbumSearch(String artistName, File file, SearchCriteria criteria, Context context, List<MusicDirectory.Entry> albums, List<MusicDirectory.Entry> songs) {
|
2013-05-16 09:59:55 +02:00
|
|
|
int closeness;
|
|
|
|
for(File albumFile : FileUtil.listMediaFiles(file)) {
|
|
|
|
if(albumFile.isDirectory()) {
|
|
|
|
String albumName = getName(albumFile);
|
|
|
|
if((closeness = matchCriteria(criteria, albumName)) > 0) {
|
|
|
|
MusicDirectory.Entry album = createEntry(context, albumFile, albumName);
|
|
|
|
album.setArtist(artistName);
|
|
|
|
album.setCloseness(closeness);
|
|
|
|
albums.add(album);
|
|
|
|
}
|
|
|
|
|
|
|
|
for(File songFile : FileUtil.listMediaFiles(albumFile)) {
|
|
|
|
String songName = getName(songFile);
|
|
|
|
if(songFile.isDirectory()) {
|
|
|
|
recursiveAlbumSearch(artistName, songFile, criteria, context, albums, songs);
|
|
|
|
}
|
|
|
|
else if((closeness = matchCriteria(criteria, songName)) > 0){
|
|
|
|
MusicDirectory.Entry song = createEntry(context, albumFile, songName);
|
|
|
|
song.setArtist(artistName);
|
|
|
|
song.setAlbum(albumName);
|
|
|
|
song.setCloseness(closeness);
|
|
|
|
songs.add(song);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
String songName = getName(albumFile);
|
|
|
|
if((closeness = matchCriteria(criteria, songName)) > 0) {
|
|
|
|
MusicDirectory.Entry song = createEntry(context, albumFile, songName);
|
|
|
|
song.setArtist(artistName);
|
|
|
|
song.setAlbum(songName);
|
|
|
|
song.setCloseness(closeness);
|
|
|
|
songs.add(song);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-07-17 10:59:58 +02:00
|
|
|
|
2013-06-07 04:27:45 +02:00
|
|
|
private int matchCriteria(SearchCriteria criteria, String name) {
|
2013-05-16 09:59:55 +02:00
|
|
|
String query = criteria.getQuery().toLowerCase();
|
|
|
|
String[] queryParts = query.split(" ");
|
|
|
|
String[] nameParts = name.toLowerCase().split(" ");
|
2013-07-17 10:59:58 +02:00
|
|
|
|
2013-05-16 09:59:55 +02:00
|
|
|
int closeness = 0;
|
|
|
|
for(String queryPart : queryParts) {
|
|
|
|
for(String namePart : nameParts) {
|
|
|
|
if(namePart.equals(queryPart)) {
|
|
|
|
closeness++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-07-17 10:59:58 +02:00
|
|
|
|
2013-05-16 09:59:55 +02:00
|
|
|
return closeness;
|
|
|
|
}
|
2013-02-08 10:09:55 +01:00
|
|
|
|
2013-07-17 10:59:58 +02:00
|
|
|
@Override
|
|
|
|
public List<Playlist> getPlaylists(boolean refresh, Context context, ProgressListener progressListener) throws Exception {
|
|
|
|
List<Playlist> playlists = new ArrayList<Playlist>();
|
|
|
|
File root = FileUtil.getPlaylistDirectory();
|
|
|
|
String lastServer = null;
|
|
|
|
boolean removeServer = true;
|
|
|
|
for (File folder : FileUtil.listFiles(root)) {
|
|
|
|
if(folder.isDirectory()) {
|
|
|
|
String server = folder.getName();
|
|
|
|
SortedSet<File> fileList = FileUtil.listFiles(folder);
|
|
|
|
for(File file: fileList) {
|
|
|
|
if(FileUtil.isPlaylistFile(file)) {
|
|
|
|
String id = file.getName();
|
|
|
|
String filename = server + ": " + FileUtil.getBaseName(id);
|
|
|
|
Playlist playlist = new Playlist(server, filename);
|
|
|
|
playlists.add(playlist);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!server.equals(lastServer) && fileList.size() > 0) {
|
|
|
|
if(lastServer != null) {
|
|
|
|
removeServer = false;
|
|
|
|
}
|
|
|
|
lastServer = server;
|
|
|
|
}
|
2013-05-16 09:59:55 +02:00
|
|
|
} else {
|
|
|
|
// Delete legacy playlist files
|
|
|
|
try {
|
2013-07-17 10:59:58 +02:00
|
|
|
folder.delete();
|
2013-05-16 09:59:55 +02:00
|
|
|
} catch(Exception e) {
|
2013-07-17 10:59:58 +02:00
|
|
|
Log.w(TAG, "Failed to delete old playlist file: " + folder.getName());
|
2013-05-16 09:59:55 +02:00
|
|
|
}
|
|
|
|
}
|
2013-07-17 10:59:58 +02:00
|
|
|
}
|
2013-02-08 10:09:55 +01:00
|
|
|
|
2013-07-17 10:59:58 +02:00
|
|
|
if(removeServer) {
|
|
|
|
for(Playlist playlist: playlists) {
|
|
|
|
playlist.setName(playlist.getName().substring(playlist.getId().length() + 2));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return playlists;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public MusicDirectory getPlaylist(String id, String name, Context context, ProgressListener progressListener) throws Exception {
|
2013-05-16 09:59:55 +02:00
|
|
|
DownloadService downloadService = DownloadServiceImpl.getInstance();
|
2013-07-17 10:59:58 +02:00
|
|
|
if (downloadService == null) {
|
|
|
|
return new MusicDirectory();
|
|
|
|
}
|
|
|
|
|
|
|
|
Reader reader = null;
|
2013-05-16 09:59:55 +02:00
|
|
|
BufferedReader buffer = null;
|
|
|
|
try {
|
2013-07-17 10:59:58 +02:00
|
|
|
int firstIndex = name.indexOf(id);
|
|
|
|
if(firstIndex != -1) {
|
|
|
|
name = name.substring(id.length() + 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
File playlistFile = FileUtil.getPlaylistFile(id, name);
|
2013-05-16 09:59:55 +02:00
|
|
|
reader = new FileReader(playlistFile);
|
|
|
|
buffer = new BufferedReader(reader);
|
2013-07-17 10:59:58 +02:00
|
|
|
|
2013-05-16 09:59:55 +02:00
|
|
|
MusicDirectory playlist = new MusicDirectory();
|
|
|
|
String line = buffer.readLine();
|
2013-07-17 10:59:58 +02:00
|
|
|
if(!"#EXTM3U".equals(line)) return playlist;
|
|
|
|
|
2013-05-16 09:59:55 +02:00
|
|
|
while( (line = buffer.readLine()) != null ){
|
|
|
|
File entryFile = new File(line);
|
|
|
|
String entryName = getName(entryFile);
|
|
|
|
if(entryFile.exists() && entryName != null){
|
|
|
|
playlist.addChild(createEntry(context, entryFile, entryName));
|
|
|
|
}
|
|
|
|
}
|
2013-07-17 10:59:58 +02:00
|
|
|
|
2013-05-16 09:59:55 +02:00
|
|
|
return playlist;
|
|
|
|
} finally {
|
|
|
|
Util.close(buffer);
|
|
|
|
Util.close(reader);
|
|
|
|
}
|
2013-07-17 10:59:58 +02:00
|
|
|
}
|
2013-02-08 10:09:55 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void createPlaylist(String id, String name, List<MusicDirectory.Entry> entries, Context context, ProgressListener progressListener) throws Exception {
|
|
|
|
throw new OfflineException("Playlists not available in offline mode");
|
|
|
|
}
|
2013-05-16 09:59:55 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void deletePlaylist(String id, Context context, ProgressListener progressListener) throws Exception {
|
|
|
|
throw new OfflineException("Playlists not available in offline mode");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-05-18 12:50:54 +02:00
|
|
|
public void updatePlaylist(String id, List<MusicDirectory.Entry> toAdd, Context context, ProgressListener progressListener) throws Exception {
|
2013-05-16 09:59:55 +02:00
|
|
|
throw new OfflineException("Updating playlist not available in offline mode");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void removeFromPlaylist(String id, List<Integer> toRemove, Context context, ProgressListener progressListener) throws Exception {
|
|
|
|
throw new OfflineException("Removing from playlist not available in offline mode");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void updatePlaylist(String id, String name, String comment, boolean pub, Context context, ProgressListener progressListener) throws Exception {
|
|
|
|
throw new OfflineException("Updating playlist not available in offline mode");
|
|
|
|
}
|
2013-02-08 10:09:55 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public Lyrics getLyrics(String artist, String title, Context context, ProgressListener progressListener) throws Exception {
|
|
|
|
throw new OfflineException("Lyrics not available in offline mode");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void scrobble(String id, boolean submission, Context context, ProgressListener progressListener) throws Exception {
|
|
|
|
throw new OfflineException("Scrobbling not available in offline mode");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public MusicDirectory getAlbumList(String type, int size, int offset, Context context, ProgressListener progressListener) throws Exception {
|
|
|
|
throw new OfflineException("Album lists not available in offline mode");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-05-31 10:02:25 +02:00
|
|
|
public String getVideoUrl(Context context, String id, boolean useFlash) {
|
2013-05-16 09:59:55 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getVideoStreamUrl(int maxBitrate, Context context, String id) {
|
2013-02-08 10:09:55 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public JukeboxStatus updateJukeboxPlaylist(List<String> ids, Context context, ProgressListener progressListener) throws Exception {
|
|
|
|
throw new OfflineException("Jukebox not available in offline mode");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public JukeboxStatus skipJukebox(int index, int offsetSeconds, Context context, ProgressListener progressListener) throws Exception {
|
|
|
|
throw new OfflineException("Jukebox not available in offline mode");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public JukeboxStatus stopJukebox(Context context, ProgressListener progressListener) throws Exception {
|
|
|
|
throw new OfflineException("Jukebox not available in offline mode");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public JukeboxStatus startJukebox(Context context, ProgressListener progressListener) throws Exception {
|
|
|
|
throw new OfflineException("Jukebox not available in offline mode");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public JukeboxStatus getJukeboxStatus(Context context, ProgressListener progressListener) throws Exception {
|
|
|
|
throw new OfflineException("Jukebox not available in offline mode");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public JukeboxStatus setJukeboxGain(float gain, Context context, ProgressListener progressListener) throws Exception {
|
|
|
|
throw new OfflineException("Jukebox not available in offline mode");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public SearchResult getStarred(Context context, ProgressListener progressListener) throws Exception {
|
|
|
|
throw new OfflineException("Starred not available in offline mode");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public MusicDirectory getRandomSongs(int size, Context context, ProgressListener progressListener) throws Exception {
|
|
|
|
File root = FileUtil.getMusicDirectory(context);
|
|
|
|
List<File> children = new LinkedList<File>();
|
|
|
|
listFilesRecursively(root, children);
|
|
|
|
MusicDirectory result = new MusicDirectory();
|
|
|
|
|
|
|
|
if (children.isEmpty()) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
Random random = new Random();
|
|
|
|
for (int i = 0; i < size; i++) {
|
|
|
|
File file = children.get(random.nextInt(children.size()));
|
|
|
|
result.addChild(createEntry(context, file, getName(file)));
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2013-04-20 23:58:59 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public MusicDirectory getSongsByGenre(String genre, int count, int offset, Context context, ProgressListener progressListener) throws Exception {
|
|
|
|
throw new OfflineException("Getting Songs By Genre not available in offline mode");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public List<Genre> getGenres(Context context, ProgressListener progressListener) throws Exception {
|
|
|
|
throw new OfflineException("Getting Genres not available in offline mode");
|
|
|
|
}
|
2013-02-08 10:09:55 +01:00
|
|
|
|
2013-07-17 10:59:58 +02:00
|
|
|
private void listFilesRecursively(File parent, List<File> children) {
|
|
|
|
for (File file : FileUtil.listMediaFiles(parent)) {
|
|
|
|
if (file.isFile()) {
|
|
|
|
children.add(file);
|
|
|
|
} else {
|
|
|
|
listFilesRecursively(file, children);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-02-08 10:09:55 +01:00
|
|
|
}
|