OSDN Git Service

Automatic translation import
[android-x86/packages-apps-Eleven.git] / src / com / andrew / apollo / adapters / ArtistAlbumAdapter.java
1 /*
2  * Copyright (C) 2012 Andrew Neal Licensed under the Apache License, Version 2.0
3  * (the "License"); you may not use this file except in compliance with the
4  * License. You may obtain a copy of the License at
5  * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
6  * or agreed to in writing, software distributed under the License is
7  * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
8  * KIND, either express or implied. See the License for the specific language
9  * governing permissions and limitations under the License.
10  */
11
12 package com.andrew.apollo.adapters;
13
14 import android.app.Activity;
15 import android.content.Context;
16 import android.view.LayoutInflater;
17 import android.view.View;
18 import android.view.View.OnClickListener;
19 import android.view.ViewGroup;
20 import android.widget.ArrayAdapter;
21 import android.widget.ImageView;
22
23 import com.andrew.apollo.Config;
24 import com.andrew.apollo.R;
25 import com.andrew.apollo.cache.ImageFetcher;
26 import com.andrew.apollo.model.Album;
27 import com.andrew.apollo.ui.MusicHolder;
28 import com.andrew.apollo.ui.fragments.profile.ArtistAlbumFragment;
29 import com.andrew.apollo.utils.ApolloUtils;
30 import com.andrew.apollo.utils.Lists;
31 import com.andrew.apollo.utils.MusicUtils;
32
33 import java.util.List;
34
35 /**
36  * This {@link ArrayAdapter} is used to display the albums for a particular
37  * artist for {@link ArtistAlbumFragment} .
38  * 
39  * @author Andrew Neal (andrewdneal@gmail.com)
40  */
41 public class ArtistAlbumAdapter extends ArrayAdapter<Album> {
42
43     /**
44      * The header view
45      */
46     private static final int ITEM_VIEW_TYPE_HEADER = 0;
47
48     /**
49      * * The data in the list.
50      */
51     private static final int ITEM_VIEW_TYPE_MUSIC = 1;
52
53     /**
54      * Number of views (ImageView, TextView, header)
55      */
56     private static final int VIEW_TYPE_COUNT = 3;
57
58     /**
59      * LayoutInflater
60      */
61     private final LayoutInflater mInflater;
62
63     /**
64      * Fake header
65      */
66     private final View mHeader;
67
68     /**
69      * The resource Id of the layout to inflate
70      */
71     private final int mLayoutId;
72
73     /**
74      * Image cache and image fetcher
75      */
76     private final ImageFetcher mImageFetcher;
77
78     /**
79      * Used to set the size of the data in the adapter
80      */
81     private List<Album> mCount = Lists.newArrayList();
82
83     /**
84      * Constructor of <code>ArtistAlbumAdapter</code>
85      * 
86      * @param context The {@link Context} to use
87      * @param layoutId The resource Id of the view to inflate.
88      */
89     public ArtistAlbumAdapter(final Activity context, final int layoutId) {
90         super(context, 0);
91         // Used to create the custom layout
92         mInflater = LayoutInflater.from(context);
93         // Cache the header
94         mHeader = mInflater.inflate(R.layout.faux_carousel, null);
95         // Get the layout Id
96         mLayoutId = layoutId;
97         // Initialize the cache & image fetcher
98         mImageFetcher = ApolloUtils.getImageFetcher(context);
99     }
100
101     /**
102      * {@inheritDoc}
103      */
104     @Override
105     public View getView(final int position, View convertView, final ViewGroup parent) {
106
107         // Return a faux header at position 0
108         if (position == 0) {
109             return mHeader;
110         }
111
112         // Recycle MusicHolder's items
113         MusicHolder holder;
114         if (convertView == null) {
115             convertView = LayoutInflater.from(getContext()).inflate(mLayoutId, parent, false);
116             holder = new MusicHolder(convertView);
117             // Remove the background layer
118             holder.mOverlay.get().setBackgroundColor(0);
119             convertView.setTag(holder);
120         } else {
121             holder = (MusicHolder)convertView.getTag();
122         }
123
124         // Retrieve the album
125         final Album album = getItem(position - 1);
126         final String albumName = album.mAlbumName;
127
128         // Set each album name (line one)
129         holder.mLineOne.get().setText(albumName);
130         // Set the number of songs (line two)
131         holder.mLineTwo.get().setText(MusicUtils.makeLabel(getContext(),
132                 R.plurals.Nsongs, album.mSongNumber));
133         // Set the album year (line three)
134         holder.mLineThree.get().setText(album.mYear);
135         // Asynchronously load the album images into the adapter
136         mImageFetcher.loadAlbumImage(album.mArtistName, albumName, album.mAlbumId,
137                 holder.mImage.get());
138         // Play the album when the artwork is touched
139         playAlbum(holder.mImage.get(), position);
140         return convertView;
141     }
142
143     /**
144      * {@inheritDoc}
145      */
146     @Override
147     public boolean hasStableIds() {
148         return true;
149     }
150
151     /**
152      * {@inheritDoc}
153      */
154     @Override
155     public int getCount() {
156         final int size = mCount.size();
157         return size == 0 ? 0 : size + 1;
158     }
159
160     /**
161      * {@inheritDoc}
162      */
163     @Override
164     public long getItemId(final int position) {
165         if (position == 0) {
166             return -1;
167         }
168         return position - 1;
169     }
170
171     /**
172      * {@inheritDoc}
173      */
174     @Override
175     public int getViewTypeCount() {
176         return VIEW_TYPE_COUNT;
177     }
178
179     /**
180      * {@inheritDoc}
181      */
182     @Override
183     public int getItemViewType(final int position) {
184         if (position == 0) {
185             return ITEM_VIEW_TYPE_HEADER;
186         }
187         return ITEM_VIEW_TYPE_MUSIC;
188     }
189
190     /**
191      * Starts playing an album if the user touches the artwork in the list.
192      * 
193      * @param album The {@link ImageView} holding the album
194      * @param position The position of the album to play.
195      */
196     private void playAlbum(final ImageView album, final int position) {
197         album.setOnClickListener(new OnClickListener() {
198
199             @Override
200             public void onClick(final View v) {
201                 final long id = getItem(position - 1).mAlbumId;
202                 final long[] list = MusicUtils.getSongListForAlbum(getContext(), id);
203                 MusicUtils.playAll(getContext(), list, 0, false);
204             }
205         });
206     }
207
208     /**
209      * Method that unloads and clears the items in the adapter
210      */
211     public void unload() {
212         clear();
213     }
214
215     /**
216      * @param pause True to temporarily pause the disk cache, false otherwise.
217      */
218     public void setPauseDiskCache(final boolean pause) {
219         if (mImageFetcher != null) {
220             mImageFetcher.setPauseDiskCache(pause);
221         }
222     }
223
224     /**
225      * @param album The key used to find the cached album to remove
226      */
227     public void removeFromCache(final Album album) {
228         if (mImageFetcher != null) {
229             mImageFetcher.removeFromCache(
230                      ImageFetcher.generateAlbumCacheKey(album.mAlbumName, album.mArtistName));
231         }
232     }
233
234     /**
235      * @param data The {@link List} used to return the count for the adapter.
236      */
237     public void setCount(final List<Album> data) {
238         mCount = data;
239     }
240
241     /**
242      * Flushes the disk cache.
243      */
244     public void flush() {
245         mImageFetcher.flush();
246     }
247 }