muzei-api / com.google.android.apps.muzei.api / MuzeiContract / Artwork

Artwork

object Artwork

Constants and helper methods for the Artwork table, providing access to the current artwork.

The Artwork table contains the details of the artwork that has been loaded by Muzei. It also provides direct access to the cached image already downloaded by Muzei, ensuring that you do not need to do additional networks requests or have internet access when retrieving previously loaded artwork.

Working with the Artwork table

Querying CONTENT_URI will return either zero rows (in cases where the user has never activated Muzei before) or a row for each previously loaded artwork with all of the details needed to create an Artwork object. The helper method getCurrentArtwork builds an Artwork object for the most recent artwork, although you can certainly query the CONTENT_URI directly and either use the columns directly or use Artwork.fromCursor to parse the Cursor for you.

If instead you use ContentResolver.openInputStream, you'll get an InputStream for the cached image Bitmap. This can then be passed to BitmapFactory.decodeStream or similar methods to retrieve the Bitmap itself. The helper method getCurrentArtworkBitmap does this operation, although note that this may return a very large Bitmap so following the Handling Bitmaps documentation advice is highly suggested.

Listening for changes

Just like any ContentProvider, listening for changes can be done by implementing a ContentObserver on CONTENT_URI to listen for updates.

On API 24+ devices, it is strongly recommended to use WorkManager or JobScheduler to listen for content URI changes in the background without maintaining a constantly running ContentObserver.

To support earlier versions of Android, you can listen for the ACTION_ARTWORK_CHANGED broadcast, sent out immediately after an update is made:

<receiver android:name=".ExampleArtworkUpdateReceiver">
  <intent-filter>
    <action android:name="com.google.android.apps.muzei.ACTION_ARTWORK_CHANGED" />
  </intent-filter>
</receiver>

No data is sent alongside the broadcast, but this can be used to kick off background processing to retrieve the latest artwork or start other processing.

Properties

_ID

The unique ID of the Artwork, suitable for use with ContentUris.withAppendedId.

const val _ID: String

ACTION_ARTWORK_CHANGED

Intent action that will be broadcast when the artwork is changed. This happens immediately after the ContentProvider is updated with data and should be considered the signal that you can retrieve the new artwork.

const val ACTION_ARTWORK_CHANGED: String

COLUMN_NAME_ATTRIBUTION

Column name for the artwork's attribution info.

const val COLUMN_NAME_ATTRIBUTION: String

COLUMN_NAME_BYLINE

Column name for the artwork's byline (such as author and date).

const val COLUMN_NAME_BYLINE: String

COLUMN_NAME_DATE_ADDED

Column name for when this artwork was added.

const val COLUMN_NAME_DATE_ADDED: String

COLUMN_NAME_IMAGE_URI

Column name of the artwork image URI. In almost all cases you should use ContentResolver.openInputStream(CONTENT_URI) to retrieve the already downloaded artwork.

const val COLUMN_NAME_IMAGE_URI: String

COLUMN_NAME_PROVIDER_AUTHORITY

Column name for the authority of the provider for this artwork.

const val COLUMN_NAME_PROVIDER_AUTHORITY: String

COLUMN_NAME_TITLE

Column name for the artwork's title.

const val COLUMN_NAME_TITLE: String

CONTENT_ITEM_TYPE

The MIME type of CONTENT_URI providing a single piece of artwork.

const val CONTENT_ITEM_TYPE: String

CONTENT_TYPE

The MIME type of CONTENT_URI providing artwork.

const val CONTENT_TYPE: String

CONTENT_URI

The content:// style URL for this table. This is the main entry point for queries and for opening an InputStream to the current artwork's image.

val CONTENT_URI: Uri

DEFAULT_SORT_ORDER

The default sort order for this table

const val DEFAULT_SORT_ORDER: String

TABLE_NAME

The table name offered by this provider.

const val TABLE_NAME: String

Functions

getCurrentArtwork

Returns the current Muzei Artwork.

fun getCurrentArtwork(context: Context): Artwork?

getCurrentArtworkBitmap

Naively gets the current artwork image without any subsampling or optimization for output size

fun getCurrentArtworkBitmap(context: Context): Bitmap?