Skip to content

Instantly share code, notes, and snippets.

View antonis's full-sized avatar

Antonis Lilis antonis

View GitHub Profile
@antonis
antonis / ExchangeRates.kt
Created December 20, 2019 14:11
Fetches exchange rate from the European Central Bank https://exchangeratesapi.io
import com.google.gson.Gson
import com.google.gson.JsonObject
enum class CurrencySymbol {
EUR, CAD, HKD, ISK, PHP, DKK, HUF, CZK, AUD, RON, SEK,
IDR, INR, BRL, RUB, HRK, JPY, THB, CHF, SGD, PLN, BGN,
TRY, CNY, NOK, NZD, ZAR, USD, MXN, ILS, GBP, KRW, MYR
}
/**
@antonis
antonis / WebViewHeight.kt
Created May 22, 2019 11:31
Various ways to get the height of a Webview on Android. Nothing really works on all cases
import android.view.ViewTreeObserver
import android.webkit.JavascriptInterface
import android.webkit.WebView
import android.webkit.WebViewClient
// Try 1
private fun getHeightFromDocumentBodyScrollHeight(webView: WebView, setHeight: (Int)->Unit) {
webView.webViewClient = object : WebViewClient() {
override fun onPageFinished(view: WebView?, url: String?) {
@antonis
antonis / AndroidAppHashSignature.kt
Created April 3, 2019 14:13
Google Play services uses the hash string to determine which verification messages to send to your app. The hash string is made of your app's package name and your app's public key certificate https://developers.google.com/identity/sms-retriever/verify#computing_your_apps_hash_string
fun getAppSignature(context: Context) = context.packageManager.getPackageInfo(context.packageName, PackageManager.GET_SIGNATURES)
.signatures.mapNotNull { hash(context.packageName, it.toCharsString()) }.firstOrNull()
private fun hash(packageName: String, signature: String) = try {
val messageDigest = MessageDigest.getInstance("SHA-256")
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
messageDigest.update("$packageName $signature".toByteArray(StandardCharsets.UTF_8))
}
var hashSignature = messageDigest.digest()
hashSignature = Arrays.copyOfRange(hashSignature, 0, 9)
import com.google.gson.*
fun wikipedia(keyword: String): String {
val url = "https://en.wikipedia.org/w/api.php" +
"?action=query&format=json&prop=extracts" +
"&exsectionformat=plain&exsentences=2&explaintext=1" +
"&titles=$keyword"
val json = java.net.URL(url).readText()
val jsonObject = Gson().fromJson<JsonObject>(json, JsonObject::class.java)
val pages = jsonObject["query"].asJsonObject["pages"].asJsonObject
import org.jsoup.Jsoup
fun google(keyword: String): String {
val doc = Jsoup.connect("https://google.com/search?q=$keyword")
.userAgent("Mozilla/5.0").get()
val title = doc.selectFirst("h3.r a")?.text()
val description = doc.selectFirst("span.st")?.text()
return "$title. $description"
}
fun getBytesFromStringFileSize(fileSizeString: String): Long? {
fun parseBytes(string: String, unit: String, scale: Double): Long {
val numberString = string.toUpperCase().replace(unit, "")
val number = numberString.toDouble()
return (number * Math.pow(1024.0, scale)).toLong()
}
try {
if (fileSizeString.endsWith("GB", ignoreCase = true)) {
return parseBytes(fileSizeString, "GB", 3.0)
}
@antonis
antonis / GooglePlacesAutocomplete.kt
Last active March 20, 2019 12:00
Quick hack to get autocomplete suggestions from Google Places https://developers.google.com/places/web-service/autocomplete
import com.github.kittinunf.fuel.Fuel
import com.github.kittinunf.fuel.core.HeaderValues
import com.github.kittinunf.fuel.core.Method
import com.github.kittinunf.fuel.core.Parameters
import com.github.kittinunf.fuel.core.Request
import com.github.kittinunf.fuel.core.requests.tryCancel
import com.github.kittinunf.fuel.json.FuelJson
import com.github.kittinunf.fuel.json.responseJson
import com.github.kittinunf.fuel.util.FuelRouting
import java.util.*
Verifying that +antonis is my blockchain ID. https://onename.com/antonis