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.Config;
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.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 albums on a user's
34 * device for {@link RecentsFragment} and {@link AlbumsFragment}.
36 * @author Andrew Neal (andrewdneal@gmail.com)
38 public class AlbumAdapter extends ArrayAdapter<Album> {
41 * Number of views (ImageView and TextView)
43 private static final int VIEW_TYPE_COUNT = 2;
46 * The resource Id of the layout to inflate
48 private final int mLayoutId;
51 * Image cache and image fetcher
53 private final ImageFetcher mImageFetcher;
56 * Semi-transparent overlay
58 private final int mOverlay;
61 * Determines if the grid or list should be the default style
63 private boolean mLoadExtraData = false;
66 * Sets the album art on click listener to start playing them album when
69 private boolean mTouchPlay = false;
72 * Used to cache the album info
74 private DataHolder[] mData;
77 * Constructor of <code>AlbumAdapter</code>
79 * @param context The {@link Context} to use.
80 * @param layoutId The resource Id of the view to inflate.
81 * @param style Determines which layout to use and therefore which items to
84 public AlbumAdapter(final Activity context, final int layoutId) {
88 // Initialize the cache & image fetcher
89 mImageFetcher = ApolloUtils.getImageFetcher(context);
90 // Cache the transparent overlay
91 mOverlay = context.getResources().getColor(R.color.list_item_background);
98 public View getView(final int position, View convertView, final ViewGroup parent) {
99 // Recycle ViewHolder's items
101 if (convertView == null) {
102 convertView = LayoutInflater.from(getContext()).inflate(mLayoutId, parent, false);
103 holder = new MusicHolder(convertView);
104 convertView.setTag(holder);
106 holder = (MusicHolder)convertView.getTag();
109 // Retrieve the data holder
110 final DataHolder dataHolder = mData[position];
112 // Set each album name (line one)
113 holder.mLineOne.get().setText(dataHolder.mLineOne);
114 // Set the artist name (line two)
115 holder.mLineTwo.get().setText(dataHolder.mLineTwo);
116 // Asynchronously load the album images into the adapter
117 mImageFetcher.loadAlbumImage(dataHolder.mLineTwo, dataHolder.mLineOne, dataHolder.mItemId,
118 holder.mImage.get());
119 // List view only items
120 if (mLoadExtraData) {
121 // Make sure the background layer gets set
122 holder.mOverlay.get().setBackgroundColor(mOverlay);
123 // Set the number of songs (line three)
124 holder.mLineThree.get().setText(dataHolder.mLineThree);
125 // Asynchronously load the artist image on the background view
126 mImageFetcher.loadArtistImage(dataHolder.mLineTwo, holder.mBackground.get());
129 // Play the album when the artwork is touched
130 playAlbum(holder.mImage.get(), position);
139 public boolean hasStableIds() {
147 public int getViewTypeCount() {
148 return VIEW_TYPE_COUNT;
152 * Method used to cache the data used to populate the list or grid. The idea
153 * is to cache everything before {@code #getView(int, View, ViewGroup)} is
156 public void buildCache() {
157 mData = new DataHolder[getCount()];
158 for (int i = 0; i < getCount(); i++) {
160 final Album album = getItem(i);
162 // Build the data holder
163 mData[i] = new DataHolder();
165 mData[i].mItemId = album.mAlbumId;
166 // Album names (line one)
167 mData[i].mLineOne = album.mAlbumName;
168 // Album artist names (line two)
169 mData[i].mLineTwo = album.mArtistName;
170 // Number of songs for each album (line three)
171 mData[i].mLineThree = MusicUtils.makeLabel(getContext(),
172 R.plurals.Nsongs, album.mSongNumber);
177 * Starts playing an album if the user touches the artwork in the list.
179 * @param album The {@link ImageView} holding the album
180 * @param position The position of the album to play.
182 private void playAlbum(final ImageView album, final int position) {
183 album.setOnClickListener(new OnClickListener() {
186 public void onClick(final View v) {
187 final long id = getItem(position).mAlbumId;
188 final long[] list = MusicUtils.getSongListForAlbum(getContext(), id);
189 MusicUtils.playAll(getContext(), list, 0, false);
195 * Method that unloads and clears the items in the adapter
197 public void unload() {
203 * @param pause True to temporarily pause the disk cache, false otherwise.
205 public void setPauseDiskCache(final boolean pause) {
206 if (mImageFetcher != null) {
207 mImageFetcher.setPauseDiskCache(pause);
212 * @param album The key used to find the cached album to remove
214 public void removeFromCache(final Album album) {
215 if (mImageFetcher != null) {
216 mImageFetcher.removeFromCache(
217 ImageFetcher.generateAlbumCacheKey(album.mAlbumName, album.mArtistName));
222 * Flushes the disk cache.
224 public void flush() {
225 mImageFetcher.flush();
229 * @param extra True to load line three and the background image, false
232 public void setLoadExtraData(final boolean extra) {
233 mLoadExtraData = extra;
238 * @param play True to play the album when the artwork is touched, false
241 public void setTouchPlay(final boolean play) {