Refactor code and clean up unused resources

- Replace `suspendCoroutine` with `suspendCancellableCoroutine` in helper classes for better cancellation support.
- Replace `bundleOf` with manual `Bundle` initialization in `MediaControllerExt` and `SettingsFragment`.
- Remove unused constants in `Keys.kt` and redundant `onTerminate` override in `Radio.kt`.
- Clean up syntax and remove semicolons in `MainActivity.kt`.
This commit is contained in:
2026-03-24 12:36:04 +01:00
parent 5978aab0aa
commit 5334b88f1d
7 changed files with 22 additions and 29 deletions

View File

@@ -78,7 +78,6 @@ object Keys {
// default const values
const val DEFAULT_SIZE_OF_METADATA_HISTORY: Int = 25
const val DEFAULT_MAX_LENGTH_OF_METADATA_ENTRY: Int = 127
const val DEFAULT_DOWNLOAD_OVER_MOBILE: Boolean = false
const val ACTIVE_DOWNLOADS_EMPTY: String = "zero"
const val DEFAULT_MAX_RECONNECTION_COUNT: Int = 30
const val LARGE_BUFFER_SIZE_MULTIPLIER: Int = 8
@@ -149,9 +148,6 @@ object Keys {
const val RADIO_BROWSER_API_BASE: String = "all.api.radio-browser.info"
const val RADIO_BROWSER_API_DEFAULT: String = "de1.api.radio-browser.info"
// locations
const val LOCATION_DEFAULT_STATION_IMAGE: String = "android.resource://com.michatec.radio/drawable/ic_default_station_image_24dp"
// sizes (in dp)
const val SIZE_STATION_IMAGE_CARD: Int = 72
const val SIZE_STATION_IMAGE_MAXIMUM: Int = 640

View File

@@ -25,7 +25,7 @@ import androidx.navigation.ui.navigateUp
import com.michatec.radio.helpers.AppThemeHelper
import com.michatec.radio.helpers.FileHelper
import com.michatec.radio.helpers.PreferencesHelper
import org.woheller69.freeDroidWarn.FreeDroidWarn;
import org.woheller69.freeDroidWarn.FreeDroidWarn
/*
* MainActivity class
@@ -41,7 +41,7 @@ class MainActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
// Free Android
FreeDroidWarn.showWarningOnUpgrade(this, BuildConfig.VERSION_CODE);
FreeDroidWarn.showWarningOnUpgrade(this, BuildConfig.VERSION_CODE)
// set up views
setContentView(R.layout.activity_main)

View File

@@ -34,9 +34,4 @@ class Radio : Application() {
AppThemeHelper.setTheme(PreferencesHelper.loadThemeSelection())
}
/* Implements onTerminate */
override fun onTerminate() {
super.onTerminate()
}
}

View File

@@ -28,7 +28,6 @@ import androidx.activity.result.ActivityResult
import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult
import androidx.appcompat.app.AppCompatActivity
import androidx.core.net.toUri
import androidx.core.os.bundleOf
import androidx.navigation.fragment.findNavController
import androidx.preference.*
import com.google.android.material.snackbar.Snackbar
@@ -427,9 +426,9 @@ class SettingsFragment : PreferenceFragmentCompat(), YesNoDialog.YesNoDialogList
val sourceUri: Uri? = result.data?.data
if (sourceUri != null) {
// open and import OPML in player fragment
val bundle: Bundle = bundleOf(
Keys.ARG_RESTORE_COLLECTION to "$sourceUri"
)
val bundle = Bundle().apply {
putString(Keys.ARG_RESTORE_COLLECTION, "$sourceUri")
}
this.findNavController().navigate(R.id.player_destination, bundle)
}
}
@@ -445,7 +444,9 @@ class SettingsFragment : PreferenceFragmentCompat(), YesNoDialog.YesNoDialogList
Snackbar.LENGTH_LONG
).show()
// update collection in player screen
val bundle: Bundle = bundleOf(Keys.ARG_UPDATE_COLLECTION to true)
val bundle = Bundle().apply {
putBoolean(Keys.ARG_UPDATE_COLLECTION, true)
}
this.findNavController().navigate(R.id.player_destination, bundle)
} else {
ErrorDialog().show(
@@ -466,9 +467,9 @@ class SettingsFragment : PreferenceFragmentCompat(), YesNoDialog.YesNoDialogList
Snackbar.LENGTH_LONG
).show()
// update collection in player screen
val bundle: Bundle = bundleOf(
Keys.ARG_UPDATE_IMAGES to true
)
val bundle = Bundle().apply {
putBoolean(Keys.ARG_UPDATE_IMAGES, true)
}
this.findNavController().navigate(R.id.player_destination, bundle)
} else {
ErrorDialog().show(

View File

@@ -16,7 +16,6 @@ package com.michatec.radio.extensions
import android.content.Context
import android.os.Bundle
import androidx.core.os.bundleOf
import androidx.media3.session.MediaController
import androidx.media3.session.SessionCommand
import androidx.media3.session.SessionResult
@@ -71,8 +70,11 @@ fun MediaController.play(context: Context, station: Station) {
/* Starts playback with of a stream url */
fun MediaController.playStreamDirectly(streamUri: String) {
val bundle = Bundle().apply {
putString(Keys.KEY_STREAM_URI, streamUri)
}
sendCustomCommand(
SessionCommand(Keys.CMD_PLAY_STREAM, Bundle.EMPTY),
bundleOf(Pair(Keys.KEY_STREAM_URI, streamUri))
bundle
)
}

View File

@@ -33,7 +33,6 @@ import kotlinx.coroutines.Dispatchers.IO
import java.io.*
import java.util.*
import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine
/*
@@ -292,7 +291,7 @@ object FileHelper {
collection: Collection,
lastUpdate: Date
) {
return suspendCoroutine { cont ->
return suspendCancellableCoroutine { cont ->
cont.resume(saveCollection(context, collection, lastUpdate))
}
}
@@ -311,7 +310,7 @@ object FileHelper {
originalFileUri: Uri,
targetFileUri: Uri
): Boolean {
return suspendCoroutine { cont ->
return suspendCancellableCoroutine { cont ->
cont.resume(copyFile(context, originalFileUri, targetFileUri))
}
}
@@ -319,7 +318,7 @@ object FileHelper {
/* Suspend function: Exports collection of stations as M3U file - local backup copy */
suspend fun backupCollectionAsM3uSuspended(context: Context, collection: Collection) {
return suspendCoroutine { cont ->
return suspendCancellableCoroutine { cont ->
Log.v(TAG, "Backing up collection as M3U - Thread: ${Thread.currentThread().name}")
// create M3U string
val m3uString: String = CollectionHelper.createM3uString(collection)
@@ -338,7 +337,7 @@ object FileHelper {
/* Suspend function: Exports collection of stations as PLS file - local backup copy */
suspend fun backupCollectionAsPlsSuspended(context: Context, collection: Collection) {
return suspendCoroutine { cont ->
return suspendCancellableCoroutine { cont ->
Log.v(TAG, "Backing up collection as PLS - Thread: ${Thread.currentThread().name}")
// create PLS string
val plsString: String = CollectionHelper.createPlsString(collection)

View File

@@ -19,13 +19,13 @@ import android.net.ConnectivityManager
import android.net.Network
import android.util.Log
import com.michatec.radio.Keys
import kotlinx.coroutines.suspendCancellableCoroutine
import java.net.HttpURLConnection
import java.net.InetAddress
import java.net.URL
import java.net.UnknownHostException
import java.util.*
import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine
/*
@@ -105,7 +105,7 @@ object NetworkHelper {
/* Suspend function: Detects content type (mime type) from given URL string - async using coroutine */
suspend fun detectContentTypeSuspended(urlString: String): ContentType {
return suspendCoroutine { cont ->
return suspendCancellableCoroutine { cont ->
cont.resume(detectContentType(urlString))
}
}
@@ -113,7 +113,7 @@ object NetworkHelper {
/* Suspend function: Gets a random radio-browser.info api address - async using coroutine */
suspend fun getRadioBrowserServerSuspended(): String {
return suspendCoroutine { cont ->
return suspendCancellableCoroutine { cont ->
val serverAddress: String = try {
// get all available radio browser servers
val serverAddressList: Array<InetAddress> =