Skip to content

Instantly share code, notes, and snippets.

@alexshr
alexshr / MockDispatcher.java
Created May 6, 2017 19:31
MockWebServer dispatcher supports requests plan, assert the request history, can take file for response from path/
package com.skb.goodsapp;
import android.util.Log;
import org.junit.Assert;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
@alexshr
alexshr / TestSchedulerRule.java
Created May 6, 2017 19:44
Test rule for rxjava test (override schedulers for testing, reset hook).
package com.skb.goodsapp;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import io.reactivex.Scheduler;
import io.reactivex.android.plugins.RxAndroidPlugins;
import io.reactivex.internal.schedulers.ExecutorScheduler;
import io.reactivex.plugins.RxJavaPlugins;
@alexshr
alexshr / gist:a1703a28c5742b5a1996deeab0bf2ad3
Last active June 2, 2017 19:56
Call it in Activity.onCreate to see facebook debug key hash
/**
* Call it in Activity.onCreate to see facebook debug key hash
*
* @param activity
*/
public static void logFbKeyHash(Activity activity) {
try {
PackageInfo info = activity.getPackageManager().getPackageInfo(
activity.getPackageName(),
PackageManager.GET_SIGNATURES);
@alexshr
alexshr / MockMyApplication.java
Last active August 28, 2017 23:22
Extended AndroidJUnitRunner for starting with different Application class (e.g. to replace dagger component)
package com.skb.goodsapp;
import com.skb.goodsapp.di.AppComponent;
import com.skb.goodsapp.di.AppModule;
import com.skb.goodsapp.di.DaggerService;
/**
* to replace MyApplication (using MockTestRunner)
* for MockWebServer usage
*
@alexshr
alexshr / init_timber.java
Last active February 9, 2019 00:56
init Timber output format (with class, method, thread names and line number)
private void initTimber(){
if (BuildConfig.DEBUG) {
Timber.plant(new DebugTree() {
@SuppressLint("DefaultLocale")
@Override
protected String createStackElementTag(@NonNull StackTraceElement element) {
return String.format("%s: %s:%d (%s)",
super.createStackElementTag(element),
element.getMethodName(),
element.getLineNumber(),
@alexshr
alexshr / drawableToBitmap.java
Created February 12, 2019 01:05
drawable to bitmap
public static Bitmap drawableToBitmap(Drawable drawable) {
if (drawable instanceof BitmapDrawable) {
return ((BitmapDrawable) drawable).getBitmap();
}
int width = drawable.getIntrinsicWidth();
width = width > 0 ? width : 1;
int height = drawable.getIntrinsicHeight();
height = height > 0 ? height : 1;
@alexshr
alexshr / isServiceRunning.java
Created February 22, 2019 01:56
isServiceRunning
private boolean isServiceRunning() {
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
boolean isRunning = false;
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (LocationService.class.getName().equals(service.service.getClassName())) {
isRunning = true;
}
}
Timber.d("isRunning=%b", isRunning);
return isRunning;
@alexshr
alexshr / EspressoExecutor.java
Last active March 4, 2019 13:50
The utility repeats runnable or callable executing until it pass without errors or throws throwable after timeout. It works perfectly for Espresso tests.
/**
* Created by alexshr on 02.05.2017.
*/
package com.skb.goodsapp;
import android.os.SystemClock;
import android.util.Log;
import java.util.Date;
@alexshr
alexshr / kotlin_supress_expiremental_api
Created April 30, 2019 20:38
kotlin coroutines @file:Suppress("EXPERIMENTAL_API_USAGE")
@file:Suppress("EXPERIMENTAL_API_USAGE")
@alexshr
alexshr / GetFragArgsFromNavActionKtx.kt
Last active August 7, 2019 15:55
Get fragment args from navigation action (lazy delegate from navigation-ktx)
//using ktx for args https://developer.android.com/kotlin/ktx#navigation
val arguments by navArgs<SleepQualityFragmentArgs>()