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 / ArtistAdapter.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.content.res.Resources;
17 import android.view.LayoutInflater;
18 import android.view.View;
19 import android.view.View.OnClickListener;
20 import android.view.ViewGroup;
21 import android.widget.ArrayAdapter;
22 import android.widget.ImageView;
23 import android.widget.TextView;
24
25 import com.cyngn.eleven.R;
26 import com.cyngn.eleven.cache.ImageFetcher;
27 import com.cyngn.eleven.model.Artist;
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
34 /**
35  * This {@link ArrayAdapter} is used to display all of the artists on a user's
36  * device for {@link ArtistFragment}.
37  * 
38  * @author Andrew Neal (andrewdneal@gmail.com)
39  */
40 /**
41  * @author Andrew Neal (andrewdneal@gmail.com)
42  */
43 public class ArtistAdapter extends ArrayAdapter<Artist> implements SectionAdapter.BasicAdapter {
44
45     /**
46      * Number of views (ImageView and TextView)
47      */
48     private static final int VIEW_TYPE_COUNT = 2;
49
50     /**
51      * The resource Id of the layout to inflate
52      */
53     private final int mLayoutId;
54
55     /**
56      * Image cache and image fetcher
57      */
58     private final ImageFetcher mImageFetcher;
59
60     /**
61      * Semi-transparent overlay
62      */
63     private final int mOverlay;
64
65     /**
66      * Used to cache the artist info
67      */
68     private DataHolder[] mData;
69
70     /**
71      * Loads line three and the background image if the user decides to.
72      */
73     private boolean mLoadExtraData = false;
74
75     /**
76      * Constructor of <code>ArtistAdapter</code>
77      * 
78      * @param context The {@link Context} to use.
79      * @param layoutId The resource Id of the view to inflate.
80      */
81     public ArtistAdapter(final Activity context, final int layoutId) {
82         super(context, 0);
83         // Get the layout Id
84         mLayoutId = layoutId;
85         // Initialize the cache & image fetcher
86         mImageFetcher = ApolloUtils.getImageFetcher(context);
87         // Cache the transparent overlay
88         mOverlay = context.getResources().getColor(R.color.list_item_background);
89     }
90
91     /**
92      * {@inheritDoc}
93      */
94     @Override
95     public View getView(final int position, View convertView, final ViewGroup parent) {
96         // Recycle ViewHolder's items
97         MusicHolder holder;
98         if (convertView == null) {
99             convertView = LayoutInflater.from(getContext()).inflate(mLayoutId, parent, false);
100             holder = new MusicHolder(convertView);
101             convertView.setTag(holder);
102         } else {
103             holder = (MusicHolder)convertView.getTag();
104         }
105
106         // Retrieve the data holder
107         final DataHolder dataHolder = mData[position];
108
109         // Set each artist name (line one)
110         holder.mLineOne.get().setText(dataHolder.mLineOne);
111         // Set the number of albums (line two)
112         holder.mLineTwo.get().setText(dataHolder.mLineTwo);
113         // Asynchronously load the artist image into the adapter
114         mImageFetcher.loadArtistImage(dataHolder.mLineOne, holder.mImage.get());
115
116         TextView lineThree = holder.mLineThree.get();
117         if (mLoadExtraData) {
118             // Make sure the background layer gets set
119             holder.mOverlay.get().setBackgroundColor(mOverlay);
120             if (lineThree != null) {
121                 // Set the number of songs (line three)
122                 lineThree.setText(dataHolder.mLineThree);
123                 lineThree.setVisibility(View.VISIBLE);
124             }
125
126             // Set the background image
127             mImageFetcher.loadArtistImage(dataHolder.mLineOne, holder.mBackground.get());
128             // Play the artist when the artwork is touched
129             playArtist(holder.mImage.get(), position);
130         } else if (lineThree != null) {
131             lineThree.setVisibility(View.GONE);
132         }
133         return convertView;
134     }
135
136     /**
137      * {@inheritDoc}
138      */
139     @Override
140     public boolean hasStableIds() {
141         return true;
142     }
143
144     /**
145      * {@inheritDoc}
146      */
147     @Override
148     public int getViewTypeCount() {
149         return VIEW_TYPE_COUNT;
150     }
151
152     /**
153      * Method used to cache the data used to populate the list or grid. The idea
154      * is to cache everything before {@code #getView(int, View, ViewGroup)} is
155      * called.
156      */
157     public void buildCache() {
158         mData = new DataHolder[getCount()];
159         for (int i = 0; i < getCount(); i++) {
160             // Build the artist
161             final Artist artist = getItem(i);
162
163             // Build the data holder
164             mData[i] = new DataHolder();
165             // Artist Id
166             mData[i].mItemId = artist.mArtistId;
167             // Artist names (line one)
168             mData[i].mLineOne = artist.mArtistName;
169
170             String albumNumber = MusicUtils.makeLabel(getContext(),
171                     R.plurals.Nalbums, artist.mAlbumNumber);
172             String songNumber = MusicUtils.makeLabel(getContext(),
173                     R.plurals.Nsongs, artist.mSongNumber);
174
175             final Resources resources = getContext().getResources();
176             // combine the ablum number and song number
177             String combinedString = resources.getString(R.string.combine_two_strings,
178                     albumNumber, songNumber);
179
180             mData[i].mLineTwo = combinedString;
181         }
182     }
183
184     /**
185      * Starts playing an artist if the user touches the artist image in the
186      * list.
187      * 
188      * @param artist The {@link ImageView} holding the aritst image
189      * @param position The position of the artist to play.
190      */
191     private void playArtist(final ImageView artist, final int position) {
192         artist.setOnClickListener(new OnClickListener() {
193
194             @Override
195             public void onClick(final View v) {
196                 final long id = getItem(position).mArtistId;
197                 final long[] list = MusicUtils.getSongListForArtist(getContext(), id);
198                 MusicUtils.playAll(getContext(), list, 0, false);
199             }
200         });
201     }
202
203     /**
204      * Method that unloads and clears the items in the adapter
205      */
206     public void unload() {
207         clear();
208         mData = null;
209     }
210
211     /**
212      * @param pause True to temporarily pause the disk cache, false otherwise.
213      */
214     public void setPauseDiskCache(final boolean pause) {
215         if (mImageFetcher != null) {
216             mImageFetcher.setPauseDiskCache(pause);
217         }
218     }
219
220     /**
221      * @param artist The key used to find the cached artist to remove
222      */
223     public void removeFromCache(final Artist artist) {
224         if (mImageFetcher != null) {
225             mImageFetcher.removeFromCache(artist.mArtistName);
226         }
227     }
228
229     /**
230      * Flushes the disk cache.
231      */
232     public void flush() {
233         mImageFetcher.flush();
234     }
235
236     /**
237      * Gets the item position for a given id
238      * @param id identifies the object
239      * @return the position if found, -1 otherwise
240      */
241     @Override
242     public int getItemPosition(long id) {
243         for (int i = 0; i < getCount(); i++) {
244             if (getItem(i).mArtistId == id) {
245                 return i;
246             }
247         }
248
249         return  -1;
250     }
251
252     /**
253      * @param extra True to load line three and the background image, false
254      *            otherwise.
255      */
256     public void setLoadExtraData(final boolean extra) {
257         mLoadExtraData = extra;
258     }
259 }