From 9bc4925ff0a07415b58513044ac4ce1d64caeb6c Mon Sep 17 00:00:00 2001 From: Scott Jackson Date: Sat, 3 Jan 2015 20:52:18 -0800 Subject: Add Similar Artists button under artist's options --- res/menu/select_album.xml | 4 + res/values/strings.xml | 1 + src/github/daneren2005/dsub/domain/ArtistInfo.java | 67 ++++++++++++ .../dsub/fragments/SelectDirectoryFragment.java | 12 ++ .../dsub/fragments/SimilarArtistFragment.java | 121 +++++++++++++++++++++ .../dsub/service/CachedMusicService.java | 6 + .../daneren2005/dsub/service/MusicService.java | 3 + .../dsub/service/OfflineMusicService.java | 6 + .../daneren2005/dsub/service/RESTMusicService.java | 11 ++ .../dsub/service/parser/ArtistInfoParser.java | 69 ++++++++++++ 10 files changed, 300 insertions(+) create mode 100644 src/github/daneren2005/dsub/domain/ArtistInfo.java create mode 100644 src/github/daneren2005/dsub/fragments/SimilarArtistFragment.java create mode 100644 src/github/daneren2005/dsub/service/parser/ArtistInfoParser.java diff --git a/res/menu/select_album.xml b/res/menu/select_album.xml index fa887c28..39eb2206 100644 --- a/res/menu/select_album.xml +++ b/res/menu/select_album.xml @@ -17,6 +17,10 @@ android:id="@+id/menu_top_tracks" android:title="@string/menu.top_tracks"/> + + diff --git a/res/values/strings.xml b/res/values/strings.xml index 3bcdec17..ebffb19b 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -104,6 +104,7 @@ Rescan Server Set Rating Last.FM Top Tracks + Similar Artists Playlists Update Information diff --git a/src/github/daneren2005/dsub/domain/ArtistInfo.java b/src/github/daneren2005/dsub/domain/ArtistInfo.java new file mode 100644 index 00000000..325f1b6f --- /dev/null +++ b/src/github/daneren2005/dsub/domain/ArtistInfo.java @@ -0,0 +1,67 @@ +/* + 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 . + Copyright 2014 (C) Scott Jackson +*/ + +package github.daneren2005.dsub.domain; + +import java.io.Serializable; +import java.util.List; + +public class ArtistInfo implements Serializable { + private String biography; + private String musicBrainzId; + private String lastFMUrl; + private String imageUrl; + private List similarArtists; + + public String getBiography() { + return biography; + } + + public void setBiography(String biography) { + this.biography = biography; + } + + public String getMusicBrainzId() { + return musicBrainzId; + } + + public void setMusicBrainzId(String musicBrainzId) { + this.musicBrainzId = musicBrainzId; + } + + public String getLastFMUrl() { + return lastFMUrl; + } + + public void setLastFMUrl(String lastFMUrl) { + this.lastFMUrl = lastFMUrl; + } + + public String getImageUrl() { + return imageUrl; + } + + public void setImageUrl(String imageUrl) { + this.imageUrl = imageUrl; + } + + public List getSimilarArtists() { + return similarArtists; + } + + public void setSimilarArtists(List similarArtists) { + this.similarArtists = similarArtists; + } +} diff --git a/src/github/daneren2005/dsub/fragments/SelectDirectoryFragment.java b/src/github/daneren2005/dsub/fragments/SelectDirectoryFragment.java index 3fbb90cf..fa9eea36 100644 --- a/src/github/daneren2005/dsub/fragments/SelectDirectoryFragment.java +++ b/src/github/daneren2005/dsub/fragments/SelectDirectoryFragment.java @@ -298,6 +298,9 @@ public class SelectDirectoryFragment extends SubsonicFragment implements Adapter case R.id.menu_top_tracks: showTopTracks(); return true; + case R.id.menu_similar_artists: + showSimilarArtists(id); + return true; } return super.onOptionsItemSelected(item); @@ -1211,6 +1214,15 @@ public class SelectDirectoryFragment extends SubsonicFragment implements Adapter replaceFragment(fragment, true); } + private void showSimilarArtists(String artistId) { + SubsonicFragment fragment = new SimilarArtistFragment(); + Bundle args = new Bundle(); + args.putString(Constants.INTENT_EXTRA_NAME_ARTIST, artistId); + fragment.setArguments(args); + + replaceFragment(fragment, true); + } + private View createHeader(List entries) { View header = entryList.findViewById(R.id.select_album_header); boolean add = false; diff --git a/src/github/daneren2005/dsub/fragments/SimilarArtistFragment.java b/src/github/daneren2005/dsub/fragments/SimilarArtistFragment.java new file mode 100644 index 00000000..8fdd6994 --- /dev/null +++ b/src/github/daneren2005/dsub/fragments/SimilarArtistFragment.java @@ -0,0 +1,121 @@ +/* + 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 . + Copyright 2014 (C) Scott Jackson +*/ + +package github.daneren2005.dsub.fragments; + +import android.os.Build; +import android.os.Bundle; +import android.support.v4.widget.SwipeRefreshLayout; +import android.util.Log; +import android.view.ContextMenu; +import android.view.LayoutInflater; +import android.view.Menu; +import android.view.MenuInflater; +import android.view.MenuItem; +import android.view.View; +import android.view.ViewGroup; +import android.widget.AbsListView; +import android.widget.AdapterView; +import android.widget.ArrayAdapter; +import android.widget.LinearLayout; +import android.widget.ListView; +import android.widget.TextView; +import github.daneren2005.dsub.R; +import github.daneren2005.dsub.domain.Artist; +import github.daneren2005.dsub.domain.ArtistInfo; +import github.daneren2005.dsub.domain.Indexes; +import github.daneren2005.dsub.domain.MusicDirectory; +import github.daneren2005.dsub.domain.MusicFolder; +import github.daneren2005.dsub.service.MusicService; +import github.daneren2005.dsub.service.MusicServiceFactory; +import github.daneren2005.dsub.util.BackgroundTask; +import github.daneren2005.dsub.util.Constants; +import github.daneren2005.dsub.util.ProgressListener; +import github.daneren2005.dsub.util.TabBackgroundTask; +import github.daneren2005.dsub.util.Util; +import github.daneren2005.dsub.view.ArtistAdapter; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.List; + +public class SimilarArtistFragment extends SelectListFragment { + private static final String TAG = SimilarArtistFragment.class.getSimpleName(); + private String artistId; + + @Override + public void onCreate(Bundle bundle) { + super.onCreate(bundle); + artist = true; + + artistId = getArguments().getString(Constants.INTENT_EXTRA_NAME_ARTIST); + } + + @Override + public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo) { + super.onCreateContextMenu(menu, view, menuInfo); + + AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo; + Object entry = listView.getItemAtPosition(info.position); + onCreateContextMenu(menu, view, menuInfo, entry); + + recreateContextMenu(menu); + } + + @Override + public boolean onContextItemSelected(MenuItem menuItem) { + if(menuItem.getGroupId() != getSupportTag()) { + return false; + } + + AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuItem.getMenuInfo(); + Artist artist = (Artist) listView.getItemAtPosition(info.position); + return onContextItemSelected(menuItem, artist); + } + + @Override + public void onItemClick(AdapterView parent, View view, int position, long id) { + Artist artist = (Artist) parent.getItemAtPosition(position); + SubsonicFragment fragment = new SelectDirectoryFragment(); + Bundle args = new Bundle(); + args.putString(Constants.INTENT_EXTRA_NAME_ID, artist.getId()); + args.putString(Constants.INTENT_EXTRA_NAME_NAME, artist.getName()); + args.putBoolean(Constants.INTENT_EXTRA_NAME_ARTIST, true); + fragment.setArguments(args); + + replaceFragment(fragment); + } + + @Override + public int getOptionsMenu() { + return R.menu.empty; + } + + @Override + public ArrayAdapter getAdapter(List objects) { + return new ArtistAdapter(context, objects); + } + + @Override + public List getObjects(MusicService musicService, boolean refresh, ProgressListener listener) throws Exception { + ArtistInfo info = musicService.getArtistInfo(artistId, refresh, context, listener); + return info.getSimilarArtists(); + } + + @Override + public int getTitleResource() { + return R.string.menu_similar_artists; + } +} diff --git a/src/github/daneren2005/dsub/service/CachedMusicService.java b/src/github/daneren2005/dsub/service/CachedMusicService.java index 8e8e120d..51f96ffb 100644 --- a/src/github/daneren2005/dsub/service/CachedMusicService.java +++ b/src/github/daneren2005/dsub/service/CachedMusicService.java @@ -32,6 +32,7 @@ import android.content.Context; import android.graphics.Bitmap; import github.daneren2005.dsub.domain.Artist; +import github.daneren2005.dsub.domain.ArtistInfo; import github.daneren2005.dsub.domain.Bookmark; import github.daneren2005.dsub.domain.ChatMessage; import github.daneren2005.dsub.domain.Genre; @@ -890,6 +891,11 @@ public class CachedMusicService implements MusicService { return musicService.getAvatar(username, size, context, progressListener, task); } + @Override + public ArtistInfo getArtistInfo(String id, boolean refresh, Context context, ProgressListener progressListener) throws Exception { + return musicService.getArtistInfo(id, refresh, context, progressListener); + } + @Override public int processOfflineSyncs(final Context context, final ProgressListener progressListener) throws Exception{ return musicService.processOfflineSyncs(context, progressListener); diff --git a/src/github/daneren2005/dsub/service/MusicService.java b/src/github/daneren2005/dsub/service/MusicService.java index 765f498a..64061191 100644 --- a/src/github/daneren2005/dsub/service/MusicService.java +++ b/src/github/daneren2005/dsub/service/MusicService.java @@ -25,6 +25,7 @@ import org.apache.http.HttpResponse; import android.content.Context; import android.graphics.Bitmap; +import github.daneren2005.dsub.domain.ArtistInfo; import github.daneren2005.dsub.domain.Bookmark; import github.daneren2005.dsub.domain.ChatMessage; import github.daneren2005.dsub.domain.Genre; @@ -177,6 +178,8 @@ public interface MusicService { void changePassword(String username, String password, Context context, ProgressListener progressListener) throws Exception; Bitmap getAvatar(String username, int size, Context context, ProgressListener progressListener, SilentBackgroundTask task) throws Exception; + + ArtistInfo getArtistInfo(String id, boolean refresh, Context context, ProgressListener progressListener) throws Exception; int processOfflineSyncs(final Context context, final ProgressListener progressListener) throws Exception; diff --git a/src/github/daneren2005/dsub/service/OfflineMusicService.java b/src/github/daneren2005/dsub/service/OfflineMusicService.java index 887ad84f..dc4651d2 100644 --- a/src/github/daneren2005/dsub/service/OfflineMusicService.java +++ b/src/github/daneren2005/dsub/service/OfflineMusicService.java @@ -37,6 +37,7 @@ import android.util.Log; import org.apache.http.HttpResponse; import github.daneren2005.dsub.domain.Artist; +import github.daneren2005.dsub.domain.ArtistInfo; import github.daneren2005.dsub.domain.ChatMessage; import github.daneren2005.dsub.domain.Genre; import github.daneren2005.dsub.domain.Indexes; @@ -782,6 +783,11 @@ public class OfflineMusicService implements MusicService { throw new OfflineException(ERRORMSG); } + @Override + public ArtistInfo getArtistInfo(String id, boolean refresh, Context context, ProgressListener progressListener) throws Exception { + throw new OfflineException(ERRORMSG); + } + @Override public int processOfflineSyncs(final Context context, final ProgressListener progressListener) throws Exception{ throw new OfflineException(ERRORMSG); diff --git a/src/github/daneren2005/dsub/service/RESTMusicService.java b/src/github/daneren2005/dsub/service/RESTMusicService.java index c7ca2708..53d797b5 100644 --- a/src/github/daneren2005/dsub/service/RESTMusicService.java +++ b/src/github/daneren2005/dsub/service/RESTMusicService.java @@ -69,6 +69,7 @@ import android.util.Log; import github.daneren2005.dsub.R; import github.daneren2005.dsub.domain.*; import github.daneren2005.dsub.service.parser.AlbumListParser; +import github.daneren2005.dsub.service.parser.ArtistInfoParser; import github.daneren2005.dsub.service.parser.BookmarkParser; import github.daneren2005.dsub.service.parser.ChatMessageParser; import github.daneren2005.dsub.service.parser.ErrorParser; @@ -1378,6 +1379,16 @@ public class RESTMusicService implements MusicService { } } + @Override + public ArtistInfo getArtistInfo(String id, boolean refresh, Context context, ProgressListener progressListener) throws Exception { + Reader reader = getReader(context, progressListener, Util.isTagBrowsing(context, getInstance(context)) ? "getArtistInfo2" : "getArtistInfo", null, Arrays.asList("id"), Arrays.asList(id)); + try { + return new ArtistInfoParser(context, getInstance(context)).parse(reader, progressListener); + } finally { + Util.close(reader); + } + } + @Override public int processOfflineSyncs(final Context context, final ProgressListener progressListener) throws Exception{ return processOfflineScrobbles(context, progressListener) + processOfflineStars(context, progressListener); diff --git a/src/github/daneren2005/dsub/service/parser/ArtistInfoParser.java b/src/github/daneren2005/dsub/service/parser/ArtistInfoParser.java new file mode 100644 index 00000000..9ffb354c --- /dev/null +++ b/src/github/daneren2005/dsub/service/parser/ArtistInfoParser.java @@ -0,0 +1,69 @@ +/* + 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 . + Copyright 2014 (C) Scott Jackson +*/ + +package github.daneren2005.dsub.service.parser; + +import android.content.Context; + +import org.xmlpull.v1.XmlPullParser; + +import java.io.Reader; +import java.util.ArrayList; +import java.util.List; + +import github.daneren2005.dsub.domain.Artist; +import github.daneren2005.dsub.domain.ArtistInfo; +import github.daneren2005.dsub.util.ProgressListener; + +public class ArtistInfoParser extends AbstractParser { + + public ArtistInfoParser(Context context, int instance) { + super(context, instance); + } + + public ArtistInfo parse(Reader reader, ProgressListener progressListener) throws Exception { + init(reader); + + ArtistInfo info = new ArtistInfo(); + List artists = new ArrayList(); + + int eventType; + do { + eventType = nextParseEvent(); + if (eventType == XmlPullParser.START_TAG) { + String name = getElementName(); + if ("biography".equals(name)) { + info.setBiography(getText()); + } else if ("musicBrainzId".equals(name)) { + info.setMusicBrainzId(getText()); + } else if ("lastFmUrl".equals(name)) { + info.setLastFMUrl(getText()); + } else if ("largeImageUrl".equals(name)) { + info.setImageUrl(getText()); + } else if ("similarArtist".equals(name)) { + Artist artist = new Artist(); + artist.setId(get("id")); + artist.setName(get("name")); + artists.add(artist); + } else if ("error".equals(name)) { + handleError(); + } + } + } while (eventType != XmlPullParser.END_DOCUMENT); + + info.setSimilarArtists(artists); + return info; + } +} -- cgit v1.2.3