Deleted the old MediaItem download system - TODO: to be redone with the new Media3

This commit is contained in:
CappielloAntonio 2021-12-29 10:16:47 +01:00
parent fb9187482f
commit 185a9d7107
4 changed files with 29 additions and 318 deletions

View file

@ -1,154 +0,0 @@
package com.cappielloantonio.play.service;
import static com.google.android.exoplayer2.util.Assertions.checkNotNull;
import android.content.Context;
import android.net.Uri;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.cappielloantonio.play.App;
import com.cappielloantonio.play.model.Song;
import com.cappielloantonio.play.repository.DownloadRepository;
import com.cappielloantonio.play.util.MappingUtil;
import com.cappielloantonio.play.util.MusicUtil;
import com.google.android.exoplayer2.MediaItem;
import com.google.android.exoplayer2.offline.Download;
import com.google.android.exoplayer2.offline.DownloadCursor;
import com.google.android.exoplayer2.offline.DownloadHelper;
import com.google.android.exoplayer2.offline.DownloadIndex;
import com.google.android.exoplayer2.offline.DownloadManager;
import com.google.android.exoplayer2.offline.DownloadRequest;
import com.google.android.exoplayer2.offline.DownloadService;
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector;
import com.google.android.exoplayer2.upstream.HttpDataSource;
import com.google.android.exoplayer2.util.Log;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.CopyOnWriteArraySet;
public class DownloadTracker {
private static final String TAG = "DownloadTracker";
private final Context context;
private final HttpDataSource.Factory httpDataSourceFactory;
private final CopyOnWriteArraySet<Listener> listeners;
private final HashMap<Uri, Download> downloads;
private final DownloadIndex downloadIndex;
private final DefaultTrackSelector.Parameters trackSelectorParameters;
public DownloadTracker(Context context, HttpDataSource.Factory httpDataSourceFactory, DownloadManager downloadManager) {
this.context = context.getApplicationContext();
this.httpDataSourceFactory = httpDataSourceFactory;
listeners = new CopyOnWriteArraySet<>();
downloads = new HashMap<>();
downloadIndex = downloadManager.getDownloadIndex();
trackSelectorParameters = DownloadHelper.getDefaultTrackSelectorParameters(context);
downloadManager.addListener(new DownloadManagerListener());
loadDownloads();
}
public void addListener(Listener listener) {
checkNotNull(listener);
listeners.add(listener);
}
public void removeListener(Listener listener) {
listeners.remove(listener);
}
public boolean isDownloaded(Song song) {
MediaItem mediaItem = MusicUtil.getSongDownloadItem(song);
@Nullable Download download = downloads.get(checkNotNull(mediaItem.playbackProperties).uri);
return download != null && download.state != Download.STATE_FAILED;
}
public boolean isDownloaded(List<Song> songs) {
for (Song song : songs) {
MediaItem mediaItem = MusicUtil.getSongDownloadItem(song);
@Nullable Download download = downloads.get(checkNotNull(mediaItem.playbackProperties).uri);
if (download != null && download.state != Download.STATE_FAILED) {
return true;
}
}
return false;
}
@Nullable
public DownloadRequest getDownloadRequest(String id, Uri uri) {
return new DownloadRequest.Builder(id, uri).build();
}
public void download(List<Song> songs, String playlistId, String playlistName) {
DownloadRepository downloadRepository = new DownloadRepository(App.getInstance());
for (Song song : songs) {
if (isDownloaded(song)) {
downloadRepository.insert(MappingUtil.mapToDownload(song, playlistId, playlistName));
continue;
}
MediaItem mediaItem = MusicUtil.getSongDownloadItem(song);
DownloadService.sendAddDownload(context, DownloaderService.class, getDownloadRequest(song.getId(), checkNotNull(mediaItem.playbackProperties).uri), false);
downloadRepository.insert(MappingUtil.mapToDownload(song, playlistId, playlistName));
}
}
public void remove(List<Song> songs) {
DownloadRepository downloadRepository = new DownloadRepository(App.getInstance());
for (Song song : songs) {
MediaItem mediaItem = MusicUtil.getSongDownloadItem(song);
@Nullable Download download = downloads.get(checkNotNull(mediaItem.playbackProperties).uri);
if (download != null && download.state != Download.STATE_FAILED) {
DownloadService.sendRemoveDownload(context, DownloaderService.class, download.request.id, false);
downloadRepository.delete(MappingUtil.mapToDownload(song, null, null));
}
}
}
private void loadDownloads() {
try (DownloadCursor loadedDownloads = downloadIndex.getDownloads()) {
while (loadedDownloads.moveToNext()) {
Download download = loadedDownloads.getDownload();
downloads.put(download.request.uri, download);
}
} catch (IOException e) {
Log.w(TAG, "Failed to query downloads", e);
}
}
public interface Listener {
void onDownloadsChanged();
}
private class DownloadManagerListener implements DownloadManager.Listener {
@Override
public void onDownloadChanged(@NonNull DownloadManager downloadManager, @NonNull Download download, @Nullable Exception finalException) {
downloads.put(download.request.uri, download);
for (Listener listener : listeners) {
listener.onDownloadsChanged();
}
}
@Override
public void onDownloadRemoved(@NonNull DownloadManager downloadManager, @NonNull Download download) {
downloads.remove(download.request.uri);
for (Listener listener : listeners) {
listener.onDownloadsChanged();
}
}
}
}

View file

@ -1,77 +0,0 @@
package com.cappielloantonio.play.service;
import android.app.Notification;
import android.content.Context;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.cappielloantonio.play.R;
import com.cappielloantonio.play.util.DownloadUtil;
import com.google.android.exoplayer2.offline.Download;
import com.google.android.exoplayer2.offline.DownloadManager;
import com.google.android.exoplayer2.offline.DownloadService;
import com.google.android.exoplayer2.scheduler.Scheduler;
import com.google.android.exoplayer2.ui.DownloadNotificationHelper;
import com.google.android.exoplayer2.util.NotificationUtil;
import com.google.android.exoplayer2.util.Util;
import java.util.List;
public class DownloaderService extends DownloadService {
private static final int FOREGROUND_NOTIFICATION_ID = 1;
public DownloaderService() {
super(FOREGROUND_NOTIFICATION_ID, DEFAULT_FOREGROUND_NOTIFICATION_UPDATE_INTERVAL, DownloadUtil.DOWNLOAD_NOTIFICATION_CHANNEL_ID, R.string.exo_download_notification_channel_name, 0);
}
@Nullable
@Override
protected Scheduler getScheduler() {
return null;
}
@Override
@NonNull
protected DownloadManager getDownloadManager() {
DownloadManager downloadManager = DownloadUtil.getDownloadManager(this);
DownloadNotificationHelper downloadNotificationHelper = DownloadUtil.getDownloadNotificationHelper(this);
downloadManager.addListener(new TerminalStateNotificationHelper(this, downloadNotificationHelper, FOREGROUND_NOTIFICATION_ID + 1));
return downloadManager;
}
@Override
@NonNull
protected Notification getForegroundNotification(@NonNull List<Download> downloads) {
return DownloadUtil.getDownloadNotificationHelper(this).buildProgressNotification(this, R.drawable.ic_download, null, null, downloads);
}
private static final class TerminalStateNotificationHelper implements DownloadManager.Listener {
private final Context context;
private final DownloadNotificationHelper notificationHelper;
private int nextNotificationId;
public TerminalStateNotificationHelper(Context context, DownloadNotificationHelper notificationHelper, int firstNotificationId) {
this.context = context.getApplicationContext();
this.notificationHelper = notificationHelper;
nextNotificationId = firstNotificationId;
}
@Override
public void onDownloadChanged(DownloadManager downloadManager, Download download, @Nullable Exception finalException) {
Notification notification;
if (download.state == Download.STATE_COMPLETED) {
notification = notificationHelper.buildDownloadCompletedNotification(context, R.drawable.ic_check_circle, null, Util.fromUtf8Bytes(download.request.data));
} else if (download.state == Download.STATE_FAILED) {
notification = notificationHelper.buildDownloadFailedNotification(context, R.drawable.ic_error, null, Util.fromUtf8Bytes(download.request.data));
} else {
return;
}
NotificationUtil.setNotification(context, nextNotificationId++, notification);
}
}
}

View file

@ -1,66 +0,0 @@
package com.cappielloantonio.play.util;
import android.content.Context;
import com.google.android.exoplayer2.database.DatabaseProvider;
import com.google.android.exoplayer2.database.ExoDatabaseProvider;
import com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory;
import com.google.android.exoplayer2.upstream.HttpDataSource;
import com.google.android.exoplayer2.upstream.cache.LeastRecentlyUsedCacheEvictor;
import com.google.android.exoplayer2.upstream.cache.NoOpCacheEvictor;
import com.google.android.exoplayer2.upstream.cache.SimpleCache;
import java.io.File;
import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.CookiePolicy;
public final class CacheUtil {
private static final String CACHE_CONTENT_DIRECTORY = "cache";
private static HttpDataSource.Factory httpDataSourceFactory;
private static File cacheDirectory;
private static SimpleCache simpleCache;
private static ExoDatabaseProvider databaseProvider;
public static synchronized HttpDataSource.Factory getHttpDataSourceFactory() {
if (httpDataSourceFactory == null) {
CookieManager cookieManager = new CookieManager();
cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ORIGINAL_SERVER);
CookieHandler.setDefault(cookieManager);
httpDataSourceFactory = new DefaultHttpDataSourceFactory();
}
return httpDataSourceFactory;
}
public static synchronized SimpleCache getCache(Context context) {
if (simpleCache == null) {
File downloadContentDirectory = new File(getCacheDirectory(context), CACHE_CONTENT_DIRECTORY);
long cacheSize = PreferenceUtil.getInstance(context).getMediaCacheSize();
LeastRecentlyUsedCacheEvictor cacheEvictor = new LeastRecentlyUsedCacheEvictor(cacheSize);
ExoDatabaseProvider databaseProvider = getDatabaseProvider(context);
simpleCache = new SimpleCache(downloadContentDirectory, cacheEvictor, databaseProvider);
}
return simpleCache;
}
private static synchronized ExoDatabaseProvider getDatabaseProvider(Context context) {
if (databaseProvider == null) {
databaseProvider = new ExoDatabaseProvider(context);
}
return databaseProvider;
}
private static synchronized File getCacheDirectory(Context context) {
if (cacheDirectory == null) {
cacheDirectory = context.getExternalFilesDir(null);
if (cacheDirectory == null) {
cacheDirectory = context.getFilesDir();
}
}
return cacheDirectory;
}
}

View file

@ -1,26 +1,26 @@
package com.cappielloantonio.play.util;
import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;
import com.cappielloantonio.play.service.DownloadTracker;
import com.google.android.exoplayer2.database.DatabaseProvider;
import com.google.android.exoplayer2.database.ExoDatabaseProvider;
import com.google.android.exoplayer2.offline.ActionFileUpgradeUtil;
import com.google.android.exoplayer2.offline.DefaultDownloadIndex;
import com.google.android.exoplayer2.offline.DownloadManager;
import com.google.android.exoplayer2.ui.DownloadNotificationHelper;
import com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory;
import com.google.android.exoplayer2.upstream.HttpDataSource;
import com.google.android.exoplayer2.upstream.cache.Cache;
import com.google.android.exoplayer2.upstream.cache.NoOpCacheEvictor;
import com.google.android.exoplayer2.upstream.cache.SimpleCache;
import com.google.android.exoplayer2.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;
import java.io.File;
import java.io.IOException;
import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.CookiePolicy;
import java.util.Map;
import java.util.concurrent.Executors;
public final class DownloadUtil {
@ -36,7 +36,6 @@ public final class DownloadUtil {
private static File downloadDirectory;
private static Cache downloadCache;
private static DownloadManager downloadManager;
private static DownloadTracker downloadTracker;
private static DownloadNotificationHelper downloadNotificationHelper;
public static synchronized HttpDataSource.Factory getHttpDataSourceFactory() {
@ -44,11 +43,22 @@ public final class DownloadUtil {
CookieManager cookieManager = new CookieManager();
cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ORIGINAL_SERVER);
CookieHandler.setDefault(cookieManager);
httpDataSourceFactory = new DefaultHttpDataSourceFactory();
httpDataSourceFactory = new HttpDataSource.Factory() {
@Override
public HttpDataSource createDataSource() {
return null;
}
@Override
public HttpDataSource.Factory setDefaultRequestProperties(Map<String, String> defaultRequestProperties) {
return null;
}
};
}
return httpDataSourceFactory;
}
@SuppressLint("UnsafeOptInUsageError")
public static synchronized DownloadNotificationHelper getDownloadNotificationHelper(Context context) {
if (downloadNotificationHelper == null) {
downloadNotificationHelper = new DownloadNotificationHelper(context, DOWNLOAD_NOTIFICATION_CHANNEL_ID);
@ -61,11 +71,7 @@ public final class DownloadUtil {
return downloadManager;
}
public static synchronized DownloadTracker getDownloadTracker(Context context) {
ensureDownloadManagerInitialized(context);
return downloadTracker;
}
@SuppressLint("UnsafeOptInUsageError")
public static synchronized Cache getDownloadCache(Context context) {
if (downloadCache == null) {
File downloadContentDirectory = new File(getDownloadDirectory(context), DOWNLOAD_CONTENT_DIRECTORY);
@ -75,16 +81,17 @@ public final class DownloadUtil {
return downloadCache;
}
@SuppressLint("UnsafeOptInUsageError")
private static synchronized void ensureDownloadManagerInitialized(Context context) {
if (downloadManager == null) {
DefaultDownloadIndex downloadIndex = new DefaultDownloadIndex(getDatabaseProvider(context));
upgradeActionFile(context, DOWNLOAD_ACTION_FILE, downloadIndex, false);
upgradeActionFile(context, DOWNLOAD_TRACKER_ACTION_FILE, downloadIndex, true);
downloadManager = new DownloadManager(context, getDatabaseProvider(context), getDownloadCache(context), getHttpDataSourceFactory(), Executors.newFixedThreadPool(6));
downloadTracker = new DownloadTracker(context, getHttpDataSourceFactory(), downloadManager);
}
}
@SuppressLint("UnsafeOptInUsageError")
private static synchronized void upgradeActionFile(Context context, String fileName, DefaultDownloadIndex downloadIndex, boolean addNewDownloadsAsCompleted) {
try {
ActionFileUpgradeUtil.upgradeAndDelete(new File(getDownloadDirectory(context), fileName), null, downloadIndex, true, addNewDownloadsAsCompleted);
@ -93,6 +100,7 @@ public final class DownloadUtil {
}
}
@SuppressLint("UnsafeOptInUsageError")
private static synchronized DatabaseProvider getDatabaseProvider(Context context) {
if (databaseProvider == null) {
databaseProvider = new ExoDatabaseProvider(context);