Skip to content

Instantly share code, notes, and snippets.

@alexshr
alexshr / EspressoExecutor.java
Last active Mar 4, 2019
The utility repeats runnable or callable executing until it pass without errors or throws throwable after timeout. It works perfectly for Espresso tests.
View EspressoExecutor.java
/**
* 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 / MockDispatcher.java
Created May 6, 2017
MockWebServer dispatcher supports requests plan, assert the request history, can take file for response from path/
View MockDispatcher.java
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
Test rule for rxjava test (override schedulers for testing, reset hook).
View TestSchedulerRule.java
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 / MockMyApplication.java
Last active Aug 28, 2017
Extended AndroidJUnitRunner for starting with different Application class (e.g. to replace dagger component)
View MockMyApplication.java
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 / gist:a1703a28c5742b5a1996deeab0bf2ad3
Last active Jun 2, 2017
Call it in Activity.onCreate to see facebook debug key hash
View gist:a1703a28c5742b5a1996deeab0bf2ad3
/**
* 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 / init_timber.java
Last active Feb 9, 2019
init Timber output format (with class, method, thread names and line number)
View init_timber.java
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(),
View drawableToBitmap.java
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;
View isServiceRunning.java
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 / kotlin_supress_expiremental_api
Created Apr 30, 2019
kotlin coroutines @file:Suppress("EXPERIMENTAL_API_USAGE")
View kotlin_supress_expiremental_api
@file:Suppress("EXPERIMENTAL_API_USAGE")
@alexshr
alexshr / GetFragArgsFromNavActionKtx.kt
Last active Aug 7, 2019
Get fragment args from navigation action (lazy delegate from navigation-ktx)
View GetFragArgsFromNavActionKtx.kt
//using ktx for args https://developer.android.com/kotlin/ktx#navigation
val arguments by navArgs<SleepQualityFragmentArgs>()