OSDN Git Service

Eleven: Cleanup all the whitespace
[android-x86/packages-apps-Eleven.git] / src / com / cyanogenmod / eleven / adapters / ArtistAdapter.java
1 /*
2  * Copyright (C) 2012 Andrew Neal
3  * Copyright (C) 2014 The CyanogenMod Project
4  * Licensed under the Apache License, Version 2.0
5  * (the "License"); you may not use this file except in compliance with the
6  * License. You may obtain a copy of the License at
7  * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
8  * or agreed to in writing, software distributed under the License is
9  * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
10  * KIND, either express or implied. See the License for the specific language
11  * governing permissions and limitations under the License.
12  */
13
14 package com.cyanogenmod.eleven.adapters;
15
16 import android.app.Activity;
17 import android.content.Context;
18 import android.view.LayoutInflater;
19 import android.view.View;
20 import android.view.ViewGroup;
21 import android.widget.ArrayAdapter;
22
23 import com.cyanogenmod.eleven.R;
24 import com.cyanogenmod.eleven.cache.ImageFetcher;
25 import com.cyanogenmod.eleven.model.Artist;
26 import com.cyanogenmod.eleven.sectionadapter.SectionAdapter.BasicAdapter;
27 import com.cyanogenmod.eleven.ui.MusicHolder;
28 import com.cyanogenmod.eleven.ui.MusicHolder.DataHolder;
29 import com.cyanogenmod.eleven.utils.ApolloUtils;
30 import com.cyanogenmod.eleven.utils.MusicUtils;
31 import com.cyanogenmod.eleven.widgets.IPopupMenuCallback;
32
33 /**
34  * This {@link ArrayAdapter} is used to display all of the artists on a user's
35  * device for {@link ArtistFragment}.
36  *
37  * @author Andrew Neal (andrewdneal@gmail.com)
38  */
39 /**
40  * @author Andrew Neal (andrewdneal@gmail.com)
41  */
42 public class ArtistAdapter extends ArrayAdapter<Artist> implements BasicAdapter, IPopupMenuCallback {
43
44     /**
45      * Number of views (ImageView and TextView)
46      */
47     private static final int VIEW_TYPE_COUNT = 2;
48
49     /**
50      * The resource Id of the layout to inflate
51      */
52     private final int mLayoutId;
53
54     /**
55      * Image cache and image fetcher
56      */
57     private final ImageFetcher mImageFetcher;
58
59     /**
60      * Semi-transparent overlay
61      */
62     private final int mOverlay;
63
64     /**
65      * Used to cache the artist info
66      */
67     private DataHolder[] mData;
68
69     /**
70      * Used to listen to the pop up menu callbacks
71      */
72     private IListener mListener;
73
74     /**
75      * Constructor of <code>ArtistAdapter</code>
76      *
77      * @param context The {@link Context} to use.
78      * @param layoutId The resource Id of the view to inflate.
79      */
80     public ArtistAdapter(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
102             // set the pop up menu listener
103             holder.mPopupMenuButton.get().setPopupMenuClickedListener(mListener);
104         } else {
105             holder = (MusicHolder)convertView.getTag();
106         }
107
108         // Retrieve the data holder
109         final DataHolder dataHolder = mData[position];
110
111         // Set each artist name (line one)
112         holder.mLineOne.get().setText(dataHolder.mLineOne);
113         // Set the number of albums (line two)
114         holder.mLineTwo.get().setText(dataHolder.mLineTwo);
115         // Asynchronously load the artist image into the adapter
116         mImageFetcher.loadArtistImage(dataHolder.mLineOne, holder.mImage.get());
117         // because of recycling, we need to set the position each time
118         holder.mPopupMenuButton.get().setPosition(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 artist
148             final Artist artist = getItem(i);
149
150             // Build the data holder
151             mData[i] = new DataHolder();
152             // Artist Id
153             mData[i].mItemId = artist.mArtistId;
154             // Artist names (line one)
155             mData[i].mLineOne = artist.mArtistName;
156
157             String albumNumber = MusicUtils.makeLabel(getContext(),
158                     R.plurals.Nalbums, artist.mAlbumNumber);
159             String songNumber = MusicUtils.makeLabel(getContext(),
160                     R.plurals.Nsongs, artist.mSongNumber);
161
162             mData[i].mLineTwo = MusicUtils.makeCombinedString(getContext(), albumNumber, songNumber);
163         }
164     }
165
166     /**
167      * Method that unloads and clears the items in the adapter
168      */
169     public void unload() {
170         clear();
171         mData = null;
172     }
173
174     /**
175      * @param pause True to temporarily pause the disk cache, false otherwise.
176      */
177     public void setPauseDiskCache(final boolean pause) {
178         if (mImageFetcher != null) {
179             mImageFetcher.setPauseDiskCache(pause);
180         }
181     }
182
183     /**
184      * @param artist The key used to find the cached artist to remove
185      */
186     public void removeFromCache(final Artist artist) {
187         if (mImageFetcher != null) {
188             mImageFetcher.removeFromCache(artist.mArtistName);
189         }
190     }
191
192     /**
193      * Flushes the disk cache.
194      */
195     public void flush() {
196         mImageFetcher.flush();
197     }
198
199     /**
200      * Gets the item position for a given id
201      * @param id identifies the object
202      * @return the position if found, -1 otherwise
203      */
204     @Override
205     public int getItemPosition(long id) {
206         for (int i = 0; i < getCount(); i++) {
207             if (getItem(i).mArtistId == id) {
208                 return i;
209             }
210         }
211
212         return  -1;
213     }
214
215     @Override
216     public void setPopupMenuClickedListener(IListener listener) {
217         mListener = listener;
218     }
219 }