From 976b765629642f6f338624a536d0d4b06ecb8532 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=A4=CE=BF=20=CE=BC=CE=BF=CF=87=CE=B8=CE=B7=CF=81=CF=8C?= =?UTF-8?q?=20=5E=5F=5E?= <69906215+Malus-risus@users.noreply.github.com> Date: Sat, 24 Feb 2024 01:09:56 +0800 Subject: [PATCH 1/5] Create renovate.json --- renovate.json | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 renovate.json diff --git a/renovate.json b/renovate.json new file mode 100644 index 00000000..5db72dd6 --- /dev/null +++ b/renovate.json @@ -0,0 +1,6 @@ +{ + "$schema": "https://docs.renovatebot.com/renovate-schema.json", + "extends": [ + "config:recommended" + ] +} From e858179204d215b6ebe657e193d456e6d91c93b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=A4=CE=BF=20=CE=BC=CE=BF=CF=87=CE=B8=CE=B7=CF=81=CF=8C?= =?UTF-8?q?=20=5E=5F=5E?= <69906215+Malus-risus@users.noreply.github.com> Date: Sun, 25 Feb 2024 18:31:42 +0800 Subject: [PATCH 2/5] Update _Ext.kt --- .../kotlin/com/v2ray/ang/extension/_Ext.kt | 89 ++++++++----------- 1 file changed, 38 insertions(+), 51 deletions(-) diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/extension/_Ext.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/extension/_Ext.kt index 2c2159aa..2e66d6fd 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/extension/_Ext.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/extension/_Ext.kt @@ -10,75 +10,62 @@ import java.net.URI import java.net.URLConnection /** - * Some extensions + * Some extensions for the AngApplication */ +// Extension property to retrieve AngApplication instance from Context val Context.v2RayApplication: AngApplication get() = applicationContext as AngApplication -fun Context.toast(message: Int): Toast = ToastCompat - .makeText(this, message, Toast.LENGTH_SHORT) - .apply { - show() - } +// Extension functions to show a toast message for Int and CharSequence types +fun Context.toast(message: Int, duration: Int = Toast.LENGTH_SHORT) = + ToastCompat.makeText(this, message, duration).show() -fun Context.toast(message: CharSequence): Toast = ToastCompat - .makeText(this, message, Toast.LENGTH_SHORT) - .apply { - show() - } +fun Context.toast(message: CharSequence, duration: Int = Toast.LENGTH_SHORT) = + ToastCompat.makeText(this, message, duration).show() +// JSONObject Extensions to put a Pair or Map of data fun JSONObject.putOpt(pair: Pair) = putOpt(pair.first, pair.second) -fun JSONObject.putOpt(pairs: Map) = pairs.forEach { putOpt(it.key to it.value) } +fun JSONObject.putOpt(pairs: Map) = pairs.forEach { (key, value) -> + putOpt(key to value) +} + +// Constants for traffic and speed conversions const val threshold = 1000 const val divisor = 1024F -fun Long.toSpeedString() = toTrafficString() + "/s" +// Extension function to convert Long byte value to a string with speed units +fun Long.toSpeedString() = "${toTrafficString()}/s" -fun Long.toTrafficString(): String { - if (this == 0L) - return "\t\t\t0\t B" - - if (this < threshold) - return "${this.toFloat().toShortString()}\t B" - - val kib = this / divisor - if (kib < threshold) - return "${kib.toShortString()}\t KB" - - val mib = kib / divisor - if (mib < threshold) - return "${mib.toShortString()}\t MB" - - val gib = mib / divisor - if (gib < threshold) - return "${gib.toShortString()}\t GB" - - val tib = gib / divisor - if (tib < threshold) - return "${tib.toShortString()}\t TB" - - val pib = tib / divisor - if (pib < threshold) - return "${pib.toShortString()}\t PB" - - return "∞" +// Extension function to convert Long byte value to a string with traffic units +fun Long.toTrafficString(): String = when { + this == 0L -> "0 B" + this < threshold -> "${this.formatBytes()} B" + else -> { + val (value, unit) = listOf( + divisor to "KB", + divisor * divisor to "MB", + divisor * divisor * divisor to "GB", + divisor * divisor * divisor * divisor to "TB", + divisor * divisor * divisor * divisor * divisor to "PB" + ).fold(this to "B") { (value, _), (div, unit) -> + if (value < threshold * divisor) return@fold value to unit + (value / divisor) to unit + } + "${value.formatBytes()} $unit" + } } -private fun Float.toShortString(): String { - val s = "%.2f".format(this) - if (s.length <= 4) - return s - return s.substring(0, 4).removeSuffix(".") -} +private fun Long.formatBytes() = "%.2f".format(this / divisor).trimEnd('0').trimEnd('.') +// URLConnection extension property to return the response length with backward compatibility val URLConnection.responseLength: Long get() = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) contentLengthLong else contentLength.toLong() +// URI extension property to return an IDN Hostname val URI.idnHost: String - get() = host!!.replace("[", "").replace("]", "") + get() = host?.replace("[", "")?.replace("]", "") ?: "" -fun String.removeWhiteSpace(): String { - return this.replace(" ", "") -} +// String extension function to remove all white spaces +fun String.removeWhiteSpace() = replace(" ", "") From c7efcde8685d0bb1d2412d45f229872a1a3eff60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=A4=CE=BF=20=CE=BC=CE=BF=CF=87=CE=B8=CE=B7=CF=81=CF=8C?= =?UTF-8?q?=20=5E=5F=5E?= <69906215+Malus-risus@users.noreply.github.com> Date: Sun, 25 Feb 2024 18:42:39 +0800 Subject: [PATCH 3/5] Update _Ext.kt --- .../kotlin/com/v2ray/ang/extension/_Ext.kt | 78 ++++++++----------- 1 file changed, 31 insertions(+), 47 deletions(-) diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/extension/_Ext.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/extension/_Ext.kt index 2e66d6fd..4b7ff2a0 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/extension/_Ext.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/extension/_Ext.kt @@ -1,7 +1,6 @@ package com.v2ray.ang.extension import android.content.Context -import android.os.Build import android.widget.Toast import com.v2ray.ang.AngApplication import me.drakeet.support.toast.ToastCompat @@ -9,63 +8,48 @@ import org.json.JSONObject import java.net.URI import java.net.URLConnection -/** - * Some extensions for the AngApplication - */ - -// Extension property to retrieve AngApplication instance from Context val Context.v2RayApplication: AngApplication - get() = applicationContext as AngApplication + get() = applicationContext as? AngApplication ?: throw IllegalStateException("Not an AngApplication") -// Extension functions to show a toast message for Int and CharSequence types -fun Context.toast(message: Int, duration: Int = Toast.LENGTH_SHORT) = - ToastCompat.makeText(this, message, duration).show() +fun Context.toast(message: Int) = showToast(message) +fun Context.toast(message: CharSequence) = showToast(message) -fun Context.toast(message: CharSequence, duration: Int = Toast.LENGTH_SHORT) = - ToastCompat.makeText(this, message, duration).show() - -// JSONObject Extensions to put a Pair or Map of data -fun JSONObject.putOpt(pair: Pair) = putOpt(pair.first, pair.second) - -fun JSONObject.putOpt(pairs: Map) = pairs.forEach { (key, value) -> - putOpt(key to value) +private fun Context.showToast(message: Any) { + ToastCompat.makeText(this, message.toString(), Toast.LENGTH_SHORT).apply { show() } } -// Constants for traffic and speed conversions -const val threshold = 1000 -const val divisor = 1024F - -// Extension function to convert Long byte value to a string with speed units -fun Long.toSpeedString() = "${toTrafficString()}/s" - -// Extension function to convert Long byte value to a string with traffic units -fun Long.toTrafficString(): String = when { - this == 0L -> "0 B" - this < threshold -> "${this.formatBytes()} B" - else -> { - val (value, unit) = listOf( - divisor to "KB", - divisor * divisor to "MB", - divisor * divisor * divisor to "GB", - divisor * divisor * divisor * divisor to "TB", - divisor * divisor * divisor * divisor * divisor to "PB" - ).fold(this to "B") { (value, _), (div, unit) -> - if (value < threshold * divisor) return@fold value to unit - (value / divisor) to unit - } - "${value.formatBytes()} $unit" +fun JSONObject.putOpt(pair: Pair) { + pair.second?.let { + put(pair.first, it) } } -private fun Long.formatBytes() = "%.2f".format(this / divisor).trimEnd('0').trimEnd('.') +fun JSONObject.putOpt(pairs: Map) { + pairs.forEach { (key, value) -> + putOpt(key to value) + } +} + +private const val KILOBYTE = 1024.0 +private const val THRESHOLD = 1024 + +fun Long.toTrafficString(): String { + var value = this.toDouble() + val units = listOf("B", "KB", "MB", "GB", "TB", "PB") + var unitIndex = 0 + + while (value >= THRESHOLD && unitIndex < units.lastIndex) { + value /= KILOBYTE + unitIndex++ + } + + return String.format("%.2f %s", value, units[unitIndex]) +} -// URLConnection extension property to return the response length with backward compatibility val URLConnection.responseLength: Long - get() = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) contentLengthLong else contentLength.toLong() + get() = if (Build.VERSION.SDK_INT >= 23) contentLengthLong else contentLength.toLong() -// URI extension property to return an IDN Hostname val URI.idnHost: String get() = host?.replace("[", "")?.replace("]", "") ?: "" -// String extension function to remove all white spaces -fun String.removeWhiteSpace() = replace(" ", "") +fun String.removeWhiteSpace() = replace("\\s+".toRegex(), "") From 802f2cf3eb113c8107f60945af0b2a5b909626bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=A4=CE=BF=20=CE=BC=CE=BF=CF=87=CE=B8=CE=B7=CF=81=CF=8C?= =?UTF-8?q?=20=5E=5F=5E?= <69906215+Malus-risus@users.noreply.github.com> Date: Sun, 25 Feb 2024 19:05:48 +0800 Subject: [PATCH 4/5] Update _Ext.kt --- .../kotlin/com/v2ray/ang/extension/_Ext.kt | 68 +++++++++++-------- 1 file changed, 41 insertions(+), 27 deletions(-) diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/extension/_Ext.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/extension/_Ext.kt index 4b7ff2a0..9b764c0d 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/extension/_Ext.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/extension/_Ext.kt @@ -1,6 +1,7 @@ package com.v2ray.ang.extension import android.content.Context +import android.os.Build import android.widget.Toast import com.v2ray.ang.AngApplication import me.drakeet.support.toast.ToastCompat @@ -9,47 +10,60 @@ import java.net.URI import java.net.URLConnection val Context.v2RayApplication: AngApplication - get() = applicationContext as? AngApplication ?: throw IllegalStateException("Not an AngApplication") + get() = applicationContext as AngApplication -fun Context.toast(message: Int) = showToast(message) -fun Context.toast(message: CharSequence) = showToast(message) - -private fun Context.showToast(message: Any) { - ToastCompat.makeText(this, message.toString(), Toast.LENGTH_SHORT).apply { show() } +fun Context.toast(message: Int) { + ToastCompat.makeText(this, message, Toast.LENGTH_SHORT).apply { show() } } -fun JSONObject.putOpt(pair: Pair) { - pair.second?.let { - put(pair.first, it) - } +fun Context.toast(message: CharSequence) { + ToastCompat.makeText(this, message, Toast.LENGTH_SHORT).apply { show() } } -fun JSONObject.putOpt(pairs: Map) { - pairs.forEach { (key, value) -> - putOpt(key to value) - } +fun JSONObject.putOpt(pair: Pair) { + put(pair.first, pair.second) } -private const val KILOBYTE = 1024.0 -private const val THRESHOLD = 1024 +fun JSONObject.putOpt(pairs: Map) { + pairs.forEach { put(it.key, it.value) } +} + +const val THRESHOLD = 1000L +const val DIVISOR = 1024.0 + +fun Long.toSpeedString(): String = this.toTrafficString() + "/s" fun Long.toTrafficString(): String { - var value = this.toDouble() - val units = listOf("B", "KB", "MB", "GB", "TB", "PB") - var unitIndex = 0 - - while (value >= THRESHOLD && unitIndex < units.lastIndex) { - value /= KILOBYTE - unitIndex++ + if (this < THRESHOLD) { + return "$this B" } - - return String.format("%.2f %s", value, units[unitIndex]) + val kb = this / DIVISOR + if (kb < THRESHOLD) { + return "${String.format("%.1f KB", kb)}" + } + val mb = kb / DIVISOR + if (mb < THRESHOLD) { + return "${String.format("%.1f MB", mb)}" + } + val gb = mb / DIVISOR + if (gb < THRESHOLD) { + return "${String.format("%.1f GB", gb)}" + } + val tb = gb / DIVISOR + if (tb < THRESHOLD) { + return "${String.format("%.1f TB", tb)}" + } + return String.format("%.1f PB", tb / DIVISOR) } val URLConnection.responseLength: Long - get() = if (Build.VERSION.SDK_INT >= 23) contentLengthLong else contentLength.toLong() + get() = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + contentLengthLong + } else { + contentLength.toLong() + } val URI.idnHost: String get() = host?.replace("[", "")?.replace("]", "") ?: "" -fun String.removeWhiteSpace() = replace("\\s+".toRegex(), "") +fun String.removeWhiteSpace(): String = replace("\\s+".toRegex(), "") From 617fc6339326cc8dcb9e3225f01dc00e64c150fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=A4=CE=BF=20=CE=BC=CE=BF=CF=87=CE=B8=CE=B7=CF=81=CF=8C?= =?UTF-8?q?=20=5E=5F=5E?= <69906215+Malus-risus@users.noreply.github.com> Date: Sun, 25 Feb 2024 19:59:53 +0800 Subject: [PATCH 5/5] Delete renovate.json --- renovate.json | 6 ------ 1 file changed, 6 deletions(-) delete mode 100644 renovate.json diff --git a/renovate.json b/renovate.json deleted file mode 100644 index 5db72dd6..00000000 --- a/renovate.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "$schema": "https://docs.renovatebot.com/renovate-schema.json", - "extends": [ - "config:recommended" - ] -}