Skip to content

Instantly share code, notes, and snippets.

@venator85
venator85 / DelTagHandler.java
Created January 15, 2020 11:02
A Html.fromHtml() TagHandler to support <del> on Android <= 6
import android.text.Editable;
import android.text.Html;
import android.text.Spannable;
import android.text.Spanned;
import android.text.style.StrikethroughSpan;
import org.xml.sax.XMLReader;
public class DelTagHandler implements Html.TagHandler {
public void handleTag(boolean opening, String tag, Editable output, XMLReader xmlReader) {
@venator85
venator85 / coroutines_sample.kt
Created November 12, 2019 16:55
Coroutine Samples
// implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.2'
// for lifecycleScope: implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.2.0-rc02'
class MyFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val request = Request.Builder().get()
.url("https://icanhazip.com")
.build()
@venator85
venator85 / itemclick_recyclerview.kt
Created August 25, 2018 08:13
RecyclerView with item click support
package eu.alessiobianchi
import android.support.v7.widget.RecyclerView
import android.view.View
interface ItemClickListener<T> {
fun onClick(view: View, position: Int, item: T)
}
abstract class ClickableAdapter<T, V: ClickableViewHolder<T>>(
@venator85
venator85 / build.gradle
Created January 12, 2018 09:56
Gradle task to print APK signing certificate information (e.g. SHA1 hash)
android.applicationVariants.all { variant ->
tasks.create(name: "signatureHash${variant.name.capitalize()}", type: Exec) {
group "util"
commandLine 'keytool', '-exportcert', '-list', '-v',
'-alias', variant.signingConfig.keyAlias,
'-keypass', variant.signingConfig.keyPassword,
'-keystore', variant.signingConfig.storeFile,
'-storepass', variant.signingConfig.storePassword
doFirst {
println ":: Signing config for variant ${variant.name} is: ${variant.signingConfig.name}\n"
@venator85
venator85 / constraint_layout_gridlayout.xml
Last active March 16, 2017 22:53
Trying to replicate GridLayout behavior with ConstraintLayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
@venator85
venator85 / asuswrt-route53.sh
Created January 26, 2017 22:05
Custom AsusWRT DDNS script for AWS Route53
#!/bin/sh
# note: when debugging on PC/MAC, this script must be run with bash
# Custom AsusWRT DDNS script for AWS Route53 - v1.0
# Author: Alessio Bianchi <me@alessiobianchi.eu>
# Credits: http://czak.pl/2015/09/15/s3-rest-api-with-curl.html
ACCESS_KEY_ID="XXXXXXXXXXXXXXXXXXXX"
SECRET_ACCESS_KEY="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
# These keys can be associated to a IAM user with the following policy:
@venator85
venator85 / fold.sh
Created January 26, 2017 10:51
fold (hard wrap) in pure bash
fold() {
data="$1"
width="$2"
length=$(echo -n "$data" | wc -c)
while [ $length -gt $width ]
do
echo ${data:0:$width}
data=${data:$width}
length=$(echo -n "$data" | wc -c)
done
@venator85
venator85 / CertificateValidation.java
Created October 24, 2016 13:50
Manually verify a certificate (chain) against a root
public static void validateCertificate(X509Certificate toVerify, X509Certificate root) throws Exception {
CertificateFactory cf = CertificateFactory.getInstance("X.509");
CertPath cp = cf.generateCertPath(Collections.singletonList(toVerify));
TrustAnchor trustAnchor = new TrustAnchor(root, null);
CertPathValidator cpv = CertPathValidator.getInstance("PKIX");
PKIXParameters pkixParams = new PKIXParameters(Collections.singleton(trustAnchor));
pkixParams.setRevocationEnabled(false);
@venator85
venator85 / ApkCertificate.java
Created October 24, 2016 10:41
Extract the X509Certificate used to sign the app from the APK
package com.example.alessio.testapp;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import java.io.IOException;
import java.io.InputStream;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.zip.ZipEntry;
@venator85
venator85 / LibraryProjectTestRunner.java
Created September 6, 2016 10:43
A Robolectric test runner for library projects compatible with Android Gradle plugin 2.2.0-alpha6 and later
import org.junit.runners.model.InitializationError;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.internal.bytecode.InstrumentationConfiguration;
import org.robolectric.manifest.AndroidManifest;
import org.robolectric.res.FileFsFile;
import org.robolectric.res.FsFile;
public class LibraryProjectTestRunner extends RobolectricTestRunner {