OSDN Git Service

Repackaged com.andrew.apollo to com.cyngn.eleven
[android-x86/packages-apps-Eleven.git] / src / com / cyngn / eleven / lastfm / Artist.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.cyngn.eleven.lastfm;
23
24 import android.content.Context;
25
26 import com.cyngn.eleven.Config;
27
28 import java.util.Locale;
29 import java.util.Map;
30 import java.util.WeakHashMap;
31
32 /**
33  * Bean that contains artist information.<br/>
34  * This class contains static methods that executes API methods relating to
35  * artists.<br/>
36  * Method names are equivalent to the last.fm API method names.
37  * 
38  * @author Janni Kovacs
39  */
40 public class Artist extends MusicEntry {
41
42     protected final static ItemFactory<Artist> FACTORY = new ArtistFactory();
43
44     protected Artist(final String name, final String url) {
45         super(name, url);
46     }
47
48     /**
49      * Retrieves detailed artist info for the given artist or mbid entry.
50      * 
51      * @param artistOrMbid Name of the artist or an mbid
52      * @return detailed artist info
53      */
54     public final static Artist getInfo(final Context context, final String artistOrMbid) {
55         return getInfo(context, artistOrMbid, Locale.getDefault(), Config.LASTFM_API_KEY);
56     }
57
58     /**
59      * Retrieves detailed artist info for the given artist or mbid entry.
60      * 
61      * @param artistOrMbid Name of the artist or an mbid
62      * @param locale The language to fetch info in, or <code>null</code>
63      * @param apiKey The API key
64      * @return detailed artist info
65      */
66     public final static Artist getInfo(final Context context, final String artistOrMbid,
67             final Locale locale, final String apiKey) {
68         final Map<String, String> mParams = new WeakHashMap<String, String>();
69         mParams.put("artist", artistOrMbid);
70         if (locale != null && locale.getLanguage().length() != 0) {
71             mParams.put("lang", locale.getLanguage());
72         }
73         final Result mResult = Caller.getInstance(context).call("artist.getInfo", apiKey, mParams);
74         return ResponseBuilder.buildItem(mResult, Artist.class);
75     }
76
77     /**
78      * Use the last.fm corrections data to check whether the supplied artist has
79      * a correction to a canonical artist. This method returns a new
80      * {@link Artist} object containing the corrected data, or <code>null</code>
81      * if the supplied Artist was not found.
82      * 
83      * @param artist The artist name to correct
84      * @return a new {@link Artist}, or <code>null</code>
85      */
86     public final static Artist getCorrection(final Context context, final String artist) {
87         Result result = null;
88         try {
89             result = Caller.getInstance(context).call("artist.getCorrection",
90                     Config.LASTFM_API_KEY, "artist", artist);
91             if (!result.isSuccessful()) {
92                 return null;
93             }
94             final DomElement correctionElement = result.getContentElement().getChild("correction");
95             if (correctionElement == null) {
96                 return new Artist(artist, null);
97             }
98             final DomElement artistElem = correctionElement.getChild("artist");
99             return FACTORY.createItemFromElement(artistElem);
100         } catch (final Exception ignored) {
101             return null;
102         }
103     }
104
105     private final static class ArtistFactory implements ItemFactory<Artist> {
106
107         /**
108          * {@inheritDoc}
109          */
110         @Override
111         public Artist createItemFromElement(final DomElement element) {
112             if (element == null) {
113                 return null;
114             }
115             final Artist artist = new Artist(null, null);
116             MusicEntry.loadStandardInfo(artist, element);
117             return artist;
118         }
119     }
120 }