OSDN Git Service

This hooks up the show all part of the search as well as footers
[android-x86/packages-apps-Eleven.git] / src / com / cyngn / eleven / adapters / SummarySearchAdapter.java
1 /*
2  * Copyright (C) 2014 Cyanogen, Inc.
3  */
4 package com.cyngn.eleven.adapters;
5
6 import android.app.Activity;
7 import android.text.TextUtils;
8 import android.view.LayoutInflater;
9 import android.view.View;
10 import android.view.ViewGroup;
11 import android.widget.ArrayAdapter;
12 import android.widget.TextView;
13
14 import com.cyngn.eleven.R;
15 import com.cyngn.eleven.cache.ImageFetcher;
16 import com.cyngn.eleven.format.PrefixHighlighter;
17 import com.cyngn.eleven.model.SearchResult;
18 import com.cyngn.eleven.sectionadapter.SectionAdapter;
19 import com.cyngn.eleven.ui.MusicHolder;
20 import com.cyngn.eleven.utils.ApolloUtils;
21 import com.cyngn.eleven.utils.MusicUtils;
22
23 import java.util.Locale;
24
25 /**
26  * Used to populate the list view with the search results.
27  */
28 public final class SummarySearchAdapter extends ArrayAdapter<SearchResult> implements SectionAdapter.BasicAdapter {
29
30     /**
31      * no-image list item type and with image type
32      */
33     private static final int VIEW_TYPE_COUNT = 2;
34
35     /**
36      * Image cache and image fetcher
37      */
38     private final ImageFetcher mImageFetcher;
39
40     /**
41      * Highlights the query
42      */
43     private final PrefixHighlighter mHighlighter;
44
45     /**
46      * The prefix that's highlighted
47      */
48     private char[] mPrefix;
49
50     /**
51      * Constructor for <code>SearchAdapter</code>
52      *
53      * @param context The {@link Activity} to use.
54      */
55     public SummarySearchAdapter(final Activity context) {
56         super(context, 0);
57         // Initialize the cache & image fetcher
58         mImageFetcher = ApolloUtils.getImageFetcher(context);
59         // Create the prefix highlighter
60         mHighlighter = new PrefixHighlighter(context);
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
71         if (convertView == null) {
72             convertView = LayoutInflater.from(getContext()).inflate(
73                     getViewResourceId(position), parent, false);
74             holder = new MusicHolder(convertView);
75             convertView.setTag(holder);
76         } else {
77             holder = (MusicHolder)convertView.getTag();
78         }
79
80         final SearchResult item = getItem(position);
81
82         switch (item.mType) {
83             case Artist:
84                 // Asynchronously load the artist image into the adapter
85                 mImageFetcher.loadArtistImage(item.mArtist, holder.mImage.get());
86
87                 setText(holder.mLineOne.get(), item.mArtist);
88
89                 String songCount = MusicUtils.makeLabel(getContext(), R.plurals.Nsongs, item.mSongCount);
90                 String albumCount = MusicUtils.makeLabel(getContext(), R.plurals.Nalbums, item.mAlbumCount);
91                 // Album Name | Artist Name (line two)
92                 holder.mLineTwo.get().setText(MusicUtils.makeCombinedString(getContext(), songCount, albumCount));
93
94                 break;
95             case Album:
96                 // Asynchronously load the album images into the adapter
97                 mImageFetcher.loadAlbumImage(item.mArtist, item.mAlbum,
98                         item.mId, holder.mImage.get());
99
100                 setText(holder.mLineOne.get(), item.mAlbum);
101                 setText(holder.mLineTwo.get(), item.mArtist);
102                 break;
103             case Song:
104                 // Asynchronously load the album images into the adapter
105                 mImageFetcher.loadAlbumImage(item.mArtist, item.mAlbum,
106                         item.mAlbumId, holder.mImage.get());
107
108                 setText(holder.mLineOne.get(), item.mTitle);
109                 setText(holder.mLineTwo.get(),
110                         MusicUtils.makeCombinedString(getContext(), item.mArtist, item.mAlbum));
111                 break;
112             case Playlist:
113                 setText(holder.mLineOne.get(), item.mTitle);
114                 String songs = MusicUtils.makeLabel(getContext(), R.plurals.Nsongs, item.mSongCount);
115                 holder.mLineTwo.get().setText(songs);
116                 holder.mLineThree.get().setVisibility(View.GONE);
117                 break;
118         }
119
120         return convertView;
121     }
122
123     /**
124      * Sets the text onto the textview with highlighting if a prefix is defined
125      * @param textView
126      * @param text
127      */
128     private void setText(final TextView textView, final String text) {
129         if (mPrefix == null) {
130             textView.setText(text);
131         } else {
132             mHighlighter.setText(textView, text, mPrefix);
133         }
134     }
135
136     /**
137      * {@inheritDoc}
138      */
139     @Override
140     public boolean hasStableIds() {
141         return true;
142     }
143
144     /**
145      * {@inheritDoc}
146      */
147     @Override
148     public int getViewTypeCount() {
149         return VIEW_TYPE_COUNT;
150     }
151
152     /**
153      * This categorizes the view types we want for each item type
154      * @param position of the item
155      * @return categorization
156      */
157     @Override
158     public int getItemViewType(int position) {
159         switch (getItem(position).mType) {
160             case Artist:
161             case Album:
162             case Song:
163                 return 0;
164             default:
165             case Playlist:
166                 return 1;
167         }
168     }
169
170     /**
171      * this returns the layout needed for the item
172      * @param position of the item
173      * @return layout id
174      */
175     public int getViewResourceId(int position) {
176         switch (getItemViewType(position)) {
177             case 0:
178                 return R.layout.list_item_normal;
179             default:
180                 return R.layout.list_item_simple;
181         }
182     }
183
184     /**
185      * @param pause True to temporarily pause the disk cache, false
186      *            otherwise.
187      */
188     public void setPauseDiskCache(final boolean pause) {
189         if (mImageFetcher != null) {
190             mImageFetcher.setPauseDiskCache(pause);
191         }
192     }
193
194     /**
195      * @param prefix The query to filter.
196      */
197     public void setPrefix(final CharSequence prefix) {
198         if (!TextUtils.isEmpty(prefix)) {
199             mPrefix = prefix.toString().toUpperCase(Locale.getDefault()).toCharArray();
200         } else {
201             mPrefix = null;
202         }
203     }
204
205     @Override
206     public void unload() {
207         clear();
208     }
209
210     @Override
211     public void buildCache() {
212
213     }
214
215     @Override
216     public void flush() {
217         mImageFetcher.flush();
218     }
219
220     /**
221      * Gets the item position for a given id
222      * @param id identifies the object
223      * @return the position if found, -1 otherwise
224      */
225     @Override
226     public int getItemPosition(long id) {
227         for (int i = 0; i < getCount(); i++) {
228             if (getItem(i).mId == id) {
229                 return i;
230             }
231         }
232
233         return  -1;
234     }
235 }