aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/github/daneren2005/dsub/service/AutoMediaBrowserService.java
blob: d2378ccb6272f376137bfa3646bff825809b04d6 (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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
/*
	This file is part of Subsonic.

	Subsonic is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	Subsonic is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with Subsonic. If not, see <http://www.gnu.org/licenses/>.

	Copyright 2015 (C) Scott Jackson
*/
package github.daneren2005.dsub.service;

import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.media.MediaBrowserCompat;
import android.support.v4.media.MediaBrowserServiceCompat;
import android.support.v4.media.MediaDescriptionCompat;
import android.util.Log;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import github.daneren2005.dsub.R;
import github.daneren2005.dsub.domain.Artist;
import github.daneren2005.dsub.domain.Indexes;
import github.daneren2005.dsub.domain.MusicDirectory;
import github.daneren2005.dsub.domain.MusicDirectory.Entry;
import github.daneren2005.dsub.domain.MusicFolder;
import github.daneren2005.dsub.domain.Playlist;
import github.daneren2005.dsub.domain.PodcastChannel;
import github.daneren2005.dsub.domain.PodcastEpisode;
import github.daneren2005.dsub.util.Constants;
import github.daneren2005.dsub.util.SilentServiceTask;
import github.daneren2005.dsub.util.Util;
import github.daneren2005.dsub.util.compat.RemoteControlClientLP;

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public class AutoMediaBrowserService extends MediaBrowserServiceCompat {
	private static final String TAG = AutoMediaBrowserService.class.getSimpleName();
	private static final String BROWSER_ROOT = "root";
	private static final String BROWSER_ALBUM_LISTS = "albumLists";
	private static final String BROWSER_LIBRARY = "library";
	private static final String BROWSER_PLAYLISTS = "playlists";
	private static final String BROWSER_PODCASTS = "podcasts";
	private static final String BROWSER_BOOKMARKS = "bookmarks";
	private static final String PLAYLIST_PREFIX = "pl-";
	private static final String PODCAST_PREFIX = "po-";
	private static final String ALBUM_TYPE_PREFIX = "ty-";
	private static final String MUSIC_DIRECTORY_PREFIX = "md-";
	private static final String MUSIC_FOLDER_PREFIX = "mf-";
	private static final String MUSIC_DIRECTORY_CONTENTS_PREFIX = "mdc-";
	private static final String ARTIST_CONTENTS_PREFIX = "art-";
	private static final String ALBUM_CONTENTS_PREFIX = "alb-";

	private DownloadService downloadService;
	private Handler handler = new Handler();

	@Override
	public void onCreate() {
		super.onCreate();
		getDownloadService();
	}

	@Nullable
	@Override
	public BrowserRoot onGetRoot(String clientPackageName, int clientUid, Bundle rootHints) {
		BrowserRoot root = new BrowserRoot(BROWSER_ROOT, null);
		return root;
	}

	@Override
	public void onLoadChildren(String parentId, Result<List<MediaBrowserCompat.MediaItem>> result) {
		if(BROWSER_ROOT.equals(parentId)) {
			getRootFolders(result);
		} else if(BROWSER_ALBUM_LISTS.equals(parentId)) {
			getAlbumLists(result);
		} else if(parentId.startsWith(ALBUM_TYPE_PREFIX)) {
			int id = Integer.valueOf(parentId.substring(ALBUM_TYPE_PREFIX.length()));
			getAlbumList(result, id);
		}  else if(parentId.startsWith(MUSIC_DIRECTORY_PREFIX)) {
			String id = parentId.substring(MUSIC_DIRECTORY_PREFIX.length());
			getPlayOptions(result, id, Constants.INTENT_EXTRA_NAME_ID);
		} else if(BROWSER_LIBRARY.equals(parentId)) {
			if(Util.isTagBrowsing(downloadService)) {
				getArtists(result);
			} else {
				getLibrary(result);
			}
		}  else if(parentId.startsWith(MUSIC_FOLDER_PREFIX)) {
			String id = parentId.substring(MUSIC_FOLDER_PREFIX.length());
			getIndexes(result, id);
		}  else if(parentId.startsWith(MUSIC_DIRECTORY_CONTENTS_PREFIX)) {
			String id = parentId.substring(MUSIC_DIRECTORY_CONTENTS_PREFIX.length());
			getMusicDirectory(result, id);
		}  else if(parentId.startsWith(ARTIST_CONTENTS_PREFIX)) {
			String id = parentId.substring(ARTIST_CONTENTS_PREFIX.length());
			getArtist(result, id);
		} else if(parentId.startsWith(ALBUM_CONTENTS_PREFIX)) {
			String id = parentId.substring(ALBUM_CONTENTS_PREFIX.length());
			getAlbum(result, id);
		} else if(BROWSER_PLAYLISTS.equals(parentId)) {
			getPlaylists(result);
		} else if(parentId.startsWith(PLAYLIST_PREFIX)) {
			String id = parentId.substring(PLAYLIST_PREFIX.length());
			getPlayOptions(result, id, Constants.INTENT_EXTRA_NAME_PLAYLIST_ID);
		} else if(BROWSER_PODCASTS.equals(parentId)) {
			getPodcasts(result);
		} else if(parentId.startsWith(PODCAST_PREFIX)) {
			String id = parentId.substring(PODCAST_PREFIX.length());
			getPodcastEpisodes(result, id);
		} else if(BROWSER_BOOKMARKS.equals(parentId)) {
			getBookmarks(result);
		} else {
			// No idea what it is, send empty result
			result.sendResult(new ArrayList<MediaBrowserCompat.MediaItem>());
		}
	}

	private void getRootFolders(Result<List<MediaBrowserCompat.MediaItem>> result) {
		List<MediaBrowserCompat.MediaItem> mediaItems = new ArrayList<>();

		MediaDescriptionCompat.Builder albumLists = new MediaDescriptionCompat.Builder();
		albumLists.setTitle(downloadService.getString(R.string.main_albums_title))
				.setMediaId(BROWSER_ALBUM_LISTS);
		mediaItems.add(new MediaBrowserCompat.MediaItem(albumLists.build(), MediaBrowserCompat.MediaItem.FLAG_BROWSABLE));

		MediaDescriptionCompat.Builder library = new MediaDescriptionCompat.Builder();
		library.setTitle(downloadService.getString(R.string.button_bar_browse))
			.setMediaId(BROWSER_LIBRARY);
		mediaItems.add(new MediaBrowserCompat.MediaItem(library.build(), MediaBrowserCompat.MediaItem.FLAG_BROWSABLE));

		MediaDescriptionCompat.Builder playlists = new MediaDescriptionCompat.Builder();
		playlists.setTitle(downloadService.getString(R.string.button_bar_playlists))
				.setMediaId(BROWSER_PLAYLISTS);
		mediaItems.add(new MediaBrowserCompat.MediaItem(playlists.build(), MediaBrowserCompat.MediaItem.FLAG_BROWSABLE));

		if(Util.getPreferences(downloadService).getBoolean(Constants.PREFERENCES_KEY_PODCASTS_ENABLED, true)) {
			MediaDescriptionCompat.Builder podcasts = new MediaDescriptionCompat.Builder();
			podcasts.setTitle(downloadService.getString(R.string.button_bar_podcasts))
					.setMediaId(BROWSER_PODCASTS);
			mediaItems.add(new MediaBrowserCompat.MediaItem(podcasts.build(), MediaBrowserCompat.MediaItem.FLAG_BROWSABLE));
		}

		if(Util.getPreferences(downloadService).getBoolean(Constants.PREFERENCES_KEY_BOOKMARKS_ENABLED, true)) {
			MediaDescriptionCompat.Builder podcasts = new MediaDescriptionCompat.Builder();
			podcasts.setTitle(downloadService.getString(R.string.button_bar_bookmarks))
					.setMediaId(BROWSER_BOOKMARKS);
			mediaItems.add(new MediaBrowserCompat.MediaItem(podcasts.build(), MediaBrowserCompat.MediaItem.FLAG_BROWSABLE));
		}

		result.sendResult(mediaItems);
	}

	private void getAlbumLists(Result<List<MediaBrowserCompat.MediaItem>> result) {
		List<Integer> albums = new ArrayList<>();
		albums.add(R.string.main_albums_newest);
		albums.add(R.string.main_albums_random);
		if(!Util.isTagBrowsing(downloadService)) {
			albums.add(R.string.main_albums_highest);
		}
		albums.add(R.string.main_albums_starred);
		albums.add(R.string.main_albums_recent);
		albums.add(R.string.main_albums_frequent);

		List<MediaBrowserCompat.MediaItem> mediaItems = new ArrayList<>();

		for(Integer id: albums) {
			MediaDescriptionCompat description = new MediaDescriptionCompat.Builder()
					.setTitle(downloadService.getResources().getString(id))
					.setMediaId(ALBUM_TYPE_PREFIX + id)
					.build();

			mediaItems.add(new MediaBrowserCompat.MediaItem(description, MediaBrowserCompat.MediaItem.FLAG_BROWSABLE));
		}

		result.sendResult(mediaItems);
	}
	private void getAlbumList(final Result<List<MediaBrowserCompat.MediaItem>> result, final int id) {
		new SilentServiceTask<MusicDirectory>(downloadService) {
			@Override
			protected MusicDirectory doInBackground(MusicService musicService) throws Throwable {
				String albumListType;
				switch(id) {
					case R.string.main_albums_newest:
						albumListType = "newest";
						break;
					case R.string.main_albums_random:
						albumListType = "random";
						break;
					case R.string.main_albums_highest:
						albumListType = "highest";
						break;
					case R.string.main_albums_starred:
						albumListType = "starred";
						break;
					case R.string.main_albums_recent:
						albumListType = "recent";
						break;
					case R.string.main_albums_frequent:
						albumListType = "frequent";
						break;
					default:
						albumListType = "newest";
				}

				return musicService.getAlbumList(albumListType, 20, 0, true, downloadService, null);
			}

			@Override
			protected void done(MusicDirectory albumSet) {
				List<MediaBrowserCompat.MediaItem> mediaItems = new ArrayList<>();

				for(Entry album: albumSet.getChildren(true, false)) {
					MediaDescriptionCompat description = new MediaDescriptionCompat.Builder()
							.setTitle(album.getAlbumDisplay())
							.setSubtitle(album.getArtist())
							.setMediaId(MUSIC_DIRECTORY_PREFIX + album.getId())
							.build();

					mediaItems.add(new MediaBrowserCompat.MediaItem(description, MediaBrowserCompat.MediaItem.FLAG_BROWSABLE));
				}

				result.sendResult(mediaItems);
			}
		}.execute();

		result.detach();
	}

	private void getLibrary(final Result<List<MediaBrowserCompat.MediaItem>> result) {
		new SilentServiceTask<List<MusicFolder>>(downloadService) {
			@Override
			protected List<MusicFolder> doInBackground(MusicService musicService) throws Throwable {
				return musicService.getMusicFolders(false, downloadService, null);
			}

			@Override
			protected void done(List<MusicFolder> folders) {
				List<MediaBrowserCompat.MediaItem> mediaItems = new ArrayList<>();

				for(MusicFolder folder: folders) {
					MediaDescriptionCompat description = new MediaDescriptionCompat.Builder()
							.setTitle(folder.getName())
							.setMediaId(MUSIC_FOLDER_PREFIX + folder.getId())
							.build();

					mediaItems.add(new MediaBrowserCompat.MediaItem(description, MediaBrowserCompat.MediaItem.FLAG_BROWSABLE));
				}

				result.sendResult(mediaItems);
			}
		}.execute();

		result.detach();
	}
	private void getIndexes(final Result<List<MediaBrowserCompat.MediaItem>> result, final String musicFolderId) {
		new SilentServiceTask<Indexes>(downloadService) {
			@Override
			protected Indexes doInBackground(MusicService musicService) throws Throwable {
				return musicService.getIndexes(musicFolderId, false, downloadService, null);
			}

			@Override
			protected void done(Indexes indexes) {
				List<MediaBrowserCompat.MediaItem> mediaItems = new ArrayList<>();

				// music directories
				for(Artist artist : indexes.getArtists()) {
					MediaDescriptionCompat description = new MediaDescriptionCompat.Builder()
							.setTitle(artist.getName())
							.setMediaId(MUSIC_DIRECTORY_CONTENTS_PREFIX + artist.getId())
							.build();

					mediaItems.add(new MediaBrowserCompat.MediaItem(description, MediaBrowserCompat.MediaItem.FLAG_BROWSABLE));
				}

				// music files
				for(Entry entry: indexes.getEntries()) {
					try {
						entry.setBookmark(null);    // don't resume from a bookmark in a browse listing
						Bundle extras = new Bundle();
						extras.putByteArray(Constants.INTENT_EXTRA_ENTRY_BYTES, entry.toByteArray());
						extras.putString(Constants.INTENT_EXTRA_NAME_CHILD_ID, entry.getId());

						MediaDescriptionCompat description = new MediaDescriptionCompat.Builder()
								.setTitle(entry.getTitle())
								.setMediaId(entry.getId())
								.setExtras(extras)
								.build();

						mediaItems.add(new MediaBrowserCompat.MediaItem(description, MediaBrowserCompat.MediaItem.FLAG_PLAYABLE));
					} catch(IOException e) {
						Log.e(TAG, "Failed to add entry", e);
					}
				}

				result.sendResult(mediaItems);
			}
		}.execute();

		result.detach();
	}

	private void getMusicDirectory(final Result<List<MediaBrowserCompat.MediaItem>> result, final String musicDirectoryId) {
		new SilentServiceTask<MusicDirectory>(downloadService) {
			@Override
			protected MusicDirectory doInBackground(MusicService musicService) throws Throwable {
				return musicService.getMusicDirectory(musicDirectoryId, "", false, downloadService, null);
			}

			@Override
			protected void done(MusicDirectory directory) {
				List<MediaBrowserCompat.MediaItem> mediaItems = new ArrayList<>();

				addPlayOptions(mediaItems, musicDirectoryId, Constants.INTENT_EXTRA_NAME_ID);

				for(Entry entry : directory.getChildren()) {
					MediaDescriptionCompat description;
					if (entry.isDirectory()) {
						// browse deeper
						description = new MediaDescriptionCompat.Builder()
								.setTitle(entry.getTitle())
								.setMediaId(MUSIC_DIRECTORY_CONTENTS_PREFIX + entry.getId())
								.build();

						mediaItems.add(new MediaBrowserCompat.MediaItem(description, MediaBrowserCompat.MediaItem.FLAG_BROWSABLE));
					} else {
						try {
							// mark individual songs as directly playable
							entry.setBookmark(null);    // don't resume from a bookmark in a browse listing
							Bundle extras = new Bundle();
							extras.putByteArray(Constants.INTENT_EXTRA_ENTRY_BYTES, entry.toByteArray());
							extras.putString(Constants.INTENT_EXTRA_NAME_CHILD_ID, entry.getId());

							description = new MediaDescriptionCompat.Builder()
									.setTitle(entry.getTitle())
									.setMediaId(entry.getId())
									.setExtras(extras)
									.build();

							mediaItems.add(new MediaBrowserCompat.MediaItem(description, MediaBrowserCompat.MediaItem.FLAG_PLAYABLE));
						} catch (IOException e) {
							Log.e(TAG, "Failed to add entry", e);
						}
					}
				}
				result.sendResult(mediaItems);
			}
		}.execute();

		result.detach();
	}

	private void getArtists(final Result<List<MediaBrowserCompat.MediaItem>> result) {
		new SilentServiceTask<Indexes>(downloadService) {
			@Override
			protected Indexes doInBackground(MusicService musicService) throws Throwable {
				return musicService.getIndexes(null, false, downloadService, null);
			}

			@Override
			protected void done(Indexes indexes) {
				List<MediaBrowserCompat.MediaItem> mediaItems = new ArrayList<>();

				// music directories
				for(Artist artist : indexes.getArtists()) {
					MediaDescriptionCompat description = new MediaDescriptionCompat.Builder()
							.setTitle(artist.getName())
							.setMediaId(ARTIST_CONTENTS_PREFIX + artist.getId())
							.build();

					mediaItems.add(new MediaBrowserCompat.MediaItem(description, MediaBrowserCompat.MediaItem.FLAG_BROWSABLE));
				}

				result.sendResult(mediaItems);
			}
		}.execute();

		result.detach();
	}

	private void getAlbum(final Result<List<MediaBrowserCompat.MediaItem>> result, final String musicFolderId) {
		new SilentServiceTask<MusicDirectory>(downloadService) {
			@Override
			protected MusicDirectory doInBackground(MusicService musicService) throws Throwable {
				return musicService.getAlbum(musicFolderId, "", false, downloadService, null);
			}

			@Override
			protected void done(MusicDirectory songs) {
				List<MediaBrowserCompat.MediaItem> mediaItems = new ArrayList<>();

				addPlayOptions(mediaItems, musicFolderId, Constants.INTENT_EXTRA_NAME_ID);

				// songs
				for(Entry entry : songs.getSongs()) {
					try {
						Bundle extras = new Bundle();
						extras.putByteArray(Constants.INTENT_EXTRA_ENTRY_BYTES, entry.toByteArray());
						extras.putString(Constants.INTENT_EXTRA_NAME_CHILD_ID, entry.getId());

						MediaDescriptionCompat description = new MediaDescriptionCompat.Builder()
								.setTitle(entry.getTitle())
								.setMediaId(entry.getId())
								.setExtras(extras)
								.build();

						mediaItems.add(new MediaBrowserCompat.MediaItem(description, MediaBrowserCompat.MediaItem.FLAG_PLAYABLE));
					} catch(IOException e) {
						Log.e(TAG, "Failed to add entry", e);
					}
				}

				result.sendResult(mediaItems);
			}
		}.execute();

		result.detach();
	}

	private void getArtist(final Result<List<MediaBrowserCompat.MediaItem>> result, final String musicDirectoryId) {
		new SilentServiceTask<MusicDirectory>(downloadService) {
			@Override
			protected MusicDirectory doInBackground(MusicService musicService) throws Throwable {
				return musicService.getArtist(musicDirectoryId, "", false, downloadService, null);
			}

			@Override
			protected void done(MusicDirectory directory) {
				List<MediaBrowserCompat.MediaItem> mediaItems = new ArrayList<>();

				for(Entry entry : directory.getChildren()) {
					MediaDescriptionCompat description;
					if (entry.isDirectory()) {
						// browse deeper
						description = new MediaDescriptionCompat.Builder()
								.setTitle(entry.getTitle())
								.setMediaId(ALBUM_CONTENTS_PREFIX + entry.getId())
								.build();

						mediaItems.add(new MediaBrowserCompat.MediaItem(description, MediaBrowserCompat.MediaItem.FLAG_BROWSABLE));
					} else {
						try {
							// mark individual songs as directly playable
							Bundle extras = new Bundle();
							extras.putByteArray(Constants.INTENT_EXTRA_ENTRY_BYTES, entry.toByteArray());
							extras.putString(Constants.INTENT_EXTRA_NAME_CHILD_ID, entry.getId());

							description = new MediaDescriptionCompat.Builder()
									.setTitle(entry.getTitle())
									.setMediaId(entry.getId())
									.setExtras(extras)
									.build();

							mediaItems.add(new MediaBrowserCompat.MediaItem(description, MediaBrowserCompat.MediaItem.FLAG_PLAYABLE));
						} catch (IOException e) {
							Log.e(TAG, "Failed to add entry", e);
						}
					}
				}
				result.sendResult(mediaItems);
			}
		}.execute();

		result.detach();
	}

	private void getPlaylists(final Result<List<MediaBrowserCompat.MediaItem>> result) {
		new SilentServiceTask<List<Playlist>>(downloadService) {
			@Override
			protected List<Playlist> doInBackground(MusicService musicService) throws Throwable {
				return musicService.getPlaylists(false, downloadService, null);
			}

			@Override
			protected void done(List<Playlist> playlists) {
				List<MediaBrowserCompat.MediaItem> mediaItems = new ArrayList<>();

				for(Playlist playlist: playlists) {
					MediaDescriptionCompat description = new MediaDescriptionCompat.Builder()
							.setTitle(playlist.getName())
							.setMediaId(PLAYLIST_PREFIX + playlist.getId())
							.build();

					mediaItems.add(new MediaBrowserCompat.MediaItem(description, MediaBrowserCompat.MediaItem.FLAG_BROWSABLE));
				}

				result.sendResult(mediaItems);
			}
		}.execute();

		result.detach();
	}

	private void getPodcasts(final Result<List<MediaBrowserCompat.MediaItem>> result) {
		new SilentServiceTask<List<PodcastChannel>>(downloadService) {
			@Override
			protected List<PodcastChannel> doInBackground(MusicService musicService) throws Throwable {
				return musicService.getPodcastChannels(false, downloadService, null);
			}

			@Override
			protected void done(List<PodcastChannel> podcasts) {
				List<MediaBrowserCompat.MediaItem> mediaItems = new ArrayList<>();

				for(PodcastChannel podcast: podcasts) {
					MediaDescriptionCompat description = new MediaDescriptionCompat.Builder()
							.setTitle(podcast.getName())
							.setMediaId(PODCAST_PREFIX + podcast.getId())
							.build();

					mediaItems.add(new MediaBrowserCompat.MediaItem(description, MediaBrowserCompat.MediaItem.FLAG_BROWSABLE));
				}

				result.sendResult(mediaItems);
			}
		}.execute();

		result.detach();
	}
	private void getPodcastEpisodes(final Result<List<MediaBrowserCompat.MediaItem>> result, final String podcastId) {
		new SilentServiceTask<MusicDirectory>(downloadService) {
			@Override
			protected MusicDirectory doInBackground(MusicService musicService) throws Throwable {
				return musicService.getPodcastEpisodes(false, podcastId, downloadService, null);
			}

			@Override
			protected void done(MusicDirectory podcasts) {
				List<MediaBrowserCompat.MediaItem> mediaItems = new ArrayList<>();

				for(Entry entry: podcasts.getChildren(false, true)) {
					try {
						PodcastEpisode podcast = (PodcastEpisode) entry;
						Bundle podcastExtras = new Bundle();
						podcastExtras.putByteArray(Constants.INTENT_EXTRA_ENTRY_BYTES, podcast.toByteArray());
						podcastExtras.putString(Constants.INTENT_EXTRA_NAME_PODCAST_ID, podcast.getId());

						MediaDescriptionCompat description = new MediaDescriptionCompat.Builder()
								.setTitle(podcast.getTitle())
								.setSubtitle(Util.formatDate(downloadService, podcast.getDate(), false))
								.setMediaId(PODCAST_PREFIX + podcast.getId())
								.setExtras(podcastExtras)
								.build();

						mediaItems.add(new MediaBrowserCompat.MediaItem(description, MediaBrowserCompat.MediaItem.FLAG_PLAYABLE));
					} catch(IOException e) {
						Log.e(TAG, "Failed to add podcast", e);
					}
				}

				result.sendResult(mediaItems);
			}
		}.execute();

		result.detach();
	}

	private void getBookmarks(final Result<List<MediaBrowserCompat.MediaItem>> result) {
		new SilentServiceTask<MusicDirectory>(downloadService) {
			@Override
			protected MusicDirectory doInBackground(MusicService musicService) throws Throwable {
				return musicService.getBookmarks(false, downloadService, null);
			}

			@Override
			protected void done(MusicDirectory bookmarkList) {
				List<MediaBrowserCompat.MediaItem> mediaItems = new ArrayList<>();

				for(Entry entry: bookmarkList.getChildren(false, true)) {
					try {
						Bundle extras = new Bundle();
						extras.putByteArray(Constants.INTENT_EXTRA_ENTRY_BYTES, entry.toByteArray());
						extras.putString(Constants.INTENT_EXTRA_NAME_CHILD_ID, entry.getId());

						MediaDescriptionCompat description = new MediaDescriptionCompat.Builder()
								.setTitle(entry.getTitle())
								.setSubtitle(Util.formatDuration(entry.getBookmark().getPosition() / 1000))
								.setMediaId(entry.getId())
								.setExtras(extras)
								.build();

						mediaItems.add(new MediaBrowserCompat.MediaItem(description, MediaBrowserCompat.MediaItem.FLAG_PLAYABLE));
					} catch(IOException e) {
						Log.e(TAG, "Failed to add entry", e);
					}
				}

				result.sendResult(mediaItems);
			}
		}.execute();

		result.detach();
	}

	private void addPlayOptions(List<MediaBrowserCompat.MediaItem> mediaItems, String id, String idConstant) {
		Bundle playAllExtras = new Bundle();
		playAllExtras.putString(idConstant, id);

		MediaDescriptionCompat.Builder playAll = new MediaDescriptionCompat.Builder();
		playAll.setTitle(downloadService.getString(R.string.menu_play))
				.setMediaId("play-" + id)
				.setExtras(playAllExtras);
		mediaItems.add(new MediaBrowserCompat.MediaItem(playAll.build(), MediaBrowserCompat.MediaItem.FLAG_PLAYABLE));

		Bundle shuffleExtras = new Bundle();
		shuffleExtras.putString(idConstant, id);
		shuffleExtras.putBoolean(Constants.INTENT_EXTRA_NAME_SHUFFLE, true);

		MediaDescriptionCompat.Builder shuffle = new MediaDescriptionCompat.Builder();
		shuffle.setTitle(downloadService.getString(R.string.menu_shuffle))
				.setMediaId("shuffle-" + id)
				.setExtras(shuffleExtras);
		mediaItems.add(new MediaBrowserCompat.MediaItem(shuffle.build(), MediaBrowserCompat.MediaItem.FLAG_PLAYABLE));

		Bundle playLastExtras = new Bundle();
		playLastExtras.putString(idConstant, id);
		playLastExtras.putBoolean(Constants.INTENT_EXTRA_PLAY_LAST, true);

		MediaDescriptionCompat.Builder playLast = new MediaDescriptionCompat.Builder();
		playLast.setTitle(downloadService.getString(R.string.menu_play_last))
				.setMediaId("playLast-" + id)
				.setExtras(playLastExtras);
		mediaItems.add(new MediaBrowserCompat.MediaItem(playLast.build(), MediaBrowserCompat.MediaItem.FLAG_PLAYABLE));
	}

	private void getPlayOptions(Result<List<MediaBrowserCompat.MediaItem>> result, String id, String idConstant) {
		List<MediaBrowserCompat.MediaItem> mediaItems = new ArrayList<>();

		addPlayOptions(mediaItems, id, idConstant);

		result.sendResult(mediaItems);
	}

	public void getDownloadService() {
		if(DownloadService.getInstance() == null) {
			DownloadService.startService(this);
		}

		waitForDownloadService();
	}
	public void waitForDownloadService() {
		downloadService = DownloadService.getInstance();
		if(downloadService == null) {
			handler.postDelayed(new Runnable() {
				@Override
				public void run() {
					waitForDownloadService();
				}
			}, 100);
		} else {
			RemoteControlClientLP remoteControlClient = (RemoteControlClientLP) downloadService.getRemoteControlClient();
			setSessionToken(remoteControlClient.getMediaSession().getSessionToken());
		}
	}
}