2023-06-17 15:30:23 +02:00
|
|
|
package com.cappielloantonio.tempo.util;
|
2021-04-26 19:17:42 +02:00
|
|
|
|
2023-09-15 13:09:48 +02:00
|
|
|
import android.app.Notification;
|
2021-04-26 19:17:42 +02:00
|
|
|
import android.content.Context;
|
2021-12-29 10:16:47 +01:00
|
|
|
|
2023-09-15 13:09:48 +02:00
|
|
|
import androidx.core.app.NotificationCompat;
|
2022-12-29 19:11:02 +01:00
|
|
|
import androidx.media3.common.util.UnstableApi;
|
2021-12-29 10:16:47 +01:00
|
|
|
import androidx.media3.database.DatabaseProvider;
|
2021-12-31 21:36:50 +01:00
|
|
|
import androidx.media3.database.StandaloneDatabaseProvider;
|
|
|
|
|
import androidx.media3.datasource.DataSource;
|
2024-03-11 17:21:28 +08:00
|
|
|
import androidx.media3.datasource.DataSpec;
|
2021-12-31 21:36:50 +01:00
|
|
|
import androidx.media3.datasource.DefaultDataSource;
|
|
|
|
|
import androidx.media3.datasource.DefaultHttpDataSource;
|
2024-03-11 17:21:28 +08:00
|
|
|
import androidx.media3.datasource.ResolvingDataSource;
|
2021-12-29 10:16:47 +01:00
|
|
|
import androidx.media3.datasource.cache.Cache;
|
2021-12-31 21:36:50 +01:00
|
|
|
import androidx.media3.datasource.cache.CacheDataSource;
|
2024-03-11 17:21:28 +08:00
|
|
|
import androidx.media3.datasource.cache.LeastRecentlyUsedCacheEvictor;
|
2021-12-29 10:16:47 +01:00
|
|
|
import androidx.media3.datasource.cache.NoOpCacheEvictor;
|
|
|
|
|
import androidx.media3.datasource.cache.SimpleCache;
|
2022-12-29 19:11:02 +01:00
|
|
|
import androidx.media3.exoplayer.DefaultRenderersFactory;
|
|
|
|
|
import androidx.media3.exoplayer.RenderersFactory;
|
2021-12-29 10:16:47 +01:00
|
|
|
import androidx.media3.exoplayer.offline.DownloadManager;
|
|
|
|
|
import androidx.media3.exoplayer.offline.DownloadNotificationHelper;
|
2021-04-26 19:17:42 +02:00
|
|
|
|
2023-06-17 15:30:23 +02:00
|
|
|
import com.cappielloantonio.tempo.service.DownloaderManager;
|
2021-12-31 21:36:50 +01:00
|
|
|
|
2021-04-26 19:17:42 +02:00
|
|
|
import java.io.File;
|
|
|
|
|
import java.net.CookieHandler;
|
|
|
|
|
import java.net.CookieManager;
|
|
|
|
|
import java.net.CookiePolicy;
|
2023-08-19 12:33:39 +02:00
|
|
|
import java.util.ArrayList;
|
2021-04-26 19:17:42 +02:00
|
|
|
import java.util.concurrent.Executors;
|
|
|
|
|
|
2022-12-29 19:11:02 +01:00
|
|
|
@UnstableApi
|
2021-04-26 19:17:42 +02:00
|
|
|
public final class DownloadUtil {
|
|
|
|
|
|
|
|
|
|
public static final String DOWNLOAD_NOTIFICATION_CHANNEL_ID = "download_channel";
|
2023-09-15 22:02:31 +02:00
|
|
|
public static final String DOWNLOAD_NOTIFICATION_SUCCESSFUL_GROUP = "com.cappielloantonio.tempo.SuccessfulDownload";
|
|
|
|
|
public static final String DOWNLOAD_NOTIFICATION_FAILED_GROUP = "com.cappielloantonio.tempo.FailedDownload";
|
2021-12-31 21:36:50 +01:00
|
|
|
|
2021-04-26 19:17:42 +02:00
|
|
|
private static final String DOWNLOAD_CONTENT_DIRECTORY = "downloads";
|
|
|
|
|
|
2021-12-31 21:36:50 +01:00
|
|
|
private static DataSource.Factory dataSourceFactory;
|
2022-12-29 19:11:02 +01:00
|
|
|
private static DataSource.Factory httpDataSourceFactory;
|
2021-04-26 19:17:42 +02:00
|
|
|
private static DatabaseProvider databaseProvider;
|
|
|
|
|
private static File downloadDirectory;
|
|
|
|
|
private static Cache downloadCache;
|
2024-03-11 17:21:28 +08:00
|
|
|
private static SimpleCache streamingCache;
|
2021-04-26 19:17:42 +02:00
|
|
|
private static DownloadManager downloadManager;
|
2022-01-02 16:40:23 +01:00
|
|
|
private static DownloaderManager downloaderManager;
|
2021-04-26 19:17:42 +02:00
|
|
|
private static DownloadNotificationHelper downloadNotificationHelper;
|
|
|
|
|
|
2022-12-29 19:11:02 +01:00
|
|
|
public static boolean useExtensionRenderers() {
|
|
|
|
|
// return true;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static RenderersFactory buildRenderersFactory(Context context, boolean preferExtensionRenderer) {
|
|
|
|
|
@DefaultRenderersFactory.ExtensionRendererMode int extensionRendererMode =
|
|
|
|
|
useExtensionRenderers()
|
|
|
|
|
? (preferExtensionRenderer ? DefaultRenderersFactory.EXTENSION_RENDERER_MODE_PREFER : DefaultRenderersFactory.EXTENSION_RENDERER_MODE_ON)
|
|
|
|
|
: DefaultRenderersFactory.EXTENSION_RENDERER_MODE_OFF;
|
|
|
|
|
|
|
|
|
|
return new DefaultRenderersFactory(context.getApplicationContext()).setExtensionRendererMode(extensionRendererMode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static synchronized DataSource.Factory getHttpDataSourceFactory() {
|
2021-04-26 19:17:42 +02:00
|
|
|
if (httpDataSourceFactory == null) {
|
|
|
|
|
CookieManager cookieManager = new CookieManager();
|
|
|
|
|
cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ORIGINAL_SERVER);
|
|
|
|
|
CookieHandler.setDefault(cookieManager);
|
2021-12-31 21:36:50 +01:00
|
|
|
httpDataSourceFactory = new DefaultHttpDataSource.Factory();
|
2021-04-26 19:17:42 +02:00
|
|
|
}
|
2021-12-31 21:36:50 +01:00
|
|
|
|
2021-04-26 19:17:42 +02:00
|
|
|
return httpDataSourceFactory;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-31 21:36:50 +01:00
|
|
|
public static synchronized DataSource.Factory getDataSourceFactory(Context context) {
|
|
|
|
|
if (dataSourceFactory == null) {
|
|
|
|
|
context = context.getApplicationContext();
|
2022-01-01 21:55:15 +01:00
|
|
|
DefaultDataSource.Factory upstreamFactory = new DefaultDataSource.Factory(context, getHttpDataSourceFactory());
|
2024-03-11 17:21:28 +08:00
|
|
|
|
|
|
|
|
if (Preferences.getStreamingCacheSize() > 0) {
|
|
|
|
|
CacheDataSource.Factory streamCacheFactory = new CacheDataSource.Factory()
|
|
|
|
|
.setCache(getStreamingCache(context))
|
|
|
|
|
.setUpstreamDataSourceFactory(upstreamFactory);
|
|
|
|
|
|
|
|
|
|
ResolvingDataSource.Factory resolvingFactory = new ResolvingDataSource.Factory(
|
|
|
|
|
new StreamingCacheDataSource.Factory(streamCacheFactory),
|
|
|
|
|
dataSpec -> {
|
|
|
|
|
DataSpec.Builder builder = dataSpec.buildUpon();
|
|
|
|
|
builder.setFlags(dataSpec.flags & ~DataSpec.FLAG_DONT_CACHE_IF_LENGTH_UNKNOWN);
|
|
|
|
|
return builder.build();
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
dataSourceFactory = buildReadOnlyCacheDataSource(resolvingFactory, getDownloadCache(context));
|
|
|
|
|
} else {
|
|
|
|
|
dataSourceFactory = buildReadOnlyCacheDataSource(upstreamFactory, getDownloadCache(context));
|
|
|
|
|
}
|
2021-12-31 21:36:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return dataSourceFactory;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-28 18:10:22 +02:00
|
|
|
public static synchronized DownloadNotificationHelper getDownloadNotificationHelper(Context context) {
|
2021-04-26 19:17:42 +02:00
|
|
|
if (downloadNotificationHelper == null) {
|
2021-04-28 18:10:22 +02:00
|
|
|
downloadNotificationHelper = new DownloadNotificationHelper(context, DOWNLOAD_NOTIFICATION_CHANNEL_ID);
|
2021-04-26 19:17:42 +02:00
|
|
|
}
|
2021-12-31 21:36:50 +01:00
|
|
|
|
2021-04-26 19:17:42 +02:00
|
|
|
return downloadNotificationHelper;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static synchronized DownloadManager getDownloadManager(Context context) {
|
|
|
|
|
ensureDownloadManagerInitialized(context);
|
|
|
|
|
return downloadManager;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-02 16:40:23 +01:00
|
|
|
public static synchronized DownloaderManager getDownloadTracker(Context context) {
|
2021-12-31 21:36:50 +01:00
|
|
|
ensureDownloadManagerInitialized(context);
|
2022-01-02 16:40:23 +01:00
|
|
|
return downloaderManager;
|
2021-12-31 21:36:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static synchronized Cache getDownloadCache(Context context) {
|
2021-04-26 19:17:42 +02:00
|
|
|
if (downloadCache == null) {
|
2021-04-28 18:10:22 +02:00
|
|
|
File downloadContentDirectory = new File(getDownloadDirectory(context), DOWNLOAD_CONTENT_DIRECTORY);
|
|
|
|
|
downloadCache = new SimpleCache(downloadContentDirectory, new NoOpCacheEvictor(), getDatabaseProvider(context));
|
2021-04-26 19:17:42 +02:00
|
|
|
}
|
2022-01-01 21:55:15 +01:00
|
|
|
|
2021-04-26 19:17:42 +02:00
|
|
|
return downloadCache;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-11 17:21:28 +08:00
|
|
|
private static synchronized SimpleCache getStreamingCache(Context context) {
|
|
|
|
|
if (streamingCache == null) {
|
|
|
|
|
File streamingCacheDirectory = new File(context.getCacheDir(), "streamingCache");
|
2024-05-25 17:55:30 +02:00
|
|
|
|
2024-03-11 17:21:28 +08:00
|
|
|
streamingCache = new SimpleCache(
|
|
|
|
|
streamingCacheDirectory,
|
|
|
|
|
new LeastRecentlyUsedCacheEvictor(Preferences.getStreamingCacheSize() * 1024 * 1024),
|
|
|
|
|
getDatabaseProvider(context)
|
|
|
|
|
);
|
|
|
|
|
}
|
2024-05-25 17:55:30 +02:00
|
|
|
|
2024-03-11 17:21:28 +08:00
|
|
|
return streamingCache;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-26 19:17:42 +02:00
|
|
|
private static synchronized void ensureDownloadManagerInitialized(Context context) {
|
|
|
|
|
if (downloadManager == null) {
|
2022-12-29 19:11:02 +01:00
|
|
|
downloadManager = new DownloadManager(
|
|
|
|
|
context,
|
|
|
|
|
getDatabaseProvider(context),
|
|
|
|
|
getDownloadCache(context),
|
|
|
|
|
getHttpDataSourceFactory(),
|
|
|
|
|
Executors.newFixedThreadPool(6)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
downloaderManager = new DownloaderManager(context, getHttpDataSourceFactory(), downloadManager);
|
2021-04-26 19:17:42 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static synchronized DatabaseProvider getDatabaseProvider(Context context) {
|
|
|
|
|
if (databaseProvider == null) {
|
2021-12-31 21:36:50 +01:00
|
|
|
databaseProvider = new StandaloneDatabaseProvider(context);
|
2021-04-26 19:17:42 +02:00
|
|
|
}
|
2022-01-01 21:55:15 +01:00
|
|
|
|
2021-04-26 19:17:42 +02:00
|
|
|
return databaseProvider;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static synchronized File getDownloadDirectory(Context context) {
|
|
|
|
|
if (downloadDirectory == null) {
|
2023-08-04 23:46:33 +02:00
|
|
|
if (Preferences.getDownloadStoragePreference() == 0) {
|
|
|
|
|
downloadDirectory = context.getExternalFilesDirs(null)[0];
|
|
|
|
|
if (downloadDirectory == null) {
|
|
|
|
|
downloadDirectory = context.getFilesDir();
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
try {
|
|
|
|
|
downloadDirectory = context.getExternalFilesDirs(null)[1];
|
|
|
|
|
} catch (Exception exception) {
|
|
|
|
|
downloadDirectory = context.getExternalFilesDirs(null)[0];
|
|
|
|
|
Preferences.setDownloadStoragePreference(0);
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-26 19:17:42 +02:00
|
|
|
}
|
|
|
|
|
}
|
2021-12-31 21:36:50 +01:00
|
|
|
|
2021-04-26 19:17:42 +02:00
|
|
|
return downloadDirectory;
|
|
|
|
|
}
|
2021-12-31 21:36:50 +01:00
|
|
|
|
|
|
|
|
private static CacheDataSource.Factory buildReadOnlyCacheDataSource(DataSource.Factory upstreamFactory, Cache cache) {
|
|
|
|
|
return new CacheDataSource.Factory()
|
|
|
|
|
.setCache(cache)
|
|
|
|
|
.setUpstreamDataSourceFactory(upstreamFactory)
|
|
|
|
|
.setCacheWriteDataSinkFactory(null)
|
|
|
|
|
.setFlags(CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR);
|
|
|
|
|
}
|
2023-08-19 12:33:39 +02:00
|
|
|
|
|
|
|
|
public static synchronized void eraseDownloadFolder(Context context) {
|
|
|
|
|
File directory = getDownloadDirectory(context);
|
|
|
|
|
|
|
|
|
|
ArrayList<File> files = listFiles(directory, new ArrayList<>());
|
|
|
|
|
|
|
|
|
|
for (File file : files) {
|
|
|
|
|
file.delete();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static synchronized ArrayList<File> listFiles(File directory, ArrayList<File> files) {
|
|
|
|
|
if (directory.isDirectory()) {
|
|
|
|
|
File[] list = directory.listFiles();
|
|
|
|
|
|
|
|
|
|
if (list != null) {
|
|
|
|
|
for (File file : list) {
|
|
|
|
|
if (file.isFile() && file.getName().toLowerCase().endsWith(".exo")) {
|
|
|
|
|
files.add(file);
|
|
|
|
|
} else if (file.isDirectory()) {
|
|
|
|
|
listFiles(file, files);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return files;
|
|
|
|
|
}
|
2023-09-15 13:09:48 +02:00
|
|
|
|
2024-03-11 17:21:28 +08:00
|
|
|
public static synchronized long getStreamingCacheSize(Context context) {
|
|
|
|
|
return getStreamingCache(context).getCacheSpace();
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-15 13:09:48 +02:00
|
|
|
public static Notification buildGroupSummaryNotification(Context context, String channelId, String groupId, int icon, String title) {
|
|
|
|
|
return new NotificationCompat.Builder(context, channelId)
|
|
|
|
|
.setContentTitle(title)
|
|
|
|
|
.setSmallIcon(icon)
|
|
|
|
|
.setGroup(groupId)
|
|
|
|
|
.setGroupSummary(true)
|
|
|
|
|
.build();
|
|
|
|
|
}
|
2021-04-26 19:17:42 +02:00
|
|
|
}
|