feat: added external memory cache option

This commit is contained in:
CappielloAntonio 2024-05-26 14:49:57 +02:00
parent c4e8fe5261
commit 477331da6f
7 changed files with 188 additions and 4 deletions

View file

@ -38,11 +38,13 @@ public final class DownloadUtil {
public static final String DOWNLOAD_NOTIFICATION_SUCCESSFUL_GROUP = "com.cappielloantonio.tempo.SuccessfulDownload";
public static final String DOWNLOAD_NOTIFICATION_FAILED_GROUP = "com.cappielloantonio.tempo.FailedDownload";
private static final String STREAMING_CACHE_CONTENT_DIRECTORY = "streaming_cache";
private static final String DOWNLOAD_CONTENT_DIRECTORY = "downloads";
private static DataSource.Factory dataSourceFactory;
private static DataSource.Factory httpDataSourceFactory;
private static DatabaseProvider databaseProvider;
private static File streamingCacheDirectory;
private static File downloadDirectory;
private static Cache downloadCache;
private static SimpleCache streamingCache;
@ -135,7 +137,7 @@ public final class DownloadUtil {
private static synchronized SimpleCache getStreamingCache(Context context) {
if (streamingCache == null) {
File streamingCacheDirectory = new File(context.getCacheDir(), "streamingCache");
File streamingCacheDirectory = new File(getStreamingCacheDirectory(context), STREAMING_CACHE_CONTENT_DIRECTORY);
streamingCache = new SimpleCache(
streamingCacheDirectory,
@ -169,6 +171,27 @@ public final class DownloadUtil {
return databaseProvider;
}
private static synchronized File getStreamingCacheDirectory(Context context) {
if (streamingCacheDirectory == null) {
if (Preferences.getStreamingCacheStoragePreference() == 0) {
streamingCacheDirectory = context.getExternalFilesDirs(null)[0];
if (streamingCacheDirectory == null) {
streamingCacheDirectory = context.getFilesDir();
}
} else {
try {
streamingCacheDirectory = context.getExternalFilesDirs(null)[1];
} catch (Exception exception) {
streamingCacheDirectory = context.getExternalFilesDirs(null)[0];
Preferences.setStreamingCacheStoragePreference(0);
}
}
}
return streamingCacheDirectory;
}
private static synchronized File getDownloadDirectory(Context context) {
if (downloadDirectory == null) {
if (Preferences.getDownloadStoragePreference() == 0) {