diff options
Diffstat (limited to 'app/src/main/java/app/fedilab/android/helper/CacheDataSourceFactory.java')
-rw-r--r-- | app/src/main/java/app/fedilab/android/helper/CacheDataSourceFactory.java | 22 |
1 files changed, 15 insertions, 7 deletions
diff --git a/app/src/main/java/app/fedilab/android/helper/CacheDataSourceFactory.java b/app/src/main/java/app/fedilab/android/helper/CacheDataSourceFactory.java index 2ab4f83c0..a28cde7bc 100644 --- a/app/src/main/java/app/fedilab/android/helper/CacheDataSourceFactory.java +++ b/app/src/main/java/app/fedilab/android/helper/CacheDataSourceFactory.java @@ -18,15 +18,15 @@ import java.io.File; public class CacheDataSourceFactory implements DataSource.Factory { + private static SimpleCache sDownloadCache; private final Context context; private final DefaultDataSourceFactory defaultDatasourceFactory; - private final long maxFileSize, maxCacheSize; + private final long maxFileSize; - public CacheDataSourceFactory(Context context, long maxCacheSize, long maxFileSize) { + public CacheDataSourceFactory(Context context) { super(); this.context = context; - this.maxCacheSize = maxCacheSize; - this.maxFileSize = maxFileSize; + this.maxFileSize = 5 * 1024 * 1024; SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); String userAgent = sharedpreferences.getString(Helper.SET_CUSTOM_USER_AGENT, Helper.USER_AGENT); DefaultBandwidthMeter.Builder bandwidthMeterBuilder = new DefaultBandwidthMeter.Builder(context); @@ -36,11 +36,19 @@ public class CacheDataSourceFactory implements DataSource.Factory { new DefaultHttpDataSourceFactory(userAgent, bandwidthMeter)); } + public static SimpleCache getInstance(Context context) { + SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); + int video_cache = sharedpreferences.getInt(Helper.SET_VIDEO_CACHE, Helper.DEFAULT_VIDEO_CACHE_MB); + LeastRecentlyUsedCacheEvictor evictor = new LeastRecentlyUsedCacheEvictor(video_cache * 1024 * 1024); + ExoDatabaseProvider exoDatabaseProvider = new ExoDatabaseProvider(context); + if (sDownloadCache == null) + sDownloadCache = new SimpleCache(new File(context.getCacheDir(), "media"), evictor, exoDatabaseProvider); + return sDownloadCache; + } + @Override public DataSource createDataSource() { - LeastRecentlyUsedCacheEvictor evictor = new LeastRecentlyUsedCacheEvictor(maxCacheSize); - ExoDatabaseProvider exoDatabaseProvider = new ExoDatabaseProvider(context); - SimpleCache simpleCache = new SimpleCache(new File(context.getCacheDir(), "media"), evictor, exoDatabaseProvider); + SimpleCache simpleCache = getInstance(context); return new CacheDataSource(simpleCache, defaultDatasourceFactory.createDataSource(), new FileDataSource(), new CacheDataSink(simpleCache, maxFileSize), CacheDataSource.FLAG_BLOCK_ON_CACHE | CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR, null); |