Reimplemented a basic track download functionality

This commit is contained in:
CappielloAntonio 2022-01-01 21:55:15 +01:00
parent b7a77cf32b
commit dee845ebff
17 changed files with 154 additions and 214 deletions

View file

@ -4,6 +4,7 @@ import android.annotation.SuppressLint;
import android.app.Notification;
import android.content.Context;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.media3.common.util.NotificationUtil;
import androidx.media3.common.util.Util;
@ -30,6 +31,7 @@ public class DownloaderService extends androidx.media3.exoplayer.offline.Downloa
super(FOREGROUND_NOTIFICATION_ID, DEFAULT_FOREGROUND_NOTIFICATION_UPDATE_INTERVAL, DownloadUtil.DOWNLOAD_NOTIFICATION_CHANNEL_ID, R.string.exo_download_notification_channel_name, 0);
}
@NonNull
@Override
protected DownloadManager getDownloadManager() {
DownloadManager downloadManager = DownloadUtil.getDownloadManager(this);
@ -38,17 +40,18 @@ public class DownloaderService extends androidx.media3.exoplayer.offline.Downloa
return downloadManager;
}
@NonNull
@Override
protected Scheduler getScheduler() {
return new PlatformScheduler(this, JOB_ID);
}
@NonNull
@Override
protected Notification getForegroundNotification(List<Download> downloads, @Requirements.RequirementFlags int notMetRequirements) {
protected Notification getForegroundNotification(@NonNull List<Download> downloads, @Requirements.RequirementFlags int notMetRequirements) {
return DownloadUtil.getDownloadNotificationHelper(this).buildProgressNotification(this, R.drawable.ic_download, null, null, downloads, notMetRequirements);
}
private static final class TerminalStateNotificationHelper implements DownloadManager.Listener {
private final Context context;
private final DownloadNotificationHelper notificationHelper;
@ -63,7 +66,7 @@ public class DownloaderService extends androidx.media3.exoplayer.offline.Downloa
@SuppressLint("UnsafeOptInUsageError")
@Override
public void onDownloadChanged(DownloadManager downloadManager, Download download, @Nullable Exception finalException) {
public void onDownloadChanged(@NonNull DownloadManager downloadManager, Download download, @Nullable Exception finalException) {
Notification notification;
if (download.state == Download.STATE_COMPLETED) {

View file

@ -4,17 +4,12 @@ import static androidx.media3.common.util.Assertions.checkNotNull;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.DialogInterface;
import android.net.Uri;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentManager;
import androidx.media3.common.MediaItem;
import androidx.media3.common.util.Log;
import androidx.media3.common.util.Util;
import androidx.media3.datasource.HttpDataSource;
import androidx.media3.exoplayer.RenderersFactory;
import androidx.media3.exoplayer.offline.Download;
import androidx.media3.exoplayer.offline.DownloadCursor;
import androidx.media3.exoplayer.offline.DownloadHelper;
@ -22,39 +17,31 @@ import androidx.media3.exoplayer.offline.DownloadIndex;
import androidx.media3.exoplayer.offline.DownloadManager;
import androidx.media3.exoplayer.offline.DownloadRequest;
import androidx.media3.exoplayer.offline.DownloadService;
import androidx.media3.exoplayer.trackselection.DefaultTrackSelector;
import androidx.media3.exoplayer.trackselection.MappingTrackSelector;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.CopyOnWriteArraySet;
public class DownloaderTracker {
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;
@Nullable
private StartDownloadDialogHelper startDownloadDialogHelper;
public interface Listener {
void onDownloadsChanged();
}
@SuppressLint("UnsafeOptInUsageError")
public DownloaderTracker(Context context, HttpDataSource.Factory httpDataSourceFactory, DownloadManager downloadManager) {
public DownloaderTracker(Context context, 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();
@ -70,28 +57,48 @@ public class DownloaderTracker {
listeners.remove(listener);
}
@SuppressLint("UnsafeOptInUsageError")
private DownloadRequest buildDownloadRequest(MediaItem mediaItem) {
return DownloadHelper.forMediaItem(context, mediaItem).getDownloadRequest(Util.getUtf8Bytes(checkNotNull(mediaItem.mediaId)));
}
@SuppressLint("UnsafeOptInUsageError")
public boolean isDownloaded(MediaItem mediaItem) {
@Nullable Download download = downloads.get(checkNotNull(mediaItem.localConfiguration).uri);
return download != null && download.state != Download.STATE_FAILED;
}
@Nullable
public DownloadRequest getDownloadRequest(Uri uri) {
@Nullable Download download = downloads.get(uri);
return download != null && download.state != Download.STATE_FAILED ? download.request : null;
@SuppressLint("UnsafeOptInUsageError")
public boolean areDownloaded(List<MediaItem> mediaItems) {
for (MediaItem mediaItem : mediaItems) {
@Nullable Download download = downloads.get(checkNotNull(mediaItem.localConfiguration).uri);
if (download != null && download.state != Download.STATE_FAILED) {
return true;
}
}
return false;
}
@SuppressLint("UnsafeOptInUsageError")
public void toggleDownload(FragmentManager fragmentManager, MediaItem mediaItem, RenderersFactory renderersFactory) {
@Nullable Download download = downloads.get(checkNotNull(mediaItem.localConfiguration).uri);
if (download != null && download.state != Download.STATE_FAILED) {
androidx.media3.exoplayer.offline.DownloadService.sendRemoveDownload(context, DownloaderService.class, download.request.id, false);
} else {
if (startDownloadDialogHelper != null) {
startDownloadDialogHelper.release();
}
startDownloadDialogHelper = new StartDownloadDialogHelper(fragmentManager, DownloadHelper.forMediaItem(context, mediaItem, renderersFactory, httpDataSourceFactory), mediaItem);
public void download(MediaItem mediaItem) {
DownloadService.sendAddDownload(context, DownloaderService.class, buildDownloadRequest(mediaItem), false);
}
public void download(List<MediaItem> mediaItems) {
for (MediaItem mediaItem : mediaItems) {
download(mediaItem);
}
}
@SuppressLint("UnsafeOptInUsageError")
public void remove(MediaItem mediaItem) {
DownloadService.sendRemoveDownload(context, DownloaderService.class, buildDownloadRequest(mediaItem).id, false);
}
public void remove(List<MediaItem> mediaItems) {
for (MediaItem mediaItem : mediaItems) {
remove(mediaItem);
}
}
@ -124,109 +131,4 @@ public class DownloaderTracker {
}
}
}
private final class StartDownloadDialogHelper implements DownloadHelper.Callback, DialogInterface.OnClickListener, DialogInterface.OnDismissListener {
private final FragmentManager fragmentManager;
private final DownloadHelper downloadHelper;
private final MediaItem mediaItem;
private TrackSelectionDialog trackSelectionDialog;
private MappingTrackSelector.MappedTrackInfo mappedTrackInfo;
@SuppressLint("UnsafeOptInUsageError")
public StartDownloadDialogHelper(FragmentManager fragmentManager, DownloadHelper downloadHelper, MediaItem mediaItem) {
this.fragmentManager = fragmentManager;
this.downloadHelper = downloadHelper;
this.mediaItem = mediaItem;
downloadHelper.prepare(this);
}
@SuppressLint("UnsafeOptInUsageError")
public void release() {
downloadHelper.release();
if (trackSelectionDialog != null) {
trackSelectionDialog.dismiss();
}
}
@SuppressLint("UnsafeOptInUsageError")
@Override
public void onPrepared(DownloadHelper helper) {
onDownloadPrepared(helper);
}
@SuppressLint("UnsafeOptInUsageError")
@Override
public void onPrepareError(DownloadHelper helper, IOException e) {
Toast.makeText(context, R.string.download_start_error, Toast.LENGTH_LONG).show();
Log.e(TAG, e.getMessage());
}
// DialogInterface.OnClickListener implementation.
@SuppressLint("UnsafeOptInUsageError")
@Override
public void onClick(DialogInterface dialog, int which) {
for (int periodIndex = 0; periodIndex < downloadHelper.getPeriodCount(); periodIndex++) {
downloadHelper.clearTrackSelections(periodIndex);
for (int i = 0; i < mappedTrackInfo.getRendererCount(); i++) {
if (!trackSelectionDialog.getIsDisabled(i)) {
downloadHelper.addTrackSelectionForSingleRenderer(periodIndex, i, trackSelectorParameters, trackSelectionDialog.getOverrides(i));
}
}
}
DownloadRequest downloadRequest = buildDownloadRequest();
if (downloadRequest.streamKeys.isEmpty()) {
return;
}
startDownload(downloadRequest);
}
@SuppressLint("UnsafeOptInUsageError")
@Override
public void onDismiss(DialogInterface dialogInterface) {
trackSelectionDialog = null;
downloadHelper.release();
}
@SuppressLint("UnsafeOptInUsageError")
private void onDownloadPrepared(DownloadHelper helper) {
if (helper.getPeriodCount() == 0) {
Log.d(TAG, "No periods found. Downloading entire stream.");
startDownload();
downloadHelper.release();
return;
}
mappedTrackInfo = downloadHelper.getMappedTrackInfo(0);
if (!TrackSelectionDialog.willHaveContent(mappedTrackInfo)) {
Log.d(TAG, "No dialog content. Downloading entire stream.");
startDownload();
downloadHelper.release();
return;
}
trackSelectionDialog = TrackSelectionDialog.createForMappedTrackInfoAndParameters(R.string.exo_download_description, mappedTrackInfo, trackSelectorParameters, false, true, this, this);
trackSelectionDialog.show(fragmentManager, null);
}
private void startDownload() {
startDownload(buildDownloadRequest());
}
@SuppressLint("UnsafeOptInUsageError")
private void startDownload(DownloadRequest downloadRequest) {
DownloadService.sendAddDownload(context, DownloaderService.class, downloadRequest, false);
}
@SuppressLint("UnsafeOptInUsageError")
private DownloadRequest buildDownloadRequest() {
return downloadHelper.getDownloadRequest(Util.getUtf8Bytes(checkNotNull(mediaItem.mediaMetadata.title.toString()))).copyWithKeySetId(keySetId);
}
}
}

View file

@ -61,7 +61,7 @@ public class MediaManager {
try {
if (mediaBrowserListenableFuture.isDone()) {
mediaBrowserListenableFuture.get().clearMediaItems();
mediaBrowserListenableFuture.get().setMediaItems(MappingUtil.mapMediaItems(context, songs));
mediaBrowserListenableFuture.get().setMediaItems(MappingUtil.mapMediaItems(context, songs, true));
mediaBrowserListenableFuture.get().seekTo(getQueueRepository().getLastPlayedSongIndex(), 0);
mediaBrowserListenableFuture.get().prepare();
}
@ -148,7 +148,7 @@ public class MediaManager {
try {
if (mediaBrowserListenableFuture.isDone()) {
mediaBrowserListenableFuture.get().clearMediaItems();
mediaBrowserListenableFuture.get().setMediaItems(MappingUtil.mapMediaItems(context, songs));
mediaBrowserListenableFuture.get().setMediaItems(MappingUtil.mapMediaItems(context, songs, true));
mediaBrowserListenableFuture.get().prepare();
mediaBrowserListenableFuture.get().seekTo(startIndex, 0);
mediaBrowserListenableFuture.get().play();
@ -167,7 +167,7 @@ public class MediaManager {
try {
if (mediaBrowserListenableFuture.isDone()) {
mediaBrowserListenableFuture.get().clearMediaItems();
mediaBrowserListenableFuture.get().setMediaItem(MappingUtil.mapMediaItem(context, song));
mediaBrowserListenableFuture.get().setMediaItem(MappingUtil.mapMediaItem(context, song, true));
mediaBrowserListenableFuture.get().prepare();
mediaBrowserListenableFuture.get().play();
enqueueDatabase(song, true, 0);
@ -186,10 +186,10 @@ public class MediaManager {
if (mediaBrowserListenableFuture.isDone()) {
if (playImmediatelyAfter && mediaBrowserListenableFuture.get().getNextMediaItemIndex() != -1) {
enqueueDatabase(songs, false, mediaBrowserListenableFuture.get().getNextMediaItemIndex());
mediaBrowserListenableFuture.get().addMediaItems(mediaBrowserListenableFuture.get().getNextMediaItemIndex(), MappingUtil.mapMediaItems(context, songs));
mediaBrowserListenableFuture.get().addMediaItems(mediaBrowserListenableFuture.get().getNextMediaItemIndex(), MappingUtil.mapMediaItems(context, songs, true));
} else {
enqueueDatabase(songs, false, mediaBrowserListenableFuture.get().getMediaItemCount());
mediaBrowserListenableFuture.get().addMediaItems(MappingUtil.mapMediaItems(context, songs));
mediaBrowserListenableFuture.get().addMediaItems(MappingUtil.mapMediaItems(context, songs, true));
}
}
} catch (ExecutionException | InterruptedException e) {
@ -206,10 +206,10 @@ public class MediaManager {
if (mediaBrowserListenableFuture.isDone()) {
if (playImmediatelyAfter && mediaBrowserListenableFuture.get().getNextMediaItemIndex() != -1) {
enqueueDatabase(song, false, mediaBrowserListenableFuture.get().getNextMediaItemIndex());
mediaBrowserListenableFuture.get().addMediaItem(mediaBrowserListenableFuture.get().getNextMediaItemIndex(), MappingUtil.mapMediaItem(context, song));
mediaBrowserListenableFuture.get().addMediaItem(mediaBrowserListenableFuture.get().getNextMediaItemIndex(), MappingUtil.mapMediaItem(context, song, true));
} else {
enqueueDatabase(song, false, mediaBrowserListenableFuture.get().getMediaItemCount());
mediaBrowserListenableFuture.get().addMediaItem(MappingUtil.mapMediaItem(context, song));
mediaBrowserListenableFuture.get().addMediaItem(MappingUtil.mapMediaItem(context, song, true));
}
}
} catch (ExecutionException | InterruptedException e) {

View file

@ -9,11 +9,15 @@ import androidx.media3.common.AudioAttributes;
import androidx.media3.common.C;
import androidx.media3.common.MediaItem;
import androidx.media3.common.Player;
import androidx.media3.datasource.DataSource;
import androidx.media3.exoplayer.ExoPlayer;
import androidx.media3.exoplayer.source.DefaultMediaSourceFactory;
import androidx.media3.exoplayer.source.MediaSourceFactory;
import androidx.media3.session.MediaLibraryService;
import androidx.media3.session.MediaSession;
import com.cappielloantonio.play.ui.activity.MainActivity;
import com.cappielloantonio.play.util.DownloadUtil;
public class MediaService extends MediaLibraryService {
private static final String TAG = "MediaService";
@ -21,11 +25,14 @@ public class MediaService extends MediaLibraryService {
public static final int REQUEST_CODE = 432;
private ExoPlayer player;
private DataSource.Factory dataSourceFactory;
private MediaSourceFactory mediaSourceFactory;
private MediaLibrarySession mediaLibrarySession;
@Override
public void onCreate() {
super.onCreate();
initializeMediaSource();
initializePlayer();
initializePlayerListener();
}
@ -42,9 +49,16 @@ public class MediaService extends MediaLibraryService {
return mediaLibrarySession;
}
@SuppressLint("UnsafeOptInUsageError")
private void initializeMediaSource() {
dataSourceFactory = DownloadUtil.getDataSourceFactory(this);
mediaSourceFactory = new DefaultMediaSourceFactory(dataSourceFactory);
}
@SuppressLint("UnsafeOptInUsageError")
private void initializePlayer() {
player = new ExoPlayer.Builder(this)
.setMediaSourceFactory(mediaSourceFactory)
.setAudioAttributes(AudioAttributes.DEFAULT, true)
.setHandleAudioBecomingNoisy(true)
.setWakeMode(C.WAKE_MODE_NETWORK)