2 * Copyright (C) 2014 Cyanogen, Inc.
4 package com.cyngn.eleven.adapters;
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;
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;
23 import java.util.Locale;
26 * Used to populate the list view with the search results.
28 public final class SummarySearchAdapter extends ArrayAdapter<SearchResult> implements SectionAdapter.BasicAdapter {
31 * no-image list item type and with image type
33 private static final int VIEW_TYPE_COUNT = 2;
36 * Image cache and image fetcher
38 private final ImageFetcher mImageFetcher;
41 * Highlights the query
43 private final PrefixHighlighter mHighlighter;
46 * The prefix that's highlighted
48 private char[] mPrefix;
51 * Constructor for <code>SearchAdapter</code>
53 * @param context The {@link Activity} to use.
55 public SummarySearchAdapter(final Activity context) {
57 // Initialize the cache & image fetcher
58 mImageFetcher = ApolloUtils.getImageFetcher(context);
59 // Create the prefix highlighter
60 mHighlighter = new PrefixHighlighter(context);
67 public View getView(final int position, View convertView, final ViewGroup parent) {
68 /* Recycle ViewHolder's items */
71 if (convertView == null) {
72 convertView = LayoutInflater.from(getContext()).inflate(
73 getViewResourceId(position), parent, false);
74 holder = new MusicHolder(convertView);
75 convertView.setTag(holder);
77 holder = (MusicHolder)convertView.getTag();
80 final SearchResult item = getItem(position);
84 // Asynchronously load the artist image into the adapter
85 mImageFetcher.loadArtistImage(item.mArtist, holder.mImage.get());
87 setText(holder.mLineOne.get(), item.mArtist);
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));
96 // Asynchronously load the album images into the adapter
97 mImageFetcher.loadAlbumImage(item.mArtist, item.mAlbum,
98 item.mId, holder.mImage.get());
100 setText(holder.mLineOne.get(), item.mAlbum);
101 setText(holder.mLineTwo.get(), item.mArtist);
104 // Asynchronously load the album images into the adapter
105 mImageFetcher.loadAlbumImage(item.mArtist, item.mAlbum,
106 item.mAlbumId, holder.mImage.get());
108 setText(holder.mLineOne.get(), item.mTitle);
109 setText(holder.mLineTwo.get(),
110 MusicUtils.makeCombinedString(getContext(), item.mArtist, item.mAlbum));
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);
124 * Sets the text onto the textview with highlighting if a prefix is defined
128 private void setText(final TextView textView, final String text) {
129 if (mPrefix == null) {
130 textView.setText(text);
132 mHighlighter.setText(textView, text, mPrefix);
140 public boolean hasStableIds() {
148 public int getViewTypeCount() {
149 return VIEW_TYPE_COUNT;
153 * This categorizes the view types we want for each item type
154 * @param position of the item
155 * @return categorization
158 public int getItemViewType(int position) {
159 switch (getItem(position).mType) {
171 * this returns the layout needed for the item
172 * @param position of the item
175 public int getViewResourceId(int position) {
176 switch (getItemViewType(position)) {
178 return R.layout.list_item_normal;
180 return R.layout.list_item_simple;
185 * @param pause True to temporarily pause the disk cache, false
188 public void setPauseDiskCache(final boolean pause) {
189 if (mImageFetcher != null) {
190 mImageFetcher.setPauseDiskCache(pause);
195 * @param prefix The query to filter.
197 public void setPrefix(final CharSequence prefix) {
198 if (!TextUtils.isEmpty(prefix)) {
199 mPrefix = prefix.toString().toUpperCase(Locale.getDefault()).toCharArray();
206 public void unload() {
211 public void buildCache() {
216 public void flush() {
217 mImageFetcher.flush();
221 * Gets the item position for a given id
222 * @param id identifies the object
223 * @return the position if found, -1 otherwise
226 public int getItemPosition(long id) {
227 for (int i = 0; i < getCount(); i++) {
228 if (getItem(i).mId == id) {