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.ui.MusicHolder;
30 import com.cyngn.eleven.ui.MusicHolder.DataHolder;
31 import com.cyngn.eleven.utils.ApolloUtils;
32 import com.cyngn.eleven.utils.MusicUtils;
35 * This {@link ArrayAdapter} is used to display all of the artists on a user's
36 * device for {@link ArtistFragment}.
38 * @author Andrew Neal (andrewdneal@gmail.com)
41 * @author Andrew Neal (andrewdneal@gmail.com)
43 public class ArtistAdapter extends ArrayAdapter<Artist> implements SectionAdapter.BasicAdapter {
46 * Number of views (ImageView and TextView)
48 private static final int VIEW_TYPE_COUNT = 2;
51 * The resource Id of the layout to inflate
53 private final int mLayoutId;
56 * Image cache and image fetcher
58 private final ImageFetcher mImageFetcher;
61 * Semi-transparent overlay
63 private final int mOverlay;
66 * Used to cache the artist info
68 private DataHolder[] mData;
71 * Loads line three and the background image if the user decides to.
73 private boolean mLoadExtraData = false;
76 * Constructor of <code>ArtistAdapter</code>
78 * @param context The {@link Context} to use.
79 * @param layoutId The resource Id of the view to inflate.
81 public ArtistAdapter(final Activity context, final int layoutId) {
85 // Initialize the cache & image fetcher
86 mImageFetcher = ApolloUtils.getImageFetcher(context);
87 // Cache the transparent overlay
88 mOverlay = context.getResources().getColor(R.color.list_item_background);
95 public View getView(final int position, View convertView, final ViewGroup parent) {
96 // Recycle ViewHolder's items
98 if (convertView == null) {
99 convertView = LayoutInflater.from(getContext()).inflate(mLayoutId, parent, false);
100 holder = new MusicHolder(convertView);
101 convertView.setTag(holder);
103 holder = (MusicHolder)convertView.getTag();
106 // Retrieve the data holder
107 final DataHolder dataHolder = mData[position];
109 // Set each artist name (line one)
110 holder.mLineOne.get().setText(dataHolder.mLineOne);
111 // Set the number of albums (line two)
112 holder.mLineTwo.get().setText(dataHolder.mLineTwo);
113 // Asynchronously load the artist image into the adapter
114 mImageFetcher.loadArtistImage(dataHolder.mLineOne, holder.mImage.get());
116 TextView lineThree = holder.mLineThree.get();
117 if (mLoadExtraData) {
118 // Make sure the background layer gets set
119 holder.mOverlay.get().setBackgroundColor(mOverlay);
120 if (lineThree != null) {
121 // Set the number of songs (line three)
122 lineThree.setText(dataHolder.mLineThree);
123 lineThree.setVisibility(View.VISIBLE);
126 // Set the background image
127 mImageFetcher.loadArtistImage(dataHolder.mLineOne, holder.mBackground.get());
128 // Play the artist when the artwork is touched
129 playArtist(holder.mImage.get(), position);
130 } else if (lineThree != null) {
131 lineThree.setVisibility(View.GONE);
140 public boolean hasStableIds() {
148 public int getViewTypeCount() {
149 return VIEW_TYPE_COUNT;
153 * Method used to cache the data used to populate the list or grid. The idea
154 * is to cache everything before {@code #getView(int, View, ViewGroup)} is
157 public void buildCache() {
158 mData = new DataHolder[getCount()];
159 for (int i = 0; i < getCount(); i++) {
161 final Artist artist = getItem(i);
163 // Build the data holder
164 mData[i] = new DataHolder();
166 mData[i].mItemId = artist.mArtistId;
167 // Artist names (line one)
168 mData[i].mLineOne = artist.mArtistName;
170 String albumNumber = MusicUtils.makeLabel(getContext(),
171 R.plurals.Nalbums, artist.mAlbumNumber);
172 String songNumber = MusicUtils.makeLabel(getContext(),
173 R.plurals.Nsongs, artist.mSongNumber);
175 final Resources resources = getContext().getResources();
176 // combine the ablum number and song number
177 String combinedString = resources.getString(R.string.combine_two_strings,
178 albumNumber, songNumber);
180 mData[i].mLineTwo = combinedString;
185 * Starts playing an artist if the user touches the artist image in the
188 * @param artist The {@link ImageView} holding the aritst image
189 * @param position The position of the artist to play.
191 private void playArtist(final ImageView artist, final int position) {
192 artist.setOnClickListener(new OnClickListener() {
195 public void onClick(final View v) {
196 final long id = getItem(position).mArtistId;
197 final long[] list = MusicUtils.getSongListForArtist(getContext(), id);
198 MusicUtils.playAll(getContext(), list, 0, false);
204 * Method that unloads and clears the items in the adapter
206 public void unload() {
212 * @param pause True to temporarily pause the disk cache, false otherwise.
214 public void setPauseDiskCache(final boolean pause) {
215 if (mImageFetcher != null) {
216 mImageFetcher.setPauseDiskCache(pause);
221 * @param artist The key used to find the cached artist to remove
223 public void removeFromCache(final Artist artist) {
224 if (mImageFetcher != null) {
225 mImageFetcher.removeFromCache(artist.mArtistName);
230 * Flushes the disk cache.
232 public void flush() {
233 mImageFetcher.flush();
237 * Gets the item position for a given id
238 * @param id identifies the object
239 * @return the position if found, -1 otherwise
242 public int getItemPosition(long id) {
243 for (int i = 0; i < getCount(); i++) {
244 if (getItem(i).mArtistId == id) {
253 * @param extra True to load line three and the background image, false
256 public void setLoadExtraData(final boolean extra) {
257 mLoadExtraData = extra;