adding an initial implementation of showing album art

This commit is contained in:
tibbi 2020-10-03 23:15:31 +02:00
parent bc44bb240e
commit c1f2c2a6a7
6 changed files with 62 additions and 9 deletions

View file

@ -63,6 +63,7 @@ dependencies {
implementation 'com.squareup.picasso:picasso:2.71828'
implementation 'androidx.media:media:1.2.0'
implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
kapt "androidx.room:room-compiler:2.2.5"
implementation "androidx.room:room-runtime:2.2.5"

View file

@ -1,8 +1,14 @@
package com.simplemobiletools.musicplayer.adapters
import android.content.ContentUris
import android.net.Uri
import android.view.Menu
import android.view.View
import android.view.ViewGroup
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.simplemobiletools.commons.adapters.MyRecyclerViewAdapter
import com.simplemobiletools.commons.views.MyRecyclerView
import com.simplemobiletools.musicplayer.R
@ -55,6 +61,17 @@ class ArtistsAdapter(activity: SimpleActivity, val artists: ArrayList<Artist>, r
artist_frame?.isSelected = selectedKeys.contains(artist.id)
artist_title.text = artist.title
artist_title.setTextColor(textColor)
val artworkUri = Uri.parse("content://media/external/audio/albumart")
val albumArtUri = ContentUris.withAppendedId(artworkUri, artist.albumArtId)
val options = RequestOptions()
.transform(CenterCrop(), RoundedCorners(10))
Glide.with(activity)
.load(albumArtUri)
.apply(options)
.into(findViewById(R.id.artist_image))
}
}
}

View file

@ -7,6 +7,7 @@ import android.provider.MediaStore.Audio
import android.util.AttributeSet
import com.google.gson.Gson
import com.simplemobiletools.commons.extensions.getIntValue
import com.simplemobiletools.commons.extensions.getLongValue
import com.simplemobiletools.commons.extensions.getStringValue
import com.simplemobiletools.commons.extensions.showErrorToast
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
@ -54,7 +55,8 @@ class ArtistsFragment(context: Context, attributeSet: AttributeSet) : MyViewPage
val title = cursor.getStringValue(Audio.Artists.ARTIST)
val albumCnt = cursor.getIntValue(Audio.ArtistColumns.NUMBER_OF_ALBUMS)
val trackCnt = cursor.getIntValue(Audio.ArtistColumns.NUMBER_OF_TRACKS)
val artist = Artist(id, title, albumCnt, trackCnt)
val albumArtId = getArtistAlbumId(activity, id)
val artist = Artist(id, title, albumCnt, trackCnt, albumArtId)
artists.add(artist)
} while (cursor.moveToNext())
}
@ -68,4 +70,22 @@ class ArtistsFragment(context: Context, attributeSet: AttributeSet) : MyViewPage
}
}
}
private fun getArtistAlbumId(activity: Activity, artistId: Int): Long {
val uri = Audio.Albums.EXTERNAL_CONTENT_URI
val projection = arrayOf(Audio.Albums._ID)
val selection = "${Audio.Albums.ARTIST_ID} = ?"
val selectionArgs = arrayOf(artistId.toString())
try {
val cursor = activity.contentResolver.query(uri, projection, selection, selectionArgs, null)
cursor?.use {
if (cursor.moveToFirst()) {
return cursor.getLongValue(Audio.Albums._ID)
}
}
} catch (e: Exception) {
}
return 0L
}
}

View file

@ -1,3 +1,3 @@
package com.simplemobiletools.musicplayer.models
data class Artist(val id: Int, val title: String, val albumCnt: Int, val trackCnt: Int)
data class Artist(val id: Int, val title: String, val albumCnt: Int, val trackCnt: Int, val albumArtId: Long)

View file

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/artist_frame"
android:layout_width="match_parent"
android:layout_height="wrap_content"
@ -8,20 +9,33 @@
android:focusable="true"
android:foreground="@drawable/selector">
<RelativeLayout
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/artist_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/activity_margin">
<ImageView
android:id="@+id/artist_image"
android:layout_width="@dimen/artist_image_size"
android:layout_height="@dimen/artist_image_size"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.simplemobiletools.commons.views.MyTextView
android:id="@+id/artist_title"
android:layout_width="match_parent"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:paddingLeft="@dimen/medium_margin"
android:paddingRight="@dimen/medium_margin"
android:text="@string/text_color" />
android:ellipsize="end"
android:gravity="start"
android:maxLines="2"
android:paddingStart="@dimen/activity_margin"
android:text="@string/text_color"
android:textSize="@dimen/bigger_text_size"
app:layout_constraintBottom_toBottomOf="@+id/artist_image"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/artist_image"
app:layout_constraintTop_toTopOf="@+id/artist_image" />
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout>

View file

@ -1,4 +1,5 @@
<resources>
<dimen name="controls_row_height">60dp</dimen>
<dimen name="top_art_height">220dp</dimen>
<dimen name="artist_image_size">80dp</dimen>
</resources>