Skip to content

Instantly share code, notes, and snippets.

View MaTriXy's full-sized avatar

Yossi Elkrief MaTriXy

View GitHub Profile
/*
* Copyright 2016 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
@MaTriXy
MaTriXy / LifecycleJob.kt
Created September 15, 2018 04:00 — forked from LouisCAD/LifecycleCoroutines.kt
A job that automatically gets cancelled when the lifecycle is destroyed. Meant to be used as a parent to your coroutines in lifecycle aware components.
import android.arch.lifecycle.GenericLifecycleObserver
import android.arch.lifecycle.Lifecycle
import android.arch.lifecycle.Lifecycle.Event.ON_DESTROY
import android.arch.lifecycle.LifecycleOwner
import kotlinx.coroutines.experimental.CoroutineScope
import kotlinx.coroutines.experimental.Dispatchers
import kotlinx.coroutines.experimental.Job
import kotlinx.coroutines.experimental.android.Main
fun Lifecycle.createJob(cancelEvent: Lifecycle.Event = ON_DESTROY): Job = Job().also { job ->
@MaTriXy
MaTriXy / DetailViewModel.kt
Created September 13, 2018 09:00 — forked from chrisbanes/ScopedViewModel.kt
ScopedViewModel
class DetailViewModel : ScopedViewModel() {
fun startTask() {
launch {
// Switch the 'background' thread
withContext(Dispatchers.Default) {
// do your long-running thing
}
// We're now back on the Android's Main thread
updateUi()
@MaTriXy
MaTriXy / RoundedAvatarDrawable.java
Created August 19, 2018 08:20 — forked from eveliotc/RoundedAvatarDrawable.java
RoundedAvatarDrawable: A Drawable that draws an oval with given {@link Bitmap} See http://evel.io/2013/07/21/rounded-avatars-in-android/
/*
* Copyright 2013 Evelio Tarazona Cáceres <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
@MaTriXy
MaTriXy / gist:b773d9fce498dc4b07f45cea7cb80f60
Created June 5, 2018 13:53 — forked from johnrees/gist:b116e426bc73040876c0
streaming desktop to rtmp using ffmpeg
$ git clone https://github.com/iizukanao/node-rtsp-rtmp-server
$ coffee -c .
$ node server.js
$ ffmpeg -f avfoundation -i "0" -vcodec libx264 -tune zerolatency -s 320x480 -vf crop=320:480:0:0 -f flv rtmp://0.0.0.0/live/desktop
# stream on ios device
$ git clone https://github.com/iMoreApps/ffmpeg-avplayer-for-ios-tvos
$ open AVPlayerDemo.xcodeproj (change rtmp stream to rtmp://<LOCAL_IP>/live/desktop, build and run)
@MaTriXy
MaTriXy / CenterImageSpan.java
Created June 4, 2018 08:34 — forked from rajeefmk/CenterImageSpan.java
Image Span used to center align the image with surrounding text inside a textview.
package com.hashlearn.common.utils;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.text.style.DynamicDrawableSpan;
import java.lang.ref.WeakReference;
@MaTriXy
MaTriXy / AccountAuthenticator.java
Created May 31, 2018 09:12 — forked from burgalon/AccountAuthenticator.java
Implementing OAuth2 with AccountManager, Retrofit and Dagger
public class AccountAuthenticator extends AbstractAccountAuthenticator {
private final Context context;
@Inject @ClientId String clientId;
@Inject @ClientSecret String clientSecret;
@Inject ApiService apiService;
public AccountAuthenticator(Context context) {
super(context);
@MaTriXy
MaTriXy / AndroidManifest.xml
Created May 31, 2018 09:11 — forked from TomTasche/AndroidManifest.xml
OAuth flow using the AccountManager on Android
<!-- ... -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<!-- ... -->
@MaTriXy
MaTriXy / frameMetrics.kt
Created May 24, 2018 19:32
measure layout duration and some more frame metrics
val handler = Handler()
window.addOnFrameMetricsAvailableListener(
{ _, frameMetrics, _ ->
val frameMetricsCopy = FrameMetrics(frameMetrics)
// Layout measure duration in nanoseconds
val layoutMeasureDurationNs =
frameMetricsCopy.getMetric(FrameMetrics.LAYOUT_MEASURE_DURATION)
Timber.d("LAYOUT_MEASURE_DURATION: %s",
(layoutMeasureDurationNs / Math.pow(10.0, 6.0)).toString())
}, handler)
@MaTriXy
MaTriXy / AndroidCamera2TouchToFocus.java
Created April 23, 2018 19:12 — forked from royshil/AndroidCamera2TouchToFocus.java
How to implement Touch-to-Focus in Android using Camera2 APIs
//Override in your touch-enabled view (this can be differen than the view you use for displaying the cam preview)
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
final int actionMasked = motionEvent.getActionMasked();
if (actionMasked != MotionEvent.ACTION_DOWN) {
return false;
}
if (mManualFocusEngaged) {
Log.d(TAG, "Manual focus already engaged");
return true;