1package android.media;
2
3import android.annotation.Nullable;
4import android.graphics.Bitmap;
5import android.net.Uri;
6import android.os.Bundle;
7import android.os.Parcel;
8import android.os.Parcelable;
9
10/**
11 * A simple set of metadata for a media item suitable for display. This can be
12 * created using the Builder or retrieved from existing metadata using
13 * {@link MediaMetadata#getDescription()}.
14 */
15public class MediaDescription implements Parcelable {
16    /**
17     * A unique persistent id for the content or null.
18     */
19    private final String mMediaId;
20    /**
21     * A primary title suitable for display or null.
22     */
23    private final CharSequence mTitle;
24    /**
25     * A subtitle suitable for display or null.
26     */
27    private final CharSequence mSubtitle;
28    /**
29     * A description suitable for display or null.
30     */
31    private final CharSequence mDescription;
32    /**
33     * A bitmap icon suitable for display or null.
34     */
35    private final Bitmap mIcon;
36    /**
37     * A Uri for an icon suitable for display or null.
38     */
39    private final Uri mIconUri;
40    /**
41     * Extras for opaque use by apps/system.
42     */
43    private final Bundle mExtras;
44    /**
45     * A Uri to identify this content.
46     */
47    private final Uri mMediaUri;
48
49    private MediaDescription(String mediaId, CharSequence title, CharSequence subtitle,
50            CharSequence description, Bitmap icon, Uri iconUri, Bundle extras, Uri mediaUri) {
51        mMediaId = mediaId;
52        mTitle = title;
53        mSubtitle = subtitle;
54        mDescription = description;
55        mIcon = icon;
56        mIconUri = iconUri;
57        mExtras = extras;
58        mMediaUri = mediaUri;
59    }
60
61    private MediaDescription(Parcel in) {
62        mMediaId = in.readString();
63        mTitle = in.readCharSequence();
64        mSubtitle = in.readCharSequence();
65        mDescription = in.readCharSequence();
66        mIcon = in.readParcelable(null);
67        mIconUri = in.readParcelable(null);
68        mExtras = in.readBundle();
69        mMediaUri = in.readParcelable(null);
70    }
71
72    /**
73     * Returns the media id or null. See
74     * {@link MediaMetadata#METADATA_KEY_MEDIA_ID}.
75     */
76    public @Nullable String getMediaId() {
77        return mMediaId;
78    }
79
80    /**
81     * Returns a title suitable for display or null.
82     *
83     * @return A title or null.
84     */
85    public @Nullable CharSequence getTitle() {
86        return mTitle;
87    }
88
89    /**
90     * Returns a subtitle suitable for display or null.
91     *
92     * @return A subtitle or null.
93     */
94    public @Nullable CharSequence getSubtitle() {
95        return mSubtitle;
96    }
97
98    /**
99     * Returns a description suitable for display or null.
100     *
101     * @return A description or null.
102     */
103    public @Nullable CharSequence getDescription() {
104        return mDescription;
105    }
106
107    /**
108     * Returns a bitmap icon suitable for display or null.
109     *
110     * @return An icon or null.
111     */
112    public @Nullable Bitmap getIconBitmap() {
113        return mIcon;
114    }
115
116    /**
117     * Returns a Uri for an icon suitable for display or null.
118     *
119     * @return An icon uri or null.
120     */
121    public @Nullable Uri getIconUri() {
122        return mIconUri;
123    }
124
125    /**
126     * Returns any extras that were added to the description.
127     *
128     * @return A bundle of extras or null.
129     */
130    public @Nullable Bundle getExtras() {
131        return mExtras;
132    }
133
134    /**
135     * Returns a Uri representing this content or null.
136     *
137     * @return A media Uri or null.
138     */
139    public @Nullable Uri getMediaUri() {
140        return mMediaUri;
141    }
142
143    @Override
144    public int describeContents() {
145        return 0;
146    }
147
148    @Override
149    public void writeToParcel(Parcel dest, int flags) {
150        dest.writeString(mMediaId);
151        dest.writeCharSequence(mTitle);
152        dest.writeCharSequence(mSubtitle);
153        dest.writeCharSequence(mDescription);
154        dest.writeParcelable(mIcon, flags);
155        dest.writeParcelable(mIconUri, flags);
156        dest.writeBundle(mExtras);
157        dest.writeParcelable(mMediaUri, flags);
158    }
159
160    @Override
161    public String toString() {
162        return mTitle + ", " + mSubtitle + ", " + mDescription;
163    }
164
165    public static final Parcelable.Creator<MediaDescription> CREATOR =
166            new Parcelable.Creator<MediaDescription>() {
167                @Override
168                public MediaDescription createFromParcel(Parcel in) {
169                    return new MediaDescription(in);
170                }
171
172                @Override
173                public MediaDescription[] newArray(int size) {
174                    return new MediaDescription[size];
175                }
176            };
177
178    /**
179     * Builder for {@link MediaDescription} objects.
180     */
181    public static class Builder {
182        private String mMediaId;
183        private CharSequence mTitle;
184        private CharSequence mSubtitle;
185        private CharSequence mDescription;
186        private Bitmap mIcon;
187        private Uri mIconUri;
188        private Bundle mExtras;
189        private Uri mMediaUri;
190
191        /**
192         * Creates an initially empty builder.
193         */
194        public Builder() {
195        }
196
197        /**
198         * Sets the media id.
199         *
200         * @param mediaId The unique id for the item or null.
201         * @return this
202         */
203        public Builder setMediaId(@Nullable String mediaId) {
204            mMediaId = mediaId;
205            return this;
206        }
207
208        /**
209         * Sets the title.
210         *
211         * @param title A title suitable for display to the user or null.
212         * @return this
213         */
214        public Builder setTitle(@Nullable CharSequence title) {
215            mTitle = title;
216            return this;
217        }
218
219        /**
220         * Sets the subtitle.
221         *
222         * @param subtitle A subtitle suitable for display to the user or null.
223         * @return this
224         */
225        public Builder setSubtitle(@Nullable CharSequence subtitle) {
226            mSubtitle = subtitle;
227            return this;
228        }
229
230        /**
231         * Sets the description.
232         *
233         * @param description A description suitable for display to the user or
234         *            null.
235         * @return this
236         */
237        public Builder setDescription(@Nullable CharSequence description) {
238            mDescription = description;
239            return this;
240        }
241
242        /**
243         * Sets the icon.
244         *
245         * @param icon A {@link Bitmap} icon suitable for display to the user or
246         *            null.
247         * @return this
248         */
249        public Builder setIconBitmap(@Nullable Bitmap icon) {
250            mIcon = icon;
251            return this;
252        }
253
254        /**
255         * Sets the icon uri.
256         *
257         * @param iconUri A {@link Uri} for an icon suitable for display to the
258         *            user or null.
259         * @return this
260         */
261        public Builder setIconUri(@Nullable Uri iconUri) {
262            mIconUri = iconUri;
263            return this;
264        }
265
266        /**
267         * Sets a bundle of extras.
268         *
269         * @param extras The extras to include with this description or null.
270         * @return this
271         */
272        public Builder setExtras(@Nullable Bundle extras) {
273            mExtras = extras;
274            return this;
275        }
276
277        /**
278         * Sets the media uri.
279         *
280         * @param mediaUri The content's {@link Uri} for the item or null.
281         * @return this
282         */
283        public Builder setMediaUri(@Nullable Uri mediaUri) {
284            mMediaUri = mediaUri;
285            return this;
286        }
287
288        public MediaDescription build() {
289            return new MediaDescription(mMediaId, mTitle, mSubtitle, mDescription, mIcon, mIconUri,
290                    mExtras, mMediaUri);
291        }
292    }
293}
294