m0skit0 / ak47

AK47 - Android Kotlin Utility Extensions

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

AK47 - Android Kotlin Utility Extensions

Small Kotlin extension functions for Android that make your app development with Kotlin more fun!

Context

Check permission extension with block support and no need to check for Android version

activity.checkAllPermissions(setOf(CAMERA, WRITE_EXTERNAL_STORAGE)) {
    // Execute if all permissions granted
} ?: "Permissions not granted!".logWarning()

No need to check for Android version when requesting permissions

activity.requestPermissions(arrayOf(WRITE_EXTERNAL_STORAGE), 123456)

Simpler system services

context.getAlarmManager()

BroadcastReceiver lambdas

context.registerReceiver(broadcastReceiver { context, intent -> intent.log() }, IntentFilter("Action Filter!"))

GUI

Progress dialog

activity.progressDialog(R.string.title) {
    // Do your backgroud task 
}

Confirmation dialog (YES/NO)

activity.confirmationDialog(R.string.title, R.string.message) {
    // Code to control OK
}

Single choice dialog (OK/CANCEL)

activity.singleChoiceDialog(R.string.title, listOf("Element 1", "Element 2"), R.string.notSelected) { selectedItem ->
    // Do something with the selected String    
}

Simpler toasts

longToast(R.string.translatedString)
toast("Hello there!") // Avoid this, you really should always use resources

View extensions

myView.invisible()
myView.disable()
myViewGroup.disableChildren()
myView.doAnimation(R.anim.myAnimation)
myView.slideUp()

Convert an image ByteArray to Bitmap or Drawable directly

byteArray.toBitmap()
byteArray.toDrawable()

Convert a Bitmap to a PNG

myBitmap.toPNG()

Get a View by name

"tvName".findViewByName(activity)

Software keyboard

closeKeyboard()
showKeyboard()

Easy set text to EditText

myEditText.textString = "Simpler than setText(text, TextView.BufferType.NORMAL)"

Resources

Get a String resource by name

"@string/that_useful_string".getStringResourceByName(activity)

Convert Bundle into Map and viceversa

myBundle.toMap()
myMap.toBundle()

Date and time

Straightforward simple date formatting

Date().format("MM/dd/yyyy")

Files

Get Android default directories in a simpler way

val downloadDir = defaultDownloadDirectory
val picturesDir = defaultPicturesDirectory

Get file images directly as Bitmap and Drawables

File(getPictureDirectory(), "image.png").asBitmap()
File(getPictureDirectory(), "image.png").asDrawable()

Simple APK installation

File(getDownloadDirectory(), "BestApp.apk").installAPK(activity)
File(getDownloadDirectory(), "SystemApp.apk").installSystemAPK(activity)
File(getDownloadDirectory(), "StealthApp.apk").installAPKNoPrompt(activity)

File's MD5 hash

File("IsThisFileLegit.apk").md5()

GREP!

File("A File With Names.txt").grep("^J".toRegex())

Control

ifTrue/ifFalse

File("uselss file").delete().ifFalse { "Why I can't delete this useless file!".log() }

Logging

Log debugs of any type with alternative syntax

"Ada Lovelace".log("OMGLOG")
0.log("INTLOG!")
listOf("Ada", "Boole", "Dijkstra").log("LISTLOG!")
arrayOf("Ada", "Boole", "Dijkstra").log("OMGARRAYSWORKTOO!")

Easier chaining

listOf("Ada", "Von Neumann", "Turing")
    .log("Original list")
    .filter { it.contains("a") }
    .log("Filtered list")

Supports different logging levels

"Ada Lovelace".logDebug("OMGLOG")
0.logInfo("INTLOG!")
listOf("Ada", "Boole", "Dijkstra").logWarning("LISTLOG!")
arrayOf("Ada", "Boole", "Dijkstra").logError("OMGARRAYSWORKTOO!")

Direct exception logging with ERROR level

try {
    throw Exception("Something nasty happened")
} catch (e: Exception) {
    e.log("Your exception tag")
}

Default tag value "LogUtils" for all log function extensions

"Ada Lovelace".log()

Log and ignore exceptions

ignoreErrors {
    throw IOException("File not found!")
}

Easier execution time logging

logExecutionTime {
    (0..100_000).forEach { it.log("A number") }
}

Log call stack trace to this point

logCallTrace()

Shell

Execute command as normal user or as superuser and get its output

"ls -l /data".runCommand()
"ls -l /system".runCommandAsSu()

System utilities

Reboot or shutdown the device with one line

reboot()
shutdown()

Binary utilities

Simple byte array to hexadecimal conversion

listOf(1.toByte(), 2.toByte()).toTypedArray().toHex().log("My sweet little array")

Base64 extension functions

val byteArray = base64String.decode()
val base64String = byteArray.encode()

About

AK47 - Android Kotlin Utility Extensions


Languages

Language:Kotlin 100.0%