1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.example.android.leanback;
18 
19 import android.content.ContentResolver;
20 import android.content.Context;
21 import android.net.Uri;
22 
23 import com.google.gson.annotations.SerializedName;
24 
25 /**
26  * Abstract data type to represent music item.
27  */
28 public class MusicItem {
29     // Duration information of current.
30     @SerializedName("duration")
31     private String mDuration;
32 
33     // File name of this media item.
34     @SerializedName("file")
35     private String mFile;
36 
37     // The title of this media item.
38     @SerializedName("title")
39     private String mMediaTitle;
40 
41     // The description of media item.
42     @SerializedName("description")
43     private String mDescription;
44 
45     // Art information (i.e. cover image) of this media item.
46     @SerializedName("art")
47     private String mArt;
48 
49     /**
50      * The conversion function which can return media item's uri through the file name.
51      *
52      * @param context The context used to get resources of this app.
53      * @return The Uri of the selected media item.
54      */
getMediaSourceUri(Context context)55     public Uri getMediaSourceUri(Context context) {
56         return getResourceUri(context, context.getResources()
57                 .getIdentifier(mFile, "raw", context.getPackageName()));
58     }
59 
60     /**
61      * Return the title of current media item.
62      *
63      * @return The title of current media item.
64      */
getMediaTitle()65     public String getMediaTitle() {
66         return mMediaTitle;
67     }
68 
69     /**
70      * Return the description of current media item.
71      *
72      * @return The description of current media item.
73      */
getMediaDescription()74     public String getMediaDescription() {
75         return mDescription;
76     }
77 
78     /**
79      * Return the resource id through art file's name.
80      *
81      * @param context The context used to get resources of this app.
82      * @return The resource Id of the selected media item.
83      */
getMediaAlbumArtResId(Context context)84     public int getMediaAlbumArtResId(Context context) {
85         return context.getResources()
86                 .getIdentifier(mArt, "drawable", context.getPackageName());
87     }
88 
89     /**
90      * Helper function to get resource uri based on android resource scheme
91      *
92      * @param context Context to get resources.
93      * @param resID   Resource ID.
94      * @return The Uri of current resource.
95      */
getResourceUri(Context context, int resID)96     public static Uri getResourceUri(Context context, int resID) {
97         return Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE
98                 + "://"
99                 + context.getResources().getResourcePackageName(resID)
100                 + '/'
101                 + context.getResources().getResourceTypeName(resID)
102                 + '/'
103                 + context.getResources().getResourceEntryName(resID));
104     }
105 }
106