OSDN Git Service

Eleven: Add context menus to all top level fragments and playlists
[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.sectionadapter.SectionAdapter.BasicAdapter;
30 import com.cyngn.eleven.ui.MusicHolder;
31 import com.cyngn.eleven.ui.MusicHolder.DataHolder;
32 import com.cyngn.eleven.utils.ApolloUtils;
33 import com.cyngn.eleven.utils.MusicUtils;
34 import com.cyngn.eleven.widgets.IPopupMenuCallback;
35
36 /**
37  * This {@link ArrayAdapter} is used to display all of the artists on a user's
38  * device for {@link ArtistFragment}.
39  * 
40  * @author Andrew Neal (andrewdneal@gmail.com)
41  */
42 /**
43  * @author Andrew Neal (andrewdneal@gmail.com)
44  */
45 public class ArtistAdapter extends ArrayAdapter<Artist> implements BasicAdapter, IPopupMenuCallback {
46
47     /**
48      * Number of views (ImageView and TextView)
49      */
50     private static final int VIEW_TYPE_COUNT = 2;
51
52     /**
53      * The resource Id of the layout to inflate
54      */
55     private final int mLayoutId;
56
57     /**
58      * Image cache and image fetcher
59      */
60     private final ImageFetcher mImageFetcher;
61
62     /**
63      * Semi-transparent overlay
64      */
65     private final int mOverlay;
66
67     /**
68      * Used to cache the artist info
69      */
70     private DataHolder[] mData;
71
72     /**
73      * Used to listen to the pop up menu callbacks
74      */
75     private IListener mListener;
76
77     /**
78      * Constructor of <code>ArtistAdapter</code>
79      * 
80      * @param context The {@link Context} to use.
81      * @param layoutId The resource Id of the view to inflate.
82      */
83     public ArtistAdapter(final Activity context, final int layoutId) {
84         super(context, 0);
85         // Get the layout Id
86         mLayoutId = layoutId;
87         // Initialize the cache & image fetcher
88         mImageFetcher = ApolloUtils.getImageFetcher(context);
89         // Cache the transparent overlay
90         mOverlay = context.getResources().getColor(R.color.list_item_background);
91     }
92
93     /**
94      * {@inheritDoc}
95      */
96     @Override
97     public View getView(final int position, View convertView, final ViewGroup parent) {
98         // Recycle ViewHolder's items
99         MusicHolder holder;
100         if (convertView == null) {
101             convertView = LayoutInflater.from(getContext()).inflate(mLayoutId, parent, false);
102             holder = new MusicHolder(convertView);
103             convertView.setTag(holder);
104
105             // set the pop up menu listener
106             holder.mPopupMenuButton.get().setPopupMenuClickedListener(mListener);
107         } else {
108             holder = (MusicHolder)convertView.getTag();
109         }
110
111         // Retrieve the data holder
112         final DataHolder dataHolder = mData[position];
113
114         // Set each artist name (line one)
115         holder.mLineOne.get().setText(dataHolder.mLineOne);
116         // Set the number of albums (line two)
117         holder.mLineTwo.get().setText(dataHolder.mLineTwo);
118         // Asynchronously load the artist image into the adapter
119         mImageFetcher.loadArtistImage(dataHolder.mLineOne, holder.mImage.get());
120         // because of recycling, we need to set the position each time
121         holder.mPopupMenuButton.get().setPosition(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
160             String albumNumber = MusicUtils.makeLabel(getContext(),
161                     R.plurals.Nalbums, artist.mAlbumNumber);
162             String songNumber = MusicUtils.makeLabel(getContext(),
163                     R.plurals.Nsongs, artist.mSongNumber);
164
165             mData[i].mLineTwo = MusicUtils.makeCombinedString(getContext(), albumNumber, songNumber);
166         }
167     }
168
169     /**
170      * Starts playing an artist if the user touches the artist image in the
171      * list.
172      * 
173      * @param artist The {@link ImageView} holding the aritst image
174      * @param position The position of the artist to play.
175      */
176     private void playArtist(final ImageView artist, final int position) {
177         artist.setOnClickListener(new OnClickListener() {
178
179             @Override
180             public void onClick(final View v) {
181                 final long id = getItem(position).mArtistId;
182                 final long[] list = MusicUtils.getSongListForArtist(getContext(), id);
183                 MusicUtils.playAll(getContext(), list, 0, false);
184             }
185         });
186     }
187
188     /**
189      * Method that unloads and clears the items in the adapter
190      */
191     public void unload() {
192         clear();
193         mData = null;
194     }
195
196     /**
197      * @param pause True to temporarily pause the disk cache, false otherwise.
198      */
199     public void setPauseDiskCache(final boolean pause) {
200         if (mImageFetcher != null) {
201             mImageFetcher.setPauseDiskCache(pause);
202         }
203     }
204
205     /**
206      * @param artist The key used to find the cached artist to remove
207      */
208     public void removeFromCache(final Artist artist) {
209         if (mImageFetcher != null) {
210             mImageFetcher.removeFromCache(artist.mArtistName);
211         }
212     }
213
214     /**
215      * Flushes the disk cache.
216      */
217     public void flush() {
218         mImageFetcher.flush();
219     }
220
221     /**
222      * Gets the item position for a given id
223      * @param id identifies the object
224      * @return the position if found, -1 otherwise
225      */
226     @Override
227     public int getItemPosition(long id) {
228         for (int i = 0; i < getCount(); i++) {
229             if (getItem(i).mArtistId == id) {
230                 return i;
231             }
232         }
233
234         return  -1;
235     }
236
237     @Override
238     public void setPopupMenuClickedListener(IListener listener) {
239         mListener = listener;
240     }
241 }