OSDN Git Service

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