summaryrefslogtreecommitdiff
path: root/java/com/android/pump/db/AudioStore.java
blob: 4cd569b1640332cc07bb810da816939b64d27df7 (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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
/*
 * Copyright 2018 The Android Open Source 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.android.pump.db;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;

import androidx.annotation.AnyThread;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.WorkerThread;

import com.android.pump.util.Clog;
import com.android.pump.util.Collections;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;

@WorkerThread
class AudioStore extends ContentObserver {
    private static final String TAG = Clog.tag(AudioStore.class);

    private final ContentResolver mContentResolver;
    private final ChangeListener mChangeListener;
    private final MediaProvider mMediaProvider;

    interface ChangeListener {
        void onAudiosAdded(@NonNull Collection<Audio> audios);
        void onArtistsAdded(@NonNull Collection<Artist> artists);
        void onAlbumsAdded(@NonNull Collection<Album> albums);
        void onGenresAdded(@NonNull Collection<Genre> genres);
        void onPlaylistsAdded(@NonNull Collection<Playlist> playlists);
    }

    @AnyThread
    AudioStore(@NonNull ContentResolver contentResolver, @NonNull ChangeListener changeListener,
            @NonNull MediaProvider mediaProvider) {
        super(null);

        Clog.i(TAG, "AudioStore(" + contentResolver + ", " + changeListener
                + ", " + mediaProvider + ")");
        mContentResolver = contentResolver;
        mChangeListener = changeListener;
        mMediaProvider = mediaProvider;

        // TODO(123705758) Do we need content observer for other content uris? (E.g. album, artist)
        mContentResolver.registerContentObserver(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                true, this);

        // TODO(123705758) When to call unregisterContentObserver?
        // mContentResolver.unregisterContentObserver(this);
    }

    void load() {
        Clog.i(TAG, "load()");
        ArrayList<Artist> artists = new ArrayList<>();
        ArrayList<Album> albums = new ArrayList<>();
        ArrayList<Audio> audios = new ArrayList<>();
        ArrayList<Playlist> playlists = new ArrayList<>();
        ArrayList<Genre> genres = new ArrayList<>();

        // #1 Load artists
        {
            Uri contentUri = MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI;
            String[] projection = {
                MediaStore.Audio.Artists._ID
            };
            String sortOrder = MediaStore.Audio.Artists._ID;
            Cursor cursor = mContentResolver.query(contentUri, projection, null, null, sortOrder);
            if (cursor != null) {
                try {
                    int idColumn = cursor.getColumnIndexOrThrow(MediaStore.Audio.Artists._ID);

                    for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
                        long id = cursor.getLong(idColumn);

                        Artist artist = new Artist(id);
                        artists.add(artist);
                    }
                } finally {
                    cursor.close();
                }
            }
        }

        // #2 Load albums and connect each to artist
        {
            Uri contentUri = MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI;
            String[] projection = {
                MediaStore.Audio.Albums._ID,
                MediaStore.Audio.Media.ARTIST_ID // TODO MediaStore.Audio.Albums.ARTIST_ID
            };
            String sortOrder = MediaStore.Audio.Albums._ID;
            Cursor cursor = mContentResolver.query(contentUri, projection, null, null, sortOrder);
            if (cursor != null) {
                try {
                    int idColumn = cursor.getColumnIndexOrThrow(MediaStore.Audio.Albums._ID);
                    int artistIdColumn = cursor.getColumnIndexOrThrow(
                            MediaStore.Audio.Media.ARTIST_ID); // TODO MediaStore.Audio.Albums.ARTIST_ID

                    for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
                        long id = cursor.getLong(idColumn);

                        Album album = new Album(id);
                        albums.add(album);

                        if (!cursor.isNull(artistIdColumn)) {
                            long artistId = cursor.getLong(artistIdColumn);

                            Artist artist = Collections.find(artists, artistId, Artist::getId);
                            album.setArtist(artist);
                        }
                    }
                } finally {
                    cursor.close();
                }
            }
        }

        // #3 Load songs and connect each to album and artist
        {
            Uri contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
            String[] projection = {
                MediaStore.Audio.Media._ID,
                MediaStore.Audio.Media.MIME_TYPE,
                MediaStore.Audio.Media.ARTIST_ID,
                MediaStore.Audio.Media.ALBUM_ID
            };
            String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
            String sortOrder = MediaStore.Audio.Media._ID;
            Cursor cursor = mContentResolver.query(contentUri, projection, selection, null, sortOrder);
            if (cursor != null) {
                try {
                    int idColumn = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID);
                    int mimeTypeColumn = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.MIME_TYPE);
                    int artistIdColumn = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST_ID);
                    int albumIdColumn = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID);

                    for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
                        long id = cursor.getLong(idColumn);
                        String mimeType = cursor.getString(mimeTypeColumn);

                        Audio audio = new Audio(id, mimeType);
                        audios.add(audio);

                        if (!cursor.isNull(artistIdColumn)) {
                            long artistId = cursor.getLong(artistIdColumn);

                            Artist artist = Collections.find(artists, artistId, Artist::getId);
                            audio.setArtist(artist);
                            artist.addAudio(audio);
                        }
                        if (!cursor.isNull(albumIdColumn)) {
                            long albumId = cursor.getLong(albumIdColumn);

                            Album album = Collections.find(albums, albumId, Album::getId);
                            audio.setAlbum(album);
                            album.addAudio(audio);
                        }
                    }
                } finally {
                    cursor.close();
                }
            }
        }

        // #4 Load playlists (optional?)
        {
            Uri contentUri = MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI;
            String[] projection = {
                MediaStore.Audio.Playlists._ID
            };
            String sortOrder = MediaStore.Audio.Playlists._ID;
            Cursor cursor = mContentResolver.query(contentUri, projection, null, null, sortOrder);
            if (cursor != null) {
                try {
                    int idColumn = cursor.getColumnIndexOrThrow(MediaStore.Audio.Playlists._ID);

                    for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
                        long id = cursor.getLong(idColumn);

                        Playlist playlist = new Playlist(id);
                        playlists.add(playlist);
                    }
                } finally {
                    cursor.close();
                }
            }
        }

        // #5 Load genres (optional?)
        {
            Uri contentUri = MediaStore.Audio.Genres.EXTERNAL_CONTENT_URI;
            String[] projection = {
                MediaStore.Audio.Genres._ID
            };
            String sortOrder = MediaStore.Audio.Genres._ID;
            Cursor cursor = mContentResolver.query(contentUri, projection, null, null, sortOrder);
            if (cursor != null) {
                try {
                    int idColumn = cursor.getColumnIndexOrThrow(MediaStore.Audio.Genres._ID);

                    for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
                        long id = cursor.getLong(idColumn);

                        Genre genre = new Genre(id);
                        genres.add(genre);
                    }
                } finally {
                    cursor.close();
                }
            }
        }

        mChangeListener.onAudiosAdded(audios);
        mChangeListener.onArtistsAdded(artists);
        mChangeListener.onAlbumsAdded(albums);
        mChangeListener.onGenresAdded(genres);
        mChangeListener.onPlaylistsAdded(playlists);
    }

    boolean loadData(@NonNull Audio audio) {
        boolean updated = false;

        Uri contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        String[] projection = {
            MediaStore.Audio.Media.TITLE,
            MediaStore.Audio.Media.ARTIST_ID,
            MediaStore.Audio.Media.ALBUM_ID
        };
        String selection = MediaStore.Audio.Media._ID + " = ?";
        String[] selectionArgs = { Long.toString(audio.getId()) };
        Cursor cursor = mContentResolver.query(
                contentUri, projection, selection, selectionArgs, null);
        if (cursor != null) {
            try {
                int titleColumn = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE);
                int artistIdColumn = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST_ID);
                int albumIdColumn = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID);

                if (cursor.moveToFirst()) {
                    if (!cursor.isNull(titleColumn)) {
                        String title = cursor.getString(titleColumn);
                        updated |= audio.setTitle(title);
                    }
                    if (!cursor.isNull(artistIdColumn)) {
                        long artistId = cursor.getLong(artistIdColumn);
                        Artist artist = mMediaProvider.getArtistById(artistId);
                        updated |= audio.setArtist(artist);
                        updated |= loadData(artist); // TODO(b/123707561) Load separate from audio
                    }
                    if (!cursor.isNull(albumIdColumn)) {
                        long albumId = cursor.getLong(albumIdColumn);
                        Album album = mMediaProvider.getAlbumById(albumId);
                        updated |= audio.setAlbum(album);
                        updated |= loadData(album); // TODO(b/123707561) Load separate from audio
                    }
                }
            } finally {
                cursor.close();
            }
        }

        return updated;
    }

    boolean loadData(@NonNull Artist artist) {
        boolean updated = false;

        Uri contentUri = MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI;
        String[] projection = { MediaStore.Audio.Artists.ARTIST };
        String selection = MediaStore.Audio.Artists._ID + " = ?";
        String[] selectionArgs = { Long.toString(artist.getId()) };
        Cursor cursor = mContentResolver.query(
                contentUri, projection, selection, selectionArgs, null);
        if (cursor != null) {
            try {
                int artistColumn = cursor.getColumnIndexOrThrow(MediaStore.Audio.Artists.ARTIST);

                if (cursor.moveToFirst()) {
                    if (!cursor.isNull(artistColumn)) {
                        String name = cursor.getString(artistColumn);
                        updated |= artist.setName(name);
                    }
                }
            } finally {
                cursor.close();
            }
        }

        updated |= loadAlbums(artist); // TODO(b/123707561) Load separate from artist

        return updated;
    }

    boolean loadData(@NonNull Album album) {
        boolean updated = false;

        Uri contentUri = MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI;
        String[] projection = {
            MediaStore.Audio.Albums.ALBUM_ART,
            MediaStore.Audio.Albums.ALBUM,
            MediaStore.Audio.Media.ARTIST_ID // TODO MediaStore.Audio.Albums.ARTIST_ID
        };
        String selection = MediaStore.Audio.Albums._ID + " = ?";
        String[] selectionArgs = { Long.toString(album.getId()) };
        Cursor cursor = mContentResolver.query(
                contentUri, projection, selection, selectionArgs, null);
        if (cursor != null) {
            try {
                int albumArtColumn = cursor.getColumnIndexOrThrow(
                        MediaStore.Audio.Albums.ALBUM_ART);
                int albumColumn = cursor.getColumnIndexOrThrow(MediaStore.Audio.Albums.ALBUM);
                int artistIdColumn = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST_ID); // TODO MediaStore.Audio.Albums.ARTIST_ID

                if (cursor.moveToFirst()) {
                    if (!cursor.isNull(albumColumn)) {
                        String albumTitle = cursor.getString(albumColumn);
                        updated |= album.setTitle(albumTitle);
                    }
                    if (!cursor.isNull(albumArtColumn)) {
                        Uri albumArtUri = Uri.fromFile(new File(cursor.getString(albumArtColumn)));
                        updated |= album.setAlbumArtUri(albumArtUri);
                    }
                    if (!cursor.isNull(artistIdColumn)) {
                        long artistId = cursor.getLong(artistIdColumn);
                        Artist artist = mMediaProvider.getArtistById(artistId);
                        updated |= album.setArtist(artist);
                        updated |= loadData(artist); // TODO(b/123707561) Load separate from album
                    }
                }
            } finally {
                cursor.close();
            }
        }

        return updated;
    }

    boolean loadData(@NonNull Genre genre) {
        boolean updated = false;

        Uri contentUri = MediaStore.Audio.Genres.EXTERNAL_CONTENT_URI;
        String[] projection = { MediaStore.Audio.Genres.NAME };
        String selection = MediaStore.Audio.Genres._ID + " = ?";
        String[] selectionArgs = { Long.toString(genre.getId()) };
        Cursor cursor = mContentResolver.query(
                contentUri, projection, selection, selectionArgs, null);
        if (cursor != null) {
            try {
                int nameColumn = cursor.getColumnIndexOrThrow(MediaStore.Audio.Genres.NAME);

                if (cursor.moveToFirst()) {
                    if (!cursor.isNull(nameColumn)) {
                        String name = cursor.getString(nameColumn);
                        updated |= genre.setName(name);
                    }
                }
            } finally {
                cursor.close();
            }
        }

        updated |= loadAudios(genre); // TODO(b/123707561) Load separate from genre

        return updated;
    }

    boolean loadData(@NonNull Playlist playlist) {
        boolean updated = false;

        Uri contentUri = MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI;
        String[] projection = { MediaStore.Audio.Playlists.NAME };
        String selection = MediaStore.Audio.Playlists._ID + " = ?";
        String[] selectionArgs = { Long.toString(playlist.getId()) };
        Cursor cursor = mContentResolver.query(
                contentUri, projection, selection, selectionArgs, null);
        if (cursor != null) {
            try {
                int nameColumn = cursor.getColumnIndexOrThrow(MediaStore.Audio.Playlists.NAME);

                if (cursor.moveToFirst()) {
                    if (!cursor.isNull(nameColumn)) {
                        String name = cursor.getString(nameColumn);
                        updated |= playlist.setName(name);
                    }
                }
            } finally {
                cursor.close();
            }
        }

        updated |= loadAudios(playlist); // TODO(b/123707561) Load separate from playlist

        return updated;
    }

    boolean loadAlbums(@NonNull Artist artist) {
        boolean updated = false;

        // TODO Remove hardcoded value
        Uri contentUri = MediaStore.Audio.Artists.Albums.getContentUri("external", artist.getId());
        /*
         * On some devices MediaStore doesn't use ALBUM_ID as key from Artist to Album, but rather
         * _ID. In order to support these devices we don't pass a projection, to avoid the
         * IllegalArgumentException(Invalid column) exception, and then resort to _ID.
         */
        String[] projection = null; // { MediaStore.Audio.Artists.Albums.ALBUM_ID };
        Cursor cursor = mContentResolver.query(contentUri, projection, null, null, null);
        if (cursor != null) {
            try {
                int albumIdColumn = cursor.getColumnIndex(MediaStore.Audio.Artists.Albums.ALBUM_ID);
                if (albumIdColumn < 0) {
                    // On some devices the ALBUM_ID column doesn't exist and _ID is used instead.
                    albumIdColumn = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID);
                }

                for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
                    long albumId = cursor.getLong(albumIdColumn);
                    Album album = mMediaProvider.getAlbumById(albumId);
                    updated |= artist.addAlbum(album);
                    //updated |= loadData(album); // TODO(b/123707561) Load separate from artist
                }
            } finally {
                cursor.close();
            }
        }

        return updated;
    }

    boolean loadAudios(@NonNull Genre genre) {
        boolean updated = false;

        // TODO Remove hardcoded value
        Uri contentUri = MediaStore.Audio.Genres.Members.getContentUri("external", genre.getId());
        String[] projection = { MediaStore.Audio.Genres.Members.AUDIO_ID };
        Cursor cursor = mContentResolver.query(contentUri, projection, null, null, null);
        if (cursor != null) {
            try {
                int audioIdColumn = cursor.getColumnIndexOrThrow(
                        MediaStore.Audio.Genres.Members.AUDIO_ID);

                for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
                    long audioId = cursor.getLong(audioIdColumn);
                    Audio audio = mMediaProvider.getAudioById(audioId);
                    updated |= genre.addAudio(audio);
                    updated |= loadData(audio); // TODO(b/123707561) Load separate from genre
                }
            } finally {
                cursor.close();
            }
        }

        return updated;
    }

    boolean loadAudios(@NonNull Playlist playlist) {
        boolean updated = false;

        // TODO Remove hardcoded value
        Uri contentUri = MediaStore.Audio.Playlists.Members.getContentUri(
                "external", playlist.getId());
        String[] projection = { MediaStore.Audio.Playlists.Members.AUDIO_ID };
        Cursor cursor = mContentResolver.query(contentUri, projection, null, null, null);
        if (cursor != null) {
            try {
                int audioIdColumn = cursor.getColumnIndexOrThrow(
                        MediaStore.Audio.Playlists.Members.AUDIO_ID);

                for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
                    long audioId = cursor.getLong(audioIdColumn);
                    Audio audio = mMediaProvider.getAudioById(audioId);
                    updated |= playlist.addAudio(audio);
                    updated |= loadData(audio); // TODO(b/123707561) Load separate from playlist
                }
            } finally {
                cursor.close();
            }
        }

        return updated;
    }

    @Override
    public void onChange(boolean selfChange) {
        Clog.i(TAG, "onChange(" + selfChange + ")");
        onChange(selfChange, null);
    }

    @Override
    public void onChange(boolean selfChange, @Nullable Uri uri) {
        Clog.i(TAG, "onChange(" + selfChange + ", " + uri + ")");
        // TODO(123705758) Figure out what changed
        // onChange(false, content://media)
        // onChange(false, content://media/external)
        // onChange(false, content://media/external/audio/media/444)
        // onChange(false, content://media/external/video/media/328?blocking=1&orig_id=328&group_id=0)

        // TODO(123705758) Notify listener about changes
        // mChangeListener.xxx();
    }

    // TODO Remove unused methods
    private long createPlaylist(@NonNull String name) {
        Clog.i(TAG, "createPlaylist(" + name + ")");
        Uri contentUri = MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI;
        ContentValues contentValues = new ContentValues(1);
        contentValues.put(MediaStore.Audio.Playlists.NAME, name);
        Uri uri = mContentResolver.insert(contentUri, contentValues);
        return Long.parseLong(uri.getLastPathSegment());
    }

    private void addToPlaylist(@NonNull Playlist playlist, @NonNull Audio audio) {
        Clog.i(TAG, "addToPlaylist(" + playlist + ", " + audio + ")");
        long base = getLastPlayOrder(playlist);

        // TODO Remove hardcoded value
        Uri contentUri = MediaStore.Audio.Playlists.Members.getContentUri(
                "external", playlist.getId());
        ContentValues contentValues = new ContentValues(2);
        contentValues.put(MediaStore.Audio.Playlists.Members.AUDIO_ID, audio.getId());
        contentValues.put(MediaStore.Audio.Playlists.Members.PLAY_ORDER, base + 1);
        mContentResolver.insert(contentUri, contentValues);
    }

    private long getLastPlayOrder(@NonNull Playlist playlist) {
        Clog.i(TAG, "getLastPlayOrder(" + playlist + ")");

        long playOrder = -1;

        // TODO Remove hardcoded value
        Uri contentUri = MediaStore.Audio.Playlists.Members.getContentUri(
                "external", playlist.getId());
        String[] projection = { MediaStore.Audio.Playlists.Members.PLAY_ORDER };
        String sortOrder = MediaStore.Audio.Playlists.Members.PLAY_ORDER + " DESC LIMIT 1";
        Cursor cursor = mContentResolver.query(
                contentUri, projection, null, null, sortOrder);
        if (cursor != null) {
            try {
                int playOrderColumn = cursor.getColumnIndexOrThrow(
                        MediaStore.Audio.Playlists.Members.PLAY_ORDER);

                if (cursor.moveToFirst()) {
                    playOrder = cursor.getLong(playOrderColumn);
                }
            } finally {
                cursor.close();
            }
        }

        return playOrder;
    }
}