Skip to content

Instantly share code, notes, and snippets.

View mataanin's full-sized avatar

Maksim Golivkin mataanin

View GitHub Profile
// Intuitive Focus Extension, Kotlin, Android min 19 SDK
fun View.accessibilityFullFocus() {
sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED)
requestFocus()
}
@mataanin
mataanin / StubbornFocus.kt
Last active January 8, 2018 01:23
Reliable focus on a view on Android
// Sample using RxJava 1.x, Kotlin
var offSwitch = false
Observable
.interval(0, 100L, TimeUnit.MILLISECONDS)
.observeOn(AndroidSchedulers.mainThread())
.takeIf { !offSwitch }
.subscribe {
// try to focus every 100 milliseconds until the view or any of its children is focused
view.accessibilityFullFocus()
offSwitch = view.isAccessibilityFocused || view.hasAccessibilityFocusedChild
@mataanin
mataanin / ContrastExtensions.kt
Last active January 8, 2018 00:50
Contrast logic
// Kotlin, Android min 19 SDK
private enum class Adjustment {
DARKEN,
LIGHTEN,
NONE
}
fun Context.adjustTextColor(color: Int, isHighContrastEnabled: Boolean): Int {
if (isHighContrastEnabled) {
@mataanin
mataanin / PayWithGoogleReadyUseCase.kt
Created November 4, 2017 18:32
Pay With Google snipplet
import android.app.Activity
import android.content.Context
import com.google.android.gms.common.api.ApiException
import com.google.android.gms.wallet.IsReadyToPayRequest
import com.google.android.gms.wallet.PaymentsClient
import com.google.android.gms.wallet.Wallet
import com.google.android.gms.wallet.WalletConstants
import rx.Emitter
import rx.Observable
@mataanin
mataanin / ILJsonCollectionFile.java
Last active August 29, 2015 14:19
Save a pojo to file
package com.instacart.library.persistence;
import android.content.Context;
import com.fasterxml.jackson.core.type.TypeReference;
import java.io.IOException;
import java.util.List;
public class ILJsonCollectionFile<PAYLOAD> extends ILJsonFile<List<PAYLOAD>> {
@mataanin
mataanin / BaseFragment.java
Last active August 29, 2015 14:07
NPE on ButterKnife view references safe callbacks for Fragments.
public abstract class BaseFragment extends Fragment implements ViewableFragment {
private boolean mHasViews;
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(getFragmentLayoutId(), null);
ButterKnife.inject(this, root);
return root
}
@mataanin
mataanin / gist:92b7c53709cfa85c8463
Created August 19, 2014 06:29
Drawing a scrolled bitmap.
@Override protected void dispatchDraw(Canvas canvas) {
super.dispatchDraw(canvas);
if (mDragMode && mBitmap != null) {
//Log.v("DG", "dispatchDraw matrix " + mMatrix);
canvas.drawBitmap(mBitmap, mMatrix, new Paint());
}
}
/**
* 引用不同的数据,会得到不同的地图切片
* 卫星(无地标) WGS-84 一般 GCJ-02
* <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
* 卫星(有地标) GCJ-02 一般 GCJ-02
* <script src="https://ditu.google.cn/maps/api/js?v=3.exp&sensor=false"></script>
*/
(function (map) {
var Converter = function () {
/**
* 引用不同的数据,会得到不同的地图切片
* 卫星(无地标) WGS-84 一般 GCJ-02
* <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
* 卫星(有地标) GCJ-02 一般 GCJ-02
* <script src="https://ditu.google.cn/maps/api/js?v=3.exp&sensor=false"></script>
*/
(function (map) {
var Converter = function () {
@mataanin
mataanin / gist:5891668
Last active December 19, 2015 03:39
Android: Calculating memory cache size based on the memory available on the device
// Taken from Contacts List example
public static int calculateMemCacheSize(float percent) {
if (percent < 0.05f || percent > 0.8f) {
throw new IllegalArgumentException("setMemCacheSizePercent - percent must be "
+ "between 0.05 and 0.8 (inclusive)");
}
return Math.round(percent * Runtime.getRuntime().maxMemory() / 1024);
}