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;
22 import android.widget.TextView;
24 import com.cyngn.eleven.R;
25 import com.cyngn.eleven.cache.ImageFetcher;
26 import com.cyngn.eleven.model.Album;
27 import com.cyngn.eleven.sectionadapter.SectionAdapter;
28 import com.cyngn.eleven.ui.MusicHolder;
29 import com.cyngn.eleven.ui.MusicHolder.DataHolder;
30 import com.cyngn.eleven.utils.ApolloUtils;
31 import com.cyngn.eleven.utils.MusicUtils;
34 * This {@link ArrayAdapter} is used to display all of the albums on a user's
35 * device for {@link RecentsFragment} and {@link AlbumsFragment}.
37 * @author Andrew Neal (andrewdneal@gmail.com)
39 public class AlbumAdapter extends ArrayAdapter<Album> implements SectionAdapter.BasicAdapter {
42 * Number of views (ImageView and TextView)
44 private static final int VIEW_TYPE_COUNT = 2;
47 * The resource Id of the layout to inflate
49 private final int mLayoutId;
52 * Image cache and image fetcher
54 private final ImageFetcher mImageFetcher;
57 * Semi-transparent overlay
59 private final int mOverlay;
62 * Sets the album art on click listener to start playing them album when
65 private boolean mTouchPlay = false;
68 * Used to cache the album info
70 private DataHolder[] mData;
73 * Constructor of <code>AlbumAdapter</code>
75 * @param context The {@link Context} to use.
76 * @param layoutId The resource Id of the view to inflate.
77 * @param style Determines which layout to use and therefore which items to
80 public AlbumAdapter(final Activity context, final int layoutId) {
84 // Initialize the cache & image fetcher
85 mImageFetcher = ApolloUtils.getImageFetcher(context);
86 // Cache the transparent overlay
87 mOverlay = context.getResources().getColor(R.color.list_item_background);
94 public View getView(final int position, View convertView, final ViewGroup parent) {
95 // Recycle ViewHolder's items
97 if (convertView == null) {
98 convertView = LayoutInflater.from(getContext()).inflate(mLayoutId, parent, false);
99 holder = new MusicHolder(convertView);
100 convertView.setTag(holder);
102 holder = (MusicHolder)convertView.getTag();
105 // Retrieve the data holder
106 final DataHolder dataHolder = mData[position];
108 // Set each album name (line one)
109 holder.mLineOne.get().setText(dataHolder.mLineOne);
110 // Set the artist name (line two)
111 holder.mLineTwo.get().setText(dataHolder.mLineTwo);
112 // Asynchronously load the album images into the adapter
113 mImageFetcher.loadAlbumImage(dataHolder.mLineTwo, dataHolder.mLineOne, dataHolder.mItemId,
114 holder.mImage.get());
117 // Play the album when the artwork is touched
118 playAlbum(holder.mImage.get(), position);
127 public boolean hasStableIds() {
135 public int getViewTypeCount() {
136 return VIEW_TYPE_COUNT;
140 * Method used to cache the data used to populate the list or grid. The idea
141 * is to cache everything before {@code #getView(int, View, ViewGroup)} is
144 public void buildCache() {
145 mData = new DataHolder[getCount()];
146 for (int i = 0; i < getCount(); i++) {
148 final Album album = getItem(i);
150 // Build the data holder
151 mData[i] = new DataHolder();
153 mData[i].mItemId = album.mAlbumId;
154 // Album names (line one)
155 mData[i].mLineOne = album.mAlbumName;
156 // Album artist names (line two)
157 mData[i].mLineTwo = album.mArtistName;
162 * Starts playing an album if the user touches the artwork in the list.
164 * @param album The {@link ImageView} holding the album
165 * @param position The position of the album to play.
167 private void playAlbum(final ImageView album, final int position) {
168 album.setOnClickListener(new OnClickListener() {
171 public void onClick(final View v) {
172 final long id = getItem(position).mAlbumId;
173 final long[] list = MusicUtils.getSongListForAlbum(getContext(), id);
174 MusicUtils.playAll(getContext(), list, 0, false);
180 * Method that unloads and clears the items in the adapter
182 public void unload() {
188 * @param pause True to temporarily pause the disk cache, false otherwise.
190 public void setPauseDiskCache(final boolean pause) {
191 if (mImageFetcher != null) {
192 mImageFetcher.setPauseDiskCache(pause);
197 * @param album The key used to find the cached album to remove
199 public void removeFromCache(final Album album) {
200 if (mImageFetcher != null) {
201 mImageFetcher.removeFromCache(
202 ImageFetcher.generateAlbumCacheKey(album.mAlbumName, album.mArtistName));
207 * Flushes the disk cache.
209 public void flush() {
210 mImageFetcher.flush();
214 * Gets the item position for a given id
215 * @param id identifies the object
216 * @return the position if found, -1 otherwise
219 public int getItemPosition(long id) {
220 for (int i = 0; i < getCount(); i++) {
221 if (getItem(i).mAlbumId == id) {
230 * @param play True to play the album when the artwork is touched, false
233 public void setTouchPlay(final boolean play) {