Skip to content

Instantly share code, notes, and snippets.

View zach-klippenstein's full-sized avatar

Zach Klippenstein zach-klippenstein

View GitHub Profile
@zach-klippenstein
zach-klippenstein / ChangePassword.java
Last active April 3, 2024 18:04
The keystore password on Java keystore files is utterly pointless. You can reset it without knowing it, as shown by this code. Note that private keys are still secure, as far as I know. The JKS implementation is copyright Casey Marshall (rsdio@metastatic.org), and the original source is available at http://metastatic.org/source/JKS.java. I've in…
import java.util.*;
import java.io.*;
import java.security.*;
public class ChangePassword
{
private final static JKS j = new JKS();
public static void main(String[] args) throws Exception
{
@zach-klippenstein
zach-klippenstein / Blockify.kt
Last active March 22, 2024 21:47
A Compose modifier to turn your apps into blocks (no, this has nothing to do with NFTs)
import androidx.compose.animation.core.Animatable
import androidx.compose.animation.core.Spring
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.core.spring
import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.detectDragGestures
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.integration.demos.BlockFilter.Companion.Lighting
@zach-klippenstein
zach-klippenstein / emulator-jank.sh
Last active January 11, 2024 22:19
Shell script to trigger some jank in the Android emulator.
#!/bin/bash
# Tells an Android emulator to take a snapshot of itself and then
# delete that snapshot immediately. The act of taking a snapshot
# forces the emulator to temporarily pause and trigger unexpected
# system behaviors. When it happens during a test run, it can
# trigger certain hard-to-reproduce flakes.
SNAPSHOT=__TEMPORARY_SNAPSHOT
AUTH="$(cat ~/.emulator_console_auth_token)"
PORT=$1
fun Modifier.saturate(saturation: Float): Modifier =
drawWithCache {
// Cache the paint object so it can be reused between draw calls.
val contentPaint = Paint().apply {
colorFilter = ColorFilter.saturate(saturation)
}
onDrawWithContent {
drawIntoCanvas {
it.saveLayer(Rect(Offset.Zero, size), contentPaint)
@zach-klippenstein
zach-klippenstein / SegmentedControl.kt
Last active August 24, 2023 09:31
iOS-style segmented control in Compose
import android.annotation.SuppressLint
import androidx.compose.animation.core.animateDpAsState
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.awaitFirstDown
import androidx.compose.foundation.gestures.forEachGesture
import androidx.compose.foundation.gestures.horizontalDrag
import androidx.compose.foundation.layout.Arrangement.spacedBy
import androidx.compose.foundation.layout.Box
@Preview(showBackground = true, showSystemUi = true)
@Composable
fun App() {
Column {
MarxistRow {
Text("Left Text", Modifier.background(Color.Red))
Text("Right Text", Modifier.background(Color.Blue))
}
MarxistRow {
@zach-klippenstein
zach-klippenstein / CursedButtons.kt
Last active October 20, 2022 18:38
Code for twitter thread about why onClick handlers don't let you emit composables. https://twitter.com/zachklipp/status/1583165356322930688
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
@zach-klippenstein
zach-klippenstein / audiowave.go
Last active September 1, 2022 19:39
Terminal program that plays a sine wave.
//usr/local/bin/go run $0 "$@"; exit
/*
Terminal program that plays a sine wave.
The pitch and volume can be controlled (help is shown in the UI).
Installation:
go get github.com/gizak/termui
@zach-klippenstein
zach-klippenstein / ComposableDumper.kt
Last active June 26, 2022 01:38
Simple utility to dump the current Composition tree to logcat.
import android.util.Log
import androidx.compose.runtime.Composable
import androidx.compose.runtime.currentComposer
import androidx.compose.runtime.remember
import androidx.compose.ui.unit.IntBounds
import androidx.ui.tooling.CallGroup
import androidx.ui.tooling.Group
import androidx.ui.tooling.NodeGroup
import androidx.ui.tooling.asTree
@zach-klippenstein
zach-klippenstein / DebugComposeBounds.kt
Last active June 26, 2022 01:38
Proof-of-concept of a composable that draws information about all its children on top of them.
@Composable fun App() {
DebugBounds {
Column(Modifier.background(Color.White).fillMaxSize()) {
BasicText("Some text")
Spacer(Modifier.size(10.dp))
BasicText("More text")
Spacer(Modifier.size(5.dp))
BasicText("Button", Modifier
.clickable { }
.background(Color.Blue, RoundedCornerShape(3.dp))