OSDN Git Service

Eleven: Move dividers into the list item to allow more flexibility
[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.R;
25 import com.cyngn.eleven.cache.ImageFetcher;
26 import com.cyngn.eleven.model.Album;
27 import com.cyngn.eleven.sectionadapter.SectionAdapter;
28 import com.cyngn.eleven.ui.MusicHolder;
29 import com.cyngn.eleven.ui.MusicHolder.DataHolder;
30 import com.cyngn.eleven.utils.ApolloUtils;
31 import com.cyngn.eleven.utils.MusicUtils;
32
33 /**
34  * This {@link ArrayAdapter} is used to display all of the albums on a user's
35  * device for {@link RecentsFragment} and {@link AlbumsFragment}.
36  * 
37  * @author Andrew Neal (andrewdneal@gmail.com)
38  */
39 public class AlbumAdapter extends ArrayAdapter<Album> implements SectionAdapter.BasicAdapter {
40
41     /**
42      * Number of views (ImageView and TextView)
43      */
44     private static final int VIEW_TYPE_COUNT = 2;
45
46     /**
47      * The resource Id of the layout to inflate
48      */
49     private final int mLayoutId;
50
51     /**
52      * Image cache and image fetcher
53      */
54     private final ImageFetcher mImageFetcher;
55
56     /**
57      * Semi-transparent overlay
58      */
59     private final int mOverlay;
60
61     /**
62      * Sets the album art on click listener to start playing them album when
63      * touched.
64      */
65     private boolean mTouchPlay = false;
66
67     /**
68      * Used to cache the album info
69      */
70     private DataHolder[] mData;
71
72     /**
73      * Constructor of <code>AlbumAdapter</code>
74      * 
75      * @param context The {@link Context} to use.
76      * @param layoutId The resource Id of the view to inflate.
77      * @param style Determines which layout to use and therefore which items to
78      *            load.
79      */
80     public AlbumAdapter(final Activity context, final int layoutId) {
81         super(context, 0);
82         // Get the layout Id
83         mLayoutId = layoutId;
84         // Initialize the cache & image fetcher
85         mImageFetcher = ApolloUtils.getImageFetcher(context);
86         // Cache the transparent overlay
87         mOverlay = context.getResources().getColor(R.color.list_item_background);
88     }
89
90     /**
91      * {@inheritDoc}
92      */
93     @Override
94     public View getView(final int position, View convertView, final ViewGroup parent) {
95         // Recycle ViewHolder's items
96         MusicHolder holder;
97         if (convertView == null) {
98             convertView = LayoutInflater.from(getContext()).inflate(mLayoutId, parent, false);
99             holder = new MusicHolder(convertView);
100             convertView.setTag(holder);
101         } else {
102             holder = (MusicHolder)convertView.getTag();
103         }
104
105         // Retrieve the data holder
106         final DataHolder dataHolder = mData[position];
107
108         // Set each album name (line one)
109         holder.mLineOne.get().setText(dataHolder.mLineOne);
110         // Set the artist name (line two)
111         holder.mLineTwo.get().setText(dataHolder.mLineTwo);
112         // Asynchronously load the album images into the adapter
113         mImageFetcher.loadAlbumImage(dataHolder.mLineTwo, dataHolder.mLineOne, dataHolder.mItemId,
114                 holder.mImage.get());
115
116         if (mTouchPlay) {
117             // Play the album when the artwork is touched
118             playAlbum(holder.mImage.get(), position);
119         }
120         return convertView;
121     }
122
123     /**
124      * {@inheritDoc}
125      */
126     @Override
127     public boolean hasStableIds() {
128         return true;
129     }
130
131     /**
132      * {@inheritDoc}
133      */
134     @Override
135     public int getViewTypeCount() {
136         return VIEW_TYPE_COUNT;
137     }
138
139     /**
140      * Method used to cache the data used to populate the list or grid. The idea
141      * is to cache everything before {@code #getView(int, View, ViewGroup)} is
142      * called.
143      */
144     public void buildCache() {
145         mData = new DataHolder[getCount()];
146         for (int i = 0; i < getCount(); i++) {
147             // Build the album
148             final Album album = getItem(i);
149
150             // Build the data holder
151             mData[i] = new DataHolder();
152             // Album Id
153             mData[i].mItemId = album.mAlbumId;
154             // Album names (line one)
155             mData[i].mLineOne = album.mAlbumName;
156             // Album artist names (line two)
157             mData[i].mLineTwo = album.mArtistName;
158         }
159     }
160
161     /**
162      * Starts playing an album if the user touches the artwork in the list.
163      * 
164      * @param album The {@link ImageView} holding the album
165      * @param position The position of the album to play.
166      */
167     private void playAlbum(final ImageView album, final int position) {
168         album.setOnClickListener(new OnClickListener() {
169
170             @Override
171             public void onClick(final View v) {
172                 final long id = getItem(position).mAlbumId;
173                 final long[] list = MusicUtils.getSongListForAlbum(getContext(), id);
174                 MusicUtils.playAll(getContext(), list, 0, false);
175             }
176         });
177     }
178
179     /**
180      * Method that unloads and clears the items in the adapter
181      */
182     public void unload() {
183         clear();
184         mData = null;
185     }
186
187     /**
188      * @param pause True to temporarily pause the disk cache, false otherwise.
189      */
190     public void setPauseDiskCache(final boolean pause) {
191         if (mImageFetcher != null) {
192             mImageFetcher.setPauseDiskCache(pause);
193         }
194     }
195
196     /**
197      * @param album The key used to find the cached album to remove
198      */
199     public void removeFromCache(final Album album) {
200         if (mImageFetcher != null) {
201             mImageFetcher.removeFromCache(
202                     ImageFetcher.generateAlbumCacheKey(album.mAlbumName, album.mArtistName));
203         }
204     }
205
206     /**
207      * Flushes the disk cache.
208      */
209     public void flush() {
210         mImageFetcher.flush();
211     }
212
213     /**
214      * Gets the item position for a given id
215      * @param id identifies the object
216      * @return the position if found, -1 otherwise
217      */
218     @Override
219     public int getItemPosition(long id) {
220         for (int i = 0; i < getCount(); i++) {
221             if (getItem(i).mAlbumId == id) {
222                 return i;
223             }
224         }
225
226         return  -1;
227     }
228
229     /**
230      * @param play True to play the album when the artwork is touched, false
231      *            otherwise.
232      */
233     public void setTouchPlay(final boolean play) {
234         mTouchPlay = play;
235     }
236 }