mirror of
https://github.com/antebudimir/tempus.git
synced 2026-01-01 09:53:33 +00:00
Reimplemented a basic track download functionality
This commit is contained in:
parent
b7a77cf32b
commit
dee845ebff
17 changed files with 154 additions and 214 deletions
|
|
@ -14,8 +14,6 @@ import androidx.media3.datasource.cache.Cache;
|
|||
import androidx.media3.datasource.cache.CacheDataSource;
|
||||
import androidx.media3.datasource.cache.NoOpCacheEvictor;
|
||||
import androidx.media3.datasource.cache.SimpleCache;
|
||||
import androidx.media3.exoplayer.DefaultRenderersFactory;
|
||||
import androidx.media3.exoplayer.RenderersFactory;
|
||||
import androidx.media3.exoplayer.offline.ActionFileUpgradeUtil;
|
||||
import androidx.media3.exoplayer.offline.DefaultDownloadIndex;
|
||||
import androidx.media3.exoplayer.offline.DownloadManager;
|
||||
|
|
@ -48,26 +46,7 @@ public final class DownloadUtil {
|
|||
private static DownloaderTracker downloaderTracker;
|
||||
private static DownloadNotificationHelper downloadNotificationHelper;
|
||||
|
||||
/**
|
||||
* Returns whether extension renderers should be used.
|
||||
*/
|
||||
public static boolean useExtensionRenderers() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@SuppressLint("UnsafeOptInUsageError")
|
||||
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 HttpDataSource.Factory getHttpDataSourceFactory(Context context) {
|
||||
public static synchronized HttpDataSource.Factory getHttpDataSourceFactory() {
|
||||
if (httpDataSourceFactory == null) {
|
||||
CookieManager cookieManager = new CookieManager();
|
||||
cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ORIGINAL_SERVER);
|
||||
|
|
@ -81,7 +60,7 @@ public final class DownloadUtil {
|
|||
public static synchronized DataSource.Factory getDataSourceFactory(Context context) {
|
||||
if (dataSourceFactory == null) {
|
||||
context = context.getApplicationContext();
|
||||
DefaultDataSource.Factory upstreamFactory = new DefaultDataSource.Factory(context, getHttpDataSourceFactory(context));
|
||||
DefaultDataSource.Factory upstreamFactory = new DefaultDataSource.Factory(context, getHttpDataSourceFactory());
|
||||
dataSourceFactory = buildReadOnlyCacheDataSource(upstreamFactory, getDownloadCache(context));
|
||||
}
|
||||
|
||||
|
|
@ -113,6 +92,7 @@ public final class DownloadUtil {
|
|||
File downloadContentDirectory = new File(getDownloadDirectory(context), DOWNLOAD_CONTENT_DIRECTORY);
|
||||
downloadCache = new SimpleCache(downloadContentDirectory, new NoOpCacheEvictor(), getDatabaseProvider(context));
|
||||
}
|
||||
|
||||
return downloadCache;
|
||||
}
|
||||
|
||||
|
|
@ -120,10 +100,10 @@ public final class DownloadUtil {
|
|||
private static synchronized void ensureDownloadManagerInitialized(Context context) {
|
||||
if (downloadManager == null) {
|
||||
DefaultDownloadIndex downloadIndex = new DefaultDownloadIndex(getDatabaseProvider(context));
|
||||
upgradeActionFile(context, DOWNLOAD_ACTION_FILE, downloadIndex, /* addNewDownloadsAsCompleted= */ false);
|
||||
upgradeActionFile(context, DOWNLOAD_ACTION_FILE, downloadIndex, false);
|
||||
upgradeActionFile(context, DOWNLOAD_TRACKER_ACTION_FILE, downloadIndex, true);
|
||||
downloadManager = new DownloadManager(context, getDatabaseProvider(context), getDownloadCache(context), getHttpDataSourceFactory(context), Executors.newFixedThreadPool(/* nThreads= */ 6));
|
||||
downloaderTracker = new DownloaderTracker(context, getHttpDataSourceFactory(context), downloadManager);
|
||||
downloadManager = new DownloadManager(context, getDatabaseProvider(context), getDownloadCache(context), getHttpDataSourceFactory(), Executors.newFixedThreadPool(6));
|
||||
downloaderTracker = new DownloaderTracker(context, downloadManager);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -141,12 +121,13 @@ public final class DownloadUtil {
|
|||
if (databaseProvider == null) {
|
||||
databaseProvider = new StandaloneDatabaseProvider(context);
|
||||
}
|
||||
|
||||
return databaseProvider;
|
||||
}
|
||||
|
||||
private static synchronized File getDownloadDirectory(Context context) {
|
||||
if (downloadDirectory == null) {
|
||||
downloadDirectory = context.getExternalFilesDir(/* type= */ null);
|
||||
downloadDirectory = context.getExternalFilesDir(null);
|
||||
if (downloadDirectory == null) {
|
||||
downloadDirectory = context.getFilesDir();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -197,7 +197,7 @@ public class MappingUtil {
|
|||
return genres;
|
||||
}
|
||||
|
||||
public static MediaItem mapMediaItem(Context context, Song song) {
|
||||
public static MediaItem mapMediaItem(Context context, Song song, boolean isForStreaming) {
|
||||
Bundle bundle = new Bundle();
|
||||
bundle.putString("id", song.getId());
|
||||
bundle.putString("albumId", song.getAlbumId());
|
||||
|
|
@ -207,7 +207,7 @@ public class MappingUtil {
|
|||
.setMediaId(song.getId())
|
||||
.setMediaMetadata(
|
||||
new MediaMetadata.Builder()
|
||||
.setMediaUri(MusicUtil.getSongStreamUri(context, song))
|
||||
.setMediaUri(isForStreaming ? MusicUtil.getSongStreamUri(context, song) : MusicUtil.getSongDownloadUri(song))
|
||||
.setTitle(MusicUtil.getReadableString(song.getTitle()))
|
||||
.setTrackNumber(song.getTrackNumber())
|
||||
.setDiscNumber(song.getDiscNumber())
|
||||
|
|
@ -217,14 +217,15 @@ public class MappingUtil {
|
|||
.setExtras(bundle)
|
||||
.build()
|
||||
)
|
||||
.setUri(isForStreaming ? MusicUtil.getSongStreamUri(context, song) : MusicUtil.getSongDownloadUri(song))
|
||||
.build();
|
||||
}
|
||||
|
||||
public static ArrayList<MediaItem> mapMediaItems(Context context, List<Song> songs) {
|
||||
public static ArrayList<MediaItem> mapMediaItems(Context context, List<Song> songs, boolean isForStreaming) {
|
||||
ArrayList<MediaItem> mediaItems = new ArrayList();
|
||||
|
||||
for (Song song : songs) {
|
||||
mediaItems.add(mapMediaItem(context, song));
|
||||
mediaItems.add(mapMediaItem(context, song, isForStreaming));
|
||||
}
|
||||
|
||||
return mediaItems;
|
||||
|
|
|
|||
|
|
@ -7,8 +7,6 @@ import android.net.Uri;
|
|||
import android.text.Html;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.media3.common.MediaItem;
|
||||
|
||||
import com.cappielloantonio.play.App;
|
||||
import com.cappielloantonio.play.R;
|
||||
import com.cappielloantonio.play.glide.CustomGlideRequest;
|
||||
|
|
@ -45,7 +43,7 @@ public class MusicUtil {
|
|||
}
|
||||
|
||||
@SuppressLint("UnsafeOptInUsageError")
|
||||
public static MediaItem getSongDownloadItem(Song song) {
|
||||
public static Uri getSongDownloadUri(Song song) {
|
||||
Map<String, String> params = App.getSubsonicClientInstance(App.getInstance(), false).getParams();
|
||||
|
||||
String uri = App.getSubsonicClientInstance(App.getInstance(), false).getUrl() +
|
||||
|
|
@ -57,7 +55,9 @@ public class MusicUtil {
|
|||
"&c=" + params.get("c") +
|
||||
"&id=" + song.getId();
|
||||
|
||||
return MediaItem.fromUri(uri);
|
||||
Log.d(TAG, "getSongDownloadUri(): " + uri);
|
||||
|
||||
return Uri.parse(uri);
|
||||
}
|
||||
|
||||
public static String getReadableDurationString(long duration, boolean millis) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue