OSDN Git Service

2dcf932027eb1d2b7fc887997ff329459fd2767f
[android-x86/packages-apps-Eleven.git] / src / com / cyanogenmod / eleven / adapters / ProfileSongAdapter.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.view.LayoutInflater;
18 import android.view.View;
19 import android.view.ViewGroup;
20 import android.widget.ArrayAdapter;
21
22 import com.cyanogenmod.eleven.Config;
23 import com.cyanogenmod.eleven.model.Song;
24
25 import java.util.Collection;
26
27 /**
28  * This {@link ArrayAdapter} is used to display the songs for a particular playlist
29  * {@link com.cyanogenmod.eleven.ui.fragments.PlaylistDetailFragment}
30  *
31  * @author Andrew Neal (andrewdneal@gmail.com)
32  */
33 public class ProfileSongAdapter extends SongAdapter {
34     /**
35      * Instead of having random +1 and -1 sprinkled around, this variable will show what is really
36      * related to the header
37      */
38     public static final int NUM_HEADERS = 1;
39
40     /**
41      * Fake header layout Id
42      */
43     private final int mHeaderId;
44
45     /**
46      * Constructor of <code>ProfileSongAdapter</code>
47      *
48      * @param activity The {@link Activity} to use
49      * @param layoutId The resource Id of the view to inflate.
50      */
51     public ProfileSongAdapter(final long playlistId, final Activity activity, final int layoutId,
52                               final int headerId) {
53         super(activity, layoutId, playlistId, Config.IdType.Playlist);
54         // Cache the header
55         mHeaderId = headerId;
56     }
57
58     /**
59      * {@inheritDoc}
60      */
61     @Override
62     public View getView(final int position, View convertView, final ViewGroup parent) {
63
64         // Return a faux header at position 0
65         if (position == 0) {
66             if (convertView == null) {
67                 convertView = LayoutInflater.from(getContext()).inflate(mHeaderId, parent, false);
68             }
69
70             return convertView;
71         }
72
73         return super.getView(position, convertView, parent);
74     }
75
76     /**
77      * {@inheritDoc}
78      */
79     protected boolean showNowPlayingIndicator(final Song song, final int position) {
80         return super.showNowPlayingIndicator(song, position)
81                 && mCurrentlyPlayingTrack.mSourcePosition == position - NUM_HEADERS;
82     }
83
84     @Override
85     public boolean isEnabled(int position) {
86         if (position == 0) {
87             return false;
88         }
89
90         return super.isEnabled(position);
91     }
92
93     /**
94      * {@inheritDoc}
95      */
96     @Override
97     public int getViewTypeCount() {
98         return super.getViewTypeCount() + NUM_HEADERS;
99     }
100
101     /**
102      * {@inheritDoc}
103      */
104     @Override
105     public int getItemViewType(final int position) {
106         if (position == 0) {
107             // since our view type count adds 1 to the super class, we can return viewtypecount - 1
108             return getViewTypeCount() - 1;
109         }
110         return super.getItemViewType(position);
111     }
112
113     @Override
114     public void addAll(Collection<? extends Song> collection) {
115         // insert a header if one is needed
116         insertHeader();
117         super.addAll(collection);
118     }
119
120     @Override
121     public void addAll(Song... items) {
122         // insert a header if one is needed
123         insertHeader();
124         super.addAll(items);
125     }
126
127     /**
128      * Make sure we insert our header when we add items
129      */
130     private void insertHeader() {
131         if (getCount() == 0) {
132             // add a dummy entry to the underlying adapter.  This is needed otherwise the
133             // underlying adapter could crash because getCount() doesn't match up
134             add(new Song(-1, null, null, null, -1, -1, -1));
135         }
136     }
137 }