FileDocCategorySizeDatePackage
MediaMetadataEditor.javaAPI DocAndroid 5.1 API19229Thu Mar 12 22:22:30 GMT 2015android.media

MediaMetadataEditor

public abstract class MediaMetadataEditor extends Object
An abstract class for editing and storing metadata that can be published by {@link RemoteControlClient}. See the {@link RemoteControlClient#editMetadata(boolean)} method to instantiate a {@link RemoteControlClient.MetadataEditor} object.
deprecated
Use {@link MediaMetadata} instead together with {@link MediaSession}.

Fields Summary
private static final String
TAG
public static final int
BITMAP_KEY_ARTWORK
The metadata key for the content artwork / album art.
public static final int
RATING_KEY_BY_OTHERS
The metadata key for the content's average rating, not the user's rating. The value associated with this key is a {@link Rating} instance.
public static final int
RATING_KEY_BY_USER
The metadata key for the content's user rating. The value associated with this key is a {@link Rating} instance. This key can be flagged as "editable" (with {@link #addEditableKey(int)}) to enable receiving user rating values through the {@link android.media.RemoteControlClient.OnMetadataUpdateListener} interface.
public static final int
KEY_EDITABLE_MASK
protected long
mEditableKeys
protected boolean
mMetadataChanged
protected boolean
mApplied
protected boolean
mArtworkChanged
protected android.graphics.Bitmap
mEditorArtwork
protected android.os.Bundle
mEditorMetadata
protected MediaMetadata.Builder
mMetadataBuilder
protected static final int
METADATA_TYPE_INVALID
protected static final int
METADATA_TYPE_LONG
protected static final int
METADATA_TYPE_STRING
protected static final int
METADATA_TYPE_BITMAP
protected static final int
METADATA_TYPE_RATING
protected static final android.util.SparseIntArray
METADATA_KEYS_TYPE
Constructors Summary
protected MediaMetadataEditor()

hide

          
      
    
Methods Summary
public synchronized voidaddEditableKey(int key)
Flags the given key as being editable. This should only be used by metadata publishers, such as {@link RemoteControlClient}, which will declare the metadata field as eligible to be updated, with new values received through the {@link RemoteControlClient.OnMetadataUpdateListener} interface.

param
key the type of metadata that can be edited. The supported key is {@link #RATING_KEY_BY_USER}.

        if (mApplied) {
            Log.e(TAG, "Can't change editable keys of a previously applied MetadataEditor");
            return;
        }
        // only one editable key at the moment, so we're not wasting memory on an array
        // of editable keys to check the validity of the key, just hardcode the supported key.
        if (key == RATING_KEY_BY_USER) {
            mEditableKeys |= (KEY_EDITABLE_MASK & key);
            mMetadataChanged = true;
        } else {
            Log.e(TAG, "Metadata key " + key + " cannot be edited");
        }
    
public abstract voidapply()
Applies all of the metadata changes that have been set since the MediaMetadataEditor instance was created or since {@link #clear()} was called.

public synchronized voidclear()
Clears all the pending metadata changes set since the MediaMetadataEditor instance was created or since this method was last called. Note that clearing the metadata doesn't reset the editable keys (use {@link #removeEditableKeys()} instead).


                                           
        
        if (mApplied) {
            Log.e(TAG, "Can't clear a previously applied MediaMetadataEditor");
            return;
        }
        mEditorMetadata.clear();
        mEditorArtwork = null;
        mMetadataBuilder = new MediaMetadata.Builder();
    
public synchronized android.graphics.BitmapgetBitmap(int key, android.graphics.Bitmap defaultValue)
Returns the {@link Bitmap} value for the key.

param
key the {@link #BITMAP_KEY_ARTWORK} key
param
defaultValue the value returned if the key is not present
return
the {@link Bitmap} value for the key, or the supplied default value if the key is not present
throws
IllegalArgumentException

        if (key != BITMAP_KEY_ARTWORK) {
            throw(new IllegalArgumentException("Invalid type 'Bitmap' for key "+ key));
        }
        return (mEditorArtwork != null ? mEditorArtwork : defaultValue);
    
public synchronized int[]getEditableKeys()
Retrieves the keys flagged as editable.

return
null if there are no editable keys, or an array containing the keys.

        // only one editable key supported here
        if (mEditableKeys == RATING_KEY_BY_USER) {
            int[] keys = { RATING_KEY_BY_USER };
            return keys;
        } else {
            return null;
        }
    
public synchronized longgetLong(int key, long defaultValue)
Returns the long value for the key.

param
key one of the keys supported in {@link #putLong(int, long)}
param
defaultValue the value returned if the key is not present
return
the long value for the key, or the supplied default value if the key is not present
throws
IllegalArgumentException

        if (METADATA_KEYS_TYPE.get(key, METADATA_TYPE_INVALID) != METADATA_TYPE_LONG) {
            throw(new IllegalArgumentException("Invalid type 'long' for key "+ key));
        }
        return mEditorMetadata.getLong(String.valueOf(key), defaultValue);
    
public synchronized java.lang.ObjectgetObject(int key, java.lang.Object defaultValue)
Returns an object representation of the value for the key

param
key one of the keys supported in {@link #putObject(int, Object)}
param
defaultValue the value returned if the key is not present
return
the object for the key, as a {@link Long}, {@link Bitmap}, {@link String}, or {@link Rating} depending on the key value, or the supplied default value if the key is not present
throws
IllegalArgumentException

        switch (METADATA_KEYS_TYPE.get(key, METADATA_TYPE_INVALID)) {
            case METADATA_TYPE_LONG:
                if (mEditorMetadata.containsKey(String.valueOf(key))) {
                    return mEditorMetadata.getLong(String.valueOf(key));
                } else {
                    return defaultValue;
                }
            case METADATA_TYPE_STRING:
                if (mEditorMetadata.containsKey(String.valueOf(key))) {
                    return mEditorMetadata.getString(String.valueOf(key));
                } else {
                    return defaultValue;
                }
            case METADATA_TYPE_RATING:
                if (mEditorMetadata.containsKey(String.valueOf(key))) {
                    return mEditorMetadata.getParcelable(String.valueOf(key));
                } else {
                    return defaultValue;
                }
            case METADATA_TYPE_BITMAP:
                // only one key for Bitmap supported, value is not stored in mEditorMetadata Bundle
                if (key == BITMAP_KEY_ARTWORK) {
                    return (mEditorArtwork != null ? mEditorArtwork : defaultValue);
                } // else: fall through to invalid key handling
            default:
                throw(new IllegalArgumentException("Invalid key "+ key));
        }
    
public synchronized java.lang.StringgetString(int key, java.lang.String defaultValue)
Returns the {@link String} value for the key.

param
key one of the keys supported in {@link #putString(int, String)}
param
defaultValue the value returned if the key is not present
return
the {@link String} value for the key, or the supplied default value if the key is not present
throws
IllegalArgumentException

        if (METADATA_KEYS_TYPE.get(key, METADATA_TYPE_INVALID) != METADATA_TYPE_STRING) {
            throw(new IllegalArgumentException("Invalid type 'String' for key "+ key));
        }
        return mEditorMetadata.getString(String.valueOf(key), defaultValue);
    
public synchronized android.media.MediaMetadataEditorputBitmap(int key, android.graphics.Bitmap bitmap)
Adds image.

param
key the identifier of the bitmap to set. The only valid value is {@link #BITMAP_KEY_ARTWORK}
param
bitmap The bitmap for the artwork, or null if there isn't any.
return
Returns a reference to the same MediaMetadataEditor object, so you can chain put calls together.
throws
IllegalArgumentException
see
android.graphics.Bitmap

        if (mApplied) {
            Log.e(TAG, "Can't edit a previously applied MediaMetadataEditor");
            return this;
        }
        if (key != BITMAP_KEY_ARTWORK) {
            throw(new IllegalArgumentException("Invalid type 'Bitmap' for key "+ key));
        }
        mEditorArtwork = bitmap;
        mArtworkChanged = true;
        return this;
    
public synchronized android.media.MediaMetadataEditorputLong(int key, long value)
Adds numerical information. Note that none of the information added after {@link #apply()} has been called will be available to consumers of metadata stored by the MediaMetadataEditor.

param
key the identifier of a the metadata field to set. Valid values are {@link android.media.MediaMetadataRetriever#METADATA_KEY_CD_TRACK_NUMBER}, {@link android.media.MediaMetadataRetriever#METADATA_KEY_DISC_NUMBER}, {@link android.media.MediaMetadataRetriever#METADATA_KEY_DURATION} (with a value expressed in milliseconds), {@link android.media.MediaMetadataRetriever#METADATA_KEY_YEAR}.
param
value The long value for the given key
return
Returns a reference to the same MediaMetadataEditor object, so you can chain put calls together.
throws
IllegalArgumentException

        if (mApplied) {
            Log.e(TAG, "Can't edit a previously applied MediaMetadataEditor");
            return this;
        }
        if (METADATA_KEYS_TYPE.get(key, METADATA_TYPE_INVALID) != METADATA_TYPE_LONG) {
            throw(new IllegalArgumentException("Invalid type 'long' for key "+ key));
        }
        mEditorMetadata.putLong(String.valueOf(key), value);
        mMetadataChanged = true;
        return this;
    
public synchronized android.media.MediaMetadataEditorputObject(int key, java.lang.Object value)
Adds information stored as an instance. Note that none of the information added after {@link #apply()} has been called will be available to consumers of metadata stored by the MediaMetadataEditor.

param
key the identifier of a the metadata field to set. Valid keys for a:
  • {@link Bitmap} object are {@link #BITMAP_KEY_ARTWORK},
  • {@link String} object are the same as for {@link #putString(int, String)}
  • {@link Long} object are the same as for {@link #putLong(int, long)}
  • {@link Rating} object are {@link #RATING_KEY_BY_OTHERS} and {@link #RATING_KEY_BY_USER}.
param
value the metadata to add.
return
Returns a reference to the same MediaMetadataEditor object, so you can chain put calls together.
throws
IllegalArgumentException

        if (mApplied) {
            Log.e(TAG, "Can't edit a previously applied MediaMetadataEditor");
            return this;
        }
        switch(METADATA_KEYS_TYPE.get(key, METADATA_TYPE_INVALID)) {
            case METADATA_TYPE_LONG:
                if (value instanceof Long) {
                    return putLong(key, ((Long)value).longValue());
                } else {
                    throw(new IllegalArgumentException("Not a non-null Long for key "+ key));
                }
            case METADATA_TYPE_STRING:
                if ((value == null) || (value instanceof String)) {
                    return putString(key, (String) value);
                } else {
                    throw(new IllegalArgumentException("Not a String for key "+ key));
                }
            case METADATA_TYPE_RATING:
                mEditorMetadata.putParcelable(String.valueOf(key), (Parcelable)value);
                mMetadataChanged = true;
                break;
            case METADATA_TYPE_BITMAP:
                if ((value == null) || (value instanceof Bitmap))  {
                    return putBitmap(key, (Bitmap) value);
                } else {
                    throw(new IllegalArgumentException("Not a Bitmap for key "+ key));
                }
            default:
                throw(new IllegalArgumentException("Invalid key "+ key));
        }
        return this;
    
public synchronized android.media.MediaMetadataEditorputString(int key, java.lang.String value)
Adds textual information. Note that none of the information added after {@link #apply()} has been called, will be available to consumers of metadata stored by the MediaMetadataEditor.

param
key The identifier of a the metadata field to set. Valid values are {@link android.media.MediaMetadataRetriever#METADATA_KEY_ALBUM}, {@link android.media.MediaMetadataRetriever#METADATA_KEY_ALBUMARTIST}, {@link android.media.MediaMetadataRetriever#METADATA_KEY_TITLE}, {@link android.media.MediaMetadataRetriever#METADATA_KEY_ARTIST}, {@link android.media.MediaMetadataRetriever#METADATA_KEY_AUTHOR}, {@link android.media.MediaMetadataRetriever#METADATA_KEY_COMPILATION}, {@link android.media.MediaMetadataRetriever#METADATA_KEY_COMPOSER}, {@link android.media.MediaMetadataRetriever#METADATA_KEY_DATE}, {@link android.media.MediaMetadataRetriever#METADATA_KEY_GENRE}, {@link android.media.MediaMetadataRetriever#METADATA_KEY_WRITER}.
param
value The text for the given key, or {@code null} to signify there is no valid information for the field.
return
Returns a reference to the same MediaMetadataEditor object, so you can chain put calls together.

        if (mApplied) {
            Log.e(TAG, "Can't edit a previously applied MediaMetadataEditor");
            return this;
        }
        if (METADATA_KEYS_TYPE.get(key, METADATA_TYPE_INVALID) != METADATA_TYPE_STRING) {
            throw(new IllegalArgumentException("Invalid type 'String' for key "+ key));
        }
        mEditorMetadata.putString(String.valueOf(key), value);
        mMetadataChanged = true;
        return this;
    
public synchronized voidremoveEditableKeys()
Causes all metadata fields to be read-only.

        if (mApplied) {
            Log.e(TAG, "Can't remove all editable keys of a previously applied MetadataEditor");
            return;
        }
        if (mEditableKeys != 0) {
            mEditableKeys = 0;
            mMetadataChanged = true;
        }