forked from maxmpz/powerampapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExampleProvider.java
1481 lines (1244 loc) · 70 KB
/
ExampleProvider.java
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
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package com.maxmpz.powerampproviderexample;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.graphics.Point;
import android.media.MediaFormat;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.CancellationSignal;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.ParcelFileDescriptor;
import android.os.ProxyFileDescriptorCallback;
import android.os.storage.StorageManager;
import android.provider.DocumentsContract;
import android.provider.DocumentsProvider;
import android.provider.MediaStore;
import android.system.ErrnoException;
import android.system.Os;
import android.system.OsConstants;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import com.maxmpz.poweramp.player.PowerampAPI;
import com.maxmpz.poweramp.player.PowerampAPI.Lyrics;
import com.maxmpz.poweramp.player.PowerampAPI.Track;
import com.maxmpz.poweramp.player.PowerampAPIHelper;
import com.maxmpz.poweramp.player.TrackProviderConsts;
import com.maxmpz.poweramp.player.TrackProviderHelper;
import com.maxmpz.poweramp.player.TrackProviderProto;
import java.io.Closeable;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.channels.FileChannel;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Objects;
import java.util.Set;
/**
* Example provider demonstrating:
* - providing tracks hierarchy for the Poweramp scanner
* - providing tracks via direct file descriptor and via seekable socket protocol
*/
public class ExampleProvider extends DocumentsProvider {
private static final String TAG = "ExampleProvider";
private static final boolean LOG = true;
/**
* If true, we're copying our mp3s from assets to local filesystem. This is because Poweramp can't reliable play assets fd - fd pointing to apk file itself, mostly as
* end of file can't be properly detected for many formats without proper length header
*/
private static final boolean USE_MP3_COPY = true;
/**
* If true, use {@link android.os.storage.StorageManager#openProxyFileDescriptor} on Android 8+ which provides seekable file descriptors implemented by Android Framework.<br>
* It's much easier to implement vs Seekable sockets protocol and recommended for apps targeting Android 8+
*/
private static final boolean USE_OPEN_PROXY_FILE_DESCRIPTOR = true;
/** Link to mp3 track to demonstrate http track with the duration */
private static final String DUBSTEP_HTTP_URL = "https://raw.githubusercontent.com/maxmpz/powerampapi/master/poweramp_provider_example/app/src/main/assets/bensound-dubstep.mp3";
/** Link to mp3 track to demonstrate http track with the duration */
private static final String SUMMER_HTTP_URL = "https://raw.githubusercontent.com/maxmpz/powerampapi/master/poweramp_provider_example/app/src/main/assets/bensound-summer.mp3";
/** Link to flac track to demonstrate http track with the duration */
private static final String DUBSTEP_FLAC_HTTP_URL = "https://raw.githubusercontent.com/maxmpz/powerampapi/master/poweramp_provider_example/app/src/main/assets/bensound-dubstep.flac";
/** Docid suffix for static url tracks */
private static final String DOCID_STATIC_URL_SUFFIX = ".url";
/** Docid suffix for dynamic url tracks */
private static final String DOCID_DYNAMIC_URL_SUFFIX = ".dynamicurl";
private static final long DUBSTEP_SIZE = 2044859L;
private static final long DUBSTEP_DURATION_MS = 125000L;
private static final long SUMMER_SIZE = 4620151L;
private static final long SUMMER_DURATION_MS = 217000L;
private static final long DUBSTEP_FAKE_FLAC_SIZE = 14000000;
// ~116 bytes per ms in the real bensound-dubstep.flac, but "fake" value is an approximation
private static final long DUBSTEP_FAKE_AVERAGE_BYTES_PER_MS = 112;
/** If > 0, we'll force-stop the playback after these bytes played. Works for seekable sockets/PA protocol */
private static final long DEBUG_STOP_PROTOCOL_AFTER_BYTES = 0; // e.g. = 500000
/** If true, we'll force-stop playback immediately in open. Works for seekable sockets/PA protocol */
private static final boolean DEBUG_ALWAYS_STOP_IN_OPEN = false;
/** If true, we'll force-stop playback immediately in thread. Works for seekable sockets/PA protocol */
private static final boolean DEBUG_ALWAYS_STOP_PROTOCOL = false;
/** If true, we'll force-stop playback after the header. Works for seekable sockets/PA protocol */
private static final boolean DEBUG_STOP_PROTOCOL_AFTER_HEADER = false;
/** Default columns returned for roots */
private static final String[] DEFAULT_ROOT_PROJECTION = {
DocumentsContract.Root.COLUMN_ROOT_ID,
DocumentsContract.Root.COLUMN_TITLE,
DocumentsContract.Root.COLUMN_SUMMARY,
DocumentsContract.Root.COLUMN_FLAGS,
DocumentsContract.Root.COLUMN_ICON,
DocumentsContract.Root.COLUMN_DOCUMENT_ID,
};
/** Default columns returned for documents - folders and tracks (not including metadata) */
private static final String[] DEFAULT_DOCUMENT_PROJECTION = {
DocumentsContract.Document.COLUMN_DOCUMENT_ID,
DocumentsContract.Document.COLUMN_MIME_TYPE,
DocumentsContract.Document.COLUMN_DISPLAY_NAME,
DocumentsContract.Document.COLUMN_LAST_MODIFIED,
DocumentsContract.Document.COLUMN_FLAGS,
DocumentsContract.Document.COLUMN_SIZE,
};
/**
* Default columns returned for tracks, including metadata.<br>
* NOTE: where possible, we're using standard MediaStore or MediaFormat column names<br><br>
*
* NOTE: if TITLE and DURATION columns are missing, empty, or null, Poweramp assumes cursor doesn't contain metadata and doesn't check other metadata columns.<br>
* Instead of relying on the cursor metadata, Poweramp will try to open the file via openDocument and will scan tags in it.
* If TITLE or DURATION exists in cursor, Poweramp won't attempt to read any tags directly from the track, using what is given in cursor.
* NOTE: if TITLE and DURATION are missing, Poweramp also won't use thumbnail API (even if FLAG_SUPPORTS_THUMBNAIL is set). Instead Poweramp reads cover directly
* from track (via another openDocument call)
*/
private static final String[] DEFAULT_TRACK_AND_METADATA_PROJECTION = {
DocumentsContract.Document.COLUMN_DOCUMENT_ID,
DocumentsContract.Document.COLUMN_MIME_TYPE,
DocumentsContract.Document.COLUMN_DISPLAY_NAME,
DocumentsContract.Document.COLUMN_LAST_MODIFIED,
DocumentsContract.Document.COLUMN_FLAGS,
DocumentsContract.Document.COLUMN_SIZE,
MediaStore.MediaColumns.TITLE,
MediaStore.MediaColumns.DURATION,
MediaStore.MediaColumns.ARTIST,
MediaStore.MediaColumns.ALBUM,
MediaStore.Audio.AudioColumns.YEAR,
TrackProviderConsts.COLUMN_ALBUM_ARTIST,
MediaStore.MediaColumns.COMPOSER,
TrackProviderConsts.COLUMN_GENRE,
MediaStore.Audio.AudioColumns.TRACK,
TrackProviderConsts.COLUMN_TRACK_ALT,
MediaFormat.KEY_SAMPLE_RATE,
MediaFormat.KEY_CHANNEL_COUNT,
MediaFormat.KEY_BIT_RATE,
TrackProviderConsts.COLUMN_BITS_PER_SAMPLE
};
private long mApkInstallTime;
@Override
public boolean onCreate() {
// Code to retrieve own apk install time. We use this here as lastModified. Not needed for real providers which can retrieve lastModified from the
// content itself (either from network or filesystem)
final PackageManager pm = this.getContext().getPackageManager();
try {
final PackageInfo pakInfo = pm.getPackageInfo(this.getContext().getApplicationInfo().packageName, 0);
this.mApkInstallTime = 0 < pakInfo.lastUpdateTime ? pakInfo.lastUpdateTime : pakInfo.firstInstallTime;
if(ExampleProvider.LOG) Log.w(ExampleProvider.TAG, "onCreate mApkInstallTime=" + this.mApkInstallTime);
} catch(final PackageManager.NameNotFoundException ex) {
Log.e(ExampleProvider.TAG, "", ex);
this.mApkInstallTime = System.currentTimeMillis();
}
if(ExampleProvider.USE_MP3_COPY) {
// Extract our mp3s to storage as Poweramp won't properly play asset apk fd (fd points to apk itself, so Poweramp tries to play the apk file itself,
// basically playing first found mp3 from it)
final File dir = this.getContext().getFilesDir();
this.copyAsset("bensound-dubstep.mp3", dir, false);
this.copyAsset("bensound-dubstep.flac", dir, false);
this.copyAsset("bensound-summer.mp3", dir, false);
this.copyAsset("streams-playlist.m3u8", dir, false);
}
return true;
}
@Override
public Cursor queryRoots(final String[] projection) throws FileNotFoundException {
if(ExampleProvider.LOG) Log.w(ExampleProvider.TAG, "queryRoots projection=" + Arrays.toString(projection));
try {
final MatrixCursor c = new MatrixCursor(ExampleProvider.resolveRootProjection(projection));
MatrixCursor.RowBuilder row;
// Items without metadata provided by the provider (Poweramp reads track metadata from track itself)
row = c.newRow();
row.add(DocumentsContract.Root.COLUMN_ROOT_ID, "rootId1");
row.add(DocumentsContract.Root.COLUMN_TITLE, "Root 1");
row.add(DocumentsContract.Root.COLUMN_SUMMARY, "Poweramp Example Provider");
row.add(DocumentsContract.Root.COLUMN_FLAGS, DocumentsContract.Root.FLAG_SUPPORTS_IS_CHILD); // Required
row.add(DocumentsContract.Root.COLUMN_ICON, R.mipmap.ic_launcher);
row.add(DocumentsContract.Root.COLUMN_DOCUMENT_ID, "root1");
// Items with metadata (Poweramp gets metadata from cursor and doesn't try to read tags from tracks)
row = c.newRow();
row.add(DocumentsContract.Root.COLUMN_ROOT_ID, "rootId2");
row.add(DocumentsContract.Root.COLUMN_TITLE, "Root 2");
row.add(DocumentsContract.Root.COLUMN_SUMMARY, "Poweramp Example Provider");
row.add(DocumentsContract.Root.COLUMN_FLAGS, DocumentsContract.Root.FLAG_SUPPORTS_IS_CHILD); // Required
row.add(DocumentsContract.Root.COLUMN_ICON, R.mipmap.ic_launcher);
row.add(DocumentsContract.Root.COLUMN_DOCUMENT_ID, "root2");
// Streams: m3u8 playlist, http stream with the duration, http no-duration stream (radio)
row = c.newRow();
row.add(DocumentsContract.Root.COLUMN_ROOT_ID, "rootId3");
row.add(DocumentsContract.Root.COLUMN_TITLE, "Root 3 (Streams)");
row.add(DocumentsContract.Root.COLUMN_SUMMARY, "Poweramp Example Provider");
row.add(DocumentsContract.Root.COLUMN_FLAGS, DocumentsContract.Root.FLAG_SUPPORTS_IS_CHILD); // Required
row.add(DocumentsContract.Root.COLUMN_ICON, R.mipmap.ic_launcher);
row.add(DocumentsContract.Root.COLUMN_DOCUMENT_ID, "root3");
return c;
} catch(final Throwable th) {
Log.e(ExampleProvider.TAG, "", th);
}
return null;
}
/**
* Query document is used:<br>
* - by Android picker to show appropriate directory and tracks<br>
* - by Poweramp to retrieve track metadata during the library scan<br>
* - by Poweramp to retrieve track metadata for Info/Tags/Lyrics UI<br>
* - in this case Poweramp will ask for extra fields, such as lyrics/synced lyrics<br>
*/
@Override
public Cursor queryDocument(final String documentId, final String[] projection) throws FileNotFoundException {
if(ExampleProvider.LOG) Log.w(ExampleProvider.TAG, "queryDocument documentId=" + documentId + " projection=" + Arrays.toString(projection));
try {
// NOTE: using simplified root/folder/file detection here. Real provider should base this on actual hierarchy, either retrieved from network
// or from filesystem
// If this is root, just return static root data
if(!documentId.contains("/") && documentId.startsWith("root")) {
MatrixCursor c = new MatrixCursor(ExampleProvider.resolveDocumentProjection(projection));
final MatrixCursor.RowBuilder row = c.newRow();
final AssetManager assets = this.getContext().getResources().getAssets();
this.fillFolderRow(documentId, row, this.hasSubDirs(assets, documentId) ? TrackProviderConsts.FLAG_HAS_SUBDIRS : TrackProviderConsts.FLAG_NO_SUBDIRS);
// NOTE: we return display name derived from documentId here VS returning the same label as used for Root.COLUMN_TITLE
// Real app should use same labels in both places (roots and queryDocument) for same root
row.add(DocumentsContract.Document.COLUMN_DISPLAY_NAME, this.capitalize(documentId));
return c;
} else if(documentId.startsWith("root3") && documentId.endsWith(ExampleProvider.DOCID_STATIC_URL_SUFFIX)) {
// Url mp3 with a duration. We must provide duration here to avoid endless/non-seekable stream
MatrixCursor c = new MatrixCursor(ExampleProvider.resolveTrackProjection(projection));
final int trackNum = ExampleProvider.extractTrackNum(documentId);
if(documentId.contains("dubstep")) {
this.fillURLRow(documentId, c.newRow(), ExampleProvider.DUBSTEP_HTTP_URL, ExampleProvider.DUBSTEP_SIZE, "Dubstep", 1 == trackNum ? 0 : ExampleProvider.DUBSTEP_DURATION_MS, true, true, true); // Send wave
} else {
final boolean emptyWave = 4 > trackNum; // 1..4 summer tracks with empty wave, for the others - allow Poweramp to scan them
this.fillURLRow(documentId, c.newRow(), ExampleProvider.SUMMER_HTTP_URL, ExampleProvider.SUMMER_SIZE, "Summer", 1 == trackNum ? 0 : ExampleProvider.SUMMER_DURATION_MS, true, false, emptyWave);
}
return c;
} else if(documentId.startsWith("root3") && documentId.endsWith(ExampleProvider.DOCID_DYNAMIC_URL_SUFFIX)) {
// Dynamic url to mp3 with a duration. We must provide duration here to avoid endless/non-seekable stream
// NOTE: we use TrackProviderConsts.DYNAMIC_URL as URL here to indicate dynamic url track
MatrixCursor c = new MatrixCursor(ExampleProvider.resolveTrackProjection(projection));
final int trackNum = ExampleProvider.extractTrackNum(documentId);
if(documentId.contains("dubstep")) {
this.fillURLRow(documentId, c.newRow(), TrackProviderConsts.DYNAMIC_URL, ExampleProvider.DUBSTEP_SIZE, "Dubstep", ExampleProvider.DUBSTEP_DURATION_MS, true, true, true); // Send wave
} else {
final boolean emptyWave = 4 > trackNum; // 1..4 summer tracks with empty wave, for the others - allow Poweramp to scan them
this.fillURLRow(documentId, c.newRow(), TrackProviderConsts.DYNAMIC_URL, ExampleProvider.SUMMER_SIZE, "Summer", ExampleProvider.SUMMER_DURATION_MS, true, false, emptyWave);
}
return c;
} else if(documentId.endsWith(".mp3") || documentId.endsWith(".flac")) { // Seems like a track
// We are adding metadata for root2 and check if it's actually requested as a small optimization (which can be big if track metadata retrieval requires additional processing)
final boolean addMetadata = documentId.startsWith("root2/") && null != projection && this.arrayContains(projection, MediaStore.MediaColumns.TITLE);
MatrixCursor c = new MatrixCursor(ExampleProvider.resolveTrackProjection(projection));
final boolean addLyrics = null != projection
&& (this.arrayContains(projection, TrackProviderConsts.COLUMN_TRACK_LYRICS)
|| this.arrayContains(projection, TrackProviderConsts.COLUMN_TRACK_LYRICS_SYNCED));
final boolean sendWave = documentId.contains("dubstep") && null != projection
&& this.arrayContains(projection, TrackProviderConsts.COLUMN_TRACK_WAVE);
this.fillTrackRow(
documentId,
c.newRow(),
addMetadata,
sendWave, // Adding wave as well to root2 tracks
addLyrics,
ExampleProvider.extractTrackNum(documentId),
0,
TrackProviderConsts.FLAG_HAS_LYRICS // Set lyrics flag for all of these tracks
);
return c;
} else if(documentId.endsWith(".m3u")) { // Seems like a playlist
MatrixCursor c = new MatrixCursor(ExampleProvider.resolveDocumentProjection(projection));
this.fillPlaylistRow(documentId, c.newRow());
return c;
} else { // This must be a directory
MatrixCursor c = new MatrixCursor(ExampleProvider.resolveDocumentProjection(projection));
final AssetManager assets = this.getContext().getResources().getAssets();
this.fillFolderRow(documentId, c.newRow(), this.hasSubDirs(assets, documentId) ? TrackProviderConsts.FLAG_HAS_SUBDIRS : TrackProviderConsts.FLAG_NO_SUBDIRS);
return c;
}
} catch(final Throwable th) {
Log.e(ExampleProvider.TAG, "documentId=" + documentId, th);
}
return null;
}
private void fillURLRow(@NonNull final String documentId, @NonNull final MatrixCursor.RowBuilder row, @NonNull final String url,
final long size, @NonNull String title,
final long duration, final boolean sendMetadata, final boolean sendWave, final boolean sendEmptyWave
) {
row.add(DocumentsContract.Document.COLUMN_DOCUMENT_ID, documentId);
row.add(DocumentsContract.Document.COLUMN_MIME_TYPE, "audio/mpeg");
// The display name defines name of the track "file" in "Show File Names" mode. There is also a title via MediaStore.MediaColumns.TITLE.
// It's up to you how you define display name, it can be anything filename alike, or it can just match track title
row.add(DocumentsContract.Document.COLUMN_DISPLAY_NAME, ExampleProvider.getShortName(documentId));
// As our assets data is always static, we just return own apk installation time. For real folder structure, preferable last modified for given folder should be returned.
// This ensures Poweramp incremental scanning process. If we return <= 0 value here, Poweramp will be forced to rescan whole provider hierarchy each time it scans
row.add(DocumentsContract.Document.COLUMN_LAST_MODIFIED, this.mApkInstallTime);
// Optional, real provider should preferable return real track file size here or 0
row.add(DocumentsContract.Document.COLUMN_SIZE, size);
// Setting this will cause Poweramp to ask for the track album art via getDocumentThumbnail, but only if other metadata (MediaStore.MediaColumns.TITLE/MediaStore.MediaColumns.DURATION) exists
row.add(DocumentsContract.Document.COLUMN_FLAGS, DocumentsContract.Document.FLAG_SUPPORTS_THUMBNAIL);
row.add(TrackProviderConsts.COLUMN_URL, url);
row.add(MediaStore.MediaColumns.DURATION, duration); // Milliseconds, long. If duration <= 0, this is endless non-seekable stream (e.g. radio)
if(sendMetadata) { // NOTE: Poweramp doesn't need extra metadata (except COLUMN_URL/DURATION for streams) for queryDocuments, but requires that for queryDocument
if(TrackProviderConsts.DYNAMIC_URL.equals(url)) {
title += " Dynamic";
}
final String prefix = title + " ";
// Some dump tags logic - as we have 2 static files here as an example, but they have docId like dubstep1.mp3, summer2.mp3, etc.
// Real provider should get this info from network or extract from the file
final int trackNum = ExampleProvider.extractTrackNum(documentId);
row.add(MediaStore.MediaColumns.TITLE, prefix + "URL Track " + trackNum);
row.add(MediaStore.MediaColumns.ARTIST, prefix + "URL Artist");
row.add(MediaStore.MediaColumns.ALBUM, prefix + "URL Album");
row.add(MediaStore.Audio.AudioColumns.YEAR, 2020); // Integer
row.add(TrackProviderConsts.COLUMN_ALBUM_ARTIST, prefix + "URL Album Artist");
row.add(MediaStore.MediaColumns.COMPOSER, prefix + "URL Composer");
row.add(TrackProviderConsts.COLUMN_GENRE, prefix + "URL Genre");
// Track number. Optional, but needed for proper sorting in albums
row.add(MediaStore.Audio.AudioColumns.TRACK, trackNum);
// Optional, used just for Info/Tags
row.add(MediaFormat.KEY_SAMPLE_RATE, 44100);
// Optional, used just for Info/Tags
row.add(MediaFormat.KEY_CHANNEL_COUNT, 2);
// Optional, used just for Info/Tags
row.add(MediaFormat.KEY_BIT_RATE, 128000);
// Optional, used just for Info/Tags and lists (for hi-res)
row.add(TrackProviderConsts.COLUMN_BITS_PER_SAMPLE, 16);
if(sendWave && 0 < duration) {
row.add(TrackProviderConsts.COLUMN_TRACK_WAVE, TrackProviderHelper.floatsToBytes(this.genRandomWave())); // We must put byte[] array here
} else if(sendEmptyWave) {
// Add this for the default waveseek if you don't want URL to be downloaded one more time and scanned for the wave
row.add(TrackProviderConsts.COLUMN_TRACK_WAVE, new byte[0]);
} // Else we allow Poweramp to scan URL for wave
}
}
private float[] genRandomWave() {
final float[] wave = new float[100];
for(int i = 0; i < wave.length; i++) {
wave[i] = (float)(Math.random() * 2.0 - 1.0);
}
return wave;
}
private void fillFolderRow(@NonNull final String documentId, @NonNull final MatrixCursor.RowBuilder row, final int flags) {
row.add(DocumentsContract.Document.COLUMN_DOCUMENT_ID, documentId);
row.add(DocumentsContract.Document.COLUMN_MIME_TYPE, DocumentsContract.Document.MIME_TYPE_DIR);
// Here we're returning actual folder name, but Poweramp supports anything in display name for folders, not necessary the name matching or related to the documentId or path.
row.add(DocumentsContract.Document.COLUMN_DISPLAY_NAME, ExampleProvider.getShortDirName(documentId));
row.add(DocumentsContract.Document.COLUMN_LAST_MODIFIED, this.mApkInstallTime);
final boolean hasThumb = documentId.endsWith("1");
if(hasThumb) {
row.add(DocumentsContract.Document.COLUMN_FLAGS, DocumentsContract.Document.FLAG_SUPPORTS_THUMBNAIL); // Thumbnails for folders are supported since build 869
}
// If asked to add the subfolders hint, add it
if(0 != flags) {
row.add(TrackProviderConsts.COLUMN_FLAGS, flags);
}
}
private void fillPlaylistRow(@NonNull final String documentId, @NonNull final MatrixCursor.RowBuilder row) {
// NOTE: for playlists, the playlist documentId should preferable end with some extension. Poweramp also looks into mime type, or assumes it's .m3u8 playlist if no mime type
row.add(DocumentsContract.Document.COLUMN_DOCUMENT_ID, documentId);
row.add(DocumentsContract.Document.COLUMN_MIME_TYPE, "audio/mpegurl");
// The display name defines name of the track "file" in "Show File Names" mode. There is also a title via MediaStore.MediaColumns.TITLE.
// It's up to you how you define display name, it can be anything filename alike, or it can just match track title
row.add(DocumentsContract.Document.COLUMN_DISPLAY_NAME, ExampleProvider.getShortName(documentId));
// As our assets data is always static, we just return own apk installation time. For real folder structure, preferable last modified for given folder should be returned.
// This ensures Poweramp incremental scanning process. If we return <= 0 value here, Poweramp will be forced to rescan whole provider hierarchy each time it scans
row.add(DocumentsContract.Document.COLUMN_LAST_MODIFIED, this.mApkInstallTime);
}
private void fillTrackRow(
@NonNull final String documentId,
@NonNull final MatrixCursor.RowBuilder row,
final boolean addMetadata,
final boolean sendWave,
final boolean sendLyrics,
final int trackNum,
final int trackNumAlt,
final int extraFlags
) {
final boolean isFlac = documentId.endsWith(".flac");
final boolean isDubstep = documentId.contains("dubstep");
row.add(DocumentsContract.Document.COLUMN_DOCUMENT_ID, documentId);
row.add(DocumentsContract.Document.COLUMN_MIME_TYPE, isFlac ? "audio/flac" : "audio/mpeg");
// The display name defines name of the track "file" in "Show File Names" mode. There is also a title via MediaStore.MediaColumns.TITLE.
// It's up to you how you define display name, it can be anything filename alike, or it can just match track title
row.add(DocumentsContract.Document.COLUMN_DISPLAY_NAME, ExampleProvider.getShortName(documentId));
// As our assets data is always static, we just return own apk installation time. For real folder structure, preferable last modified for given folder should be returned.
// This ensures Poweramp incremental scanning process. If we return <= 0 value here, Poweramp will be forced to rescan whole provider hierarchy each time it scans
row.add(DocumentsContract.Document.COLUMN_LAST_MODIFIED, this.mApkInstallTime);
final String filePath = this.docIdToFileName(documentId); // We only have 2 real mp3s here for many "virtual" tracks
// Optional, real provider should preferable return real track file size here or 0.
row.add(DocumentsContract.Document.COLUMN_SIZE, this.getAssetFileSize(this.getContext().getResources().getAssets(), filePath));
// NOTE: Poweramp doesn't need extra metadata (except COLUMN_URL/DURATION for streams) for queryDocuments,
// but requires that for queryDocument for tracks, which are not direct fd. Direct fd tracks still can be quickly scanned by Poweramp, but
// socket/pipe/url tracks won't be scanned and thus metadata is required for them
// If provided, COLUMN_TRACK_ALT will sort tracks differently (for "by track #" sorting) in Folders/Folders Hierarchy
if(0 < trackNumAlt) {
row.add(TrackProviderConsts.COLUMN_TRACK_ALT, trackNumAlt);
}
if(addMetadata) {
// Some dump tags logic - as we have 2 static files here as an example, but they have docId like dubstep1.mp3, summer2.mp3, etc.
// Real provider should get this info from network or extract from the file
final String prefix = isDubstep ? "Dubstep " : "Summer ";
int flags = 0;
// Setting this will cause Poweramp to ask for track album art via getDocumentThumbnail, but only if other metadata (MediaStore.MediaColumns.TITLE/MediaStore.MediaColumns.DURATION) exists
flags |= DocumentsContract.Document.FLAG_SUPPORTS_THUMBNAIL;
flags |= TrackProviderConsts.FLAG_HAS_LYRICS;
row.add(DocumentsContract.Document.COLUMN_FLAGS, flags);
row.add(MediaStore.MediaColumns.TITLE, prefix + "Track " + trackNum);
row.add(MediaStore.MediaColumns.ARTIST, prefix + "Artist");
row.add(MediaStore.MediaColumns.DURATION, isDubstep ? 125000L : 217000L); // Milliseconds, long
row.add(MediaStore.MediaColumns.ALBUM, prefix + "Album");
row.add(MediaStore.Audio.AudioColumns.YEAR, isDubstep ? 2020 : 2019); // Integer
row.add(TrackProviderConsts.COLUMN_ALBUM_ARTIST, prefix + "Album Artist");
row.add(MediaStore.MediaColumns.COMPOSER, prefix + " Composer");
row.add(TrackProviderConsts.COLUMN_GENRE, prefix + " Genre");
// Track number. Optional, but needed for proper sorting in albums.
// If not defined (or set to <= 0), Poweramp will use cursor position for track - this may be useful for folders where we want default cursor based ordering of items -
// exactly as provided by cursor. Just don't send MediaStore.Audio.AudioColumns.TRACK column for such tracks.
// NOTE: Poweramp won't scan track number from filename for provider provided tracks, nor it will cut number (e.g. "01-" from "01-trackname") from displayName
// as it does by default for normal filesystem tracks
row.add(MediaStore.Audio.AudioColumns.TRACK, trackNum);
// Optional, used just for Info/Tags
row.add(MediaFormat.KEY_SAMPLE_RATE, isDubstep ? 44100 : 48000);
// Optional, used just for Info/Tags
row.add(MediaFormat.KEY_CHANNEL_COUNT, 2);
// Optional, used just for Info/Tags
row.add(MediaFormat.KEY_BIT_RATE, isFlac ? 720000 : (isDubstep ? 128000 : 192000));
// Optional, used just for Info/Tags and lists (for hi-res)
row.add(TrackProviderConsts.COLUMN_BITS_PER_SAMPLE, 16);
if(sendWave) {
row.add(TrackProviderConsts.COLUMN_TRACK_WAVE, TrackProviderHelper.floatsToBytes(this.genRandomWave()));
}
// Add our own extra flags if any
if(0 != extraFlags) {
row.add(TrackProviderConsts.COLUMN_FLAGS, extraFlags);
}
if(sendLyrics) {
if(isDubstep) {
// For dubstep add LRC (synced) lyrics
row.add(
TrackProviderConsts.COLUMN_TRACK_LYRICS_SYNCED,
"[0:00.00]La la la\n[0:05.00]Synced Lyrics for track " + prefix + "Track " + trackNum + "\n" +
"[0:05.00]Line\n" +
"[0:10.00]Line\n" +
"[0:30.00]The last line\n"
);
} else {
row.add(TrackProviderConsts.COLUMN_TRACK_LYRICS, "La la la\nLyrics for track " + prefix + "Track " + trackNum);
}
}
}
}
/**
* @param sortOrder this field is not used directly as sorting order as Poweramp always use some user defined sorting which is
* based on track # or other user selected criteria. Instead, we use sortOrder as optional additional parameter for things like
*/
@Override
public Cursor queryChildDocuments(final String parentDocumentId, final String[] projection, final String sortOrder) throws FileNotFoundException {
if(ExampleProvider.LOG) Log.w(ExampleProvider.TAG, "queryChildDocuments parentDocumentId=" + parentDocumentId + " projection=" + Arrays.toString(projection));
try {
final AssetManager assets = this.getContext().getResources().getAssets();
final String[] filesAndDirs = assets.list(parentDocumentId);
// To demonstrate folders and files sorting based on cursor position, sort and reverse the array. Do this for Root1
if("root1".equals(parentDocumentId)) {
Arrays.sort(filesAndDirs, 0, filesAndDirs.length, new Comparator<String>() {
@Override
public int compare(final String o1, final String o2) {
return o2.compareToIgnoreCase(o1);
}
});
}
// We are adding metadata for root2 and check if it's actually requested as a small optimization (which can be big if track metadata retrieval requires additional processing)
final boolean addMetadata = parentDocumentId.startsWith("root2") && null != projection && this.arrayContains(projection, MediaStore.MediaColumns.TITLE);
if(ExampleProvider.LOG) Log.w(ExampleProvider.TAG, "queryChildDocuments filesAndDirs=" + Arrays.toString(filesAndDirs));
MatrixCursor c = new MatrixCursor(ExampleProvider.resolveDocumentProjection(projection));
final int ix = 0;
for(final String fileOrDir : filesAndDirs) {
final String path = parentDocumentId + "/" + fileOrDir; // Path is our documentId. Note that this provider defines paths/documentIds format. Poweramp treats them as opaque string
if(this.isAssetDir(assets, path)) {
this.fillFolderRow(path, c.newRow(), this.hasSubDirs(assets, path) ? TrackProviderConsts.FLAG_HAS_SUBDIRS : TrackProviderConsts.FLAG_NO_SUBDIRS);
} // Else this is empty.txt file, we skip it
}
// Generate some number of files for given folder
// NOTE: Poweramp scans each directly once for its normal scans, and never rescans them again, until:
// - parent folder lastModified changed
// - full rescan required by user or external intent
final int count = parentDocumentId.length(); // Just various number based on parent document path length
String docId;
if("root3".equals(parentDocumentId)) {
// For root3 add m3u8 playlist
this.fillPlaylistRow(parentDocumentId + "/" + "streams-playlist.m3u8", c.newRow());
// Add dynamic URL tracks
docId = parentDocumentId + "/" + "dubstep" + "-" + 1 + ExampleProvider.DOCID_DYNAMIC_URL_SUFFIX;
this.fillURLRow(docId, c.newRow(),
TrackProviderConsts.DYNAMIC_URL,
ExampleProvider.DUBSTEP_SIZE,
"", // NOTE: titles not sent here
ExampleProvider.DUBSTEP_DURATION_MS,
false, false, false); // Not sending metadata here
docId = parentDocumentId + "/" + "summer" + "-" + 2 + ExampleProvider.DOCID_DYNAMIC_URL_SUFFIX;
this.fillURLRow(docId, c.newRow(),
TrackProviderConsts.DYNAMIC_URL,
ExampleProvider.SUMMER_SIZE,
"", // NOTE: titles not sent here
ExampleProvider.SUMMER_DURATION_MS,
false, false, false); // Not sending metadata here
// And fill with random number of http links to the tracks
for(int i = 0; i < count; i++) {
final boolean isDubstep = 0 != (i & 1);
final boolean isStream = 0 == i; // First track here will be a "stream" - non seekable, no duration
docId = parentDocumentId + "/" + (isDubstep ? "dubstep" : "summer") + "-" + (i + 3) + ExampleProvider.DOCID_STATIC_URL_SUFFIX;
this.fillURLRow(docId, c.newRow(),
isDubstep ? ExampleProvider.DUBSTEP_HTTP_URL : ExampleProvider.SUMMER_HTTP_URL,
isDubstep ? ExampleProvider.DUBSTEP_SIZE : ExampleProvider.SUMMER_SIZE,
"", // NOTE: titles not sent here
isStream ? 0 : (isDubstep ? ExampleProvider.DUBSTEP_DURATION_MS : ExampleProvider.SUMMER_DURATION_MS),
false, false, false);
}
} else {
// For root1 and root2 generate docId like root1/Folder2/dubstep-10.mp3
// For root1, demonstrate Folders/Folders Hierarchy sorting based on alternative track number
// We reverse number positions of tracks here, but still providing non-reversed track number to use as tag number in albums and other non-folder categories
for(int i = 0; i < count; i++) {
docId = parentDocumentId + "/" + (0 != (i & 1) ? "dubstep" : "summer") + "-" + (i + 1) + (1 == i ? ".flac" : ".mp3"); // First dubstep track will be flac
final int sort = i + 1;
int sortAlt = 0;
if("root1".equals(parentDocumentId)) {
sortAlt = count - i;
}
this.fillTrackRow(docId, c.newRow(), addMetadata, false, false, sort, sortAlt, 0);
}
}
return c;
} catch(final Throwable th) {
Log.e(ExampleProvider.TAG, "documentId=" + parentDocumentId, th);
}
return null;
}
/**
* Simple method to check our assets directory structure for children folders. We have only folders and empty.txt files there
*/
private boolean hasSubDirs(final AssetManager assets, final String path) {
try {
final String[] filesAndDirs = assets.list(path);
if(null != filesAndDirs && 0 < filesAndDirs.length) {
for(final String child : filesAndDirs) {
if(!child.endsWith(".txt")) {
return true;
}
}
}
} catch(final IOException e) {
Log.e(ExampleProvider.TAG, path, e);
}
return false;
}
/** Send album art for tracks with track-provided metadata */
@Override
public AssetFileDescriptor openDocumentThumbnail(final String documentId, final Point sizeHint, final CancellationSignal signal) throws FileNotFoundException {
final String imageSrc;
if(documentId.endsWith(".mp3") || documentId.endsWith(".flac") || documentId.endsWith(ExampleProvider.DOCID_STATIC_URL_SUFFIX)
|| documentId.endsWith(ExampleProvider.DOCID_DYNAMIC_URL_SUFFIX)
) {
// We have just 2 images here and we ignore sizeHint. Poweramp preferred image size is 1024x1024px
final boolean isDubstep = documentId.contains("dubstep");
imageSrc = isDubstep ? "cover-1.jpg" : "cover-2.jpg";
} else {
// Assume this is a folder. Real provider should verify if documentId is a real folder
imageSrc = "folder-1.jpg";
}
if(ExampleProvider.LOG) Log.w(ExampleProvider.TAG, "openDocumentThumbnail documentId=" + documentId + " sizeHint=" + sizeHint + " imageSrc=" + imageSrc);
if(null == imageSrc) throw new FileNotFoundException(documentId);
try {
return this.getContext().getResources().getAssets().openFd(imageSrc);
} catch(final IOException e) {
throw new FileNotFoundException(documentId);
}
}
/**
* Send actual track data as direct file descriptor or seekable socket protocol
*/
@Override
public ParcelFileDescriptor openDocument(final String documentId, final String mode, final CancellationSignal signal) throws FileNotFoundException {
if(ExampleProvider.LOG) Log.w(ExampleProvider.TAG, "openDocument documentId=" + documentId + " mode=" + mode + " callingPak=" + this.getCallingPackage());
final int accessMode = ParcelFileDescriptor.parseMode(mode);
if(0 == (accessMode & ParcelFileDescriptor.MODE_READ_ONLY)) throw new IllegalAccessError("documentId=" + documentId + " mode=" + mode);
// Poweramp provides CancellationSignal, so we may want to check that on open and periodically (for example, in case of using pipe here)
if(null != signal) {
signal.throwIfCanceled();
}
final String filePath = this.docIdToFileName(documentId);
if(null == filePath) throw new FileNotFoundException(documentId);
// Let's send root2 "dubstep" via seekable socket and other files - via direct fd. Don't do this for root1 where no metadata given and Poweramp expects direct fd tracks
// Check package name for Poweramp (or other known client) which supports this fd protocol
final String pak = this.getCallingPackage();
if(null != pak && pak.equals(PowerampAPIHelper.getPowerampPackageName(this.getContext()))
&& documentId.startsWith("root2/") && documentId.contains("dubstep")
) {
// Let's open dubstep-2 via milliseconds based seekbable sockets, and other dubsteps - via byte offset seekable sockets
if(documentId.endsWith("-2.flac")) {
return this.openViaSeekableSocket2(documentId, filePath, signal);
} else {
return this.openViaSeekableSocket(documentId, filePath, signal);
}
}
// Let's send root2 "summer" via seekable proxy file descriptors. No need to check for client package as these file descriptors should be supported everywhere
if(26 <= Build.VERSION.SDK_INT && ExampleProvider.USE_OPEN_PROXY_FILE_DESCRIPTOR && documentId.startsWith("root2/") && documentId.contains("summer")) {
return this.openViaProxyFd(documentId, filePath, signal);
}
return this.openViaDirectFD(documentId, filePath);
}
/** For given docId, return appropriate asset file name */
@Nullable
private String docIdToFileName(final String documentId) {
if(documentId.endsWith(".m3u8")) {
return "streams-playlist.m3u8";
} else if(documentId.endsWith(".mp3")) {
final boolean isDubstep = documentId.contains("dubstep");
return isDubstep ? "bensound-dubstep.mp3" : "bensound-summer.mp3"; // We only have 2 real mp3s here for many "virtual" tracks
} else if(documentId.endsWith(".flac")) {
return "bensound-dubstep.flac";
}
return null;
}
/** This version of the method uses byte offset based seeks */
private ParcelFileDescriptor openViaSeekableSocket(String documentId, final String filePath, CancellationSignal signal) throws FileNotFoundException {
if(ExampleProvider.LOG) Log.w(ExampleProvider.TAG, "openViaSeekableSocket documentId=" + documentId + " filePath=" + filePath);
try {
ParcelFileDescriptor[] fds = ParcelFileDescriptor.createSocketPair();
File file = new File(this.getContext().getFilesDir(), filePath);
long fileLength = file.length();
if(ExampleProvider.DEBUG_ALWAYS_STOP_IN_OPEN) {
if(ExampleProvider.LOG) Log.w(ExampleProvider.TAG, "openViaSeekableSocket STOP due to DEBUG_ALWAYS_STOP_IN_OPEN");
// NOTE: in this case if we just return socket here and stop, Poweramp will still wait for timeout
// as socket was never closed from our side
// If we want fail-fast approach from Poweramp, either return null here, or shutdown socket properly
new Thread(new Runnable() {
public void run() {
if(ExampleProvider.LOG) Log.w(ExampleProvider.TAG, "openViaSeekableSocket STOP shutdown socket");
final FileDescriptor socket = fds[1].getFileDescriptor();
try {
Os.shutdown(socket, 0);
} catch(final ErrnoException ex) {
}
try {
Os.close(socket);
} catch(final ErrnoException ex) {
}
}
}).start();
return fds[0];
}
// NOTE: it's not possible to use timeouts on this side of the socket as Poweramp may open and hold the socket for an indefinite time while in the paused state
// NOTE: don't use AsyncTask or other short-time thread pools here, as:
// - this thread will be alive as long as Poweramp holds the file
// - this can take an indefinite time, as Poweramp can be paused on the file
new Thread(new Runnable() {
public void run() {
// NOTE: we can use arbitrary buffer size here >0, but increasing buffer will increase non-seekable "window" at the end of file
// Using buffer size > MAX_DATA_SIZE will cause buffer to be split into multiple packets
final ByteBuffer buf = ByteBuffer.allocateDirect(TrackProviderProto.MAX_DATA_SIZE);
buf.order(ByteOrder.nativeOrder());
long bytesSent = 0;
try(final FileInputStream fis = new FileInputStream(file)) {
final FileChannel fc = fis.getChannel(); // We'll be using nio for the buffer loading
try(final TrackProviderProto proto = new TrackProviderProto(fds[1], fileLength)) {
if(ExampleProvider.DEBUG_ALWAYS_STOP_PROTOCOL) {
if(ExampleProvider.LOG) Log.w(ExampleProvider.TAG, "openViaSeekableSocket STOP due to DEBUG_ALWAYS_STOP_PROTOCOL");
return; // Immediately stop. NOTE: this will call proto.close automatically
}
proto.sendHeader(); // Send initial header
if(ExampleProvider.DEBUG_STOP_PROTOCOL_AFTER_HEADER) {
if(ExampleProvider.LOG) Log.w(ExampleProvider.TAG, "openViaSeekableSocket STOP due to DEBUG_STOP_PROTOCOL_AFTER_HEADER");
return; // Immediately stop. NOTE: this will call proto.close automatically
}
while(true) {
int len;
//noinspection UnusedAssignment
while(0 < (len = fc.read(buf))) {
buf.flip();
// Send some data to Poweramp and optionally receive seek request
// NOTE: avoid sending empty buffers here (!buf.hasRemaining()), as this will cause premature EOF
final long seekRequestPos = proto.sendData(buf);
ExampleProvider.this.handleSeekRequest(proto, seekRequestPos, fc, fileLength); // May be handle seek request
bytesSent += buf.limit();
buf.clear();
if(ExampleProvider.DEBUG_STOP_PROTOCOL_AFTER_BYTES > 0 && DEBUG_STOP_PROTOCOL_AFTER_BYTES <= bytesSent) {
// Force-stop playback from our side
if(ExampleProvider.LOG) Log.w(ExampleProvider.TAG, "openViaSeekableSocket STOP due to DEBUG_STOP_AFTER_BYTES");
return;
}
}
// Here we're almost done with the file and hit EOF. Still keep file and socket opened until Poweramp closes socket
//
// As we may send number of pre-loaded buffers to Poweramp we may hit EOF much earlier prior the track actually finishes playing:
// - we hit EOF here and may attempt to exit this thread/close socket
// - Poweramp plays some buffered data
// - user seeks the track. Poweramp will fail to seek it as our provider is done with the track and socket is closed
//
// Solution to this is to keep file and socket opened here and to continue seek commands processing until Poweramp actually closes socket.
// This scenario can be easily tested by pausing Poweramp close to the track end and seeking while paused
final long seekRequestPos = proto.sendEOFAndWaitForSeekOrClose();
if(ExampleProvider.this.handleSeekRequest(proto, seekRequestPos, fc, fileLength)) {
if(ExampleProvider.LOG) Log.w(ExampleProvider.TAG, "openViaSeekableSocket file seek past EOF documentId=" + documentId);
//noinspection UnnecessaryContinue
continue; // We've just processed extra seek request, continue sending buffers
} else {
break; // We done here, Poweramp closed socket
}
}
if(ExampleProvider.LOG) Log.w(ExampleProvider.TAG, " openViaSeekableSocket file DONE documentId=" + documentId);
}
} catch(final TrackProviderProto.TrackProviderProtoClosed ex) {
if(ExampleProvider.LOG) Log.w(ExampleProvider.TAG, "openViaSeekableSocket closed documentId=" + documentId + " " + ex.getMessage());
} catch(final Throwable th) {
// If we're here, we can't do much - close connection, release resources, and exit
Log.e(ExampleProvider.TAG, "documentId=" + documentId, th);
}
}
}).start();
return fds[0];
} catch(final Throwable th) {
Log.e(ExampleProvider.TAG, "documentId=" + documentId, th);
throw new FileNotFoundException(documentId);
}
}
/** This version of the method uses milliseconds offset based seek requests */
private ParcelFileDescriptor openViaSeekableSocket2(String documentId, final String filePath, CancellationSignal signal) throws FileNotFoundException {
if(ExampleProvider.LOG) Log.w(ExampleProvider.TAG, "openViaSeekableSocket2 documentId=" + documentId + " filePath=" + filePath);
try {
ParcelFileDescriptor[] fds = ParcelFileDescriptor.createSocketPair();
File file = new File(this.getContext().getFilesDir(), filePath);
if(ExampleProvider.DEBUG_ALWAYS_STOP_IN_OPEN) {
if(ExampleProvider.LOG) Log.w(ExampleProvider.TAG, "openViaSeekableSocket STOP due to DEBUG_ALWAYS_STOP_IN_OPEN - return null");
// NOTE: in this case if we just return socket here and stop, Poweramp will still wait for timeout
// as socket was never closed from our side
// If we want fail-fast approach from Poweramp, either return null here, or shutdown socket properly
return null;
}
//long fileLength = file.length();
// NOTE: for the sake of testing, let's send some approximation of the file instead, based on
// duration and average bytes per ms
// The real DUBSTEP_AVERAGE_BYTES_PER_MS depends on compression and file format
long fileLength = ExampleProvider.DUBSTEP_FAKE_AVERAGE_BYTES_PER_MS * ExampleProvider.DUBSTEP_DURATION_MS;
// NOTE: it's not possible to use timeouts on this side of the socket as Poweramp may open and hold the socket for an indefinite time while in the paused state
// NOTE: don't use AsyncTask or other short-time thread pools here, as:
// - this thread will be alive as long as Poweramp holds the file
// - this can take an indefinite time, as Poweramp can be paused on the file
new Thread(new Runnable() {
@SuppressWarnings("UnusedAssignment") public void run() {
// NOTE: we can use arbitrary buffer size here >0, but increasing buffer will increase non-seekable "window" at the end of file
// Using buffer size > MAX_DATA_SIZE will cause buffer to be split into multiple packets
final ByteBuffer buf = ByteBuffer.allocateDirect(TrackProviderProto.MAX_DATA_SIZE);
buf.order(ByteOrder.nativeOrder());
long bytesSent = 0;
try(final FileInputStream fis = new FileInputStream(file)) {
final FileChannel fc = fis.getChannel(); // We'll be using nio for the buffer loading
try(final TrackProviderProto proto = new TrackProviderProto(fds[1], fileLength)) {
if(ExampleProvider.DEBUG_ALWAYS_STOP_PROTOCOL) {
if(ExampleProvider.LOG) Log.w(ExampleProvider.TAG, "openViaSeekableSocket2 STOP due to DEBUG_ALWAYS_STOP_PROTOCOL");
return; // Immediately stop. NOTE: this will call proto.close automatically
}
proto.sendHeader(); // Send initial header
if(ExampleProvider.DEBUG_STOP_PROTOCOL_AFTER_HEADER) {
if(ExampleProvider.LOG) Log.w(ExampleProvider.TAG, "openViaSeekableSocket2 STOP due to DEBUG_STOP_PROTOCOL_AFTER_HEADER");
return; // Immediately stop. NOTE: this will call proto.close automatically
}
while(true) {
int len;
while(0 < (len = fc.read(buf))) {
buf.flip();
// Send some data to Poweramp and optionally receive seek request
// NOTE: avoid sending empty buffers here (!buf.hasRemaining()), as this will cause premature EOF
final TrackProviderProto.SeekRequest seekRequest = proto.sendData2(buf);
ExampleProvider.this.handleSeekRequest2(proto, seekRequest, fc, fileLength); // May be handle seek request
bytesSent += buf.limit();
buf.clear();
if(ExampleProvider.DEBUG_STOP_PROTOCOL_AFTER_BYTES > 0 && DEBUG_STOP_PROTOCOL_AFTER_BYTES <= bytesSent) {
// Force-stop playback from our side
if(ExampleProvider.LOG) Log.w(ExampleProvider.TAG, "openViaSeekableSocket2 STOP due to DEBUG_STOP_AFTER_BYTES");
return;
}
}
// Here we're almost done with the file and hit EOF. Still keep file and socket opened until Poweramp closes socket
//
// As we may send number of pre-loaded buffers to Poweramp we may hit EOF much earlier prior the track actually finishes playing:
// - we hit EOF here and may attempt to exit this thread/close socket
// - Poweramp plays some buffered data
// - user seeks the track. Poweramp will fail to seek it as our provider is done with the track and socket is closed
//
// Solution to this is to keep file and socket opened here and to continue seek commands processing until Poweramp actually closes socket.
// This scenario can be easily tested by pausing Poweramp close to the track end and seeking while paused
final TrackProviderProto.SeekRequest seekRequest = proto.sendEOFAndWaitForSeekOrClose2();
if(ExampleProvider.this.handleSeekRequest2(proto, seekRequest, fc, fileLength)) {
if(ExampleProvider.LOG) Log.w(ExampleProvider.TAG, "openViaSeekableSocket2 file seek past EOF documentId=" + documentId);
//noinspection UnnecessaryContinue
continue; // We've just processed extra seek request, continue sending buffers
} else {
break; // We done here, Poweramp closed socket
}
}
if(ExampleProvider.LOG) Log.w(ExampleProvider.TAG, " openViaSeekableSocket2 file DONE documentId=" + documentId);
}
} catch(final TrackProviderProto.TrackProviderProtoClosed ex) {
if(ExampleProvider.LOG) Log.w(ExampleProvider.TAG, "openViaSeekableSocket2 closed documentId=" + documentId + " " + ex.getMessage());
} catch(final Throwable th) {
// If we're here, we can't do much - close connection, release resources, and exit
Log.e(ExampleProvider.TAG, "documentId=" + documentId, th);
}
}
}).start();
return fds[0];
} catch(final Throwable th) {
Log.e(ExampleProvider.TAG, "documentId=" + documentId, th);
throw new FileNotFoundException(documentId);
}
}
@RequiresApi(api = Build.VERSION_CODES.O)
private ParcelFileDescriptor openViaProxyFd(String documentId, String filePath, CancellationSignal signal) throws FileNotFoundException {
if(ExampleProvider.LOG) Log.w(ExampleProvider.TAG, "openViaProxyFd documentId=" + documentId + " filePath=" + filePath);
FileInputStream fis = null;
try {
File file = new File(this.getContext().getFilesDir(), filePath);
long fileLength = file.length();
fis = new FileInputStream(file);
FileDescriptor fd = fis.getFD();
HandlerThread thread = new HandlerThread(documentId); // This is the thread we're handling fd reading on
thread.start();
final Handler handler = new Handler(thread.getLooper());
final StorageManager storageManager = (StorageManager) this.getContext().getSystemService(Context.STORAGE_SERVICE);
FileInputStream finalFis = fis;
final ParcelFileDescriptor pfd = storageManager.openProxyFileDescriptor(ParcelFileDescriptor.MODE_READ_ONLY, new ProxyFileDescriptorCallback() {
@Override
public long onGetSize() throws ErrnoException {
if(ExampleProvider.LOG) Log.w(ExampleProvider.TAG, "onGetSize fileLength=" + fileLength + " documentId=" + documentId);