Skip to content

Instantly share code, notes, and snippets.

View marcouberti's full-sized avatar

Marco Uberti marcouberti

  • Bending Spoons
  • Milan, Italy
View GitHub Profile
@marcouberti
marcouberti / bits_converter.py
Created December 29, 2016 17:04
Find LSB and MSB bit in Python
def bits_converter(x):
index = 0
bits = ""
while x is not 0:
bits += str(x & 1)
x = x >> 1
index+=1
return bits[::-1]
@marcouberti
marcouberti / mock_android_context.java
Last active May 2, 2024 11:43
Mock Android Context using Mockito
import org.mockito.Mock;
import static org.mockito.Mockito.when;
@Mock
private Context mockApplicationContext;
@Mock
private Resources mockContextResources;
@Mock
private SharedPreferences mockSharedPreferences;
@marcouberti
marcouberti / android-mapping-file.gradle
Last active February 16, 2017 16:41
Copy and rename mapping.txt file if minify is enable
android {
applicationVariants.all { variant ->
def versionCode = android.defaultConfig.versionCode
def versionName = android.defaultConfig.versionName
def appendToFileName = versionName + "-build-" + versionCode
// copy and rename mapping.txt file if in minify is enable
if (variant.getBuildType().isMinifyEnabled()) {
variant.assemble.doLast{
copy {
@marcouberti
marcouberti / gradient_descent.js
Last active February 13, 2017 18:35
Simple Gradient Descent example in JS
/*
Our function f(x)
*/
function f(x) {
return x*x +5;
}
/*
The derivative df/dx of our function f(x)
*/
@marcouberti
marcouberti / BinaryOperationsAndBitmasks.java
Created October 2, 2018 11:33
binary operators and bit masks in Java
public class BinaryOperationsAndBitmasks {
public static void main(String[] args) {
new BinaryOperationsAndBitmasks().bitsOperations();
}
/**
* Basic bitmap operations
*/
private void bitsOperations() {
@marcouberti
marcouberti / kotlin_channels.kt
Created February 12, 2020 14:55
Kotlin Channel with buffer and natural batching
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.channels.*
fun main() = runBlocking<Unit> {
// create a Channel with a buffer of size 20
// this means that until 20 the send() are not suspending waiting for a receive()
val channel = Channel<String>(20)
@marcouberti
marcouberti / CoroutineTestRule.kt
Last active June 1, 2021 01:51
Android Local Unit Test using Kotlin Coroutines and MockK. A CoroutineTestRule is needed if your tested classes use the Main dispatcher, otherwise an "IllegalStateException" is thrown. Also a InstantTaskExecutorRule is needed to avoid a "NullPointerException". Use runBlockingTest to skip all suspending delays. Use coEvery/coVerify to mock/verify…
import org.junit.rules.TestWatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.*
import org.junit.runner.Description
@ExperimentalCoroutinesApi
class CoroutineTestRule(val dispatcher: TestCoroutineDispatcher = TestCoroutineDispatcher()) : TestWatcher() {
override fun starting(description: Description?) {
@marcouberti
marcouberti / !SecretMenuGlobalTouchListener.kt
Last active April 24, 2020 12:07
Android Global Multitouch Events Interceptor. This multitouch events interceptor can be plugged into your app without directly coupling it with your activities.
/**
* This class is register to the [Application] activity lifecycle and
* add or remove the overlay view to each [Activity] as a content view.
*
* @see Activity.addContentView
*/
class SecretMenuGlobalTouchListener(
application: Application,
private val touchManager: SecretMenuTouchManager
): InvisibleOverlayView.TouchListener {
@marcouberti
marcouberti / Decrypter.java
Created May 5, 2020 12:28 — forked from scotttam/Decrypter.java
encrypt and decrypt with PBKDF2/SHA1 and AES
import javax.crypto.Cipher;
import java.security.spec.KeySpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import javax.crypto.SecretKeyFactory;
import java.security.AlgorithmParameters;
import javax.crypto.spec.IvParameterSpec;
public class Decrypter {
Accept-Encoding: gzip
Accept-Language: en-US;q=1.0
Authorization: Bearer 541f51af2878529f692094b222827e53bf83a0f61553932b2c91bc9ff959c514
Connection: Keep-Alive
Content-Length: 109813
Content-Type: application/json; charset=UTF-8
Host: api.staging.dec-poc1.immuni.org
Immuni-Dummy-Data: 0
User-Agent: Immuni