OSDN Git Service

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