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.Config;
25 import com.cyngn.eleven.R;
26 import com.cyngn.eleven.cache.ImageFetcher;
27 import com.cyngn.eleven.model.Album;
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;
33 import com.cyngn.eleven.widgets.IPopupMenuCallback;
36 * This {@link ArrayAdapter} is used to display all of the albums on a user's
37 * device for {@link RecentsFragment} and {@link AlbumsFragment}.
39 * @author Andrew Neal (andrewdneal@gmail.com)
41 public class AlbumAdapter extends ArrayAdapter<Album>
42 implements SectionAdapter.BasicAdapter, IPopupMenuCallback {
45 * Number of views (ImageView and TextView)
47 private static final int VIEW_TYPE_COUNT = 2;
50 * The resource Id of the layout to inflate
52 private final int mLayoutId;
55 * Image cache and image fetcher
57 private final ImageFetcher mImageFetcher;
60 * Semi-transparent overlay
62 private final int mOverlay;
65 * Sets the album art on click listener to start playing them album when
68 private boolean mTouchPlay = false;
71 * Used to cache the album info
73 private DataHolder[] mData;
76 * Used to listen to the pop up menu callbacks
78 private IPopupMenuCallback.IListener mListener;
81 * Constructor of <code>AlbumAdapter</code>
83 * @param context The {@link Context} to use.
84 * @param layoutId The resource Id of the view to inflate.
85 * @param style Determines which layout to use and therefore which items to
88 public AlbumAdapter(final Activity context, final int layoutId) {
92 // Initialize the cache & image fetcher
93 mImageFetcher = ApolloUtils.getImageFetcher(context);
94 // Cache the transparent overlay
95 mOverlay = context.getResources().getColor(R.color.list_item_background);
102 public View getView(final int position, View convertView, final ViewGroup parent) {
103 // Recycle ViewHolder's items
105 if (convertView == null) {
106 convertView = LayoutInflater.from(getContext()).inflate(mLayoutId, parent, false);
107 holder = new MusicHolder(convertView);
108 convertView.setTag(holder);
109 // set the pop up menu listener
110 holder.mPopupMenuButton.get().setPopupMenuClickedListener(mListener);
112 holder = (MusicHolder)convertView.getTag();
115 // Retrieve the data holder
116 final DataHolder dataHolder = mData[position];
118 // Sets the position each time because of recycling
119 holder.mPopupMenuButton.get().setPosition(position);
120 // Set each album name (line one)
121 holder.mLineOne.get().setText(dataHolder.mLineOne);
122 // Set the artist name (line two)
123 holder.mLineTwo.get().setText(dataHolder.mLineTwo);
124 // Asynchronously load the album images into the adapter
125 mImageFetcher.loadAlbumImage(dataHolder.mLineTwo, dataHolder.mLineOne, dataHolder.mItemId,
126 holder.mImage.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;
174 * Starts playing an album if the user touches the artwork in the list.
176 * @param album The {@link ImageView} holding the album
177 * @param position The position of the album to play.
179 private void playAlbum(final ImageView album, final int position) {
180 album.setOnClickListener(new OnClickListener() {
183 public void onClick(final View v) {
184 final long id = getItem(position).mAlbumId;
185 final long[] list = MusicUtils.getSongListForAlbum(getContext(), id);
186 MusicUtils.playAll(getContext(), list, 0, id, Config.IdType.Album, false);
192 * Method that unloads and clears the items in the adapter
194 public void unload() {
200 * @param pause True to temporarily pause the disk cache, false otherwise.
202 public void setPauseDiskCache(final boolean pause) {
203 if (mImageFetcher != null) {
204 mImageFetcher.setPauseDiskCache(pause);
209 * @param album The key used to find the cached album to remove
211 public void removeFromCache(final Album album) {
212 if (mImageFetcher != null) {
213 mImageFetcher.removeFromCache(
214 ImageFetcher.generateAlbumCacheKey(album.mAlbumName, album.mArtistName));
219 * Flushes the disk cache.
221 public void flush() {
222 mImageFetcher.flush();
226 * Gets the item position for a given id
227 * @param id identifies the object
228 * @return the position if found, -1 otherwise
231 public int getItemPosition(long id) {
232 for (int i = 0; i < getCount(); i++) {
233 if (getItem(i).mAlbumId == id) {
242 * @param play True to play the album when the artwork is touched, false
245 public void setTouchPlay(final boolean play) {
250 public void setPopupMenuClickedListener(IPopupMenuCallback.IListener listener) {
251 mListener = listener;