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.
12 package com.andrew.apollo.adapters;
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;
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;
33 import java.util.List;
36 * This {@link ArrayAdapter} is used to display the albums for a particular
37 * artist for {@link ArtistAlbumFragment} .
39 * @author Andrew Neal (andrewdneal@gmail.com)
41 public class ArtistAlbumAdapter extends ArrayAdapter<Album> {
46 private static final int ITEM_VIEW_TYPE_HEADER = 0;
49 * * The data in the list.
51 private static final int ITEM_VIEW_TYPE_MUSIC = 1;
54 * Number of views (ImageView, TextView, header)
56 private static final int VIEW_TYPE_COUNT = 3;
61 private final LayoutInflater mInflater;
66 private final View mHeader;
69 * The resource Id of the layout to inflate
71 private final int mLayoutId;
74 * Image cache and image fetcher
76 private final ImageFetcher mImageFetcher;
79 * Used to set the size of the data in the adapter
81 private List<Album> mCount = Lists.newArrayList();
84 * Constructor of <code>ArtistAlbumAdapter</code>
86 * @param context The {@link Context} to use
87 * @param layoutId The resource Id of the view to inflate.
89 public ArtistAlbumAdapter(final Activity context, final int layoutId) {
91 // Used to create the custom layout
92 mInflater = LayoutInflater.from(context);
94 mHeader = mInflater.inflate(R.layout.faux_carousel, null);
97 // Initialize the cache & image fetcher
98 mImageFetcher = ApolloUtils.getImageFetcher(context);
105 public View getView(final int position, View convertView, final ViewGroup parent) {
107 // Return a faux header at position 0
112 // Recycle MusicHolder's items
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);
121 holder = (MusicHolder)convertView.getTag();
124 // Retrieve the album
125 final Album album = getItem(position - 1);
126 final String albumName = album.mAlbumName;
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);
147 public boolean hasStableIds() {
155 public int getCount() {
156 final int size = mCount.size();
157 return size == 0 ? 0 : size + 1;
164 public long getItemId(final int position) {
175 public int getViewTypeCount() {
176 return VIEW_TYPE_COUNT;
183 public int getItemViewType(final int position) {
185 return ITEM_VIEW_TYPE_HEADER;
187 return ITEM_VIEW_TYPE_MUSIC;
191 * Starts playing an album if the user touches the artwork in the list.
193 * @param album The {@link ImageView} holding the album
194 * @param position The position of the album to play.
196 private void playAlbum(final ImageView album, final int position) {
197 album.setOnClickListener(new OnClickListener() {
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);
209 * Method that unloads and clears the items in the adapter
211 public void unload() {
216 * @param pause True to temporarily pause the disk cache, false otherwise.
218 public void setPauseDiskCache(final boolean pause) {
219 if (mImageFetcher != null) {
220 mImageFetcher.setPauseDiskCache(pause);
225 * @param album The key used to find the cached album to remove
227 public void removeFromCache(final Album album) {
228 if (mImageFetcher != null) {
229 mImageFetcher.removeFromCache(
230 ImageFetcher.generateAlbumCacheKey(album.mAlbumName, album.mArtistName));
235 * @param data The {@link List} used to return the count for the adapter.
237 public void setCount(final List<Album> data) {
242 * Flushes the disk cache.
244 public void flush() {
245 mImageFetcher.flush();