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.content.Context;
17 import android.view.LayoutInflater;
18 import android.view.View;
19 import android.view.ViewGroup;
20 import android.widget.ArrayAdapter;
22 import com.cyanogenmod.eleven.Config.SmartPlaylistType;
23 import com.cyanogenmod.eleven.R;
24 import com.cyanogenmod.eleven.cache.ImageFetcher;
25 import com.cyanogenmod.eleven.model.Playlist;
26 import com.cyanogenmod.eleven.ui.MusicHolder;
27 import com.cyanogenmod.eleven.ui.MusicHolder.DataHolder;
28 import com.cyanogenmod.eleven.ui.fragments.PlaylistFragment;
29 import com.cyanogenmod.eleven.utils.MusicUtils;
30 import com.cyanogenmod.eleven.widgets.IPopupMenuCallback;
33 * This {@link ArrayAdapter} is used to display all of the playlists on a user's
34 * device for {@link PlaylistFragment}.
36 * @author Andrew Neal (andrewdneal@gmail.com)
38 public class PlaylistAdapter extends ArrayAdapter<Playlist> implements IPopupMenuCallback {
41 * Smart playlists and normal playlists
43 private static final int VIEW_TYPE_COUNT = 2;
46 * Used to identify the view type
48 private static final int SMART_PLAYLIST_VIEW_TYPE = 1;
51 * Used to cache the playlist info
53 private DataHolder[] mData;
56 * Used to listen to the pop up menu callbacks
58 protected IListener mListener;
61 * Constructor of <code>PlaylistAdapter</code>
63 * @param context The {@link Context} to use.
65 public PlaylistAdapter(final Context context) {
73 public View getView(final int position, View convertView, final ViewGroup parent) {
74 // Recycle ViewHolder's items
76 if (convertView == null) {
77 int layoutId = R.layout.list_item_normal;
79 if (getItemViewType(position) == SMART_PLAYLIST_VIEW_TYPE) {
80 layoutId = R.layout.list_item_smart_playlist;
83 convertView = LayoutInflater.from(getContext()).inflate(layoutId, parent, false);
84 holder = new MusicHolder(convertView);
85 convertView.setTag(holder);
87 // set the pop up menu listener
88 holder.mPopupMenuButton.get().setPopupMenuClickedListener(mListener);
90 holder = (MusicHolder)convertView.getTag();
93 // Retrieve the data holder
94 final DataHolder dataHolder = mData[position];
96 // because of recycling, we need to set the position each time
97 holder.mPopupMenuButton.get().setPosition(position);
99 // Set each playlist name (line one)
100 holder.mLineOne.get().setText(dataHolder.mLineOne);
102 if (dataHolder.mLineTwo == null) {
103 holder.mLineTwo.get().setVisibility(View.GONE);
105 holder.mLineTwo.get().setVisibility(View.VISIBLE);
106 holder.mLineTwo.get().setText(dataHolder.mLineTwo);
109 SmartPlaylistType type = SmartPlaylistType.getTypeById(dataHolder.mItemId);
111 // Set the image resource based on the icon
114 holder.mImage.get().setImageResource(R.drawable.recently_added);
117 holder.mImage.get().setImageResource(R.drawable.recent_icon);
121 holder.mImage.get().setImageResource(R.drawable.top_tracks_icon);
126 ImageFetcher.getInstance(getContext()).loadPlaylistCoverArtImage(
127 dataHolder.mItemId, holder.mImage.get());
139 public boolean hasStableIds() {
147 public int getViewTypeCount() {
148 return VIEW_TYPE_COUNT;
155 public int getItemViewType(int position) {
156 if (getItem(position).isSmartPlaylist()) {
157 return SMART_PLAYLIST_VIEW_TYPE;
164 * Method used to cache the data used to populate the list or grid. The idea
165 * is to cache everything before {@code #getView(int, View, ViewGroup)} is
168 public void buildCache() {
169 mData = new DataHolder[getCount()];
170 for (int i = 0; i < getCount(); i++) {
172 final Playlist playlist = getItem(i);
174 // Build the data holder
175 mData[i] = new DataHolder();
177 mData[i].mItemId = playlist.mPlaylistId;
178 // Playlist names (line one)
179 mData[i].mLineOne = playlist.mPlaylistName;
181 if (playlist.mSongCount >= 0) {
182 mData[i].mLineTwo = MusicUtils.makeLabel(getContext(),
183 R.plurals.Nsongs, playlist.mSongCount);
189 * Method that unloads and clears the items in the adapter
191 public void unload() {
197 public void setPopupMenuClickedListener(IListener listener) {
198 mListener = listener;