Skip to content

Commit

Permalink
Use better no-cover solution
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaned1as committed May 5, 2020
1 parent 542f05f commit f282536
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class CoverArchiveEngine implements CoverEngine {
private static final String TAG = CoverArchiveEngine.class.getSimpleName();

@Override
public byte[] getCover(String trackName, String artistName, String albumName, String fileName) {
public byte[] getCover(String trackName, String artistName, String albumName) {
try {
if (trackName != null && artistName != null) {
return makeApiCall(String.format("recording:%s AND artistname:%s", trackName, artistName));
Expand All @@ -55,12 +55,13 @@ public byte[] getCover(String trackName, String artistName, String albumName, St
return makeApiCall(String.format("recording:%s AND releasegroup:%s", trackName, albumName));
}

// cover can be found by artist + album
if (artistName != null && albumName != null) {
return makeApiCall(String.format("releasegroup:%s AND artistname:%s", albumName, artistName));
}

return makeApiCall(String.format("recording:%s", fileName));

// even then track gives us pretty good idea what can it be
return makeApiCall(String.format("recording:%s", trackName));
} catch (IOException e) {
Log.w(TAG, "Couldn't connect to musicbrainz/coverartarchive REST endpoints", e);
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ public interface CoverEngine {
* Synchronous call to engine to retrieve cover. Most likely to be used in {@link HandlerThread}
* or {@link Loader}
*
* @param artistName band or artist name to search for
* @param albumName full album name to search for
* @param trackName track name to search for. Never null
* @param artistName band or artist name to search for. Can be null
* @param albumName full album name to search for. Can be null
* @return byte array containing album cover if available, null if nothing found
*/
byte[] getCover(String trackName, String artistName, String albumName, String fileName);
byte[] getCover(String trackName, String artistName, String albumName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import android.provider.MediaStore;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
Expand All @@ -44,9 +43,8 @@
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.SearchView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewSwitcher;
import android.widget.ViewFlipper;

import androidx.annotation.NonNull;
import androidx.core.content.FileProvider;
Expand All @@ -69,7 +67,6 @@
import java.util.UUID;

import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;
import static android.view.View.GONE;
import static android.view.View.VISIBLE;
import static com.kanedias.vanilla.plugins.PluginConstants.*;
import static com.kanedias.vanilla.plugins.PluginUtils.*;
Expand Down Expand Up @@ -113,7 +110,7 @@ public class CoverShowActivity extends DialogActivity {
private SharedPreferences mPrefs;

private ImageView mCoverImage;
private ViewSwitcher mSwitcher;
private ViewFlipper mSwitcher;
private Button mOkButton, mWriteButton;
private ProgressBar mProgressBar;

Expand Down Expand Up @@ -225,7 +222,7 @@ public boolean onQueryTextChange(String newText) {
case R.id.open_local_option:
case R.id.custom_search_option:
// show only when loading is complete
item.setVisible(mSwitcher != null && mSwitcher.getDisplayedChild() == 1);
item.setVisible(mSwitcher != null && mSwitcher.getDisplayedChild() != 0);
continue;
default:
break;
Expand Down Expand Up @@ -278,6 +275,18 @@ private void handleUiIntent(boolean useLocal) {
String title = getIntent().getStringExtra(EXTRA_PARAM_SONG_TITLE);
if (title != null && title.contains("No Title")) {
title = null;

// use file name
Uri fileUri = getIntent().getParcelableExtra(EXTRA_PARAM_URI);
if (fileUri != null) {
String fileName = fileUri.getLastPathSegment();
if (fileName != null) {
int extensionStart = fileName.lastIndexOf(".");
if (extensionStart > 0) {
title = fileName.substring(0, extensionStart);
}
}
}
}
String artist = getIntent().getStringExtra(EXTRA_PARAM_SONG_ARTIST);
if (artist != null && artist.contains("No Artist")) {
Expand All @@ -288,17 +297,7 @@ private void handleUiIntent(boolean useLocal) {
album = null;
}

Uri fileUri = getIntent().getParcelableExtra(EXTRA_PARAM_URI);
String fileName = null;
if (fileUri != null) {
fileName = fileUri.getLastPathSegment();
int extensionStart = fileName.lastIndexOf(".");
if (extensionStart > 0) {
fileName = fileName.substring(0, extensionStart);
}
}

new ArtworkFetcher().execute(title, artist, album, fileName);
new ArtworkFetcher().execute(title, artist, album);
}

private boolean loadFromTag() {
Expand Down Expand Up @@ -347,17 +346,14 @@ private boolean loadFromFile() {
return true;
}

/**
* Set the actual cover image for the user to see
*
* @param raw raw image bitmap. Should never be null
*/
private void setCoverImage(Bitmap raw) {
Drawable image;
if (raw == null) {
// we don't have bitmap, show sad cloud
image = getResources().getDrawable(R.drawable.sad_cloud);
mWriteButton.setEnabled(false);
} else {
// we have some bitmap
image = new BitmapDrawable(getResources(), raw);
mWriteButton.setEnabled(true);
}
Drawable image = new BitmapDrawable(getResources(), raw);
mWriteButton.setEnabled(true);

mCoverImage.setImageDrawable(image);
mSwitcher.setDisplayedChild(1);
Expand Down Expand Up @@ -589,7 +585,7 @@ protected void onPreExecute() {

@Override
protected byte[] doInBackground(String... params) {
return mEngine.getCover(params[0], params[1], params[2], params[3]);
return mEngine.getCover(params[0], params[1], params[2]);
}

@Override
Expand All @@ -599,7 +595,8 @@ protected void onPostExecute(byte[] imgData) {
if (imgData == null || imgData.length == 0) {
// no artwork - show excuse
Toast.makeText(CoverShowActivity.this, R.string.cover_not_found, Toast.LENGTH_SHORT).show();
setCoverImage(null);
mSwitcher.setDisplayedChild(2);
invalidateOptionsMenu();
return;
}

Expand Down
12 changes: 10 additions & 2 deletions app/src/main/res/layout/activity_cover_show.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
android:paddingRight="16dp"
tools:context=".CoverShowActivity">

<ViewSwitcher
<ViewFlipper
android:id="@+id/loading_switcher"
android:layout_width="match_parent"
android:layout_height="wrap_content">
Expand All @@ -32,7 +32,15 @@
android:adjustViewBounds="true"
android:maxHeight="300dp" />

</ViewSwitcher>
<ImageView
android:layout_width="96dp"
android:layout_height="96dp"
android:layout_gravity="center"
android:scaleType="fitCenter"
android:src="@drawable/sad_cloud"
android:contentDescription="@string/cover_not_found"/>

</ViewFlipper>

<LinearLayout
android:layout_width="match_parent"
Expand Down

0 comments on commit f282536

Please sign in to comment.