2 * Copyright (C) 2012 Andrew Neal
3 * Copyright (C) 2014 The CyanogenMod Project
4 * Licensed under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with the
6 * License. You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
8 * or agreed to in writing, software distributed under the License is
9 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
10 * KIND, either express or implied. See the License for the specific language
11 * governing permissions and limitations under the License.
14 package com.cyanogenmod.eleven.loaders;
16 import android.content.Context;
17 import android.database.Cursor;
18 import android.net.Uri;
19 import android.provider.BaseColumns;
20 import android.provider.MediaStore;
21 import android.text.TextUtils;
23 import com.cyanogenmod.eleven.model.Song;
24 import com.cyanogenmod.eleven.utils.Lists;
26 import java.util.ArrayList;
27 import java.util.List;
30 * @author Andrew Neal (andrewdneal@gmail.com)
32 public class SearchLoader extends WrappedAsyncTaskLoader<List<Song>> {
37 private final ArrayList<Song> mSongList = Lists.newArrayList();
42 private String mQuery;
45 * Constructor of <code>SongLoader</code>
47 * @param context The {@link Context} to use
48 * @param query The search query
50 public SearchLoader(final Context context, final String query) {
59 public List<Song> loadInBackground() {
61 Cursor cursor = makeSearchCursor(getContext(), mQuery);
62 if (cursor != null && cursor.moveToFirst()) {
68 final String songName = cursor.getString(cursor
69 .getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
71 // Check for a song Id
72 if (!TextUtils.isEmpty(songName)) {
73 id = cursor.getLong(cursor
74 .getColumnIndexOrThrow(MediaStore.Audio.Media._ID));
77 // Copy the album name
78 final String album = cursor.getString(cursor
79 .getColumnIndexOrThrow(MediaStore.Audio.Albums.ALBUM));
82 final long albumId = cursor.getLong(cursor
83 .getColumnIndexOrThrow(MediaStore.Audio.Albums.ALBUM_ID));
85 // Check for a album Id
86 if (id < 0 && !TextUtils.isEmpty(album)) {
87 id = cursor.getLong(cursor
88 .getColumnIndexOrThrow(MediaStore.Audio.Albums._ID));
91 // Copy the artist name
92 final String artist = cursor.getString(cursor
93 .getColumnIndexOrThrow(MediaStore.Audio.Artists.ARTIST));
95 // Check for a artist Id
96 if (id < 0 && !TextUtils.isEmpty(artist)) {
97 id = cursor.getLong(cursor
98 .getColumnIndexOrThrow(MediaStore.Audio.Artists._ID));
102 final Song song = new Song(id, songName, artist, album, albumId, -1, -1);
106 } while (cursor.moveToNext());
109 if (cursor != null) {
117 * * @param context The {@link Context} to use.
119 * @param query The user's query.
120 * @return The {@link Cursor} used to perform the search.
122 public static final Cursor makeSearchCursor(final Context context, final String query) {
123 return context.getContentResolver().query(
124 Uri.parse("content://media/external/audio/search/fancy/" + Uri.encode(query)),
126 BaseColumns._ID, MediaStore.Audio.Media.MIME_TYPE,
127 MediaStore.Audio.Artists.ARTIST, MediaStore.Audio.Albums.ALBUM_ID,
128 MediaStore.Audio.Albums.ALBUM, MediaStore.Audio.Media.TITLE, "data1", "data2" //$NON-NLS-2$
129 }, null, null, null);