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.cyngn.eleven.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.cyngn.eleven.R;
24 import com.cyngn.eleven.cache.ImageFetcher;
25 import com.cyngn.eleven.model.Artist;
26 import com.cyngn.eleven.sectionadapter.SectionAdapter;
27 import com.cyngn.eleven.ui.MusicHolder;
28 import com.cyngn.eleven.ui.MusicHolder.DataHolder;
29 import com.cyngn.eleven.utils.ApolloUtils;
30 import com.cyngn.eleven.utils.MusicUtils;
33 * This {@link ArrayAdapter} is used to display all of the artists on a user's
34 * device for {@link ArtistFragment}.
36 * @author Andrew Neal (andrewdneal@gmail.com)
39 * @author Andrew Neal (andrewdneal@gmail.com)
41 public class ArtistAdapter extends ArrayAdapter<Artist> implements SectionAdapter.BasicAdapter {
44 * Number of views (ImageView and TextView)
46 private static final int VIEW_TYPE_COUNT = 2;
49 * The resource Id of the layout to inflate
51 private final int mLayoutId;
54 * Image cache and image fetcher
56 private final ImageFetcher mImageFetcher;
59 * Semi-transparent overlay
61 private final int mOverlay;
64 * Used to cache the artist info
66 private DataHolder[] mData;
69 * Loads line three and the background image if the user decides to.
71 private boolean mLoadExtraData = false;
74 * Constructor of <code>ArtistAdapter</code>
76 * @param context The {@link Context} to use.
77 * @param layoutId The resource Id of the view to inflate.
79 public ArtistAdapter(final Activity context, final int layoutId) {
83 // Initialize the cache & image fetcher
84 mImageFetcher = ApolloUtils.getImageFetcher(context);
85 // Cache the transparent overlay
86 mOverlay = context.getResources().getColor(R.color.list_item_background);
93 public View getView(final int position, View convertView, final ViewGroup parent) {
94 // Recycle ViewHolder's items
96 if (convertView == null) {
97 convertView = LayoutInflater.from(getContext()).inflate(mLayoutId, parent, false);
98 holder = new MusicHolder(convertView);
99 convertView.setTag(holder);
101 holder = (MusicHolder)convertView.getTag();
104 // Retrieve the data holder
105 final DataHolder dataHolder = mData[position];
107 // Set each artist name (line one)
108 holder.mLineOne.get().setText(dataHolder.mLineOne);
109 // Set the number of albums (line two)
110 holder.mLineTwo.get().setText(dataHolder.mLineTwo);
111 // Asynchronously load the artist image into the adapter
112 mImageFetcher.loadArtistImage(dataHolder.mLineOne, holder.mImage.get());
113 if (mLoadExtraData) {
114 // Make sure the background layer gets set
115 holder.mOverlay.get().setBackgroundColor(mOverlay);
116 // Set the number of songs (line three)
117 holder.mLineThree.get().setText(dataHolder.mLineThree);
118 // Set the background image
119 mImageFetcher.loadArtistImage(dataHolder.mLineOne, holder.mBackground.get());
120 // Play the artist when the artwork is touched
121 playArtist(holder.mImage.get(), position);
130 public boolean hasStableIds() {
138 public int getViewTypeCount() {
139 return VIEW_TYPE_COUNT;
143 * Method used to cache the data used to populate the list or grid. The idea
144 * is to cache everything before {@code #getView(int, View, ViewGroup)} is
147 public void buildCache() {
148 mData = new DataHolder[getCount()];
149 for (int i = 0; i < getCount(); i++) {
151 final Artist artist = getItem(i);
153 // Build the data holder
154 mData[i] = new DataHolder();
156 mData[i].mItemId = artist.mArtistId;
157 // Artist names (line one)
158 mData[i].mLineOne = artist.mArtistName;
159 // Number of albums (line two)
160 mData[i].mLineTwo = MusicUtils.makeLabel(getContext(),
161 R.plurals.Nalbums, artist.mAlbumNumber);
162 // Number of songs (line three)
163 mData[i].mLineThree = MusicUtils.makeLabel(getContext(),
164 R.plurals.Nsongs, artist.mSongNumber);
169 * Starts playing an artist if the user touches the artist image in the
172 * @param artist The {@link ImageView} holding the aritst image
173 * @param position The position of the artist to play.
175 private void playArtist(final ImageView artist, final int position) {
176 artist.setOnClickListener(new OnClickListener() {
179 public void onClick(final View v) {
180 final long id = getItem(position).mArtistId;
181 final long[] list = MusicUtils.getSongListForArtist(getContext(), id);
182 MusicUtils.playAll(getContext(), list, 0, false);
188 * Method that unloads and clears the items in the adapter
190 public void unload() {
196 * @param pause True to temporarily pause the disk cache, false otherwise.
198 public void setPauseDiskCache(final boolean pause) {
199 if (mImageFetcher != null) {
200 mImageFetcher.setPauseDiskCache(pause);
205 * @param artist The key used to find the cached artist to remove
207 public void removeFromCache(final Artist artist) {
208 if (mImageFetcher != null) {
209 mImageFetcher.removeFromCache(artist.mArtistName);
214 * Flushes the disk cache.
216 public void flush() {
217 mImageFetcher.flush();
221 * @param extra True to load line three and the background image, false
224 public void setLoadExtraData(final boolean extra) {
225 mLoadExtraData = extra;