Skip to content

Instantly share code, notes, and snippets.

@hvisser
hvisser / KeepScreenOn.kt
Created January 21, 2022 09:16
LocalView.current can be useful if you need to tap into a view function like keepScreenOn or haptics from Compose
@Composable
fun KeepScreenOn() {
val currentView = LocalView.current
DisposableEffect(Unit) {
currentView.keepScreenOn = true
onDispose {
currentView.keepScreenOn = false
}
}
@hvisser
hvisser / MaterialConversions.kt
Last active May 15, 2024 09:16
Using both Material 3 and material components in Compose within the same theme
// conversions based on https://material.io/blog/migrating-material-3, deprecated colors set to Colors.Red
@Composable
fun fromMaterial3Theme(isLight: Boolean): Colors {
val scheme = MaterialTheme.colorScheme
return Colors(
primary = scheme.primary,
primaryVariant = Color.Red,
secondary = scheme.secondary,
secondaryVariant = Color.Red,
background = scheme.background,
@hvisser
hvisser / init.gradle
Last active February 7, 2021 15:32
Init script to load additional properties from gradle.local.properties
gradle.projectsLoaded { gradle ->
def file = rootProject.file("gradle.local.properties")
if (file.exists()) {
def properties = new Properties()
file.withInputStream { properties.load(it) }
// set the loaded properties for all projects
gradle.beforeProject { project ->
properties.entrySet().forEach { prop ->
@hvisser
hvisser / DemoModeEnabler.kt
Created January 23, 2018 11:44
Enables demo mode on a device for the purpose of taking screenshots
class DemoModeEnabler {
fun enable() {
executeShellCommand("settings put global sysui_demo_allowed 1")
sendCommand("exit")
sendCommand("enter")
sendCommand("notifications", "visible" to "false")
sendCommand("network", "wifi" to "hide")
sendCommand("battery", "level" to "100", "plugged" to "false")
sendCommand("clock", "hhmm" to "1000")
@hvisser
hvisser / Example.java
Created September 14, 2014 18:17
Another example of "embedding" entities in another Cupboard entity
// TypeToken is a Gson class
Type type = new TypeToken<List<Author>>(){}.getType();
GsonFieldConverterFactory factory = new GsonFieldConverterFactory(type);
// Register the factory and set the instance as the global Cupboard instance
CupboardFactory.setCupboard(new CupboardBuilder().registerFieldConverterFactory(factory).build());
@hvisser
hvisser / proxy.py
Created July 11, 2014 12:22
Monkey runner script to set the proxy on Android from the "modify network" screen.
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
import sys
import socket
import time
device = MonkeyRunner.waitForConnection()
host = socket.gethostbyname(socket.gethostname())
port = "8888"
if len(sys.argv) >= 2:
public class StringArrayFieldConverter implements FieldConverter<String[]> {
@Override
public String[] fromCursorValue(Cursor cursor, int columnIndex) {
return cursor.getString(columnIndex).split(",");
}
@Override
public void toContentValue(String[] value, String key, ContentValues values) {
}
@hvisser
hvisser / build.gradle
Created May 22, 2014 13:26
Snippet of a build.gradle that uses Gradle properties for key store passwords. I usually do not check in gradle.properties and on the CI machine the properties are provided using the -P switch.
apply plugin: 'android'
// use the standard Gradle version property, handy if you use plugins that also rely on this
version = "1.0"
ext {
// These properties are set in gradle.properties (not checked in to vcs) or using -P on the command line.
// If they are not set, provide a default otherwise the build won't run.
// Obviously, with the default password a release build will fail
keyStorePassword = ext.has("keyStorePassword") ? ext.keyStorePassword : "not-set"
@hvisser
hvisser / TestStringArrayConverter.java
Last active February 20, 2017 00:40
Custom field converter example for Cupboard
package nl.qbusict.cupboard;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.test.AndroidTestCase;
import nl.qbusict.cupboard.convert.EntityConverter.ColumnType;
import nl.qbusict.cupboard.convert.FieldConverter;
@hvisser
hvisser / layout.xml
Created July 24, 2013 15:58
Foreground selector maybe?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:addStatesFromChildren="true" // when any child has state_pressed, this container will also have!
android:orientation="vertical" android:background="@drawable/my_selector">
<ImageView android:clickable="true" .../>
<TextView android:clickable="true" .../>
</LinearLayout>