OSDN Git Service

Repackaged com.andrew.apollo to com.cyngn.eleven
[android-x86/packages-apps-Eleven.git] / src / com / cyngn / eleven / adapters / SongAdapter.java
1 /*
2  * Copyright (C) 2012 Andrew Neal Licensed under the Apache License, Version 2.0
3  * (the "License"); you may not use this file except in compliance with the
4  * License. You may obtain a copy of the License at
5  * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
6  * or agreed to in writing, software distributed under the License is
7  * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
8  * KIND, either express or implied. See the License for the specific language
9  * governing permissions and limitations under the License.
10  */
11
12 package com.cyngn.eleven.adapters;
13
14 import android.content.Context;
15 import android.view.LayoutInflater;
16 import android.view.View;
17 import android.view.ViewGroup;
18 import android.widget.ArrayAdapter;
19
20 import com.cyngn.eleven.model.Song;
21 import com.cyngn.eleven.ui.MusicHolder;
22 import com.cyngn.eleven.ui.MusicHolder.DataHolder;
23 import com.cyngn.eleven.ui.fragments.QueueFragment;
24 import com.cyngn.eleven.ui.fragments.SongFragment;
25 import com.cyngn.eleven.utils.MusicUtils;
26
27 /**
28  * This {@link ArrayAdapter} is used to display all of the songs on a user's
29  * device for {@link SongFragment}. It is also used to show the queue in
30  * {@link QueueFragment}.
31  * 
32  * @author Andrew Neal (andrewdneal@gmail.com)
33  */
34 public class SongAdapter extends ArrayAdapter<Song> {
35
36     /**
37      * Number of views (TextView)
38      */
39     private static final int VIEW_TYPE_COUNT = 1;
40
41     /**
42      * The resource Id of the layout to inflate
43      */
44     private final int mLayoutId;
45
46     /**
47      * Used to cache the song info
48      */
49     private DataHolder[] mData;
50
51     /**
52      * Constructor of <code>SongAdapter</code>
53      * 
54      * @param context The {@link Context} to use.
55      * @param layoutId The resource Id of the view to inflate.
56      */
57     public SongAdapter(final Context context, final int layoutId) {
58         super(context, 0);
59         // Get the layout Id
60         mLayoutId = layoutId;
61     }
62
63     /**
64      * {@inheritDoc}
65      */
66     @Override
67     public View getView(final int position, View convertView, final ViewGroup parent) {
68         // Recycle ViewHolder's items
69         MusicHolder holder;
70         if (convertView == null) {
71             convertView = LayoutInflater.from(getContext()).inflate(mLayoutId, parent, false);
72             holder = new MusicHolder(convertView);
73             // Hide the third line of text
74             holder.mLineThree.get().setVisibility(View.GONE);
75             convertView.setTag(holder);
76         } else {
77             holder = (MusicHolder)convertView.getTag();
78         }
79
80         // Retrieve the data holder
81         final DataHolder dataHolder = mData[position];
82
83         // Set each song name (line one)
84         holder.mLineOne.get().setText(dataHolder.mLineOne);
85         // Set the song duration (line one, right)
86         holder.mLineOneRight.get().setText(dataHolder.mLineOneRight);
87         // Set the album name (line two)
88         holder.mLineTwo.get().setText(dataHolder.mLineTwo);
89         return convertView;
90     }
91
92     /**
93      * {@inheritDoc}
94      */
95     @Override
96     public boolean hasStableIds() {
97         return true;
98     }
99
100     /**
101      * {@inheritDoc}
102      */
103     @Override
104     public int getViewTypeCount() {
105         return VIEW_TYPE_COUNT;
106     }
107
108     /**
109      * Method used to cache the data used to populate the list or grid. The idea
110      * is to cache everything before {@code #getView(int, View, ViewGroup)} is
111      * called.
112      */
113     public void buildCache() {
114         mData = new DataHolder[getCount()];
115         for (int i = 0; i < getCount(); i++) {
116             // Build the song
117             final Song song = getItem(i);
118
119             // Build the data holder
120             mData[i] = new DataHolder();
121             // Song Id
122             mData[i].mItemId = song.mSongId;
123             // Song names (line one)
124             mData[i].mLineOne = song.mSongName;
125             // Song duration (line one, right)
126             mData[i].mLineOneRight = MusicUtils.makeTimeString(getContext(), song.mDuration);
127             // Album names (line two)
128             mData[i].mLineTwo = song.mAlbumName;
129         }
130     }
131
132     /**
133      * Method that unloads and clears the items in the adapter
134      */
135     public void unload() {
136         clear();
137         mData = null;
138     }
139
140 }