2021-07-12 10:14:26 +02:00
|
|
|
package audio.funkwhale.ffa.utils
|
2019-08-19 16:50:33 +02:00
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
import android.widget.Toast
|
|
|
|
import com.google.android.exoplayer2.util.Log
|
2021-08-13 10:55:26 +02:00
|
|
|
import com.google.android.exoplayer2.util.Log.LOG_LEVEL_ERROR
|
|
|
|
import com.google.android.exoplayer2.util.Log.LOG_LEVEL_INFO
|
2019-08-19 16:50:33 +02:00
|
|
|
import com.preference.PowerPreference
|
|
|
|
import java.net.URI
|
|
|
|
|
|
|
|
fun Context?.toast(message: String, length: Int = Toast.LENGTH_SHORT) {
|
|
|
|
if (this != null) {
|
|
|
|
Toast.makeText(this, message, length).show()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-12 15:19:33 +02:00
|
|
|
private fun logClassName(): String {
|
|
|
|
val known = setOf(
|
|
|
|
"dalvik.system.VMStack",
|
|
|
|
"java.lang.Thread",
|
2021-07-12 10:14:26 +02:00
|
|
|
"audio.funkwhale.ffa.utils.UtilKt"
|
2020-07-12 15:19:33 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
Thread.currentThread().stackTrace.forEach {
|
|
|
|
if (!known.contains(it.className)) {
|
|
|
|
val className = it.className.split('.').last()
|
|
|
|
val line = it.lineNumber
|
|
|
|
|
|
|
|
return "$className:$line"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "UNKNOWN"
|
|
|
|
}
|
|
|
|
|
2021-08-13 10:55:26 +02:00
|
|
|
enum class LogLevel(value: Int) {
|
|
|
|
INFO(LOG_LEVEL_INFO),
|
|
|
|
DEBUG(Log.LOG_LEVEL_ALL),
|
|
|
|
ERROR(LOG_LEVEL_ERROR)
|
|
|
|
}
|
|
|
|
|
|
|
|
fun Any?.logError(prefix: String? = null) = this.log(prefix, LogLevel.ERROR)
|
|
|
|
fun Any?.logInfo(prefix: String? = null) = this.log(prefix, LogLevel.INFO)
|
|
|
|
|
|
|
|
fun Any?.log(prefix: String? = null, logLevel: LogLevel = LogLevel.DEBUG) {
|
|
|
|
val tag = "FFA"
|
|
|
|
val message = "${logClassName()} - ${prefix?.let { "$it: " }}$this"
|
|
|
|
when (logLevel) {
|
|
|
|
LogLevel.DEBUG -> Log.d(tag, message)
|
|
|
|
LogLevel.INFO -> Log.i(tag, message)
|
|
|
|
LogLevel.ERROR -> Log.e(tag, message)
|
|
|
|
}
|
2020-05-30 21:16:28 +02:00
|
|
|
}
|
|
|
|
|
2020-05-31 03:54:57 +02:00
|
|
|
fun maybeNormalizeUrl(rawUrl: String?): String? {
|
2020-06-21 14:16:30 +02:00
|
|
|
try {
|
|
|
|
if (rawUrl == null || rawUrl.isEmpty()) return null
|
2019-10-22 21:56:33 +02:00
|
|
|
|
2020-06-21 14:16:30 +02:00
|
|
|
return mustNormalizeUrl(rawUrl)
|
|
|
|
} catch (e: Exception) {
|
|
|
|
return null
|
|
|
|
}
|
2019-10-22 21:56:33 +02:00
|
|
|
}
|
|
|
|
|
2020-05-31 03:54:57 +02:00
|
|
|
fun mustNormalizeUrl(rawUrl: String): String {
|
2021-07-23 14:10:13 +02:00
|
|
|
val fallbackHost =
|
|
|
|
PowerPreference.getFileByName(AppContext.PREFS_CREDENTIALS).getString("hostname")
|
2020-05-31 03:54:57 +02:00
|
|
|
val uri = URI(rawUrl).takeIf { it.host != null } ?: URI("$fallbackHost$rawUrl")
|
2020-07-08 15:12:49 +02:00
|
|
|
return uri.toString()
|
2019-08-19 16:50:33 +02:00
|
|
|
}
|
2019-10-21 19:49:48 +02:00
|
|
|
|
2020-05-30 15:49:06 +02:00
|
|
|
fun toDurationString(duration: Long, showSeconds: Boolean = false): String {
|
|
|
|
val days = (duration / 86400)
|
|
|
|
val hours = (duration % 86400) / 3600
|
|
|
|
val minutes = (duration % 86400 % 3600) / 60
|
|
|
|
val seconds = duration % 86400 % 3600 % 60
|
2019-10-21 19:49:48 +02:00
|
|
|
|
|
|
|
val ret = StringBuilder()
|
|
|
|
|
2020-05-30 15:49:06 +02:00
|
|
|
if (days > 0) ret.append("${days}d ")
|
|
|
|
if (hours > 0) ret.append("${hours}h ")
|
|
|
|
if (minutes > 0) ret.append("${minutes}m ")
|
|
|
|
if (showSeconds && seconds > 0) ret.append("${seconds}s")
|
2019-10-21 19:49:48 +02:00
|
|
|
|
|
|
|
return ret.toString()
|
2019-11-25 23:14:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
object Settings {
|
2021-07-23 14:10:13 +02:00
|
|
|
|
|
|
|
fun isAnonymous() =
|
|
|
|
PowerPreference.getFileByName(AppContext.PREFS_CREDENTIALS).getBoolean("anonymous", false)
|
|
|
|
|
2020-09-03 12:09:56 +02:00
|
|
|
fun getScopes() = PowerPreference.getDefaultFile().getString("scope", "all").split(",")
|
2020-05-30 21:16:28 +02:00
|
|
|
}
|