OSDN Git Service

Eleven: Create one cursor per background task
[android-x86/packages-apps-Eleven.git] / src / com / cyanogenmod / eleven / loaders / SearchLoader.java
1 /*
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.
12  */
13
14 package com.cyanogenmod.eleven.loaders;
15
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;
22
23 import com.cyanogenmod.eleven.model.Song;
24 import com.cyanogenmod.eleven.utils.Lists;
25
26 import java.util.ArrayList;
27 import java.util.List;
28
29 /**
30  * @author Andrew Neal (andrewdneal@gmail.com)
31  */
32 public class SearchLoader extends WrappedAsyncTaskLoader<List<Song>> {
33
34     /**
35      * The result
36      */
37     private final ArrayList<Song> mSongList = Lists.newArrayList();
38
39     /**
40      * The query
41      */
42     private String mQuery;
43
44     /**
45      * Constructor of <code>SongLoader</code>
46      *
47      * @param context The {@link Context} to use
48      * @param query The search query
49      */
50     public SearchLoader(final Context context, final String query) {
51         super(context);
52         mQuery = query;
53     }
54
55     /**
56      * {@inheritDoc}
57      */
58     @Override
59     public List<Song> loadInBackground() {
60         // Gather the data
61         Cursor cursor = makeSearchCursor(getContext(), mQuery);
62         if (cursor != null && cursor.moveToFirst()) {
63             do {
64                 // Copy the song Id
65                 long id = -1;
66
67                 // Copy the song name
68                 final String songName = cursor.getString(cursor
69                         .getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
70
71                 // Check for a song Id
72                 if (!TextUtils.isEmpty(songName)) {
73                     id = cursor.getLong(cursor
74                             .getColumnIndexOrThrow(MediaStore.Audio.Media._ID));
75                 }
76
77                 // Copy the album name
78                 final String album = cursor.getString(cursor
79                         .getColumnIndexOrThrow(MediaStore.Audio.Albums.ALBUM));
80
81                 // Copy the album id
82                 final long albumId = cursor.getLong(cursor
83                         .getColumnIndexOrThrow(MediaStore.Audio.Albums.ALBUM_ID));
84
85                 // Check for a album Id
86                 if (id < 0 && !TextUtils.isEmpty(album)) {
87                     id = cursor.getLong(cursor
88                             .getColumnIndexOrThrow(MediaStore.Audio.Albums._ID));
89                 }
90
91                 // Copy the artist name
92                 final String artist = cursor.getString(cursor
93                         .getColumnIndexOrThrow(MediaStore.Audio.Artists.ARTIST));
94
95                 // Check for a artist Id
96                 if (id < 0 && !TextUtils.isEmpty(artist)) {
97                     id = cursor.getLong(cursor
98                             .getColumnIndexOrThrow(MediaStore.Audio.Artists._ID));
99                 }
100
101                 // Create a new song
102                 final Song song = new Song(id, songName, artist, album, albumId, -1, -1);
103
104                 // Add everything up
105                 mSongList.add(song);
106             } while (cursor.moveToNext());
107         }
108         // Close the cursor
109         if (cursor != null) {
110             cursor.close();
111             cursor = null;
112         }
113         return mSongList;
114     }
115
116     /**
117      * * @param context The {@link Context} to use.
118      *
119      * @param query The user's query.
120      * @return The {@link Cursor} used to perform the search.
121      */
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)),
125                 new String[] {
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);
130     }
131
132 }