OSDN Git Service

Repackaged com.andrew.apollo to com.cyngn.eleven
[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
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;
31
32 /**
33  * This {@link ArrayAdapter} is used to display all of the albums on a user's
34  * device for {@link RecentsFragment} and {@link AlbumsFragment}.
35  * 
36  * @author Andrew Neal (andrewdneal@gmail.com)
37  */
38 public class AlbumAdapter extends ArrayAdapter<Album> {
39
40     /**
41      * Number of views (ImageView and TextView)
42      */
43     private static final int VIEW_TYPE_COUNT = 2;
44
45     /**
46      * The resource Id of the layout to inflate
47      */
48     private final int mLayoutId;
49
50     /**
51      * Image cache and image fetcher
52      */
53     private final ImageFetcher mImageFetcher;
54
55     /**
56      * Semi-transparent overlay
57      */
58     private final int mOverlay;
59
60     /**
61      * Determines if the grid or list should be the default style
62      */
63     private boolean mLoadExtraData = false;
64
65     /**
66      * Sets the album art on click listener to start playing them album when
67      * touched.
68      */
69     private boolean mTouchPlay = false;
70
71     /**
72      * Used to cache the album info
73      */
74     private DataHolder[] mData;
75
76     /**
77      * Constructor of <code>AlbumAdapter</code>
78      * 
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
82      *            load.
83      */
84     public AlbumAdapter(final Activity context, final int layoutId) {
85         super(context, 0);
86         // Get the layout Id
87         mLayoutId = 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);
92     }
93
94     /**
95      * {@inheritDoc}
96      */
97     @Override
98     public View getView(final int position, View convertView, final ViewGroup parent) {
99         // Recycle ViewHolder's items
100         MusicHolder holder;
101         if (convertView == null) {
102             convertView = LayoutInflater.from(getContext()).inflate(mLayoutId, parent, false);
103             holder = new MusicHolder(convertView);
104             convertView.setTag(holder);
105         } else {
106             holder = (MusicHolder)convertView.getTag();
107         }
108
109         // Retrieve the data holder
110         final DataHolder dataHolder = mData[position];
111
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());
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             // Number of songs for each album (line three)
171             mData[i].mLineThree = MusicUtils.makeLabel(getContext(),
172                     R.plurals.Nsongs, album.mSongNumber);
173         }
174     }
175
176     /**
177      * Starts playing an album if the user touches the artwork in the list.
178      * 
179      * @param album The {@link ImageView} holding the album
180      * @param position The position of the album to play.
181      */
182     private void playAlbum(final ImageView album, final int position) {
183         album.setOnClickListener(new OnClickListener() {
184
185             @Override
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);
190             }
191         });
192     }
193
194     /**
195      * Method that unloads and clears the items in the adapter
196      */
197     public void unload() {
198         clear();
199         mData = null;
200     }
201
202     /**
203      * @param pause True to temporarily pause the disk cache, false otherwise.
204      */
205     public void setPauseDiskCache(final boolean pause) {
206         if (mImageFetcher != null) {
207             mImageFetcher.setPauseDiskCache(pause);
208         }
209     }
210
211     /**
212      * @param album The key used to find the cached album to remove
213      */
214     public void removeFromCache(final Album album) {
215         if (mImageFetcher != null) {
216             mImageFetcher.removeFromCache(
217                     ImageFetcher.generateAlbumCacheKey(album.mAlbumName, album.mArtistName));
218         }
219     }
220
221     /**
222      * Flushes the disk cache.
223      */
224     public void flush() {
225         mImageFetcher.flush();
226     }
227
228     /**
229      * @param extra True to load line three and the background image, false
230      *            otherwise.
231      */
232     public void setLoadExtraData(final boolean extra) {
233         mLoadExtraData = extra;
234         setTouchPlay(true);
235     }
236
237     /**
238      * @param play True to play the album when the artwork is touched, false
239      *            otherwise.
240      */
241     public void setTouchPlay(final boolean play) {
242         mTouchPlay = play;
243     }
244 }