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
|
|
|
|
|
|
|
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-12-04 07:36:02 +01:00
|
|
|
|
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;
|
2013-12-21 06:15:27 +01:00
|
|
|
import com.thejoshwa.ultrasonic.androidapp.domain.Share;
|
2013-12-07 03:43:13 +01:00
|
|
|
import com.thejoshwa.ultrasonic.androidapp.domain.UserInfo;
|
2013-04-06 21:47:24 +02:00
|
|
|
import com.thejoshwa.ultrasonic.androidapp.util.Constants;
|
|
|
|
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
|
|
|
|
2013-12-04 07:36:02 +01:00
|
|
|
import java.io.BufferedReader;
|
2013-12-28 01:29:12 +01:00
|
|
|
import java.io.BufferedWriter;
|
2013-12-04 07:36:02 +01:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileReader;
|
2013-12-28 01:29:12 +01:00
|
|
|
import java.io.FileWriter;
|
2013-12-04 07:36:02 +01:00
|
|
|
import java.io.Reader;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.Comparator;
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Random;
|
|
|
|
import java.util.SortedSet;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
2013-02-08 10:09:55 +01:00
|
|
|
/**
|
|
|
|
* @author Sindre Mehus
|
|
|
|
*/
|
2013-12-04 07:36:02 +01:00
|
|
|
public class OfflineMusicService extends RESTMusicService
|
|
|
|
{
|
2013-05-16 09:59:55 +02:00
|
|
|
private static final String TAG = OfflineMusicService.class.getSimpleName();
|
2013-12-04 07:36:02 +01:00
|
|
|
private static final Pattern COMPILE = Pattern.compile(" ");
|
2013-02-08 10:09:55 +01:00
|
|
|
|
2013-07-17 10:59:58 +02:00
|
|
|
@Override
|
2013-12-04 07:36:02 +01:00
|
|
|
public boolean isLicenseValid(Context context, ProgressListener progressListener) throws Exception
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Indexes getIndexes(String musicFolderId, boolean refresh, Context context, ProgressListener progressListener) throws Exception
|
|
|
|
{
|
2013-07-17 10:59:58 +02:00
|
|
|
List<Artist> artists = new ArrayList<Artist>();
|
|
|
|
File root = FileUtil.getMusicDirectory(context);
|
2013-12-04 07:36:02 +01:00
|
|
|
for (File file : FileUtil.listFiles(root))
|
|
|
|
{
|
|
|
|
if (file.isDirectory())
|
|
|
|
{
|
2013-07-17 10:59:58 +02:00
|
|
|
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";
|
2013-12-04 07:36:02 +01:00
|
|
|
final String[] ignoredArticles = COMPILE.split(ignoredArticlesString);
|
2013-02-08 10:09:55 +01:00
|
|
|
|
2013-12-04 07:36:02 +01:00
|
|
|
Collections.sort(artists, new Comparator<Artist>()
|
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public int compare(Artist lhsArtist, Artist rhsArtist)
|
|
|
|
{
|
2013-07-17 10:59:58 +02:00
|
|
|
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-12-04 07:36:02 +01:00
|
|
|
if (Character.isDigit(lhs1) && !Character.isDigit(rhs1))
|
|
|
|
{
|
2013-07-17 10:59:58 +02:00
|
|
|
return 1;
|
2013-12-04 07:36:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Character.isDigit(rhs1) && !Character.isDigit(lhs1))
|
|
|
|
{
|
2013-07-17 10:59:58 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2013-02-08 10:09:55 +01:00
|
|
|
|
2013-12-04 07:36:02 +01:00
|
|
|
for (String article : ignoredArticles)
|
|
|
|
{
|
|
|
|
int index = lhs.indexOf(String.format("%s ", article.toLowerCase()));
|
|
|
|
|
|
|
|
if (index == 0)
|
|
|
|
{
|
2013-07-17 10:59:58 +02:00
|
|
|
lhs = lhs.substring(article.length() + 1);
|
|
|
|
}
|
2013-12-04 07:36:02 +01:00
|
|
|
|
|
|
|
index = rhs.indexOf(String.format("%s ", article.toLowerCase()));
|
|
|
|
|
|
|
|
if (index == 0)
|
|
|
|
{
|
2013-07-17 10:59:58 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-12-30 09:33:39 +01:00
|
|
|
return new Indexes(0L, ignoredArticlesString, Collections.<Artist>emptyList(), artists);
|
2013-07-17 10:59:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-12-04 07:36:02 +01:00
|
|
|
public MusicDirectory getMusicDirectory(String id, String artistName, boolean refresh, Context context, ProgressListener progressListener) throws Exception
|
|
|
|
{
|
2013-07-17 10:59:58 +02:00
|
|
|
File dir = new File(id);
|
|
|
|
MusicDirectory result = new MusicDirectory();
|
|
|
|
result.setName(dir.getName());
|
|
|
|
|
2013-12-04 07:36:02 +01:00
|
|
|
Collection<String> names = new HashSet<String>();
|
2013-07-17 10:59:58 +02:00
|
|
|
|
2013-12-04 07:36:02 +01:00
|
|
|
for (File file : FileUtil.listMediaFiles(dir))
|
|
|
|
{
|
2013-07-17 10:59:58 +02:00
|
|
|
String name = getName(file);
|
2013-12-04 07:36:02 +01:00
|
|
|
if (name != null & !names.contains(name))
|
|
|
|
{
|
2013-07-17 10:59:58 +02:00
|
|
|
names.add(name);
|
|
|
|
result.addChild(createEntry(context, file, name));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-12-04 07:36:02 +01:00
|
|
|
private static String getName(File file)
|
|
|
|
{
|
2013-07-17 10:59:58 +02:00
|
|
|
String name = file.getName();
|
2013-12-04 07:36:02 +01:00
|
|
|
|
|
|
|
if (file.isDirectory())
|
|
|
|
{
|
2013-07-17 10:59:58 +02:00
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2013-12-04 07:36:02 +01:00
|
|
|
if (name.endsWith(".partial") || name.contains(".partial.") || name.equals(Constants.ALBUM_ART_FILE))
|
|
|
|
{
|
2013-07-17 10:59:58 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
name = name.replace(".complete", "");
|
|
|
|
return FileUtil.getBaseName(name);
|
|
|
|
}
|
2013-02-08 10:09:55 +01:00
|
|
|
|
2013-12-04 07:36:02 +01:00
|
|
|
private static MusicDirectory.Entry createEntry(Context context, File file, String name)
|
|
|
|
{
|
|
|
|
MusicDirectory.Entry entry = new MusicDirectory.Entry();
|
|
|
|
entry.setIsDirectory(file.isDirectory());
|
|
|
|
entry.setId(file.getPath());
|
|
|
|
entry.setParent(file.getParent());
|
|
|
|
entry.setSize(file.length());
|
|
|
|
String root = FileUtil.getMusicDirectory(context).getPath();
|
|
|
|
entry.setPath(file.getPath().replaceFirst(String.format("^%s/", root), ""));
|
|
|
|
entry.setTitle(name);
|
|
|
|
|
|
|
|
if (file.isFile())
|
|
|
|
{
|
|
|
|
String artist = null;
|
|
|
|
String album = null;
|
|
|
|
String title = null;
|
|
|
|
String track = null;
|
|
|
|
String disc = null;
|
|
|
|
String year = null;
|
|
|
|
String genre = null;
|
|
|
|
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);
|
|
|
|
duration = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
|
|
|
|
hasVideo = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_HAS_VIDEO);
|
|
|
|
mmr.release();
|
|
|
|
}
|
|
|
|
catch (Exception ignored)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
entry.setArtist(artist != null ? artist : file.getParentFile().getParentFile().getName());
|
|
|
|
entry.setAlbum(album != null ? album : file.getParentFile().getName());
|
|
|
|
|
|
|
|
if (title != null)
|
|
|
|
{
|
|
|
|
entry.setTitle(title);
|
|
|
|
}
|
|
|
|
|
|
|
|
entry.setIsVideo(hasVideo != null);
|
|
|
|
|
|
|
|
Log.i("OfflineMusicService", String.format("Offline Stuff: %s", track));
|
|
|
|
|
|
|
|
if (track != null)
|
|
|
|
{
|
|
|
|
|
|
|
|
int trackValue = 0;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
int slashIndex = track.indexOf('/');
|
|
|
|
|
|
|
|
if (slashIndex > 0)
|
|
|
|
{
|
|
|
|
track = track.substring(0, slashIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
trackValue = Integer.parseInt(track);
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
Log.e("OfflineMusicService", String.format("Offline Stuff: %s", ex));
|
|
|
|
}
|
|
|
|
|
|
|
|
Log.i("OfflineMusicService", String.format("Offline Stuff: Setting Track: %d", trackValue));
|
|
|
|
|
|
|
|
entry.setTrack(trackValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (disc != null)
|
|
|
|
{
|
|
|
|
int discValue = 0;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
int slashIndex = disc.indexOf('/');
|
|
|
|
|
|
|
|
if (slashIndex > 0)
|
|
|
|
{
|
|
|
|
disc = disc.substring(0, slashIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
discValue = Integer.parseInt(disc);
|
|
|
|
}
|
|
|
|
catch (Exception ignored)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
entry.setDiscNumber(discValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (year != null)
|
|
|
|
{
|
|
|
|
int yearValue = 0;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
yearValue = Integer.parseInt(year);
|
|
|
|
}
|
|
|
|
catch (Exception ignored)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
entry.setYear(yearValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (genre != null)
|
|
|
|
{
|
|
|
|
entry.setGenre(genre);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (duration != null)
|
|
|
|
{
|
|
|
|
long durationValue = 0;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
durationValue = Long.parseLong(duration);
|
|
|
|
durationValue = TimeUnit.MILLISECONDS.toSeconds(durationValue);
|
|
|
|
}
|
|
|
|
catch (Exception ignored)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
entry.setDuration(durationValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
entry.setSuffix(FileUtil.getExtension(file.getName().replace(".complete", "")));
|
|
|
|
|
|
|
|
File albumArt = FileUtil.getAlbumArtFile(context, entry);
|
|
|
|
|
|
|
|
if (albumArt.exists())
|
|
|
|
{
|
|
|
|
entry.setCoverArt(albumArt.getPath());
|
|
|
|
}
|
|
|
|
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Bitmap getCoverArt(Context context, MusicDirectory.Entry entry, int size, boolean saveToFile, boolean highQuality, ProgressListener progressListener) throws Exception
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
Bitmap bitmap = FileUtil.getAlbumArtBitmap(context, entry, size, highQuality);
|
|
|
|
return Util.scaleBitmap(bitmap, size);
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
2013-05-16 09:59:55 +02:00
|
|
|
return null;
|
|
|
|
}
|
2013-12-04 07:36:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void star(String id, String albumId, String artistId, Context context, ProgressListener progressListener) throws Exception
|
|
|
|
{
|
|
|
|
throw new OfflineException("Star not available in offline mode");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void unstar(String id, String albumId, String artistId, Context context, ProgressListener progressListener) throws Exception
|
|
|
|
{
|
|
|
|
throw new OfflineException("UnStar not available in offline mode");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public List<MusicFolder> getMusicFolders(boolean refresh, Context context, ProgressListener progressListener) throws Exception
|
|
|
|
{
|
|
|
|
throw new OfflineException("Music folders not available in offline mode");
|
|
|
|
}
|
2013-02-08 10:09:55 +01:00
|
|
|
|
2013-07-17 10:59:58 +02:00
|
|
|
@Override
|
2013-12-04 07:36:02 +01:00
|
|
|
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);
|
2013-12-04 07:36:02 +01:00
|
|
|
int closeness;
|
|
|
|
|
|
|
|
for (File artistFile : FileUtil.listFiles(root))
|
|
|
|
{
|
2013-05-16 09:59:55 +02:00
|
|
|
String artistName = artistFile.getName();
|
2013-12-04 07:36:02 +01: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-12-04 07:36:02 +01:00
|
|
|
Collections.sort(artists, new Comparator<Artist>()
|
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public int compare(Artist lhs, Artist rhs)
|
|
|
|
{
|
|
|
|
if (lhs.getCloseness() == rhs.getCloseness())
|
|
|
|
{
|
2013-05-16 09:59:55 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2013-12-04 07:36:02 +01:00
|
|
|
|
|
|
|
else return lhs.getCloseness() > rhs.getCloseness() ? -1 : 1;
|
2013-05-16 09:59:55 +02:00
|
|
|
}
|
|
|
|
});
|
2013-12-04 07:36:02 +01:00
|
|
|
Collections.sort(albums, new Comparator<MusicDirectory.Entry>()
|
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public int compare(MusicDirectory.Entry lhs, MusicDirectory.Entry rhs)
|
|
|
|
{
|
|
|
|
if (lhs.getCloseness() == rhs.getCloseness())
|
|
|
|
{
|
2013-05-16 09:59:55 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2013-12-04 07:36:02 +01:00
|
|
|
|
|
|
|
else return lhs.getCloseness() > rhs.getCloseness() ? -1 : 1;
|
2013-05-16 09:59:55 +02:00
|
|
|
}
|
|
|
|
});
|
2013-12-04 07:36:02 +01:00
|
|
|
Collections.sort(songs, new Comparator<MusicDirectory.Entry>()
|
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public int compare(MusicDirectory.Entry lhs, MusicDirectory.Entry rhs)
|
|
|
|
{
|
|
|
|
if (lhs.getCloseness() == rhs.getCloseness())
|
|
|
|
{
|
2013-05-16 09:59:55 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2013-12-04 07:36:02 +01:00
|
|
|
|
|
|
|
else return lhs.getCloseness() > rhs.getCloseness() ? -1 : 1;
|
2013-05-16 09:59:55 +02:00
|
|
|
}
|
|
|
|
});
|
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-12-04 07:36:02 +01:00
|
|
|
private static 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;
|
2013-12-04 07:36:02 +01:00
|
|
|
|
|
|
|
for (File albumFile : FileUtil.listMediaFiles(file))
|
|
|
|
{
|
|
|
|
if (albumFile.isDirectory())
|
|
|
|
{
|
2013-05-16 09:59:55 +02:00
|
|
|
String albumName = getName(albumFile);
|
2013-12-04 07:36:02 +01:00
|
|
|
if ((closeness = matchCriteria(criteria, albumName)) > 0)
|
|
|
|
{
|
2013-05-16 09:59:55 +02:00
|
|
|
MusicDirectory.Entry album = createEntry(context, albumFile, albumName);
|
|
|
|
album.setArtist(artistName);
|
|
|
|
album.setCloseness(closeness);
|
|
|
|
albums.add(album);
|
|
|
|
}
|
|
|
|
|
2013-12-04 07:36:02 +01:00
|
|
|
for (File songFile : FileUtil.listMediaFiles(albumFile))
|
|
|
|
{
|
2013-05-16 09:59:55 +02:00
|
|
|
String songName = getName(songFile);
|
2013-12-04 07:36:02 +01:00
|
|
|
|
|
|
|
if (songFile.isDirectory())
|
|
|
|
{
|
2013-05-16 09:59:55 +02:00
|
|
|
recursiveAlbumSearch(artistName, songFile, criteria, context, albums, songs);
|
|
|
|
}
|
2013-12-04 07:36:02 +01:00
|
|
|
else if ((closeness = matchCriteria(criteria, songName)) > 0)
|
|
|
|
{
|
2013-05-16 09:59:55 +02:00
|
|
|
MusicDirectory.Entry song = createEntry(context, albumFile, songName);
|
|
|
|
song.setArtist(artistName);
|
|
|
|
song.setAlbum(albumName);
|
|
|
|
song.setCloseness(closeness);
|
|
|
|
songs.add(song);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-12-04 07:36:02 +01:00
|
|
|
else
|
|
|
|
{
|
2013-05-16 09:59:55 +02:00
|
|
|
String songName = getName(albumFile);
|
2013-12-04 07:36:02 +01:00
|
|
|
|
|
|
|
if ((closeness = matchCriteria(criteria, songName)) > 0)
|
|
|
|
{
|
2013-05-16 09:59:55 +02:00
|
|
|
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-12-04 07:36:02 +01:00
|
|
|
private static int matchCriteria(SearchCriteria criteria, String name)
|
|
|
|
{
|
2013-05-16 09:59:55 +02:00
|
|
|
String query = criteria.getQuery().toLowerCase();
|
2013-12-04 07:36:02 +01:00
|
|
|
String[] queryParts = COMPILE.split(query);
|
|
|
|
String[] nameParts = COMPILE.split(name.toLowerCase());
|
2013-07-17 10:59:58 +02:00
|
|
|
|
2013-05-16 09:59:55 +02:00
|
|
|
int closeness = 0;
|
2013-12-04 07:36:02 +01:00
|
|
|
|
|
|
|
for (String queryPart : queryParts)
|
|
|
|
{
|
|
|
|
for (String namePart : nameParts)
|
|
|
|
{
|
|
|
|
if (namePart.equals(queryPart))
|
|
|
|
{
|
2013-05-16 09:59:55 +02:00
|
|
|
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
|
2013-12-04 07:36:02 +01:00
|
|
|
public List<Playlist> getPlaylists(boolean refresh, Context context, ProgressListener progressListener) throws Exception
|
|
|
|
{
|
2013-07-17 10:59:58 +02:00
|
|
|
List<Playlist> playlists = new ArrayList<Playlist>();
|
|
|
|
File root = FileUtil.getPlaylistDirectory();
|
|
|
|
String lastServer = null;
|
|
|
|
boolean removeServer = true;
|
2013-12-04 07:36:02 +01:00
|
|
|
for (File folder : FileUtil.listFiles(root))
|
|
|
|
{
|
|
|
|
if (folder.isDirectory())
|
|
|
|
{
|
2013-07-17 10:59:58 +02:00
|
|
|
String server = folder.getName();
|
|
|
|
SortedSet<File> fileList = FileUtil.listFiles(folder);
|
2013-12-04 07:36:02 +01:00
|
|
|
for (File file : fileList)
|
|
|
|
{
|
|
|
|
if (FileUtil.isPlaylistFile(file))
|
|
|
|
{
|
2013-07-17 10:59:58 +02:00
|
|
|
String id = file.getName();
|
|
|
|
String filename = server + ": " + FileUtil.getBaseName(id);
|
|
|
|
Playlist playlist = new Playlist(server, filename);
|
|
|
|
playlists.add(playlist);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-30 09:33:39 +01:00
|
|
|
if (!server.equals(lastServer) && !fileList.isEmpty())
|
2013-12-04 07:36:02 +01:00
|
|
|
{
|
|
|
|
if (lastServer != null)
|
|
|
|
{
|
2013-07-17 10:59:58 +02:00
|
|
|
removeServer = false;
|
|
|
|
}
|
|
|
|
lastServer = server;
|
|
|
|
}
|
2013-12-04 07:36:02 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-05-16 09:59:55 +02:00
|
|
|
// Delete legacy playlist files
|
2013-12-04 07:36:02 +01:00
|
|
|
try
|
|
|
|
{
|
2013-07-17 10:59:58 +02:00
|
|
|
folder.delete();
|
2013-12-04 07:36:02 +01:00
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
Log.w(TAG, String.format("Failed to delete old playlist file: %s", 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-12-04 07:36:02 +01:00
|
|
|
if (removeServer)
|
|
|
|
{
|
|
|
|
for (Playlist playlist : playlists)
|
|
|
|
{
|
2013-07-17 10:59:58 +02:00
|
|
|
playlist.setName(playlist.getName().substring(playlist.getId().length() + 2));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return playlists;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-12-04 07:36:02 +01:00
|
|
|
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-12-04 07:36:02 +01:00
|
|
|
if (downloadService == null)
|
|
|
|
{
|
2013-07-17 10:59:58 +02:00
|
|
|
return new MusicDirectory();
|
|
|
|
}
|
|
|
|
|
|
|
|
Reader reader = null;
|
2013-05-16 09:59:55 +02:00
|
|
|
BufferedReader buffer = null;
|
2013-12-04 07:36:02 +01:00
|
|
|
try
|
|
|
|
{
|
2013-07-17 10:59:58 +02:00
|
|
|
int firstIndex = name.indexOf(id);
|
2013-12-04 07:36:02 +01:00
|
|
|
|
|
|
|
if (firstIndex != -1)
|
|
|
|
{
|
2013-07-17 10:59:58 +02:00
|
|
|
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-12-04 07:36:02 +01:00
|
|
|
if (!"#EXTM3U".equals(line)) return playlist;
|
2013-07-17 10:59:58 +02:00
|
|
|
|
2013-12-04 07:36:02 +01:00
|
|
|
while ((line = buffer.readLine()) != null)
|
|
|
|
{
|
2013-05-16 09:59:55 +02:00
|
|
|
File entryFile = new File(line);
|
|
|
|
String entryName = getName(entryFile);
|
2013-12-04 07:36:02 +01:00
|
|
|
|
|
|
|
if (entryFile.exists() && entryName != null)
|
|
|
|
{
|
2013-05-16 09:59:55 +02:00
|
|
|
playlist.addChild(createEntry(context, entryFile, entryName));
|
|
|
|
}
|
|
|
|
}
|
2013-07-17 10:59:58 +02:00
|
|
|
|
2013-05-16 09:59:55 +02:00
|
|
|
return playlist;
|
2013-12-04 07:36:02 +01:00
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
2013-05-16 09:59:55 +02:00
|
|
|
Util.close(buffer);
|
|
|
|
Util.close(reader);
|
|
|
|
}
|
2013-07-17 10:59:58 +02:00
|
|
|
}
|
2013-02-08 10:09:55 +01:00
|
|
|
|
2013-05-16 09:59:55 +02:00
|
|
|
@Override
|
2013-12-04 07:36:02 +01:00
|
|
|
public void createPlaylist(String id, String name, List<MusicDirectory.Entry> entries, Context context, ProgressListener progressListener) throws Exception
|
|
|
|
{
|
2013-12-28 01:29:12 +01:00
|
|
|
File playlistFile = FileUtil.getPlaylistFile(Util.getServerName(context), name);
|
|
|
|
FileWriter fw = new FileWriter(playlistFile);
|
|
|
|
BufferedWriter bw = new BufferedWriter(fw);
|
|
|
|
try
|
|
|
|
{
|
|
|
|
fw.write("#EXTM3U\n");
|
|
|
|
for (MusicDirectory.Entry e : entries)
|
|
|
|
{
|
|
|
|
String filePath = FileUtil.getSongFile(context, e).getAbsolutePath();
|
|
|
|
if (!new File(filePath).exists())
|
|
|
|
{
|
|
|
|
String ext = FileUtil.getExtension(filePath);
|
|
|
|
String base = FileUtil.getBaseName(filePath);
|
|
|
|
filePath = base + ".complete." + ext;
|
|
|
|
}
|
|
|
|
fw.write(filePath + '\n');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
Log.w(TAG, "Failed to save playlist: " + name);
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
bw.close();
|
|
|
|
fw.close();
|
|
|
|
}
|
2013-05-16 09:59:55 +02:00
|
|
|
}
|
2013-12-04 07:36:02 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void deletePlaylist(String id, Context context, ProgressListener progressListener) throws Exception
|
|
|
|
{
|
|
|
|
throw new OfflineException("Playlists not available in offline mode");
|
|
|
|
}
|
|
|
|
|
2013-05-16 09:59:55 +02:00
|
|
|
@Override
|
2013-12-04 07:36:02 +01: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");
|
|
|
|
}
|
2013-12-04 07:36:02 +01:00
|
|
|
|
2013-05-16 09:59:55 +02:00
|
|
|
@Override
|
2013-12-04 07:36:02 +01:00
|
|
|
public void removeFromPlaylist(String id, List<Integer> toRemove, Context context, ProgressListener progressListener) throws Exception
|
|
|
|
{
|
2013-05-16 09:59:55 +02:00
|
|
|
throw new OfflineException("Removing from playlist not available in offline mode");
|
|
|
|
}
|
2013-12-04 07:36:02 +01:00
|
|
|
|
2013-05-16 09:59:55 +02:00
|
|
|
@Override
|
2013-12-04 07:36:02 +01:00
|
|
|
public void updatePlaylist(String id, String name, String comment, boolean pub, Context context, ProgressListener progressListener) throws Exception
|
|
|
|
{
|
2013-05-16 09:59:55 +02:00
|
|
|
throw new OfflineException("Updating playlist not available in offline mode");
|
|
|
|
}
|
2013-02-08 10:09:55 +01:00
|
|
|
|
2013-12-04 07:36:02 +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");
|
|
|
|
}
|
2013-02-08 10:09:55 +01:00
|
|
|
|
2013-12-04 07:36:02 +01:00
|
|
|
@Override
|
|
|
|
public void scrobble(String id, boolean submission, Context context, ProgressListener progressListener) throws Exception
|
|
|
|
{
|
|
|
|
throw new OfflineException("Scrobbling not available in offline mode");
|
|
|
|
}
|
2013-02-08 10:09:55 +01:00
|
|
|
|
2013-12-04 07:36:02 +01:00
|
|
|
@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
|
|
|
|
public String getVideoUrl(Context context, String id, boolean useFlash)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getVideoStreamUrl(int maxBitrate, Context context, String id)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
2013-02-08 10:09:55 +01:00
|
|
|
|
2013-12-04 07:36:02 +01:00
|
|
|
@Override
|
|
|
|
public JukeboxStatus updateJukeboxPlaylist(List<String> ids, Context context, ProgressListener progressListener) throws Exception
|
|
|
|
{
|
|
|
|
throw new OfflineException("Jukebox not available in offline mode");
|
|
|
|
}
|
2013-05-16 09:59:55 +02:00
|
|
|
|
|
|
|
@Override
|
2013-12-04 07:36:02 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-12-30 09:33:39 +01:00
|
|
|
Random random = new java.security.SecureRandom();
|
2013-12-04 07:36:02 +01:00
|
|
|
for (int i = 0; i < size; i++)
|
|
|
|
{
|
|
|
|
File file = children.get(random.nextInt(children.size()));
|
|
|
|
result.addChild(createEntry(context, file, getName(file)));
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
@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-12-07 03:43:13 +01:00
|
|
|
@Override
|
|
|
|
public UserInfo getUser(String username, Context context, ProgressListener progressListener) throws Exception
|
|
|
|
{
|
|
|
|
throw new OfflineException("Getting user info not available in offline mode");
|
|
|
|
}
|
|
|
|
|
2013-12-21 06:15:27 +01:00
|
|
|
@Override
|
|
|
|
public List<Share> createShare(List<String> ids, String description, Long expires, Context context, ProgressListener progressListener) throws Exception
|
|
|
|
{
|
|
|
|
throw new OfflineException("Creating shares not available in offline mode");
|
|
|
|
}
|
|
|
|
|
2013-12-28 01:29:12 +01:00
|
|
|
@Override
|
|
|
|
public List<Share> getShares(boolean refresh, Context context, ProgressListener progressListener) throws Exception
|
|
|
|
{
|
|
|
|
throw new OfflineException("Getting shares not available in offline mode");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void deleteShare(String id, Context context, ProgressListener progressListener) throws Exception
|
|
|
|
{
|
|
|
|
throw new OfflineException("Deleting shares not available in offline mode");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void updateShare(String id, String description, Long expires, Context context, ProgressListener progressListener) throws Exception
|
|
|
|
{
|
|
|
|
throw new OfflineException("Updating shares not available in offline mode");
|
|
|
|
}
|
|
|
|
|
2013-12-04 07:36:02 +01:00
|
|
|
private static void listFilesRecursively(File parent, List<File> children)
|
|
|
|
{
|
|
|
|
for (File file : FileUtil.listMediaFiles(parent))
|
|
|
|
{
|
|
|
|
if (file.isFile())
|
|
|
|
{
|
2013-07-17 10:59:58 +02:00
|
|
|
children.add(file);
|
2013-12-04 07:36:02 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-07-17 10:59:58 +02:00
|
|
|
listFilesRecursively(file, children);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-02-08 10:09:55 +01:00
|
|
|
}
|