OSDN Git Service

Eleven: lots of ui tweaks (fonts, padding, grid layout) and cancel tasks smarter
[android-x86/packages-apps-Eleven.git] / src / com / cyngn / eleven / adapters / SongAdapter.java
1 /*
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.
10  */
11
12 package com.cyngn.eleven.adapters;
13
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;
20
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;
32
33 /**
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}.
37  * 
38  * @author Andrew Neal (andrewdneal@gmail.com)
39  */
40 public class SongAdapter extends ArrayAdapter<Song> implements SectionAdapter.BasicAdapter {
41
42     /**
43      * Number of views (TextView)
44      */
45     private static final int VIEW_TYPE_COUNT = 1;
46
47     /**
48      * The resource Id of the layout to inflate
49      */
50     private final int mLayoutId;
51
52     /**
53      * Image cache and image fetcher
54      */
55     private final ImageFetcher mImageFetcher;
56
57     /**
58      * The index of the item that is currently playing
59      */
60     private long mCurrentlyPlayingSongId = -1;
61
62     /**
63      * Used to cache the song info
64      */
65     private DataHolder[] mData;
66
67     /**
68      * Constructor of <code>SongAdapter</code>
69      * 
70      * @param context The {@link Context} to use.
71      * @param layoutId The resource Id of the view to inflate.
72      */
73     public SongAdapter(final Activity context, final int layoutId) {
74         super(context, 0);
75         // Get the layout Id
76         mLayoutId = layoutId;
77         // Initialize the cache & image fetcher
78         mImageFetcher = ApolloUtils.getImageFetcher(context);
79     }
80
81     /**
82      * {@inheritDoc}
83      */
84     @Override
85     public View getView(final int position, View convertView, final ViewGroup parent) {
86         // Recycle ViewHolder's items
87         MusicHolder holder;
88         if (convertView == null) {
89             convertView = LayoutInflater.from(getContext()).inflate(mLayoutId, parent, false);
90             holder = new MusicHolder(convertView);
91             convertView.setTag(holder);
92         } else {
93             holder = (MusicHolder)convertView.getTag();
94         }
95
96         // Retrieve the data holder
97         final DataHolder dataHolder = mData[position];
98
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);
103
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());
109         }
110
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();
116
117             if (mCurrentlyPlayingSongId == dataHolder.mItemId) {
118                 // make it visible
119                 playPauseProgressButton.enableAndShow();
120                 playPauseContainer.setVisibility(View.VISIBLE);
121             } else {
122                 // hide it
123                 playPauseProgressButton.disableAndHide();
124                 playPauseContainer.setVisibility(View.GONE);
125             }
126         }
127
128         return convertView;
129     }
130
131     /**
132      * {@inheritDoc}
133      */
134     @Override
135     public boolean hasStableIds() {
136         return true;
137     }
138
139     /**
140      * {@inheritDoc}
141      */
142     @Override
143     public int getViewTypeCount() {
144         return VIEW_TYPE_COUNT;
145     }
146
147     /**
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
150      * called.
151      */
152     public void buildCache() {
153         mData = new DataHolder[getCount()];
154         for (int i = 0; i < getCount(); i++) {
155             // Build the song
156             final Song song = getItem(i);
157
158             // Build the data holder
159             mData[i] = new DataHolder();
160             // Song Id
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;
168         }
169     }
170
171     /**
172      * @param pause True to temporarily pause the disk cache, false otherwise.
173      */
174     public void setPauseDiskCache(final boolean pause) {
175         if (mImageFetcher != null) {
176             mImageFetcher.setPauseDiskCache(pause);
177         }
178     }
179
180     /**
181      * @param artist The key used to find the cached artist to remove
182      */
183     public void removeFromCache(final Artist artist) {
184         if (mImageFetcher != null) {
185             mImageFetcher.removeFromCache(artist.mArtistName);
186         }
187     }
188
189     /**
190      * Method that unloads and clears the items in the adapter
191      */
192     public void unload() {
193         clear();
194         mData = null;
195     }
196
197     /**
198      * Do nothing.
199      */
200     public void flush() {
201     }
202
203     /**
204      * Gets the item position for a given id
205      * @param id identifies the object
206      * @return the position if found, -1 otherwise
207      */
208     @Override
209     public int getItemPosition(long id) {
210         for (int i = 0; i < getCount(); i++) {
211             if (getItem(i).mSongId == id) {
212                 return i;
213             }
214         }
215
216         return  -1;
217     }
218
219     public void setCurrentlyPlayingSongId(long songId) {
220         if (mCurrentlyPlayingSongId != songId) {
221             mCurrentlyPlayingSongId = songId;
222
223             notifyDataSetChanged();
224         }
225     }
226 }