summaryrefslogtreecommitdiffstats
path: root/src/com/cyanogenmod/eleven/loaders/PlaylistSongLoader.java
blob: e998e8f988d59b325696245f703ddc3cdd7676c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
 * Copyright (C) 2012 Andrew Neal
 * Copyright (C) 2014 The CyanogenMod Project
 * Licensed under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
 * or agreed to in writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */

package com.cyanogenmod.eleven.loaders;

import android.content.ContentProviderOperation;
import android.content.Context;
import android.content.OperationApplicationException;
import android.database.Cursor;
import android.net.Uri;
import android.os.RemoteException;
import android.provider.MediaStore;
import android.provider.MediaStore.Audio.AudioColumns;
import android.provider.MediaStore.Audio.Playlists;
import android.util.Log;

import com.cyanogenmod.eleven.model.Song;
import com.cyanogenmod.eleven.utils.Lists;

import java.util.ArrayList;
import java.util.List;

/**
 * Used to query {@link MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI} and
 * return the songs for a particular playlist.
 *
 * @author Andrew Neal (andrewdneal@gmail.com)
 */
public class PlaylistSongLoader extends WrappedAsyncTaskLoader<List<Song>> {
    private static final String TAG = PlaylistSongLoader.class.getSimpleName();

    /**
     * The result
     */
    private final ArrayList<Song> mSongList = Lists.newArrayList();

    /**
     * The {@link Cursor} used to run the query.
     */
    private Cursor mCursor;

    /**
     * The Id of the playlist the songs belong to.
     */
    private final long mPlaylistID;

    /**
     * Constructor of <code>SongLoader</code>
     *
     * @param context    The {@link Context} to use
     * @param playlistId The Id of the playlist the songs belong to.
     */
    public PlaylistSongLoader(final Context context, final long playlistId) {
        super(context);
        mPlaylistID = playlistId;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List<Song> loadInBackground() {
        final int playlistCount = countPlaylist(getContext(), mPlaylistID);

        // Create the Cursor
        mCursor = makePlaylistSongCursor(getContext(), mPlaylistID);

        if (mCursor != null) {
            boolean runCleanup = false;

            // if the raw playlist count differs from the mapped playlist count (ie the raw mapping
            // table vs the mapping table join the audio table) that means the playlist mapping table
            // is messed up
            if (mCursor.getCount() != playlistCount) {
                Log.w(TAG, "Count Differs - raw is: " + playlistCount + " while cursor is " +
                        mCursor.getCount());

                runCleanup = true;
            }

            // check if the play order is already messed up by duplicates
            if (!runCleanup && mCursor.moveToFirst()) {
                final int playOrderCol = mCursor.getColumnIndexOrThrow(Playlists.Members.PLAY_ORDER);

                int lastPlayOrder = -1;
                do {
                    int playOrder = mCursor.getInt(playOrderCol);
                    // if we have duplicate play orders, we need to recreate the playlist
                    if (playOrder == lastPlayOrder) {
                        runCleanup = true;
                        break;
                    }
                    lastPlayOrder = playOrder;
                } while (mCursor.moveToNext());
            }

            if (runCleanup) {
                Log.w(TAG, "Playlist order has flaws - recreating playlist");

                // cleanup the playlist
                cleanupPlaylist(getContext(), mPlaylistID, mCursor);

                // create a new cursor
                mCursor.close();
                mCursor = makePlaylistSongCursor(getContext(), mPlaylistID);
                if (mCursor != null) {
                    Log.d(TAG, "New Count is: " + mCursor.getCount());
                }
            }
        }

        // Gather the data
        if (mCursor != null && mCursor.moveToFirst()) {
            do {
                // Copy the song Id
                final long id = mCursor.getLong(mCursor
                        .getColumnIndexOrThrow(MediaStore.Audio.Playlists.Members.AUDIO_ID));

                // Copy the song name
                final String songName = mCursor.getString(mCursor
                        .getColumnIndexOrThrow(AudioColumns.TITLE));

                // Copy the artist name
                final String artist = mCursor.getString(mCursor
                        .getColumnIndexOrThrow(AudioColumns.ARTIST));

                // Copy the album id
                final long albumId = mCursor.getLong(mCursor
                        .getColumnIndexOrThrow(AudioColumns.ALBUM_ID));

                // Copy the album name
                final String album = mCursor.getString(mCursor
                        .getColumnIndexOrThrow(AudioColumns.ALBUM));

                // Copy the duration
                final long duration = mCursor.getLong(mCursor
                        .getColumnIndexOrThrow(AudioColumns.DURATION));

                // Convert the duration into seconds
                final int durationInSecs = (int) duration / 1000;

                // Grab the Song Year
                final int year = mCursor.getInt(mCursor
                        .getColumnIndexOrThrow(AudioColumns.YEAR));

                // Create a new song
                final Song song = new Song(id, songName, artist, album, albumId, durationInSecs, year);

                // Add everything up
                mSongList.add(song);
            } while (mCursor.moveToNext());
        }
        // Close the cursor
        if (mCursor != null) {
            mCursor.close();
            mCursor = null;
        }
        return mSongList;
    }

    /**
     * Cleans up the playlist based on the passed in cursor's data
     * @param context The {@link Context} to use
     * @param playlistId playlistId to clean up
     * @param cursor data to repopulate the playlist with
     */
    private static void cleanupPlaylist(final Context context, final long playlistId,
                                 final Cursor cursor) {
        Log.w(TAG, "Cleaning up playlist: " + playlistId);

        final int idCol = cursor.getColumnIndexOrThrow(MediaStore.Audio.Playlists.Members.AUDIO_ID);
        final Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external", playlistId);

        ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();

        // Delete all results in the playlist
        ops.add(ContentProviderOperation.newDelete(uri).build());

        // yield the db every 100 records to prevent ANRs
        final int YIELD_FREQUENCY = 100;

        // for each item, reset the play order position
        if (cursor.moveToFirst() && cursor.getCount() > 0) {
            do {
                final ContentProviderOperation.Builder builder =
                        ContentProviderOperation.newInsert(uri)
                                .withValue(Playlists.Members.PLAY_ORDER, cursor.getPosition())
                                .withValue(Playlists.Members.AUDIO_ID, cursor.getLong(idCol));

                // yield at the end and not at 0 by incrementing by 1
                if ((cursor.getPosition() + 1) % YIELD_FREQUENCY == 0) {
                    builder.withYieldAllowed(true);
                }
                ops.add(builder.build());
            } while (cursor.moveToNext());
        }

        try {
            // run the batch operation
            context.getContentResolver().applyBatch(MediaStore.AUTHORITY, ops);
        } catch (RemoteException e) {
            Log.e(TAG, "RemoteException " + e + " while cleaning up playlist " + playlistId);
        } catch (OperationApplicationException e) {
            Log.e(TAG, "OperationApplicationException " + e + " while cleaning up playlist "
                    + playlistId);
        }
    }

    /**
     * Returns the playlist count for the raw playlist mapping table
     * @param context The {@link Context} to use
     * @param playlistId playlistId to count
     * @return the number of tracks in the raw playlist mapping table
     */
    private static int countPlaylist(final Context context, final long playlistId) {
        Cursor c = null;
        try {
            // when we query using only the audio_id column we will get the raw mapping table
            // results - which will tell us if the table has rows that don't exist in the normal
            // table
            c = context.getContentResolver().query(
                    MediaStore.Audio.Playlists.Members.getContentUri("external", playlistId),
                    new String[]{
                            MediaStore.Audio.Playlists.Members.AUDIO_ID,
                    }, null, null,
                    MediaStore.Audio.Playlists.Members.DEFAULT_SORT_ORDER);

            if (c != null) {
                return c.getCount();
            }
        } finally {
            if (c != null) {
                c.close();
                c = null;
            }
        }

        return 0;
    }

    /**
     * Creates the {@link Cursor} used to run the query.
     * 
     * @param context The {@link Context} to use.
     * @param playlistID The playlist the songs belong to.
     * @return The {@link Cursor} used to run the song query.
     */
    public static final Cursor makePlaylistSongCursor(final Context context, final Long playlistID) {
        String mSelection = (AudioColumns.IS_MUSIC + "=1") +
                " AND " + AudioColumns.TITLE + " != ''";
        return context.getContentResolver().query(
                MediaStore.Audio.Playlists.Members.getContentUri("external", playlistID),
                new String[] {
                        /* 0 */
                        MediaStore.Audio.Playlists.Members._ID,
                        /* 1 */
                        MediaStore.Audio.Playlists.Members.AUDIO_ID,
                        /* 2 */
                        AudioColumns.TITLE,
                        /* 3 */
                        AudioColumns.ARTIST,
                        /* 4 */
                        AudioColumns.ALBUM_ID,
                        /* 5 */
                        AudioColumns.ALBUM,
                        /* 6 */
                        AudioColumns.DURATION,
                        /* 7 */
                        AudioColumns.YEAR,
                        /* 8 */
                        Playlists.Members.PLAY_ORDER,
                }, mSelection, null,
                MediaStore.Audio.Playlists.Members.DEFAULT_SORT_ORDER);
    }
}