style: code clean up

This commit is contained in:
CappielloAntonio 2024-05-25 17:55:30 +02:00
parent a3d8b75d07
commit dacaa03eb7
5 changed files with 29 additions and 24 deletions

View file

@ -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<ListPreference>() {
@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<ListPreference>() {
@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");

View file

@ -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;
}

View file

@ -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()

View file

@ -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)