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.R;
24 import com.andrew.apollo.cache.ImageFetcher;
25 import com.andrew.apollo.model.Artist;
26 import com.andrew.apollo.ui.MusicHolder;
27 import com.andrew.apollo.ui.MusicHolder.DataHolder;
28 import com.andrew.apollo.utils.ApolloUtils;
29 import com.andrew.apollo.utils.MusicUtils;
32 * This {@link ArrayAdapter} is used to display all of the artists on a user's
33 * device for {@link ArtistFragment}.
35 * @author Andrew Neal (andrewdneal@gmail.com)
38 * @author Andrew Neal (andrewdneal@gmail.com)
40 public class ArtistAdapter extends ArrayAdapter<Artist> {
43 * Number of views (ImageView and TextView)
45 private static final int VIEW_TYPE_COUNT = 2;
48 * The resource Id of the layout to inflate
50 private final int mLayoutId;
53 * Image cache and image fetcher
55 private final ImageFetcher mImageFetcher;
58 * Semi-transparent overlay
60 private final int mOverlay;
63 * Used to cache the artist info
65 private DataHolder[] mData;
68 * Loads line three and the background image if the user decides to.
70 private boolean mLoadExtraData = false;
73 * Constructor of <code>ArtistAdapter</code>
75 * @param context The {@link Context} to use.
76 * @param layoutId The resource Id of the view to inflate.
78 public ArtistAdapter(final Activity context, final int layoutId) {
82 // Initialize the cache & image fetcher
83 mImageFetcher = ApolloUtils.getImageFetcher(context);
84 // Cache the transparent overlay
85 mOverlay = context.getResources().getColor(R.color.list_item_background);
92 public View getView(final int position, View convertView, final ViewGroup parent) {
93 // Recycle ViewHolder's items
95 if (convertView == null) {
96 convertView = LayoutInflater.from(getContext()).inflate(mLayoutId, parent, false);
97 holder = new MusicHolder(convertView);
98 convertView.setTag(holder);
100 holder = (MusicHolder)convertView.getTag();
103 // Retrieve the data holder
104 final DataHolder dataHolder = mData[position];
106 // Set each artist name (line one)
107 holder.mLineOne.get().setText(dataHolder.mLineOne);
108 // Set the number of albums (line two)
109 holder.mLineTwo.get().setText(dataHolder.mLineTwo);
110 // Asynchronously load the artist image into the adapter
111 mImageFetcher.loadArtistImage(dataHolder.mLineOne, holder.mImage.get());
112 if (mLoadExtraData) {
113 // Make sure the background layer gets set
114 holder.mOverlay.get().setBackgroundColor(mOverlay);
115 // Set the number of songs (line three)
116 holder.mLineThree.get().setText(dataHolder.mLineThree);
117 // Set the background image
118 mImageFetcher.loadArtistImage(dataHolder.mLineOne, holder.mBackground.get());
119 // Play the artist when the artwork is touched
120 playArtist(holder.mImage.get(), position);
129 public boolean hasStableIds() {
137 public int getViewTypeCount() {
138 return VIEW_TYPE_COUNT;
142 * Method used to cache the data used to populate the list or grid. The idea
143 * is to cache everything before {@code #getView(int, View, ViewGroup)} is
146 public void buildCache() {
147 mData = new DataHolder[getCount()];
148 for (int i = 0; i < getCount(); i++) {
150 final Artist artist = getItem(i);
152 // Build the data holder
153 mData[i] = new DataHolder();
155 mData[i].mItemId = artist.mArtistId;
156 // Artist names (line one)
157 mData[i].mLineOne = artist.mArtistName;
158 // Number of albums (line two)
159 mData[i].mLineTwo = MusicUtils.makeLabel(getContext(),
160 R.plurals.Nalbums, artist.mAlbumNumber);
161 // Number of songs (line three)
162 mData[i].mLineThree = MusicUtils.makeLabel(getContext(),
163 R.plurals.Nsongs, artist.mSongNumber);
168 * Starts playing an artist if the user touches the artist image in the
171 * @param artist The {@link ImageView} holding the aritst image
172 * @param position The position of the artist to play.
174 private void playArtist(final ImageView artist, final int position) {
175 artist.setOnClickListener(new OnClickListener() {
178 public void onClick(final View v) {
179 final long id = getItem(position).mArtistId;
180 final long[] list = MusicUtils.getSongListForArtist(getContext(), id);
181 MusicUtils.playAll(getContext(), list, 0, false);
187 * Method that unloads and clears the items in the adapter
189 public void unload() {
195 * @param pause True to temporarily pause the disk cache, false otherwise.
197 public void setPauseDiskCache(final boolean pause) {
198 if (mImageFetcher != null) {
199 mImageFetcher.setPauseDiskCache(pause);
204 * @param artist The key used to find the cached artist to remove
206 public void removeFromCache(final Artist artist) {
207 if (mImageFetcher != null) {
208 mImageFetcher.removeFromCache(artist.mArtistName);
213 * Flushes the disk cache.
215 public void flush() {
216 mImageFetcher.flush();
220 * @param extra True to load line three and the background image, false
223 public void setLoadExtraData(final boolean extra) {
224 mLoadExtraData = extra;