2021-04-26 19:17:42 +02:00
|
|
|
package com.cappielloantonio.play.util;
|
|
|
|
|
|
2021-12-29 10:16:47 +01:00
|
|
|
import android.annotation.SuppressLint;
|
2021-04-26 19:17:42 +02:00
|
|
|
import android.content.Context;
|
2021-12-29 10:16:47 +01:00
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
|
|
import androidx.media3.database.DatabaseProvider;
|
|
|
|
|
import androidx.media3.database.ExoDatabaseProvider;
|
|
|
|
|
import androidx.media3.datasource.HttpDataSource;
|
|
|
|
|
import androidx.media3.datasource.cache.Cache;
|
|
|
|
|
import androidx.media3.datasource.cache.NoOpCacheEvictor;
|
|
|
|
|
import androidx.media3.datasource.cache.SimpleCache;
|
|
|
|
|
import androidx.media3.exoplayer.offline.ActionFileUpgradeUtil;
|
|
|
|
|
import androidx.media3.exoplayer.offline.DefaultDownloadIndex;
|
|
|
|
|
import androidx.media3.exoplayer.offline.DownloadManager;
|
|
|
|
|
import androidx.media3.exoplayer.offline.DownloadNotificationHelper;
|
2021-04-26 19:17:42 +02:00
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.net.CookieHandler;
|
|
|
|
|
import java.net.CookieManager;
|
|
|
|
|
import java.net.CookiePolicy;
|
2021-12-29 10:16:47 +01:00
|
|
|
import java.util.Map;
|
2021-04-26 19:17:42 +02:00
|
|
|
import java.util.concurrent.Executors;
|
|
|
|
|
|
|
|
|
|
public final class DownloadUtil {
|
|
|
|
|
|
|
|
|
|
public static final String DOWNLOAD_NOTIFICATION_CHANNEL_ID = "download_channel";
|
|
|
|
|
private static final String TAG = "DemoUtil";
|
|
|
|
|
private static final String DOWNLOAD_ACTION_FILE = "actions";
|
|
|
|
|
private static final String DOWNLOAD_TRACKER_ACTION_FILE = "tracked_actions";
|
|
|
|
|
private static final String DOWNLOAD_CONTENT_DIRECTORY = "downloads";
|
|
|
|
|
|
|
|
|
|
private static HttpDataSource.Factory httpDataSourceFactory;
|
|
|
|
|
private static DatabaseProvider databaseProvider;
|
|
|
|
|
private static File downloadDirectory;
|
|
|
|
|
private static Cache downloadCache;
|
|
|
|
|
private static DownloadManager downloadManager;
|
|
|
|
|
private static DownloadNotificationHelper downloadNotificationHelper;
|
|
|
|
|
|
2021-11-27 17:07:20 +01:00
|
|
|
public static synchronized HttpDataSource.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-29 10:16:47 +01:00
|
|
|
httpDataSourceFactory = new HttpDataSource.Factory() {
|
|
|
|
|
@Override
|
|
|
|
|
public HttpDataSource createDataSource() {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public HttpDataSource.Factory setDefaultRequestProperties(Map<String, String> defaultRequestProperties) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
};
|
2021-04-26 19:17:42 +02:00
|
|
|
}
|
|
|
|
|
return httpDataSourceFactory;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-29 10:16:47 +01:00
|
|
|
@SuppressLint("UnsafeOptInUsageError")
|
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
|
|
|
}
|
|
|
|
|
return downloadNotificationHelper;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static synchronized DownloadManager getDownloadManager(Context context) {
|
|
|
|
|
ensureDownloadManagerInitialized(context);
|
|
|
|
|
return downloadManager;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-29 10:16:47 +01:00
|
|
|
@SuppressLint("UnsafeOptInUsageError")
|
2021-04-26 19:17:42 +02:00
|
|
|
public static synchronized Cache getDownloadCache(Context context) {
|
|
|
|
|
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
|
|
|
}
|
2021-11-27 17:07:20 +01:00
|
|
|
|
2021-04-26 19:17:42 +02:00
|
|
|
return downloadCache;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-29 10:16:47 +01:00
|
|
|
@SuppressLint("UnsafeOptInUsageError")
|
2021-04-26 19:17:42 +02:00
|
|
|
private static synchronized void ensureDownloadManagerInitialized(Context context) {
|
|
|
|
|
if (downloadManager == null) {
|
|
|
|
|
DefaultDownloadIndex downloadIndex = new DefaultDownloadIndex(getDatabaseProvider(context));
|
2021-04-28 18:10:22 +02:00
|
|
|
upgradeActionFile(context, DOWNLOAD_ACTION_FILE, downloadIndex, false);
|
|
|
|
|
upgradeActionFile(context, DOWNLOAD_TRACKER_ACTION_FILE, downloadIndex, true);
|
2021-11-27 17:07:20 +01:00
|
|
|
downloadManager = new DownloadManager(context, getDatabaseProvider(context), getDownloadCache(context), getHttpDataSourceFactory(), Executors.newFixedThreadPool(6));
|
2021-04-26 19:17:42 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-29 10:16:47 +01:00
|
|
|
@SuppressLint("UnsafeOptInUsageError")
|
2021-04-28 18:10:22 +02:00
|
|
|
private static synchronized void upgradeActionFile(Context context, String fileName, DefaultDownloadIndex downloadIndex, boolean addNewDownloadsAsCompleted) {
|
2021-04-26 19:17:42 +02:00
|
|
|
try {
|
2021-04-28 18:10:22 +02:00
|
|
|
ActionFileUpgradeUtil.upgradeAndDelete(new File(getDownloadDirectory(context), fileName), null, downloadIndex, true, addNewDownloadsAsCompleted);
|
2021-04-26 19:17:42 +02:00
|
|
|
} catch (IOException e) {
|
|
|
|
|
Log.e(TAG, "Failed to upgrade action file: " + fileName, e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-29 10:16:47 +01:00
|
|
|
@SuppressLint("UnsafeOptInUsageError")
|
2021-04-26 19:17:42 +02:00
|
|
|
private static synchronized DatabaseProvider getDatabaseProvider(Context context) {
|
|
|
|
|
if (databaseProvider == null) {
|
|
|
|
|
databaseProvider = new ExoDatabaseProvider(context);
|
|
|
|
|
}
|
|
|
|
|
return databaseProvider;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static synchronized File getDownloadDirectory(Context context) {
|
|
|
|
|
if (downloadDirectory == null) {
|
|
|
|
|
downloadDirectory = context.getExternalFilesDir(null);
|
|
|
|
|
if (downloadDirectory == null) {
|
|
|
|
|
downloadDirectory = context.getFilesDir();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return downloadDirectory;
|
|
|
|
|
}
|
|
|
|
|
}
|