11package org .schabi .newpipe .player .helper ;
22
33import android .content .Context ;
4- import android .util .Log ;
54
65import androidx .annotation .NonNull ;
76import androidx .annotation .Nullable ;
87
9- import com .google .android .exoplayer2 .database .StandaloneDatabaseProvider ;
108import com .google .android .exoplayer2 .upstream .DataSource ;
119import com .google .android .exoplayer2 .upstream .DefaultDataSource ;
1210import com .google .android .exoplayer2 .upstream .DefaultHttpDataSource ;
1311import com .google .android .exoplayer2 .upstream .FileDataSource ;
1412import com .google .android .exoplayer2 .upstream .TransferListener ;
1513import com .google .android .exoplayer2 .upstream .cache .CacheDataSink ;
1614import com .google .android .exoplayer2 .upstream .cache .CacheDataSource ;
17- import com .google .android .exoplayer2 .upstream .cache .LeastRecentlyUsedCacheEvictor ;
1815import com .google .android .exoplayer2 .upstream .cache .SimpleCache ;
1916
2017import org .schabi .newpipe .player .datasource .YoutubeHttpDataSource ;
2118
22- import java .io .File ;
23-
2419/* package-private */ final class CacheFactory implements DataSource .Factory {
25- private static final String TAG = CacheFactory .class .getSimpleName ();
26-
27- private static final String CACHE_FOLDER_NAME = "exoplayer" ;
2820 private static final int CACHE_FLAGS = CacheDataSource .FLAG_IGNORE_CACHE_ON_ERROR ;
29- private static SimpleCache cache ;
3021
3122 private final long maxFileSize ;
3223 private final Context context ;
3324 private final String userAgent ;
3425 private final TransferListener transferListener ;
3526 private final DataSource .Factory upstreamDataSourceFactory ;
27+ private final SimpleCache simpleCache ;
3628
3729 public static class Builder {
3830 private final Context context ;
3931 private final String userAgent ;
4032 private final TransferListener transferListener ;
4133 private DataSource .Factory upstreamDataSourceFactory ;
34+ private SimpleCache simpleCache ;
4235
4336 Builder (@ NonNull final Context context ,
4437 @ NonNull final String userAgent ,
@@ -53,34 +46,31 @@ public void setUpstreamDataSourceFactory(
5346 this .upstreamDataSourceFactory = upstreamDataSourceFactory ;
5447 }
5548
49+ public void setSimpleCache (@ NonNull final SimpleCache simpleCache ) {
50+ this .simpleCache = simpleCache ;
51+ }
52+
5653 public CacheFactory build () {
57- return new CacheFactory (context , userAgent , transferListener ,
54+ if (simpleCache == null ) {
55+ throw new IllegalStateException ("No SimpleCache instance has been specified. "
56+ + "Please specify one with setSimpleCache" );
57+ }
58+ return new CacheFactory (context , userAgent , transferListener , simpleCache ,
5859 upstreamDataSourceFactory );
5960 }
6061 }
6162
6263 private CacheFactory (@ NonNull final Context context ,
6364 @ NonNull final String userAgent ,
6465 @ NonNull final TransferListener transferListener ,
66+ @ NonNull final SimpleCache simpleCache ,
6567 @ Nullable final DataSource .Factory upstreamDataSourceFactory ) {
6668 this .context = context ;
6769 this .userAgent = userAgent ;
6870 this .transferListener = transferListener ;
71+ this .simpleCache = simpleCache ;
6972 this .upstreamDataSourceFactory = upstreamDataSourceFactory ;
7073
71- final File cacheDir = new File (context .getExternalCacheDir (), CACHE_FOLDER_NAME );
72- if (!cacheDir .exists ()) {
73- //noinspection ResultOfMethodCallIgnored
74- cacheDir .mkdir ();
75- }
76-
77- if (cache == null ) {
78- final LeastRecentlyUsedCacheEvictor evictor
79- = new LeastRecentlyUsedCacheEvictor (PlayerHelper .getPreferredCacheSize ());
80- cache = new SimpleCache (cacheDir , evictor , new StandaloneDatabaseProvider (context ));
81- Log .d (TAG , "initExoPlayerCache: cacheDir = " + cacheDir .getAbsolutePath ());
82- }
83-
8474 maxFileSize = PlayerHelper .getPreferredFileSize ();
8575 }
8676
@@ -112,7 +102,8 @@ public DataSource createDataSource() {
112102 .createDataSource ();
113103
114104 final FileDataSource fileSource = new FileDataSource ();
115- final CacheDataSink dataSink = new CacheDataSink (cache , maxFileSize );
116- return new CacheDataSource (cache , dataSource , fileSource , dataSink , CACHE_FLAGS , null );
105+ final CacheDataSink dataSink = new CacheDataSink (simpleCache , maxFileSize );
106+ return new CacheDataSource (simpleCache , dataSource , fileSource , dataSink , CACHE_FLAGS ,
107+ null );
117108 }
118109}
0 commit comments