Skip to content

Commit

Permalink
user changeable last fm api key
Browse files Browse the repository at this point in the history
  • Loading branch information
segler-alex committed Dec 28, 2020
1 parent a3a3728 commit c11a522
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 22 deletions.
9 changes: 0 additions & 9 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,6 @@ android {
}
}

buildTypes.each {
it.buildConfigField 'String', 'LastFMAPIKey', '"' + getLastFMApiKey() + '"'
}

buildTypes {
release {
minifyEnabled false
Expand Down Expand Up @@ -123,11 +119,6 @@ android {
}
}

def getLastFMApiKey() {
def value = project.getProperties().get('lastFMApiKey')
return value != null ? value : ''
}

ext {
exoplayerVersion = '2.11.7'
iconicsVersion = '4.0.2'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.os.Build;
Expand All @@ -30,6 +31,7 @@
import androidx.lifecycle.ViewModelProviders;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.paging.PagedList;
import androidx.preference.PreferenceManager;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
Expand Down Expand Up @@ -564,8 +566,12 @@ private void updateAlbumArt() {
return;
}

final RadioDroidApp radioDroidApp = (RadioDroidApp) requireActivity().getApplication();
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(radioDroidApp);
String LastFMApiKey = sharedPref.getString("last_fm_api_key", "");

if (TextUtils.isEmpty(liveInfo.getArtist()) || TextUtils.isEmpty(liveInfo.getTrack()) ||
BuildConfig.LastFMAPIKey.isEmpty()) {
LastFMApiKey.isEmpty()) {
if (station.hasIcon()) {
// TODO: Check if we already have this station's icon loaded into image view
Picasso.get()
Expand All @@ -585,7 +591,6 @@ private void updateAlbumArt() {
trackMetadataCallback.cancel();
}

final RadioDroidApp radioDroidApp = (RadioDroidApp) requireActivity().getApplication();
TrackMetadataSearcher trackMetadataSearcher = radioDroidApp.getTrackMetadataSearcher();

final WeakReference<FragmentPlayerFull> fragmentWeakReference = new WeakReference<>(this);
Expand All @@ -604,7 +609,7 @@ private void updateAlbumArt() {
fragment.requireActivity().runOnUiThread(() -> {
if (fragment.isResumed()) {
fragment.trackMetadataCallback = new PlayerTrackMetadataCallback(fragmentWeakReference, trackHistoryEntry);
trackMetadataSearcher.fetchTrackMetadata(liveInfo.getArtist(), liveInfo.getTrack(), fragment.trackMetadataCallback);
trackMetadataSearcher.fetchTrackMetadata(LastFMApiKey, liveInfo.getArtist(), liveInfo.getTrack(), fragment.trackMetadataCallback);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public TrackMetadataSearcher(OkHttpClient httpClient) {
}


public void fetchTrackMetadata(String artist, @NonNull String track, @NonNull TrackMetadataCallback trackMetadataCallback) {
lfmMetadataSearcher.fetchTrackMetadata(artist, track, trackMetadataCallback);
public void fetchTrackMetadata(String LastFMApiKey, String artist, @NonNull String track, @NonNull TrackMetadataCallback trackMetadataCallback) {
lfmMetadataSearcher.fetchTrackMetadata(LastFMApiKey, artist, track, trackMetadataCallback);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ private String tryNormalizeTrack(@NonNull final String track) {
return normalizedTrack.equals(track) ? null : normalizedTrack;
}

public void fetchTrackMetadata(String artist, @NonNull final String track, @NonNull final TrackMetadataCallback trackMetadataCallback) {
if (BuildConfig.LastFMAPIKey.isEmpty() || TextUtils.isEmpty(track)) {
public void fetchTrackMetadata(String LastFMApiKey, String artist, @NonNull final String track, @NonNull final TrackMetadataCallback trackMetadataCallback) {
if (LastFMApiKey.isEmpty() || TextUtils.isEmpty(track)) {
trackMetadataCallback.onFailure(TrackMetadataCallback.FailureType.UNRECOVERABLE);
return;
}
Expand All @@ -74,15 +74,15 @@ public void fetchTrackMetadata(String artist, @NonNull final String track, @NonN

// We want to rate limit calls to Last.fm API to prevent exceeding unknown limits.
if (rateLimiter.allowed()) {
httpClient.newCall(buildRequest(trimmedArtist, trimmedTrack))
.enqueue(new MetadataCallback(trackMetadataCallback, trimmedArtist, trimmedTrack));
httpClient.newCall(buildRequest(LastFMApiKey, trimmedArtist, trimmedTrack))
.enqueue(new MetadataCallback(trackMetadataCallback, LastFMApiKey, trimmedArtist, trimmedTrack));
} else {
trackMetadataCallback.onFailure(TrackMetadataCallback.FailureType.RECOVERABLE);
}
}

private Request buildRequest(String artist, String track) {
HttpUrl url = HttpUrl.parse(String.format(API_GET_TRACK_METADATA, BuildConfig.LastFMAPIKey, artist, track));
private Request buildRequest(String LastFMApiKey, String artist, String track) {
HttpUrl url = HttpUrl.parse(String.format(API_GET_TRACK_METADATA, LastFMApiKey, artist, track));
Request.Builder requestBuilder = new Request.Builder().url(url).get();
return requestBuilder.build();
}
Expand All @@ -91,11 +91,13 @@ private class MetadataCallback implements Callback {
private final TrackMetadataCallback trackMetadataCallback;
private final String artist;
private final String track;
private final String LastFMApiKey;

public MetadataCallback(TrackMetadataCallback trackMetadataCallback, String artist, String track) {
public MetadataCallback(TrackMetadataCallback trackMetadataCallback, String LastFMApiKey, String artist, String track) {
this.trackMetadataCallback = trackMetadataCallback;
this.track = track;
this.artist = artist;
this.LastFMApiKey = LastFMApiKey;
}

@Override
Expand All @@ -115,7 +117,7 @@ public void onResponse(Call call, Response response) throws IOException {
if (trackData == null) {
String normalizedTrack = tryNormalizeTrack(track);
if (normalizedTrack != null && normalizedTrack.length() > 3) {
httpClient.newCall(buildRequest(artist, normalizedTrack)).enqueue(new MetadataCallback(trackMetadataCallback, artist, normalizedTrack));
httpClient.newCall(buildRequest(LastFMApiKey, artist, normalizedTrack)).enqueue(new MetadataCallback(trackMetadataCallback, LastFMApiKey, artist, normalizedTrack));
} else {
trackMetadataCallback.onFailure(TrackMetadataCallback.FailureType.UNRECOVERABLE);
}
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@
<string name="player_info_recording_to">Wird aufgenommen als:\n%1$s</string>
<string name="player_info_recorded_to">Zuletzt aufgenommen:\n%1$s</string>

<string name="settings_lastfm_api_key">Last FM API Schlüssel</string>
<string name="settings_lastfm_api_key_desc">Mit LastFM werden Album Bilder im Player angezeigt</string>
<string name="station_detail_bitrate">%1$1d kbps</string>
<string name="station_detail_broken">KAPUTT</string>

Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,8 @@
<string name="action_play_in_external">Play in external player</string>
<string name="action_choose_external_player">Choose external player</string>

<string name="settings_lastfm_api_key">Last FM API key</string>
<string name="settings_lastfm_api_key_desc">Put in an api key to enable album art in full screen player</string>
<string name="settings_other">Other</string>
<string name="settings_statistics">Statistics</string>
<string name="settings_about">About RadioDroid</string>
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,11 @@
search:ignore="true"
android:key="pref_category_other">

<EditTextPreference
android:key="last_fm_api_key"
android:title="@string/settings_lastfm_api_key"
android:summary="@string/settings_lastfm_api_key_desc"/>

<Preference
android:key="show_statistics"
android:title="@string/settings_statistics" />
Expand Down

0 comments on commit c11a522

Please sign in to comment.