OSDN Git Service

Fix dependencies of packages that target earlier releases
[android-x86/packages-apps-Eleven.git] / src / org / lineageos / eleven / adapters / SummarySearchAdapter.java
1 /*
2 * Copyright (C) 2014 The CyanogenMod Project
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16 package com.cyanogenmod.eleven.adapters;
17
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;
25
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;
35
36 import java.util.Locale;
37
38 /**
39  * Used to populate the list view with the search results.
40  */
41 public final class SummarySearchAdapter extends ArrayAdapter<SearchResult>
42         implements SectionAdapter.BasicAdapter, IPopupMenuCallback {
43
44     /**
45      * Image cache and image fetcher
46      */
47     private final ImageFetcher mImageFetcher;
48
49     /**
50      * Highlights the query
51      */
52     private final PrefixHighlighter mHighlighter;
53
54     /**
55      * The prefix that's highlighted
56      */
57     private char[] mPrefix;
58
59     /**
60      * Used to listen to the pop up menu callbacks
61      */
62     private IListener mListener;
63
64     /**
65      * Constructor for <code>SearchAdapter</code>
66      *
67      * @param context The {@link Activity} to use.
68      */
69     public SummarySearchAdapter(final Activity context) {
70         super(context, 0);
71         // Initialize the cache & image fetcher
72         mImageFetcher = ApolloUtils.getImageFetcher(context);
73         // Create the prefix highlighter
74         mHighlighter = new PrefixHighlighter(context);
75     }
76
77     /**
78      * {@inheritDoc}
79      */
80     @Override
81     public View getView(final int position, View convertView, final ViewGroup parent) {
82             /* Recycle ViewHolder's items */
83         MusicHolder holder;
84
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);
92         } else {
93             holder = (MusicHolder)convertView.getTag();
94         }
95
96         // Sets the position each time because of recycling
97         holder.mPopupMenuButton.get().setPosition(position);
98
99         final SearchResult item = getItem(position);
100
101         switch (item.mType) {
102             case Artist:
103                 // Asynchronously load the artist image into the adapter
104                 mImageFetcher.loadArtistImage(item.mArtist, holder.mImage.get());
105
106                 setText(holder.mLineOne.get(), item.mArtist);
107
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));
112                 break;
113             case Album:
114                 // Asynchronously load the album images into the adapter
115                 mImageFetcher.loadAlbumImage(item.mArtist, item.mAlbum,
116                         item.mId, holder.mImage.get());
117
118                 setText(holder.mLineOne.get(), item.mAlbum);
119                 setText(holder.mLineTwo.get(), item.mArtist);
120                 break;
121             case Song:
122                 // Asynchronously load the album images into the adapter
123                 mImageFetcher.loadAlbumImage(item.mArtist, item.mAlbum,
124                         item.mAlbumId, holder.mImage.get());
125
126                 setText(holder.mLineOne.get(), item.mTitle);
127                 setText(holder.mLineTwo.get(),
128                         MusicUtils.makeCombinedString(getContext(), item.mArtist, item.mAlbum));
129                 break;
130             case Playlist:
131                 // Asynchronously load the playlist images into the adapter
132                 ImageFetcher.getInstance(getContext()).loadPlaylistCoverArtImage(
133                         item.mId, holder.mImage.get());
134
135                 setText(holder.mLineOne.get(), item.mTitle);
136                 String songs = MusicUtils.makeLabel(getContext(), R.plurals.Nsongs, item.mSongCount);
137                 holder.mLineTwo.get().setText(songs);
138                 break;
139         }
140
141         return convertView;
142     }
143
144     /**
145      * Sets the text onto the textview with highlighting if a prefix is defined
146      * @param textView
147      * @param text
148      */
149     private void setText(final TextView textView, final String text) {
150         if (mPrefix == null) {
151             textView.setText(text);
152         } else {
153             mHighlighter.setText(textView, text, mPrefix);
154         }
155     }
156
157     /**
158      * {@inheritDoc}
159      */
160     @Override
161     public boolean hasStableIds() {
162         return true;
163     }
164
165     /**
166      * @param pause True to temporarily pause the disk cache, false
167      *            otherwise.
168      */
169     public void setPauseDiskCache(final boolean pause) {
170         if (mImageFetcher != null) {
171             mImageFetcher.setPauseDiskCache(pause);
172         }
173     }
174
175     /**
176      * @param prefix The query to filter.
177      */
178     public void setPrefix(final CharSequence prefix) {
179         if (!TextUtils.isEmpty(prefix)) {
180             mPrefix = prefix.toString().toUpperCase(Locale.getDefault()).toCharArray();
181         } else {
182             mPrefix = null;
183         }
184     }
185
186     @Override
187     public void unload() {
188         clear();
189     }
190
191     @Override
192     public void buildCache() {
193
194     }
195
196     @Override
197     public void flush() {
198         mImageFetcher.flush();
199     }
200
201     /**
202      * Gets the item position for a given id
203      * @param id identifies the object
204      * @return the position if found, -1 otherwise
205      */
206     @Override
207     public int getItemPosition(long id) {
208         for (int i = 0; i < getCount(); i++) {
209             if (getItem(i).mId == id) {
210                 return i;
211             }
212         }
213
214         return  -1;
215     }
216
217     @Override
218     public void setPopupMenuClickedListener(IListener listener) {
219         mListener = listener;
220     }
221 }