This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Verifying my Blockstack ID is secured with the address 1Ngxnfmf64srg6vBwowVRg3ChCUaK7XUkQ https://explorer.blockstack.org/address/1Ngxnfmf64srg6vBwowVRg3ChCUaK7XUkQ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.util.Log | |
/** In Kotlin: add `companion object : ALog` for `log.d { "message" }`-style logging. */ | |
open class ALog { | |
val log = ALogger(javaClass.enclosingClass ?: javaClass) | |
companion object { | |
/** Decide if a statement should be printed based on its class and log level */ | |
var filter: ((Class<*>, Int) -> Boolean) = { _, _ -> true } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Wrap an object which requires closing if present | |
*/ | |
open class Closer<T>(@Volatile private var wrapped: T? = null) { | |
/** Return the current wrapped value */ | |
operator fun invoke(): T? = wrapped | |
/** If there is no value yet, store the result of [func] */ | |
fun open(func: () -> T) = synchronized(this) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyClass(val context: Context) { | |
private val wifiLock = Closer<WifiManager.MulticastLock>() | |
fun start() { | |
wifiLock.open { | |
// Create and open the lock (won't run if lock is already present) | |
val wifiManager = context.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager | |
wifiManager.createMulticastLock("MyClass.lock").apply { | |
setReferenceCounted(true) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyClass { | |
@Volatile | |
var resource: Resource? = null | |
fun start() { | |
synchronized(this) { | |
resource ?: apply { | |
resource = Resource() | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyClass { | |
var resource: Resource? = null // Something we need to use for a while | |
fun start() { | |
resource = Resource() | |
} | |
fun work() { | |
resource?.doSomething() | |
} |