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.ViewGroup;
19 import android.widget.ArrayAdapter;
21 import com.cyngn.eleven.cache.ImageFetcher;
22 import com.cyngn.eleven.model.Artist;
23 import com.cyngn.eleven.model.Song;
24 import com.cyngn.eleven.sectionadapter.SectionAdapter;
25 import com.cyngn.eleven.ui.MusicHolder;
26 import com.cyngn.eleven.ui.MusicHolder.DataHolder;
27 import com.cyngn.eleven.ui.fragments.QueueFragment;
28 import com.cyngn.eleven.ui.fragments.SongFragment;
29 import com.cyngn.eleven.utils.ApolloUtils;
30 import com.cyngn.eleven.utils.MusicUtils;
31 import com.cyngn.eleven.widgets.PlayPauseProgressButton;
34 * This {@link ArrayAdapter} is used to display all of the songs on a user's
35 * device for {@link SongFragment}. It is also used to show the queue in
36 * {@link QueueFragment}.
38 * @author Andrew Neal (andrewdneal@gmail.com)
40 public class SongAdapter extends ArrayAdapter<Song> implements SectionAdapter.BasicAdapter {
43 * Number of views (TextView)
45 private static final int VIEW_TYPE_COUNT = 1;
48 * The resource Id of the layout to inflate
50 private final int mLayoutId;
53 * Image cache and image fetcher
55 private final ImageFetcher mImageFetcher;
58 * The index of the item that is currently playing
60 private long mCurrentlyPlayingSongId = -1;
63 * Used to cache the song info
65 private DataHolder[] mData;
68 * Constructor of <code>SongAdapter</code>
70 * @param context The {@link Context} to use.
71 * @param layoutId The resource Id of the view to inflate.
73 public SongAdapter(final Activity context, final int layoutId) {
77 // Initialize the cache & image fetcher
78 mImageFetcher = ApolloUtils.getImageFetcher(context);
85 public View getView(final int position, View convertView, final ViewGroup parent) {
86 // Recycle ViewHolder's items
88 if (convertView == null) {
89 convertView = LayoutInflater.from(getContext()).inflate(mLayoutId, parent, false);
90 holder = new MusicHolder(convertView);
91 convertView.setTag(holder);
93 holder = (MusicHolder)convertView.getTag();
96 // Retrieve the data holder
97 final DataHolder dataHolder = mData[position];
99 // Set each song name (line one)
100 holder.mLineOne.get().setText(dataHolder.mLineOne);
101 // Set the album name (line two)
102 holder.mLineTwo.get().setText(dataHolder.mLineTwo);
104 // Asynchronously load the artist image into the adapter
105 Song item = getItem(position);
106 if (item.mAlbumId >= 0) {
107 mImageFetcher.loadAlbumImage(item.mArtistName, item.mAlbumName, item.mAlbumId,
108 holder.mImage.get());
111 // padding doesn't apply to included layouts, so we need
112 // to wrap it in a container and show/hide with the container
113 PlayPauseProgressButton playPauseProgressButton = holder.mPlayPauseProgressButton.get();
114 if (playPauseProgressButton != null) {
115 View playPauseContainer = holder.mPlayPauseProgressContainer.get();
117 if (mCurrentlyPlayingSongId == dataHolder.mItemId) {
119 playPauseProgressButton.enableAndShow();
120 playPauseContainer.setVisibility(View.VISIBLE);
123 playPauseProgressButton.disableAndHide();
124 playPauseContainer.setVisibility(View.GONE);
135 public boolean hasStableIds() {
143 public int getViewTypeCount() {
144 return VIEW_TYPE_COUNT;
148 * Method used to cache the data used to populate the list or grid. The idea
149 * is to cache everything before {@code #getView(int, View, ViewGroup)} is
152 public void buildCache() {
153 mData = new DataHolder[getCount()];
154 for (int i = 0; i < getCount(); i++) {
156 final Song song = getItem(i);
158 // Build the data holder
159 mData[i] = new DataHolder();
161 mData[i].mItemId = song.mSongId;
162 // Song names (line one)
163 mData[i].mLineOne = song.mSongName;
164 // Song duration (line one, right)
165 mData[i].mLineOneRight = MusicUtils.makeTimeString(getContext(), song.mDuration);
166 // Album names (line two)
167 mData[i].mLineTwo = song.mAlbumName;
172 * @param pause True to temporarily pause the disk cache, false otherwise.
174 public void setPauseDiskCache(final boolean pause) {
175 if (mImageFetcher != null) {
176 mImageFetcher.setPauseDiskCache(pause);
181 * @param artist The key used to find the cached artist to remove
183 public void removeFromCache(final Artist artist) {
184 if (mImageFetcher != null) {
185 mImageFetcher.removeFromCache(artist.mArtistName);
190 * Method that unloads and clears the items in the adapter
192 public void unload() {
200 public void flush() {
204 * Gets the item position for a given id
205 * @param id identifies the object
206 * @return the position if found, -1 otherwise
209 public int getItemPosition(long id) {
210 for (int i = 0; i < getCount(); i++) {
211 if (getItem(i).mSongId == id) {
219 public void setCurrentlyPlayingSongId(long songId) {
220 if (mCurrentlyPlayingSongId != songId) {
221 mCurrentlyPlayingSongId = songId;
223 notifyDataSetChanged();