20
20
import android .content .Context ;
21
21
import android .content .DialogInterface ;
22
22
import android .net .Uri ;
23
- import android .os .AsyncTask ;
23
+ import android .os .Handler ;
24
+ import android .os .Looper ;
24
25
import android .widget .Toast ;
25
26
import androidx .annotation .Nullable ;
26
27
import androidx .annotation .OptIn ;
54
55
import java .util .HashMap ;
55
56
import java .util .UUID ;
56
57
import java .util .concurrent .CopyOnWriteArraySet ;
58
+ import java .util .concurrent .ExecutorService ;
59
+ import java .util .concurrent .Executors ;
60
+ import java .util .concurrent .Future ;
57
61
58
62
/** Tracks media that has been downloaded. */
59
63
@ OptIn (markerClass = androidx .media3 .common .util .UnstableApi .class )
@@ -182,7 +186,7 @@ public void release() {
182
186
trackSelectionDialog .dismiss ();
183
187
}
184
188
if (widevineOfflineLicenseFetchTask != null ) {
185
- widevineOfflineLicenseFetchTask .cancel (false );
189
+ widevineOfflineLicenseFetchTask .cancel ();
186
190
}
187
191
}
188
192
@@ -358,14 +362,16 @@ private DownloadRequest buildDownloadRequest() {
358
362
359
363
/** Downloads a Widevine offline license in a background thread. */
360
364
@ RequiresApi (18 )
361
- private static final class WidevineOfflineLicenseFetchTask extends AsyncTask < Void , Void , Void > {
365
+ private static final class WidevineOfflineLicenseFetchTask {
362
366
363
367
private final Format format ;
364
368
private final MediaItem .DrmConfiguration drmConfiguration ;
365
369
private final DataSource .Factory dataSourceFactory ;
366
370
private final StartDownloadDialogHelper dialogHelper ;
367
371
private final DownloadHelper downloadHelper ;
372
+ private final ExecutorService executorService ;
368
373
374
+ @ Nullable Future <?> future ;
369
375
@ Nullable private byte [] keySetId ;
370
376
@ Nullable private DrmSession .DrmSessionException drmSessionException ;
371
377
@@ -375,39 +381,49 @@ public WidevineOfflineLicenseFetchTask(
375
381
DataSource .Factory dataSourceFactory ,
376
382
StartDownloadDialogHelper dialogHelper ,
377
383
DownloadHelper downloadHelper ) {
384
+ this .executorService = Executors .newSingleThreadExecutor ();
378
385
this .format = format ;
379
386
this .drmConfiguration = drmConfiguration ;
380
387
this .dataSourceFactory = dataSourceFactory ;
381
388
this .dialogHelper = dialogHelper ;
382
389
this .downloadHelper = downloadHelper ;
383
390
}
384
391
385
- @ Override
386
- protected Void doInBackground (Void ... voids ) {
387
- OfflineLicenseHelper offlineLicenseHelper =
388
- OfflineLicenseHelper .newWidevineInstance (
389
- drmConfiguration .licenseUri .toString (),
390
- drmConfiguration .forceDefaultLicenseUri ,
391
- dataSourceFactory ,
392
- drmConfiguration .licenseRequestHeaders ,
393
- new DrmSessionEventListener .EventDispatcher ());
394
- try {
395
- keySetId = offlineLicenseHelper .downloadLicense (format );
396
- } catch (DrmSession .DrmSessionException e ) {
397
- drmSessionException = e ;
398
- } finally {
399
- offlineLicenseHelper .release ();
392
+ public void cancel () {
393
+ if (future != null ) {
394
+ future .cancel (/* mayInterruptIfRunning= */ false );
400
395
}
401
- return null ;
402
396
}
403
397
404
- @ Override
405
- protected void onPostExecute (Void aVoid ) {
406
- if (drmSessionException != null ) {
407
- dialogHelper .onOfflineLicenseFetchedError (drmSessionException );
408
- } else {
409
- dialogHelper .onOfflineLicenseFetched (downloadHelper , checkNotNull (keySetId ));
410
- }
398
+ public void execute () {
399
+ future =
400
+ executorService .submit (
401
+ () -> {
402
+ OfflineLicenseHelper offlineLicenseHelper =
403
+ OfflineLicenseHelper .newWidevineInstance (
404
+ drmConfiguration .licenseUri .toString (),
405
+ drmConfiguration .forceDefaultLicenseUri ,
406
+ dataSourceFactory ,
407
+ drmConfiguration .licenseRequestHeaders ,
408
+ new DrmSessionEventListener .EventDispatcher ());
409
+ try {
410
+ keySetId = offlineLicenseHelper .downloadLicense (format );
411
+ } catch (DrmSession .DrmSessionException e ) {
412
+ drmSessionException = e ;
413
+ } finally {
414
+ offlineLicenseHelper .release ();
415
+ new Handler (Looper .getMainLooper ())
416
+ .post (
417
+ () -> {
418
+ if (drmSessionException != null ) {
419
+ dialogHelper .onOfflineLicenseFetchedError (drmSessionException );
420
+ } else {
421
+ dialogHelper .onOfflineLicenseFetched (
422
+ downloadHelper , checkNotNull (keySetId ));
423
+ }
424
+ });
425
+ }
426
+ });
411
427
}
412
428
}
413
429
}
0 commit comments