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.
14 package com.cyanogenmod.eleven.adapters;
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;
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;
34 * This {@link ArrayAdapter} is used to display all of the artists on a user's
35 * device for {@link ArtistFragment}.
37 * @author Andrew Neal (andrewdneal@gmail.com)
40 * @author Andrew Neal (andrewdneal@gmail.com)
42 public class ArtistAdapter extends ArrayAdapter<Artist> implements BasicAdapter, IPopupMenuCallback {
45 * Number of views (ImageView and TextView)
47 private static final int VIEW_TYPE_COUNT = 2;
50 * The resource Id of the layout to inflate
52 private final int mLayoutId;
55 * Image cache and image fetcher
57 private final ImageFetcher mImageFetcher;
60 * Semi-transparent overlay
62 private final int mOverlay;
65 * Used to cache the artist info
67 private DataHolder[] mData;
70 * Used to listen to the pop up menu callbacks
72 private IListener mListener;
75 * Constructor of <code>ArtistAdapter</code>
77 * @param context The {@link Context} to use.
78 * @param layoutId The resource Id of the view to inflate.
80 public ArtistAdapter(final Activity context, final int 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);
94 public View getView(final int position, View convertView, final ViewGroup parent) {
95 // Recycle ViewHolder's items
97 if (convertView == null) {
98 convertView = LayoutInflater.from(getContext()).inflate(mLayoutId, parent, false);
99 holder = new MusicHolder(convertView);
100 convertView.setTag(holder);
102 // set the pop up menu listener
103 holder.mPopupMenuButton.get().setPopupMenuClickedListener(mListener);
105 holder = (MusicHolder)convertView.getTag();
108 // Retrieve the data holder
109 final DataHolder dataHolder = mData[position];
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);
127 public boolean hasStableIds() {
135 public int getViewTypeCount() {
136 return VIEW_TYPE_COUNT;
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
144 public void buildCache() {
145 mData = new DataHolder[getCount()];
146 for (int i = 0; i < getCount(); i++) {
148 final Artist artist = getItem(i);
150 // Build the data holder
151 mData[i] = new DataHolder();
153 mData[i].mItemId = artist.mArtistId;
154 // Artist names (line one)
155 mData[i].mLineOne = artist.mArtistName;
157 String albumNumber = MusicUtils.makeLabel(getContext(),
158 R.plurals.Nalbums, artist.mAlbumNumber);
159 String songNumber = MusicUtils.makeLabel(getContext(),
160 R.plurals.Nsongs, artist.mSongNumber);
162 mData[i].mLineTwo = MusicUtils.makeCombinedString(getContext(), albumNumber, songNumber);
167 * Method that unloads and clears the items in the adapter
169 public void unload() {
175 * @param pause True to temporarily pause the disk cache, false otherwise.
177 public void setPauseDiskCache(final boolean pause) {
178 if (mImageFetcher != null) {
179 mImageFetcher.setPauseDiskCache(pause);
184 * @param artist The key used to find the cached artist to remove
186 public void removeFromCache(final Artist artist) {
187 if (mImageFetcher != null) {
188 mImageFetcher.removeFromCache(artist.mArtistName);
193 * Flushes the disk cache.
195 public void flush() {
196 mImageFetcher.flush();
200 * Gets the item position for a given id
201 * @param id identifies the object
202 * @return the position if found, -1 otherwise
205 public int getItemPosition(long id) {
206 for (int i = 0; i < getCount(); i++) {
207 if (getItem(i).mArtistId == id) {
216 public void setPopupMenuClickedListener(IListener listener) {
217 mListener = listener;