mirror of
https://github.com/Michatec/Radio.git
synced 2026-03-31 23:46:28 +02:00
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:
@@ -78,7 +78,6 @@ object Keys {
|
|||||||
// default const values
|
// default const values
|
||||||
const val DEFAULT_SIZE_OF_METADATA_HISTORY: Int = 25
|
const val DEFAULT_SIZE_OF_METADATA_HISTORY: Int = 25
|
||||||
const val DEFAULT_MAX_LENGTH_OF_METADATA_ENTRY: Int = 127
|
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 ACTIVE_DOWNLOADS_EMPTY: String = "zero"
|
||||||
const val DEFAULT_MAX_RECONNECTION_COUNT: Int = 30
|
const val DEFAULT_MAX_RECONNECTION_COUNT: Int = 30
|
||||||
const val LARGE_BUFFER_SIZE_MULTIPLIER: Int = 8
|
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_BASE: String = "all.api.radio-browser.info"
|
||||||
const val RADIO_BROWSER_API_DEFAULT: String = "de1.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)
|
// sizes (in dp)
|
||||||
const val SIZE_STATION_IMAGE_CARD: Int = 72
|
const val SIZE_STATION_IMAGE_CARD: Int = 72
|
||||||
const val SIZE_STATION_IMAGE_MAXIMUM: Int = 640
|
const val SIZE_STATION_IMAGE_MAXIMUM: Int = 640
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ import androidx.navigation.ui.navigateUp
|
|||||||
import com.michatec.radio.helpers.AppThemeHelper
|
import com.michatec.radio.helpers.AppThemeHelper
|
||||||
import com.michatec.radio.helpers.FileHelper
|
import com.michatec.radio.helpers.FileHelper
|
||||||
import com.michatec.radio.helpers.PreferencesHelper
|
import com.michatec.radio.helpers.PreferencesHelper
|
||||||
import org.woheller69.freeDroidWarn.FreeDroidWarn;
|
import org.woheller69.freeDroidWarn.FreeDroidWarn
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* MainActivity class
|
* MainActivity class
|
||||||
@@ -41,7 +41,7 @@ class MainActivity : AppCompatActivity() {
|
|||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
|
|
||||||
// Free Android
|
// Free Android
|
||||||
FreeDroidWarn.showWarningOnUpgrade(this, BuildConfig.VERSION_CODE);
|
FreeDroidWarn.showWarningOnUpgrade(this, BuildConfig.VERSION_CODE)
|
||||||
|
|
||||||
// set up views
|
// set up views
|
||||||
setContentView(R.layout.activity_main)
|
setContentView(R.layout.activity_main)
|
||||||
|
|||||||
@@ -34,9 +34,4 @@ class Radio : Application() {
|
|||||||
AppThemeHelper.setTheme(PreferencesHelper.loadThemeSelection())
|
AppThemeHelper.setTheme(PreferencesHelper.loadThemeSelection())
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Implements onTerminate */
|
|
||||||
override fun onTerminate() {
|
|
||||||
super.onTerminate()
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,7 +28,6 @@ import androidx.activity.result.ActivityResult
|
|||||||
import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult
|
import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import androidx.core.net.toUri
|
import androidx.core.net.toUri
|
||||||
import androidx.core.os.bundleOf
|
|
||||||
import androidx.navigation.fragment.findNavController
|
import androidx.navigation.fragment.findNavController
|
||||||
import androidx.preference.*
|
import androidx.preference.*
|
||||||
import com.google.android.material.snackbar.Snackbar
|
import com.google.android.material.snackbar.Snackbar
|
||||||
@@ -427,9 +426,9 @@ class SettingsFragment : PreferenceFragmentCompat(), YesNoDialog.YesNoDialogList
|
|||||||
val sourceUri: Uri? = result.data?.data
|
val sourceUri: Uri? = result.data?.data
|
||||||
if (sourceUri != null) {
|
if (sourceUri != null) {
|
||||||
// open and import OPML in player fragment
|
// open and import OPML in player fragment
|
||||||
val bundle: Bundle = bundleOf(
|
val bundle = Bundle().apply {
|
||||||
Keys.ARG_RESTORE_COLLECTION to "$sourceUri"
|
putString(Keys.ARG_RESTORE_COLLECTION, "$sourceUri")
|
||||||
)
|
}
|
||||||
this.findNavController().navigate(R.id.player_destination, bundle)
|
this.findNavController().navigate(R.id.player_destination, bundle)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -445,7 +444,9 @@ class SettingsFragment : PreferenceFragmentCompat(), YesNoDialog.YesNoDialogList
|
|||||||
Snackbar.LENGTH_LONG
|
Snackbar.LENGTH_LONG
|
||||||
).show()
|
).show()
|
||||||
// update collection in player screen
|
// 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)
|
this.findNavController().navigate(R.id.player_destination, bundle)
|
||||||
} else {
|
} else {
|
||||||
ErrorDialog().show(
|
ErrorDialog().show(
|
||||||
@@ -466,9 +467,9 @@ class SettingsFragment : PreferenceFragmentCompat(), YesNoDialog.YesNoDialogList
|
|||||||
Snackbar.LENGTH_LONG
|
Snackbar.LENGTH_LONG
|
||||||
).show()
|
).show()
|
||||||
// update collection in player screen
|
// update collection in player screen
|
||||||
val bundle: Bundle = bundleOf(
|
val bundle = Bundle().apply {
|
||||||
Keys.ARG_UPDATE_IMAGES to true
|
putBoolean(Keys.ARG_UPDATE_IMAGES, true)
|
||||||
)
|
}
|
||||||
this.findNavController().navigate(R.id.player_destination, bundle)
|
this.findNavController().navigate(R.id.player_destination, bundle)
|
||||||
} else {
|
} else {
|
||||||
ErrorDialog().show(
|
ErrorDialog().show(
|
||||||
|
|||||||
@@ -16,7 +16,6 @@ package com.michatec.radio.extensions
|
|||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import androidx.core.os.bundleOf
|
|
||||||
import androidx.media3.session.MediaController
|
import androidx.media3.session.MediaController
|
||||||
import androidx.media3.session.SessionCommand
|
import androidx.media3.session.SessionCommand
|
||||||
import androidx.media3.session.SessionResult
|
import androidx.media3.session.SessionResult
|
||||||
@@ -71,8 +70,11 @@ fun MediaController.play(context: Context, station: Station) {
|
|||||||
|
|
||||||
/* Starts playback with of a stream url */
|
/* Starts playback with of a stream url */
|
||||||
fun MediaController.playStreamDirectly(streamUri: String) {
|
fun MediaController.playStreamDirectly(streamUri: String) {
|
||||||
|
val bundle = Bundle().apply {
|
||||||
|
putString(Keys.KEY_STREAM_URI, streamUri)
|
||||||
|
}
|
||||||
sendCustomCommand(
|
sendCustomCommand(
|
||||||
SessionCommand(Keys.CMD_PLAY_STREAM, Bundle.EMPTY),
|
SessionCommand(Keys.CMD_PLAY_STREAM, Bundle.EMPTY),
|
||||||
bundleOf(Pair(Keys.KEY_STREAM_URI, streamUri))
|
bundle
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@@ -33,7 +33,6 @@ import kotlinx.coroutines.Dispatchers.IO
|
|||||||
import java.io.*
|
import java.io.*
|
||||||
import java.util.*
|
import java.util.*
|
||||||
import kotlin.coroutines.resume
|
import kotlin.coroutines.resume
|
||||||
import kotlin.coroutines.suspendCoroutine
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -292,7 +291,7 @@ object FileHelper {
|
|||||||
collection: Collection,
|
collection: Collection,
|
||||||
lastUpdate: Date
|
lastUpdate: Date
|
||||||
) {
|
) {
|
||||||
return suspendCoroutine { cont ->
|
return suspendCancellableCoroutine { cont ->
|
||||||
cont.resume(saveCollection(context, collection, lastUpdate))
|
cont.resume(saveCollection(context, collection, lastUpdate))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -311,7 +310,7 @@ object FileHelper {
|
|||||||
originalFileUri: Uri,
|
originalFileUri: Uri,
|
||||||
targetFileUri: Uri
|
targetFileUri: Uri
|
||||||
): Boolean {
|
): Boolean {
|
||||||
return suspendCoroutine { cont ->
|
return suspendCancellableCoroutine { cont ->
|
||||||
cont.resume(copyFile(context, originalFileUri, targetFileUri))
|
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 function: Exports collection of stations as M3U file - local backup copy */
|
||||||
suspend fun backupCollectionAsM3uSuspended(context: Context, collection: Collection) {
|
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}")
|
Log.v(TAG, "Backing up collection as M3U - Thread: ${Thread.currentThread().name}")
|
||||||
// create M3U string
|
// create M3U string
|
||||||
val m3uString: String = CollectionHelper.createM3uString(collection)
|
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 function: Exports collection of stations as PLS file - local backup copy */
|
||||||
suspend fun backupCollectionAsPlsSuspended(context: Context, collection: Collection) {
|
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}")
|
Log.v(TAG, "Backing up collection as PLS - Thread: ${Thread.currentThread().name}")
|
||||||
// create PLS string
|
// create PLS string
|
||||||
val plsString: String = CollectionHelper.createPlsString(collection)
|
val plsString: String = CollectionHelper.createPlsString(collection)
|
||||||
|
|||||||
@@ -19,13 +19,13 @@ import android.net.ConnectivityManager
|
|||||||
import android.net.Network
|
import android.net.Network
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import com.michatec.radio.Keys
|
import com.michatec.radio.Keys
|
||||||
|
import kotlinx.coroutines.suspendCancellableCoroutine
|
||||||
import java.net.HttpURLConnection
|
import java.net.HttpURLConnection
|
||||||
import java.net.InetAddress
|
import java.net.InetAddress
|
||||||
import java.net.URL
|
import java.net.URL
|
||||||
import java.net.UnknownHostException
|
import java.net.UnknownHostException
|
||||||
import java.util.*
|
import java.util.*
|
||||||
import kotlin.coroutines.resume
|
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 function: Detects content type (mime type) from given URL string - async using coroutine */
|
||||||
suspend fun detectContentTypeSuspended(urlString: String): ContentType {
|
suspend fun detectContentTypeSuspended(urlString: String): ContentType {
|
||||||
return suspendCoroutine { cont ->
|
return suspendCancellableCoroutine { cont ->
|
||||||
cont.resume(detectContentType(urlString))
|
cont.resume(detectContentType(urlString))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -113,7 +113,7 @@ object NetworkHelper {
|
|||||||
|
|
||||||
/* Suspend function: Gets a random radio-browser.info api address - async using coroutine */
|
/* Suspend function: Gets a random radio-browser.info api address - async using coroutine */
|
||||||
suspend fun getRadioBrowserServerSuspended(): String {
|
suspend fun getRadioBrowserServerSuspended(): String {
|
||||||
return suspendCoroutine { cont ->
|
return suspendCancellableCoroutine { cont ->
|
||||||
val serverAddress: String = try {
|
val serverAddress: String = try {
|
||||||
// get all available radio browser servers
|
// get all available radio browser servers
|
||||||
val serverAddressList: Array<InetAddress> =
|
val serverAddressList: Array<InetAddress> =
|
||||||
|
|||||||
Reference in New Issue
Block a user