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.content.res.Resources;
17 import android.view.LayoutInflater;
18 import android.view.View;
19 import android.view.View.OnClickListener;
20 import android.view.ViewGroup;
21 import android.widget.ArrayAdapter;
22 import android.widget.ImageView;
23 import android.widget.TextView;
25 import com.cyngn.eleven.R;
26 import com.cyngn.eleven.cache.ImageFetcher;
27 import com.cyngn.eleven.model.Artist;
28 import com.cyngn.eleven.sectionadapter.SectionAdapter;
29 import com.cyngn.eleven.sectionadapter.SectionAdapter.BasicAdapter;
30 import com.cyngn.eleven.ui.MusicHolder;
31 import com.cyngn.eleven.ui.MusicHolder.DataHolder;
32 import com.cyngn.eleven.utils.ApolloUtils;
33 import com.cyngn.eleven.utils.MusicUtils;
34 import com.cyngn.eleven.widgets.IPopupMenuCallback;
37 * This {@link ArrayAdapter} is used to display all of the artists on a user's
38 * device for {@link ArtistFragment}.
40 * @author Andrew Neal (andrewdneal@gmail.com)
43 * @author Andrew Neal (andrewdneal@gmail.com)
45 public class ArtistAdapter extends ArrayAdapter<Artist> implements BasicAdapter, IPopupMenuCallback {
48 * Number of views (ImageView and TextView)
50 private static final int VIEW_TYPE_COUNT = 2;
53 * The resource Id of the layout to inflate
55 private final int mLayoutId;
58 * Image cache and image fetcher
60 private final ImageFetcher mImageFetcher;
63 * Semi-transparent overlay
65 private final int mOverlay;
68 * Used to cache the artist info
70 private DataHolder[] mData;
73 * Used to listen to the pop up menu callbacks
75 private IListener mListener;
78 * Constructor of <code>ArtistAdapter</code>
80 * @param context The {@link Context} to use.
81 * @param layoutId The resource Id of the view to inflate.
83 public ArtistAdapter(final Activity context, final int layoutId) {
87 // Initialize the cache & image fetcher
88 mImageFetcher = ApolloUtils.getImageFetcher(context);
89 // Cache the transparent overlay
90 mOverlay = context.getResources().getColor(R.color.list_item_background);
97 public View getView(final int position, View convertView, final ViewGroup parent) {
98 // Recycle ViewHolder's items
100 if (convertView == null) {
101 convertView = LayoutInflater.from(getContext()).inflate(mLayoutId, parent, false);
102 holder = new MusicHolder(convertView);
103 convertView.setTag(holder);
105 // set the pop up menu listener
106 holder.mPopupMenuButton.get().setPopupMenuClickedListener(mListener);
108 holder = (MusicHolder)convertView.getTag();
111 // Retrieve the data holder
112 final DataHolder dataHolder = mData[position];
114 // Set each artist name (line one)
115 holder.mLineOne.get().setText(dataHolder.mLineOne);
116 // Set the number of albums (line two)
117 holder.mLineTwo.get().setText(dataHolder.mLineTwo);
118 // Asynchronously load the artist image into the adapter
119 mImageFetcher.loadArtistImage(dataHolder.mLineOne, holder.mImage.get());
120 // because of recycling, we need to set the position each time
121 holder.mPopupMenuButton.get().setPosition(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;
160 String albumNumber = MusicUtils.makeLabel(getContext(),
161 R.plurals.Nalbums, artist.mAlbumNumber);
162 String songNumber = MusicUtils.makeLabel(getContext(),
163 R.plurals.Nsongs, artist.mSongNumber);
165 mData[i].mLineTwo = MusicUtils.makeCombinedString(getContext(), albumNumber, songNumber);
170 * Starts playing an artist if the user touches the artist image in the
173 * @param artist The {@link ImageView} holding the aritst image
174 * @param position The position of the artist to play.
176 private void playArtist(final ImageView artist, final int position) {
177 artist.setOnClickListener(new OnClickListener() {
180 public void onClick(final View v) {
181 final long id = getItem(position).mArtistId;
182 final long[] list = MusicUtils.getSongListForArtist(getContext(), id);
183 MusicUtils.playAll(getContext(), list, 0, false);
189 * Method that unloads and clears the items in the adapter
191 public void unload() {
197 * @param pause True to temporarily pause the disk cache, false otherwise.
199 public void setPauseDiskCache(final boolean pause) {
200 if (mImageFetcher != null) {
201 mImageFetcher.setPauseDiskCache(pause);
206 * @param artist The key used to find the cached artist to remove
208 public void removeFromCache(final Artist artist) {
209 if (mImageFetcher != null) {
210 mImageFetcher.removeFromCache(artist.mArtistName);
215 * Flushes the disk cache.
217 public void flush() {
218 mImageFetcher.flush();
222 * Gets the item position for a given id
223 * @param id identifies the object
224 * @return the position if found, -1 otherwise
227 public int getItemPosition(long id) {
228 for (int i = 0; i < getCount(); i++) {
229 if (getItem(i).mArtistId == id) {
238 public void setPopupMenuClickedListener(IListener listener) {
239 mListener = listener;