Search in sources :

Example 1 with SearchResult

use of org.moire.ultrasonic.domain.SearchResult in project ultrasonic by ultrasonic.

the class OfflineMusicService method search.

@Override
public SearchResult search(SearchCriteria criteria, Context context, ProgressListener progressListener) throws Exception {
    List<Artist> artists = new ArrayList<Artist>();
    List<MusicDirectory.Entry> albums = new ArrayList<MusicDirectory.Entry>();
    List<MusicDirectory.Entry> songs = new ArrayList<MusicDirectory.Entry>();
    File root = FileUtil.getMusicDirectory(context);
    int closeness;
    for (File artistFile : FileUtil.listFiles(root)) {
        String artistName = artistFile.getName();
        if (artistFile.isDirectory()) {
            if ((closeness = matchCriteria(criteria, artistName)) > 0) {
                Artist artist = new Artist();
                artist.setId(artistFile.getPath());
                artist.setIndex(artistFile.getName().substring(0, 1));
                artist.setName(artistName);
                artist.setCloseness(closeness);
                artists.add(artist);
            }
            recursiveAlbumSearch(artistName, artistFile, criteria, context, albums, songs);
        }
    }
    Collections.sort(artists, new Comparator<Artist>() {

        @Override
        public int compare(Artist lhs, Artist rhs) {
            if (lhs.getCloseness() == rhs.getCloseness()) {
                return 0;
            } else
                return lhs.getCloseness() > rhs.getCloseness() ? -1 : 1;
        }
    });
    Collections.sort(albums, new Comparator<MusicDirectory.Entry>() {

        @Override
        public int compare(MusicDirectory.Entry lhs, MusicDirectory.Entry rhs) {
            if (lhs.getCloseness() == rhs.getCloseness()) {
                return 0;
            } else
                return lhs.getCloseness() > rhs.getCloseness() ? -1 : 1;
        }
    });
    Collections.sort(songs, new Comparator<MusicDirectory.Entry>() {

        @Override
        public int compare(MusicDirectory.Entry lhs, MusicDirectory.Entry rhs) {
            if (lhs.getCloseness() == rhs.getCloseness()) {
                return 0;
            } else
                return lhs.getCloseness() > rhs.getCloseness() ? -1 : 1;
        }
    });
    return new SearchResult(artists, albums, songs);
}
Also used : Artist(org.moire.ultrasonic.domain.Artist) MusicDirectory(org.moire.ultrasonic.domain.MusicDirectory) ArrayList(java.util.ArrayList) SearchResult(org.moire.ultrasonic.domain.SearchResult) File(java.io.File)

Example 2 with SearchResult

use of org.moire.ultrasonic.domain.SearchResult in project ultrasonic by ultrasonic.

the class SearchActivity method search.

private void search(final String query, final boolean autoplay) {
    final int maxArtists = Util.getMaxArtists(this);
    final int maxAlbums = Util.getMaxAlbums(this);
    final int maxSongs = Util.getMaxSongs(this);
    BackgroundTask<SearchResult> task = new TabActivityBackgroundTask<SearchResult>(this, true) {

        @Override
        protected SearchResult doInBackground() throws Throwable {
            SearchCriteria criteria = new SearchCriteria(query, maxArtists, maxAlbums, maxSongs);
            MusicService service = MusicServiceFactory.getMusicService(SearchActivity.this);
            licenseValid = service.isLicenseValid(SearchActivity.this, this);
            return service.search(criteria, SearchActivity.this, this);
        }

        @Override
        protected void done(SearchResult result) {
            searchResult = result;
            populateList();
            if (autoplay) {
                autoplay();
            }
        }
    };
    task.execute();
}
Also used : TabActivityBackgroundTask(org.moire.ultrasonic.util.TabActivityBackgroundTask) MusicService(org.moire.ultrasonic.service.MusicService) SearchResult(org.moire.ultrasonic.domain.SearchResult) SearchCriteria(org.moire.ultrasonic.domain.SearchCriteria)

Aggregations

SearchResult (org.moire.ultrasonic.domain.SearchResult)2 File (java.io.File)1 ArrayList (java.util.ArrayList)1 Artist (org.moire.ultrasonic.domain.Artist)1 MusicDirectory (org.moire.ultrasonic.domain.MusicDirectory)1 SearchCriteria (org.moire.ultrasonic.domain.SearchCriteria)1 MusicService (org.moire.ultrasonic.service.MusicService)1 TabActivityBackgroundTask (org.moire.ultrasonic.util.TabActivityBackgroundTask)1