OSDN Git Service

Eleven: Add song indicators, change ProfileSongAdapter to use SongAdapter
[android-x86/packages-apps-Eleven.git] / src / com / cyngn / eleven / adapters / AlbumAdapter.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.View.OnClickListener;
19 import android.view.ViewGroup;
20 import android.widget.ArrayAdapter;
21 import android.widget.ImageView;
22 import android.widget.TextView;
23
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;
34
35 /**
36  * This {@link ArrayAdapter} is used to display all of the albums on a user's
37  * device for {@link RecentsFragment} and {@link AlbumsFragment}.
38  * 
39  * @author Andrew Neal (andrewdneal@gmail.com)
40  */
41 public class AlbumAdapter extends ArrayAdapter<Album>
42         implements SectionAdapter.BasicAdapter, IPopupMenuCallback {
43
44     /**
45      * Number of views (ImageView and TextView)
46      */
47     private static final int VIEW_TYPE_COUNT = 2;
48
49     /**
50      * The resource Id of the layout to inflate
51      */
52     private final int mLayoutId;
53
54     /**
55      * Image cache and image fetcher
56      */
57     private final ImageFetcher mImageFetcher;
58
59     /**
60      * Semi-transparent overlay
61      */
62     private final int mOverlay;
63
64     /**
65      * Sets the album art on click listener to start playing them album when
66      * touched.
67      */
68     private boolean mTouchPlay = false;
69
70     /**
71      * Used to cache the album info
72      */
73     private DataHolder[] mData;
74
75     /**
76      * Used to listen to the pop up menu callbacks
77      */
78     private IPopupMenuCallback.IListener mListener;
79
80     /**
81      * Constructor of <code>AlbumAdapter</code>
82      * 
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
86      *            load.
87      */
88     public AlbumAdapter(final Activity context, final int layoutId) {
89         super(context, 0);
90         // Get the layout Id
91         mLayoutId = 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);
96     }
97
98     /**
99      * {@inheritDoc}
100      */
101     @Override
102     public View getView(final int position, View convertView, final ViewGroup parent) {
103         // Recycle ViewHolder's items
104         MusicHolder holder;
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);
111         } else {
112             holder = (MusicHolder)convertView.getTag();
113         }
114
115         // Retrieve the data holder
116         final DataHolder dataHolder = mData[position];
117
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());
127
128         if (mTouchPlay) {
129             // Play the album when the artwork is touched
130             playAlbum(holder.mImage.get(), position);
131         }
132         return convertView;
133     }
134
135     /**
136      * {@inheritDoc}
137      */
138     @Override
139     public boolean hasStableIds() {
140         return true;
141     }
142
143     /**
144      * {@inheritDoc}
145      */
146     @Override
147     public int getViewTypeCount() {
148         return VIEW_TYPE_COUNT;
149     }
150
151     /**
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
154      * called.
155      */
156     public void buildCache() {
157         mData = new DataHolder[getCount()];
158         for (int i = 0; i < getCount(); i++) {
159             // Build the album
160             final Album album = getItem(i);
161
162             // Build the data holder
163             mData[i] = new DataHolder();
164             // Album Id
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         }
171     }
172
173     /**
174      * Starts playing an album if the user touches the artwork in the list.
175      * 
176      * @param album The {@link ImageView} holding the album
177      * @param position The position of the album to play.
178      */
179     private void playAlbum(final ImageView album, final int position) {
180         album.setOnClickListener(new OnClickListener() {
181
182             @Override
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);
187             }
188         });
189     }
190
191     /**
192      * Method that unloads and clears the items in the adapter
193      */
194     public void unload() {
195         clear();
196         mData = null;
197     }
198
199     /**
200      * @param pause True to temporarily pause the disk cache, false otherwise.
201      */
202     public void setPauseDiskCache(final boolean pause) {
203         if (mImageFetcher != null) {
204             mImageFetcher.setPauseDiskCache(pause);
205         }
206     }
207
208     /**
209      * @param album The key used to find the cached album to remove
210      */
211     public void removeFromCache(final Album album) {
212         if (mImageFetcher != null) {
213             mImageFetcher.removeFromCache(
214                     ImageFetcher.generateAlbumCacheKey(album.mAlbumName, album.mArtistName));
215         }
216     }
217
218     /**
219      * Flushes the disk cache.
220      */
221     public void flush() {
222         mImageFetcher.flush();
223     }
224
225     /**
226      * Gets the item position for a given id
227      * @param id identifies the object
228      * @return the position if found, -1 otherwise
229      */
230     @Override
231     public int getItemPosition(long id) {
232         for (int i = 0; i < getCount(); i++) {
233             if (getItem(i).mAlbumId == id) {
234                 return i;
235             }
236         }
237
238         return  -1;
239     }
240
241     /**
242      * @param play True to play the album when the artwork is touched, false
243      *            otherwise.
244      */
245     public void setTouchPlay(final boolean play) {
246         mTouchPlay = play;
247     }
248
249     @Override
250     public void setPopupMenuClickedListener(IPopupMenuCallback.IListener listener) {
251         mListener = listener;
252     }
253 }