OSDN Git Service

b5ff603516e84ea48fbd2c6ac3767cb6fd6148d6
[android-x86/packages-apps-Eleven.git] / src / com / cyanogenmod / eleven / lastfm / Album.java
1 /*
2  * Copyright (c) 2012, the Last.fm Java Project and Committers All rights
3  * reserved. Redistribution and use of this software in source and binary forms,
4  * with or without modification, are permitted provided that the following
5  * conditions are met: - Redistributions of source code must retain the above
6  * copyright notice, this list of conditions and the following disclaimer. -
7  * Redistributions in binary form must reproduce the above copyright notice,
8  * this list of conditions and the following disclaimer in the documentation
9  * and/or other materials provided with the distribution. THIS SOFTWARE IS
10  * PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
11  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
13  * EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
14  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
15  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
16  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
17  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
18  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
19  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
20  */
21
22 package com.cyanogenmod.eleven.lastfm;
23
24 import android.content.Context;
25
26 import com.cyanogenmod.eleven.Config;
27
28 import java.util.HashMap;
29 import java.util.Map;
30
31 /**
32  * Wrapper class for Album related API calls and Album Bean.
33  *
34  * @author Janni Kovacs
35  */
36 public class Album extends MusicEntry {
37
38     protected final static ItemFactory<Album> FACTORY = new AlbumFactory();
39
40     private String artist;
41
42     /**
43      * @param name
44      * @param url
45      * @param artist
46      */
47     private Album(final String name, final String url, final String artist) {
48         super(name, url);
49         this.artist = artist;
50     }
51
52     /**
53      * Get the metadata for an album on Last.fm using the album name or a
54      * musicbrainz id. See playlist.fetch on how to get the album playlist.
55      *
56      * @param artist Artist's name
57      * @param albumOrMbid Album name or MBID
58      * @return Album metadata
59      */
60     public final static Album getInfo(final Context context, final String artist,
61             final String albumOrMbid) {
62         return getInfo(context, artist, albumOrMbid, null, Config.LASTFM_API_KEY);
63     }
64
65     /**
66      * Get the metadata for an album on Last.fm using the album name or a
67      * musicbrainz id. See playlist.fetch on how to get the album playlist.
68      *
69      * @param artist Artist's name
70      * @param albumOrMbid Album name or MBID
71      * @param username The username for the context of the request. If supplied,
72      *            the user's playcount for this album is included in the
73      *            response.
74      * @param apiKey The API key
75      * @return Album metadata
76      */
77     public final static Album getInfo(final Context context, final String artist,
78             final String albumOrMbid, final String username, final String apiKey) {
79         final Map<String, String> params = new HashMap<String, String>();
80         params.put("artist", artist);
81         params.put("album", albumOrMbid);
82         MapUtilities.nullSafePut(params, "username", username);
83         final Result result = Caller.getInstance(context).call("album.getInfo", apiKey, params);
84         return ResponseBuilder.buildItem(result, Album.class);
85     }
86
87     private final static class AlbumFactory implements ItemFactory<Album> {
88
89         /**
90          * {@inheritDoc}
91          */
92         @Override
93         public Album createItemFromElement(final DomElement element) {
94             if (element == null) {
95                 return null;
96             }
97             final Album album = new Album(null, null, null);
98             MusicEntry.loadStandardInfo(album, element);
99             if (element.hasChild("artist")) {
100                 album.artist = element.getChild("artist").getChildText("name");
101                 if (album.artist == null) {
102                     album.artist = element.getChildText("artist");
103                 }
104             }
105             return album;
106         }
107     }
108 }