From dacaa03eb75ea737b8b0b0a5e4e88bcf76748ea4 Mon Sep 17 00:00:00 2001 From: CappielloAntonio Date: Sat, 25 May 2024 17:55:30 +0200 Subject: [PATCH] style: code clean up --- .../tempo/ui/fragment/SettingsFragment.java | 38 +++++++++++-------- .../tempo/util/DownloadUtil.java | 4 +- .../tempo/util/Preferences.kt | 3 -- .../tempo/util/StreamingCacheDataSource.kt | 5 ++- app/src/main/res/values/strings.xml | 3 +- 5 files changed, 29 insertions(+), 24 deletions(-) diff --git a/app/src/main/java/com/cappielloantonio/tempo/ui/fragment/SettingsFragment.java b/app/src/main/java/com/cappielloantonio/tempo/ui/fragment/SettingsFragment.java index 0fd3fc5e..5b1cc9fe 100644 --- a/app/src/main/java/com/cappielloantonio/tempo/ui/fragment/SettingsFragment.java +++ b/app/src/main/java/com/cappielloantonio/tempo/ui/fragment/SettingsFragment.java @@ -31,6 +31,7 @@ import com.cappielloantonio.tempo.ui.dialog.DeleteDownloadStorageDialog; import com.cappielloantonio.tempo.ui.dialog.DownloadStorageDialog; import com.cappielloantonio.tempo.ui.dialog.StarredSyncDialog; import com.cappielloantonio.tempo.util.DownloadUtil; +import com.cappielloantonio.tempo.util.MusicUtil; import com.cappielloantonio.tempo.util.Preferences; import com.cappielloantonio.tempo.util.UIUtil; import com.cappielloantonio.tempo.viewmodel.SettingViewModel; @@ -85,6 +86,7 @@ public class SettingsFragment extends PreferenceFragmentCompat { checkEqualizer(); checkStorage(); + setStreamingCacheSize(); setAppLanguage(); setVersion(); @@ -114,22 +116,6 @@ public class SettingsFragment extends PreferenceFragmentCompat { return true; }); } - - ListPreference streamingCachePreference = findPreference("streaming_cache_size"); - if (streamingCachePreference != null) { - streamingCachePreference.setSummaryProvider(new Preference.SummaryProvider() { - @Nullable - @Override - public CharSequence provideSummary(@NonNull ListPreference preference) { - CharSequence entry = preference.getEntry(); - if (entry == null) { - return null; - } - long currentSizeMb = DownloadUtil.getStreamingCacheSize(requireActivity()) / (1024 * 1024); - return entry + "\nCurrently in use: " + + currentSizeMb + " MiB\nRestarting is required if changed."; - } - }); - } } private void checkEqualizer() { @@ -165,6 +151,26 @@ public class SettingsFragment extends PreferenceFragmentCompat { } } + private void setStreamingCacheSize() { + ListPreference streamingCachePreference = findPreference("streaming_cache_size"); + + if (streamingCachePreference != null) { + streamingCachePreference.setSummaryProvider(new Preference.SummaryProvider() { + @Nullable + @Override + public CharSequence provideSummary(@NonNull ListPreference preference) { + CharSequence entry = preference.getEntry(); + + if (entry == null) return null; + + long currentSizeMb = DownloadUtil.getStreamingCacheSize(requireActivity()) / (1024 * 1024); + + return getString(R.string.settings_summary_streaming_cache_size, entry, currentSizeMb); + } + }); + } + } + private void setAppLanguage() { ListPreference localePref = (ListPreference) findPreference("language"); diff --git a/app/src/main/java/com/cappielloantonio/tempo/util/DownloadUtil.java b/app/src/main/java/com/cappielloantonio/tempo/util/DownloadUtil.java index 06109f53..3a6d2237 100644 --- a/app/src/main/java/com/cappielloantonio/tempo/util/DownloadUtil.java +++ b/app/src/main/java/com/cappielloantonio/tempo/util/DownloadUtil.java @@ -81,7 +81,6 @@ public final class DownloadUtil { DefaultDataSource.Factory upstreamFactory = new DefaultDataSource.Factory(context, getHttpDataSourceFactory()); if (Preferences.getStreamingCacheSize() > 0) { - // Cache enabled CacheDataSource.Factory streamCacheFactory = new CacheDataSource.Factory() .setCache(getStreamingCache(context)) .setUpstreamDataSourceFactory(upstreamFactory); @@ -97,7 +96,6 @@ public final class DownloadUtil { dataSourceFactory = buildReadOnlyCacheDataSource(resolvingFactory, getDownloadCache(context)); } else { - // Cache disabled dataSourceFactory = buildReadOnlyCacheDataSource(upstreamFactory, getDownloadCache(context)); } } @@ -135,12 +133,14 @@ public final class DownloadUtil { private static synchronized SimpleCache getStreamingCache(Context context) { if (streamingCache == null) { File streamingCacheDirectory = new File(context.getCacheDir(), "streamingCache"); + streamingCache = new SimpleCache( streamingCacheDirectory, new LeastRecentlyUsedCacheEvictor(Preferences.getStreamingCacheSize() * 1024 * 1024), getDatabaseProvider(context) ); } + return streamingCache; } diff --git a/app/src/main/java/com/cappielloantonio/tempo/util/Preferences.kt b/app/src/main/java/com/cappielloantonio/tempo/util/Preferences.kt index a7e968c8..66d948ec 100644 --- a/app/src/main/java/com/cappielloantonio/tempo/util/Preferences.kt +++ b/app/src/main/java/com/cappielloantonio/tempo/util/Preferences.kt @@ -190,9 +190,6 @@ object Preferences { return App.getInstance().preferences.getString(IMAGE_SIZE, "-1")!!.toInt() } - /** - * Size of streaming cache in MiB. - */ @JvmStatic fun getStreamingCacheSize(): Long { return App.getInstance().preferences.getString(STREAMING_CACHE_SIZE, "256")!!.toLong() diff --git a/app/src/main/java/com/cappielloantonio/tempo/util/StreamingCacheDataSource.kt b/app/src/main/java/com/cappielloantonio/tempo/util/StreamingCacheDataSource.kt index bdb613e7..68b52cb7 100644 --- a/app/src/main/java/com/cappielloantonio/tempo/util/StreamingCacheDataSource.kt +++ b/app/src/main/java/com/cappielloantonio/tempo/util/StreamingCacheDataSource.kt @@ -35,7 +35,6 @@ class StreamingCacheDataSource private constructor( override fun open(dataSpec: DataSpec): Long { val ret = cacheDataSource.open(dataSpec) currentDataSpec = dataSpec - Log.d(TAG, "Opened $currentDataSpec") return ret } @@ -45,11 +44,13 @@ class StreamingCacheDataSource private constructor( override fun close() { cacheDataSource.close() - Log.d(TAG, "Closed $currentDataSpec") + val dataSpec = currentDataSpec + if (dataSpec != null) { val cacheKey = cacheDataSource.cacheKeyFactory.buildCacheKey(dataSpec) val contentLength = ContentMetadata.getContentLength(cacheDataSource.cache.getContentMetadata(cacheKey)); + if (contentLength == C.LENGTH_UNSET.toLong()) { Log.d(TAG, "Removing partial cache for $cacheKey") cacheDataSource.cache.removeResource(cacheKey) diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index dd1804df..ec8eccd6 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -288,11 +288,13 @@ Enable music scrobbling Enable music sharing It\'s important to note that scrobbling also relies on the server being enabled to receive this data. + Size of streaming cache When listening to an artist\'s radio, an instant mix or when shuffling all, tracks below a certain user rating will be ignored. Replay gain is a feature that allows you to adjust the volume level of audio tracks for a consistent listening experience. This setting is only effective if the track contains the necessary metadata. Scrobbling is a feature that allows your device to send information about the songs you listen to the music server. This information helps create personalized recommendations based on your music preferences. Allows the user to share music via a link. The functionality must be supported and enabled server-side and is limited to individual tracks, albums and playlists. Returns the state of the play queue for this user. This includes the tracks in the play queue, the currently playing track, and the position within this track. The server must support this feature. + %1$s \nCurrently in use: %2$s MiB Priority given to the transcoding mode. If set to \"Direct play\" the bitrate of the file will not be changed. Download transcoded media. If enabled, the download endpoint will not be used, but the following settings. \n\n If \"Transcode format for donwloads\" is set to \"Direct download\" the bitrate of the file will not be changed. When the file is transcoded on the fly, the client usually does not show the track length. It is possible to request the servers that support the functionality to estimate the duration of the track being played, but the response times may take longer. @@ -377,5 +379,4 @@ unDraw A special thanks goes to unDraw without whose illustrations we could not have made this application more beautiful. https://undraw.co/ - Size of streaming cache