Update _Ext.kt
This commit is contained in:
1 parent
e858179204
commit
c7efcde868
1 file changed
+30
-46
@@ -1,7 +1,6 @@
|
|||||||
package com.v2ray.ang.extension
|
package com.v2ray.ang.extension
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.os.Build
|
|
||||||
import android.widget.Toast
|
import android.widget.Toast
|
||||||
import com.v2ray.ang.AngApplication
|
import com.v2ray.ang.AngApplication
|
||||||
import me.drakeet.support.toast.ToastCompat
|
import me.drakeet.support.toast.ToastCompat
|
||||||
@@ -9,63 +8,48 @@ import org.json.JSONObject
|
|||||||
import java.net.URI
|
import java.net.URI
|
||||||
import java.net.URLConnection
|
import java.net.URLConnection
|
||||||
|
|
||||||
/**
|
|
||||||
* Some extensions for the AngApplication
|
|
||||||
*/
|
|
||||||
|
|
||||||
// Extension property to retrieve AngApplication instance from Context
|
|
||||||
val Context.v2RayApplication: AngApplication
|
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) = showToast(message)
|
||||||
fun Context.toast(message: Int, duration: Int = Toast.LENGTH_SHORT) =
|
fun Context.toast(message: CharSequence) = showToast(message)
|
||||||
ToastCompat.makeText(this, message, duration).show()
|
|
||||||
|
|
||||||
fun Context.toast(message: CharSequence, duration: Int = Toast.LENGTH_SHORT) =
|
private fun Context.showToast(message: Any) {
|
||||||
ToastCompat.makeText(this, message, duration).show()
|
ToastCompat.makeText(this, message.toString(), Toast.LENGTH_SHORT).apply { show() }
|
||||||
|
}
|
||||||
|
|
||||||
// JSONObject Extensions to put a Pair or Map of data
|
fun JSONObject.putOpt(pair: Pair<String, Any>) {
|
||||||
fun JSONObject.putOpt(pair: Pair<String, Any>) = putOpt(pair.first, pair.second)
|
pair.second?.let {
|
||||||
|
put(pair.first, it)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fun JSONObject.putOpt(pairs: Map<String, Any>) = pairs.forEach { (key, value) ->
|
fun JSONObject.putOpt(pairs: Map<String, Any>) {
|
||||||
|
pairs.forEach { (key, value) ->
|
||||||
putOpt(key to value)
|
putOpt(key to value)
|
||||||
}
|
|
||||||
|
|
||||||
// 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"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun Long.formatBytes() = "%.2f".format(this / divisor).trimEnd('0').trimEnd('.')
|
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
|
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
|
val URI.idnHost: String
|
||||||
get() = host?.replace("[", "")?.replace("]", "") ?: ""
|
get() = host?.replace("[", "")?.replace("]", "") ?: ""
|
||||||
|
|
||||||
// String extension function to remove all white spaces
|
fun String.removeWhiteSpace() = replace("\\s+".toRegex(), "")
|
||||||
fun String.removeWhiteSpace() = replace(" ", "")
|
|
||||||
Reference in new issue
Block a user