2 * Copyright (C) 2014 The CyanogenMod Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.cyanogenmod.eleven.adapters;
18 import android.app.Activity;
19 import android.text.TextUtils;
20 import android.view.LayoutInflater;
21 import android.view.View;
22 import android.view.ViewGroup;
23 import android.widget.ArrayAdapter;
24 import android.widget.TextView;
26 import com.cyanogenmod.eleven.R;
27 import com.cyanogenmod.eleven.cache.ImageFetcher;
28 import com.cyanogenmod.eleven.format.PrefixHighlighter;
29 import com.cyanogenmod.eleven.model.SearchResult;
30 import com.cyanogenmod.eleven.sectionadapter.SectionAdapter;
31 import com.cyanogenmod.eleven.ui.MusicHolder;
32 import com.cyanogenmod.eleven.utils.ApolloUtils;
33 import com.cyanogenmod.eleven.utils.MusicUtils;
34 import com.cyanogenmod.eleven.widgets.IPopupMenuCallback;
36 import java.util.Locale;
39 * Used to populate the list view with the search results.
41 public final class SummarySearchAdapter extends ArrayAdapter<SearchResult>
42 implements SectionAdapter.BasicAdapter, IPopupMenuCallback {
45 * Image cache and image fetcher
47 private final ImageFetcher mImageFetcher;
50 * Highlights the query
52 private final PrefixHighlighter mHighlighter;
55 * The prefix that's highlighted
57 private char[] mPrefix;
60 * Used to listen to the pop up menu callbacks
62 private IListener mListener;
65 * Constructor for <code>SearchAdapter</code>
67 * @param context The {@link Activity} to use.
69 public SummarySearchAdapter(final Activity context) {
71 // Initialize the cache & image fetcher
72 mImageFetcher = ApolloUtils.getImageFetcher(context);
73 // Create the prefix highlighter
74 mHighlighter = new PrefixHighlighter(context);
81 public View getView(final int position, View convertView, final ViewGroup parent) {
82 /* Recycle ViewHolder's items */
85 if (convertView == null) {
86 convertView = LayoutInflater.from(getContext()).inflate(
87 R.layout.list_item_normal, parent, false);
88 holder = new MusicHolder(convertView);
89 convertView.setTag(holder);
90 // set the pop up menu listener
91 holder.mPopupMenuButton.get().setPopupMenuClickedListener(mListener);
93 holder = (MusicHolder)convertView.getTag();
96 // Sets the position each time because of recycling
97 holder.mPopupMenuButton.get().setPosition(position);
99 final SearchResult item = getItem(position);
101 switch (item.mType) {
103 // Asynchronously load the artist image into the adapter
104 mImageFetcher.loadArtistImage(item.mArtist, holder.mImage.get());
106 setText(holder.mLineOne.get(), item.mArtist);
108 String songCount = MusicUtils.makeLabel(getContext(), R.plurals.Nsongs, item.mSongCount);
109 String albumCount = MusicUtils.makeLabel(getContext(), R.plurals.Nalbums, item.mAlbumCount);
110 // Album Name | Artist Name (line two)
111 holder.mLineTwo.get().setText(MusicUtils.makeCombinedString(getContext(), songCount, albumCount));
114 // Asynchronously load the album images into the adapter
115 mImageFetcher.loadAlbumImage(item.mArtist, item.mAlbum,
116 item.mId, holder.mImage.get());
118 setText(holder.mLineOne.get(), item.mAlbum);
119 setText(holder.mLineTwo.get(), item.mArtist);
122 // Asynchronously load the album images into the adapter
123 mImageFetcher.loadAlbumImage(item.mArtist, item.mAlbum,
124 item.mAlbumId, holder.mImage.get());
126 setText(holder.mLineOne.get(), item.mTitle);
127 setText(holder.mLineTwo.get(),
128 MusicUtils.makeCombinedString(getContext(), item.mArtist, item.mAlbum));
131 // Asynchronously load the playlist images into the adapter
132 ImageFetcher.getInstance(getContext()).loadPlaylistCoverArtImage(
133 item.mId, holder.mImage.get());
135 setText(holder.mLineOne.get(), item.mTitle);
136 String songs = MusicUtils.makeLabel(getContext(), R.plurals.Nsongs, item.mSongCount);
137 holder.mLineTwo.get().setText(songs);
145 * Sets the text onto the textview with highlighting if a prefix is defined
149 private void setText(final TextView textView, final String text) {
150 if (mPrefix == null) {
151 textView.setText(text);
153 mHighlighter.setText(textView, text, mPrefix);
161 public boolean hasStableIds() {
166 * @param pause True to temporarily pause the disk cache, false
169 public void setPauseDiskCache(final boolean pause) {
170 if (mImageFetcher != null) {
171 mImageFetcher.setPauseDiskCache(pause);
176 * @param prefix The query to filter.
178 public void setPrefix(final CharSequence prefix) {
179 if (!TextUtils.isEmpty(prefix)) {
180 mPrefix = prefix.toString().toUpperCase(Locale.getDefault()).toCharArray();
187 public void unload() {
192 public void buildCache() {
197 public void flush() {
198 mImageFetcher.flush();
202 * Gets the item position for a given id
203 * @param id identifies the object
204 * @return the position if found, -1 otherwise
207 public int getItemPosition(long id) {
208 for (int i = 0; i < getCount(); i++) {
209 if (getItem(i).mId == id) {
218 public void setPopupMenuClickedListener(IListener listener) {
219 mListener = listener;