Skip to content

Instantly share code, notes, and snippets.

@Jswizzy
Last active December 29, 2021 19:02
Show Gist options
  • Save Jswizzy/aedf54a42fcaaf2170870263da359ada to your computer and use it in GitHub Desktop.
Save Jswizzy/aedf54a42fcaaf2170870263da359ada to your computer and use it in GitHub Desktop.
android utils

URL

fun URL.toBitmap(): Bitmap? {
    return try {
        BitmapFactory.decodeStream(openStream())
    } catch (e: IOException) {
        null
    }
}

fun Bitmap.saveToInternalStorage(context: Context): Uri? {
    val wrapper = ContextWrapper(context)
    var file = wrapper.getDir("images", Context.MODE_PRIVATE)
    file = File(file, "${UUID.randomUUID()}.jpg")
    return try {
        FileOutputStream(file).use { stream ->
            compress(Bitmap.CompressFormat.JPEG, 100, stream)
            stream.flush()
        }
        Uri.parse(file.absolutePath)
    } catch (e: IOException) {
        e.printStackTrace()
        null
    }
}
/**
* If the lifecycle is not resumed it means this NavBackStackEntry already processed a nav event.
*
* This is used to de-duplicate navigation events.
*/
private fun NavBackStackEntry.lifecycleIsResumed() =
this.lifecycle.currentState == Lifecycle.State.RESUMED
/**
* Checks the Internet connection and performs an action if it's active.
*/
class NetworkStatusChecker(private val connectivityManager: ConnectivityManager?) {
inline fun performIfConnectedToInternet(action: () -> Unit) {
if (hasInternetConnection()) {
action()
}
}
fun hasInternetConnection(): Boolean {
val network = connectivityManager?.activeNetwork ?: return false
val capabilities = connectivityManager.getNetworkCapabilities(network) ?: return false
return capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) ||
capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) ||
capabilities.hasTransport(NetworkCapabilities.TRANSPORT_VPN)
}
}
// check this out instead https://stackoverflow.com/questions/53532406/activenetworkinfo-type-is-deprecated-in-api-level-28
import okio.ByteString.Companion.decodeBase64
import org.spongycastle.util.encoders.Base64
import java.security.KeyFactory
import java.security.PrivateKey
import java.security.spec.PKCS8EncodedKeySpec
private const val prefix = "-----BEGIN PRIVATE KEY-----\n"
private const val postfix = "\n-----END PRIVATE KEY-----"
fun PrivateKey.toPkcs8Pem(): String {
val privateKeyBase64 = String(Base64.encode(this.encoded))
return privateKeyBase64.chunked(64).joinToString(
separator = "\n",
prefix = prefix,
postfix = postfix
)
}
fun String.toPrivateKey(): PrivateKey {
return this
.replace(prefix, "")
.replace(postfix, "")
.decodeBase64()?.let { byteString ->
KeyFactory.getInstance("RSA").generatePrivate(
PKCS8EncodedKeySpec(byteString.toByteArray())
)
} ?: throw IllegalStateException("Failed to parse PrivateKey")
}

Uri

encode

Uri.encode(someText)
@Jswizzy
Copy link
Author

Jswizzy commented Dec 29, 2021

{SomeClass}Ext.kt
remember{Some}State()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment