Merge pull request #178 from Materight/lastfm
Added localized biography and wiki for albums
This commit is contained in:
commit
28e23635de
40 changed files with 213 additions and 82 deletions
|
|
@ -48,8 +48,8 @@ public class ArtistImageFetcher implements DataFetcher<InputStream> {
|
|||
|
||||
@Override
|
||||
public InputStream loadData(Priority priority) throws Exception {
|
||||
if (!MusicUtil.isArtistNameUnknown(model.artistName) && Util.isAllowedToAutoDownload(context)) {
|
||||
Response<LastFmArtist> response = lastFMRestClient.getApiService().getArtistInfo(model.artistName, model.skipOkHttpCache ? "no-cache" : null).execute();
|
||||
if (!MusicUtil.isArtistNameUnknown(model.artistName) && Util.isAllowedToDownloadMetadata(context)) {
|
||||
Response<LastFmArtist> response = lastFMRestClient.getApiService().getArtistInfo(model.artistName, null, model.skipOkHttpCache ? "no-cache" : null).execute();
|
||||
|
||||
if (!response.isSuccessful()) {
|
||||
throw new IOException("Request failed with code: " + response.code());
|
||||
|
|
|
|||
|
|
@ -18,7 +18,6 @@ import okhttp3.Response;
|
|||
import retrofit2.Retrofit;
|
||||
import retrofit2.converter.gson.GsonConverterFactory;
|
||||
|
||||
|
||||
/**
|
||||
* @author Karim Abou Zeid (kabouzeid)
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -1,4 +1,3 @@
|
|||
|
||||
package com.kabouzeid.gramophone.lastfm.rest.model;
|
||||
|
||||
import com.google.gson.annotations.Expose;
|
||||
|
|
@ -8,7 +7,6 @@ import java.util.ArrayList;
|
|||
import java.util.List;
|
||||
|
||||
public class LastFmAlbum {
|
||||
|
||||
@Expose
|
||||
private Album album;
|
||||
|
||||
|
|
@ -21,9 +19,10 @@ public class LastFmAlbum {
|
|||
}
|
||||
|
||||
public static class Album {
|
||||
|
||||
@Expose
|
||||
private List<Image> image = new ArrayList<>();
|
||||
@Expose
|
||||
private Wiki wiki;
|
||||
|
||||
public List<Image> getImage() {
|
||||
return image;
|
||||
|
|
@ -33,8 +32,26 @@ public class LastFmAlbum {
|
|||
this.image = image;
|
||||
}
|
||||
|
||||
public static class Image {
|
||||
public Wiki getWiki() { return wiki; }
|
||||
|
||||
public void setWiki(Wiki wiki) {
|
||||
this.wiki = wiki;
|
||||
}
|
||||
|
||||
public class Wiki {
|
||||
@Expose
|
||||
private String content;
|
||||
|
||||
public String getContent() {
|
||||
return content;
|
||||
}
|
||||
|
||||
public void setContent(String content) {
|
||||
this.content = content;
|
||||
}
|
||||
}
|
||||
|
||||
public static class Image {
|
||||
@SerializedName("#text")
|
||||
@Expose
|
||||
private String Text;
|
||||
|
|
@ -58,5 +75,4 @@ public class LastFmAlbum {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,3 @@
|
|||
|
||||
package com.kabouzeid.gramophone.lastfm.rest.model;
|
||||
|
||||
import com.google.gson.annotations.Expose;
|
||||
|
|
@ -8,7 +7,6 @@ import java.util.ArrayList;
|
|||
import java.util.List;
|
||||
|
||||
public class LastFmArtist {
|
||||
|
||||
@Expose
|
||||
private Artist artist;
|
||||
|
||||
|
|
@ -21,7 +19,6 @@ public class LastFmArtist {
|
|||
}
|
||||
|
||||
public static class Artist {
|
||||
|
||||
@Expose
|
||||
private List<Image> image = new ArrayList<>();
|
||||
@Expose
|
||||
|
|
@ -44,7 +41,6 @@ public class LastFmArtist {
|
|||
}
|
||||
|
||||
public class Bio {
|
||||
|
||||
@Expose
|
||||
private String content;
|
||||
|
||||
|
|
@ -55,11 +51,9 @@ public class LastFmArtist {
|
|||
public void setContent(String content) {
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class Image {
|
||||
|
||||
@SerializedName("#text")
|
||||
@Expose
|
||||
private String Text;
|
||||
|
|
@ -81,9 +75,6 @@ public class LastFmArtist {
|
|||
public void setSize(String size) {
|
||||
this.size = size;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,8 +18,8 @@ public interface LastFMService {
|
|||
String BASE_QUERY_PARAMETERS = "?format=json&autocorrect=1&api_key=" + API_KEY;
|
||||
|
||||
@GET(BASE_QUERY_PARAMETERS + "&method=album.getinfo")
|
||||
Call<LastFmAlbum> getAlbumInfo(@Query("album") String albumName, @Query("artist") String artistName);
|
||||
Call<LastFmAlbum> getAlbumInfo(@Query("album") String albumName, @Query("artist") String artistName, @Nullable @Query("lang") String language);
|
||||
|
||||
@GET(BASE_QUERY_PARAMETERS + "&method=artist.getinfo")
|
||||
Call<LastFmArtist> getArtistInfo(@Query("artist") String artistName, @Nullable @Header("Cache-Control") String cacheControl);
|
||||
Call<LastFmArtist> getArtistInfo(@Query("artist") String artistName, @Nullable @Query("lang") String language, @Nullable @Header("Cache-Control") String cacheControl);
|
||||
}
|
||||
|
|
@ -5,18 +5,23 @@ import android.content.Intent;
|
|||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v4.app.LoaderManager;
|
||||
import android.support.v4.content.Loader;
|
||||
import android.support.v7.widget.GridLayoutManager;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.support.v7.widget.Toolbar;
|
||||
import android.text.Html;
|
||||
import android.text.Spanned;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.afollestad.materialcab.MaterialCab;
|
||||
import com.afollestad.materialdialogs.MaterialDialog;
|
||||
import com.afollestad.materialdialogs.util.DialogUtils;
|
||||
import com.bumptech.glide.Glide;
|
||||
import com.bumptech.glide.request.RequestListener;
|
||||
|
|
@ -34,6 +39,8 @@ import com.kabouzeid.gramophone.helper.MusicPlayerRemote;
|
|||
import com.kabouzeid.gramophone.interfaces.CabHolder;
|
||||
import com.kabouzeid.gramophone.interfaces.LoaderIds;
|
||||
import com.kabouzeid.gramophone.interfaces.PaletteColorHolder;
|
||||
import com.kabouzeid.gramophone.lastfm.rest.LastFMRestClient;
|
||||
import com.kabouzeid.gramophone.lastfm.rest.model.LastFmAlbum;
|
||||
import com.kabouzeid.gramophone.loader.AlbumLoader;
|
||||
import com.kabouzeid.gramophone.misc.SimpleObservableScrollViewCallbacks;
|
||||
import com.kabouzeid.gramophone.misc.WrappedAsyncTaskLoader;
|
||||
|
|
@ -45,8 +52,13 @@ import com.kabouzeid.gramophone.util.NavigationUtil;
|
|||
import com.kabouzeid.gramophone.util.PhonographColorUtil;
|
||||
import com.kabouzeid.gramophone.util.Util;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
import butterknife.BindView;
|
||||
import butterknife.ButterKnife;
|
||||
import retrofit2.Call;
|
||||
import retrofit2.Callback;
|
||||
import retrofit2.Response;
|
||||
|
||||
/**
|
||||
* Be careful when changing things in this Activity!
|
||||
|
|
@ -81,6 +93,11 @@ public class AlbumDetailActivity extends AbsSlidingMusicPanelActivity implements
|
|||
private int toolbarColor;
|
||||
private float toolbarAlpha;
|
||||
|
||||
@Nullable
|
||||
private Spanned wiki;
|
||||
private MaterialDialog wikiDialog;
|
||||
private LastFMRestClient lastFMRestClient;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
|
@ -89,6 +106,8 @@ public class AlbumDetailActivity extends AbsSlidingMusicPanelActivity implements
|
|||
|
||||
supportPostponeEnterTransition();
|
||||
|
||||
lastFMRestClient = new LastFMRestClient(this);
|
||||
|
||||
setUpObservableListViewParams();
|
||||
setUpToolBar();
|
||||
setUpViews();
|
||||
|
|
@ -233,6 +252,49 @@ public class AlbumDetailActivity extends AbsSlidingMusicPanelActivity implements
|
|||
return true;
|
||||
}
|
||||
|
||||
private void loadWiki() {
|
||||
loadWiki(Locale.getDefault().getLanguage());
|
||||
}
|
||||
|
||||
private void loadWiki(@Nullable final String lang) {
|
||||
wiki = null;
|
||||
|
||||
lastFMRestClient.getApiService()
|
||||
.getAlbumInfo(getAlbum().getTitle(), getAlbum().getArtistName(), lang)
|
||||
.enqueue(new Callback<LastFmAlbum>() {
|
||||
@Override
|
||||
public void onResponse(@NonNull Call<LastFmAlbum> call, @NonNull Response<LastFmAlbum> response) {
|
||||
final LastFmAlbum lastFmAlbum = response.body();
|
||||
if (lastFmAlbum != null && lastFmAlbum.getAlbum() != null && lastFmAlbum.getAlbum().getWiki() != null) {
|
||||
final String wikiContent = lastFmAlbum.getAlbum().getWiki().getContent();
|
||||
if (wikiContent != null && !wikiContent.trim().isEmpty()) {
|
||||
wiki = Html.fromHtml(wikiContent);
|
||||
}
|
||||
}
|
||||
|
||||
// If the "lang" parameter is set and no wiki is given, retry with default language
|
||||
if (wiki == null && lang != null) {
|
||||
loadWiki(null);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!Util.isAllowedToDownloadMetadata(AlbumDetailActivity.this)) {
|
||||
if (wiki != null) {
|
||||
wikiDialog.setContent(wiki);
|
||||
} else {
|
||||
wikiDialog.dismiss();
|
||||
Toast.makeText(AlbumDetailActivity.this, getResources().getString(R.string.wiki_unavailable), Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(@NonNull Call<LastFmAlbum> call, @NonNull Throwable t) {
|
||||
t.printStackTrace();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
|
||||
int id = item.getItemId();
|
||||
|
|
@ -257,6 +319,25 @@ public class AlbumDetailActivity extends AbsSlidingMusicPanelActivity implements
|
|||
case R.id.action_go_to_artist:
|
||||
NavigationUtil.goToArtist(this, getAlbum().getArtistId());
|
||||
return true;
|
||||
case R.id.action_wiki:
|
||||
if (wikiDialog == null) {
|
||||
wikiDialog = new MaterialDialog.Builder(this)
|
||||
.title(album.getTitle())
|
||||
.positiveText(android.R.string.ok)
|
||||
.build();
|
||||
}
|
||||
if (Util.isAllowedToDownloadMetadata(this)) {
|
||||
if (wiki != null) {
|
||||
wikiDialog.setContent(wiki);
|
||||
wikiDialog.show();
|
||||
} else {
|
||||
Toast.makeText(this, getResources().getString(R.string.wiki_unavailable), Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
} else {
|
||||
wikiDialog.show();
|
||||
loadWiki();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
|
|
@ -323,6 +404,11 @@ public class AlbumDetailActivity extends AbsSlidingMusicPanelActivity implements
|
|||
private void setAlbum(Album album) {
|
||||
this.album = album;
|
||||
loadAlbumCover();
|
||||
|
||||
if (Util.isAllowedToDownloadMetadata(this)) {
|
||||
loadWiki();
|
||||
}
|
||||
|
||||
albumTitleView.setText(album.getTitle());
|
||||
adapter.swapDataSet(album.songs);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -56,6 +56,8 @@ import com.kabouzeid.gramophone.util.PhonographColorUtil;
|
|||
import com.kabouzeid.gramophone.util.PreferenceUtil;
|
||||
import com.kabouzeid.gramophone.util.Util;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
import butterknife.BindView;
|
||||
import butterknife.ButterKnife;
|
||||
import retrofit2.Call;
|
||||
|
|
@ -97,6 +99,7 @@ public class ArtistDetailActivity extends AbsSlidingMusicPanelActivity implement
|
|||
private Artist artist;
|
||||
@Nullable
|
||||
private Spanned biography;
|
||||
private MaterialDialog biographyDialog;
|
||||
private HorizontalAlbumAdapter albumAdapter;
|
||||
private ArtistSongAdapter songAdapter;
|
||||
|
||||
|
|
@ -219,34 +222,47 @@ public class ArtistDetailActivity extends AbsSlidingMusicPanelActivity implement
|
|||
}
|
||||
|
||||
private void loadBiography() {
|
||||
lastFMRestClient.getApiService().getArtistInfo(getArtist().getName(), null).enqueue(new Callback<LastFmArtist>() {
|
||||
@Override
|
||||
public void onResponse(Call<LastFmArtist> call, Response<LastFmArtist> response) {
|
||||
LastFmArtist lastFmArtist = response.body();
|
||||
if (lastFmArtist.getArtist() != null) {
|
||||
String bio = lastFmArtist.getArtist().getBio().getContent();
|
||||
if (bio != null && !bio.trim().equals("")) {
|
||||
biography = Html.fromHtml(bio);
|
||||
return;
|
||||
}
|
||||
}
|
||||
biography = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Call<LastFmArtist> call, Throwable t) {
|
||||
t.printStackTrace();
|
||||
biography = null;
|
||||
}
|
||||
});
|
||||
loadBiography(Locale.getDefault().getLanguage());
|
||||
}
|
||||
|
||||
private MaterialDialog getBiographyDialog() {
|
||||
return new MaterialDialog.Builder(ArtistDetailActivity.this)
|
||||
.title(getArtist().getName())
|
||||
.content(biography != null ? biography : "")
|
||||
.positiveText(android.R.string.ok)
|
||||
.build();
|
||||
private void loadBiography(@Nullable final String lang) {
|
||||
biography = null;
|
||||
|
||||
lastFMRestClient.getApiService()
|
||||
.getArtistInfo(getArtist().getName(), lang, null)
|
||||
.enqueue(new Callback<LastFmArtist>() {
|
||||
@Override
|
||||
public void onResponse(@NonNull Call<LastFmArtist> call, @NonNull Response<LastFmArtist> response) {
|
||||
final LastFmArtist lastFmArtist = response.body();
|
||||
if (lastFmArtist != null && lastFmArtist.getArtist() != null) {
|
||||
final String bioContent = lastFmArtist.getArtist().getBio().getContent();
|
||||
if (bioContent != null && !bioContent.trim().isEmpty()) {
|
||||
biography = Html.fromHtml(bioContent);
|
||||
}
|
||||
}
|
||||
|
||||
// If the "lang" parameter is set and no biography is given, retry with default language
|
||||
if (biography == null && lang != null) {
|
||||
loadBiography(null);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!Util.isAllowedToDownloadMetadata(ArtistDetailActivity.this)) {
|
||||
if (biography != null) {
|
||||
biographyDialog.setContent(biography);
|
||||
} else {
|
||||
biographyDialog.dismiss();
|
||||
Toast.makeText(ArtistDetailActivity.this, getResources().getString(R.string.biography_unavailable), Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(@NonNull Call<LastFmArtist> call, @NonNull Throwable t) {
|
||||
t.printStackTrace();
|
||||
biography = null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void loadArtistImage(final boolean forceDownload) {
|
||||
|
|
@ -339,10 +355,22 @@ public class ArtistDetailActivity extends AbsSlidingMusicPanelActivity implement
|
|||
super.onBackPressed();
|
||||
return true;
|
||||
case R.id.action_biography:
|
||||
if (biography != null) {
|
||||
getBiographyDialog().show();
|
||||
if (biographyDialog == null) {
|
||||
biographyDialog = new MaterialDialog.Builder(this)
|
||||
.title(artist.getName())
|
||||
.positiveText(android.R.string.ok)
|
||||
.build();
|
||||
}
|
||||
if (Util.isAllowedToDownloadMetadata(ArtistDetailActivity.this)) {
|
||||
if (biography != null) {
|
||||
biographyDialog.setContent(biography);
|
||||
biographyDialog.show();
|
||||
} else {
|
||||
Toast.makeText(ArtistDetailActivity.this, getResources().getString(R.string.biography_unavailable), Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
} else {
|
||||
Toast.makeText(ArtistDetailActivity.this, getResources().getString(R.string.biography_unavailable), Toast.LENGTH_SHORT).show();
|
||||
biographyDialog.show();
|
||||
loadBiography();
|
||||
}
|
||||
return true;
|
||||
case R.id.action_re_download_artist_image:
|
||||
|
|
@ -410,7 +438,11 @@ public class ArtistDetailActivity extends AbsSlidingMusicPanelActivity implement
|
|||
private void setArtist(Artist artist) {
|
||||
this.artist = artist;
|
||||
loadArtistImage(false);
|
||||
loadBiography();
|
||||
|
||||
if (Util.isAllowedToDownloadMetadata(this)) {
|
||||
loadBiography();
|
||||
}
|
||||
|
||||
artistName.setText(artist.getName());
|
||||
songAdapter.swapDataSet(artist.getSongs());
|
||||
albumAdapter.swapDataSet(artist.albums);
|
||||
|
|
|
|||
|
|
@ -100,7 +100,7 @@ public class AlbumTagEditorActivity extends AbsTagEditorActivity implements Text
|
|||
Toast.makeText(this, getResources().getString(R.string.album_or_artist_empty), Toast.LENGTH_SHORT).show();
|
||||
return;
|
||||
}
|
||||
lastFMRestClient.getApiService().getAlbumInfo(albumTitleStr, albumArtistNameStr).enqueue(new Callback<LastFmAlbum>() {
|
||||
lastFMRestClient.getApiService().getAlbumInfo(albumTitleStr, albumArtistNameStr, null).enqueue(new Callback<LastFmAlbum>() {
|
||||
@Override
|
||||
public void onResponse(Call<LastFmAlbum> call, Response<LastFmAlbum> response) {
|
||||
LastFmAlbum lastFmAlbum = response.body();
|
||||
|
|
|
|||
|
|
@ -121,7 +121,7 @@ public class Util {
|
|||
} else return false;
|
||||
}
|
||||
|
||||
public static boolean isAllowedToAutoDownload(final Context context) {
|
||||
public static boolean isAllowedToDownloadMetadata(final Context context) {
|
||||
switch (PreferenceUtil.getInstance(context).autoDownloadImagesPolicy()) {
|
||||
case "always":
|
||||
return true;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue