OSDN Git Service

Update Eleven headers and namespace for open source
[android-x86/packages-apps-Eleven.git] / src / com / cyanogenmod / eleven / adapters / PlaylistAdapter.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.content.Context;
17 import android.view.LayoutInflater;
18 import android.view.View;
19 import android.view.ViewGroup;
20 import android.widget.ArrayAdapter;
21
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;
31
32 /**
33  * This {@link ArrayAdapter} is used to display all of the playlists on a user's
34  * device for {@link PlaylistFragment}.
35  * 
36  * @author Andrew Neal (andrewdneal@gmail.com)
37  */
38 public class PlaylistAdapter extends ArrayAdapter<Playlist> implements IPopupMenuCallback {
39
40     /**
41      * Smart playlists and normal playlists
42      */
43     private static final int VIEW_TYPE_COUNT = 2;
44
45     /**
46      * Used to identify the view type
47      */
48     private static final int SMART_PLAYLIST_VIEW_TYPE = 1;
49
50     /**
51      * Used to cache the playlist info
52      */
53     private DataHolder[] mData;
54
55     /**
56      * Used to listen to the pop up menu callbacks
57      */
58     protected IListener mListener;
59
60     /**
61      * Constructor of <code>PlaylistAdapter</code>
62      * 
63      * @param context The {@link Context} to use.
64      */
65     public PlaylistAdapter(final Context context) {
66         super(context, 0);
67     }
68
69     /**
70      * {@inheritDoc}
71      */
72     @Override
73     public View getView(final int position, View convertView, final ViewGroup parent) {
74         // Recycle ViewHolder's items
75         MusicHolder holder;
76         if (convertView == null) {
77             int layoutId = R.layout.list_item_normal;
78
79             if (getItemViewType(position) == SMART_PLAYLIST_VIEW_TYPE) {
80                 layoutId = R.layout.list_item_smart_playlist;
81             }
82
83             convertView = LayoutInflater.from(getContext()).inflate(layoutId, parent, false);
84             holder = new MusicHolder(convertView);
85             convertView.setTag(holder);
86
87             // set the pop up menu listener
88             holder.mPopupMenuButton.get().setPopupMenuClickedListener(mListener);
89         } else {
90             holder = (MusicHolder)convertView.getTag();
91         }
92
93         // Retrieve the data holder
94         final DataHolder dataHolder = mData[position];
95
96         // because of recycling, we need to set the position each time
97         holder.mPopupMenuButton.get().setPosition(position);
98
99         // Set each playlist name (line one)
100         holder.mLineOne.get().setText(dataHolder.mLineOne);
101
102         if (dataHolder.mLineTwo == null) {
103             holder.mLineTwo.get().setVisibility(View.GONE);
104         } else {
105             holder.mLineTwo.get().setVisibility(View.VISIBLE);
106             holder.mLineTwo.get().setText(dataHolder.mLineTwo);
107         }
108
109         SmartPlaylistType type = SmartPlaylistType.getTypeById(dataHolder.mItemId);
110         if (type != null) {
111             // Set the image resource based on the icon
112             switch (type) {
113                 case LastAdded:
114                     holder.mImage.get().setImageResource(R.drawable.recently_added);
115                     break;
116                 case RecentlyPlayed:
117                     holder.mImage.get().setImageResource(R.drawable.recent_icon);
118                     break;
119                 case TopTracks:
120                 default:
121                     holder.mImage.get().setImageResource(R.drawable.top_tracks_icon);
122                     break;
123             }
124         } else {
125             // load the image
126             ImageFetcher.getInstance(getContext()).loadPlaylistCoverArtImage(
127                     dataHolder.mItemId, holder.mImage.get());
128         }
129
130
131
132         return convertView;
133     }
134
135     /**
136      * {@inheritDoc}
137      */
138     @Override
139     public boolean hasStableIds() {
140         return true;
141     }
142
143     /**
144      * {@inheritDoc}
145      */
146     @Override
147     public int getViewTypeCount() {
148         return VIEW_TYPE_COUNT;
149     }
150
151     /**
152      * {@inheritDoc}
153      */
154     @Override
155     public int getItemViewType(int position) {
156         if (getItem(position).isSmartPlaylist()) {
157             return SMART_PLAYLIST_VIEW_TYPE;
158         }
159
160         return 0;
161     }
162
163     /**
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
166      * called.
167      */
168     public void buildCache() {
169         mData = new DataHolder[getCount()];
170         for (int i = 0; i < getCount(); i++) {
171             // Build the artist
172             final Playlist playlist = getItem(i);
173
174             // Build the data holder
175             mData[i] = new DataHolder();
176             // Playlist Id
177             mData[i].mItemId = playlist.mPlaylistId;
178             // Playlist names (line one)
179             mData[i].mLineOne = playlist.mPlaylistName;
180             // # of songs
181             if (playlist.mSongCount >= 0) {
182                 mData[i].mLineTwo = MusicUtils.makeLabel(getContext(),
183                         R.plurals.Nsongs, playlist.mSongCount);
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     @Override
197     public void setPopupMenuClickedListener(IListener listener) {
198         mListener = listener;
199     }
200 }