Skip to content

Instantly share code, notes, and snippets.

@kingargyle
kingargyle / init.gradle
Created March 10, 2016 17:15
Add Nexus Mirrors to gradle builds with Init Script
// Replace the location of the nexus repository with your specific location.
// To enable this: gradlew build -I init.gradle
// This will now have gradle use the mirrors first and jcenter and other repositories second if it can't find the artifacts.
// Make sure your mirror Jcenter in your nexus repository.
allprojects{
buildscript{
repositories{
maven{ url 'http://localhost:8081/nexus/content/groups/public' }
}
@kingargyle
kingargyle / RobolectricLoggingRule.java
Last active May 9, 2016 13:53
A Junit 4 Rule to easily access the Log Output from Robolectric.
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import org.junit.rules.ExternalResource;
import org.robolectric.shadows.ShadowLog;
public class RobolectricLoggingRule extends ExternalResource {
PrintStream loggingStream;
ByteArrayOutputStream loggingOutputStream;
PrintStream defaultStream;
@kingargyle
kingargyle / ShadowTextInputLayout.java
Created May 31, 2016 17:08
A bare bones Shadow for Robolectric and TextInputLayout from the design library
import android.support.design.widget.TextInputLayout;
import org.robolectric.annotation.Implements;
import org.robolectric.annotation.RealObject;
import org.robolectric.shadows.ShadowViewGroup;
@Implements(TextInputLayout.class)
public class ShadowTextInputLayout extends ShadowViewGroup {
@RealObject private TextInputLayout realTextInputLayout;
@kingargyle
kingargyle / ShadowSupportFragment.java
Created June 3, 2016 13:44
ShadowSupportFragment implementation for Robolectric
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.shadows.ShadowApplication;
@kingargyle
kingargyle / getActivityFromContextWrapper.java
Created September 21, 2016 13:48
Retrieve an Activity from a ContextWrapper
private Activity getActivity() {
Context context = getContext();
while (context instanceof ContextWrapper) {
if (context instanceof Activity) {
return (Activity)context;
}
context = ((ContextWrapper)context).getBaseContext();
}
return null;
}
@kingargyle
kingargyle / ripple_selector.xml
Created October 17, 2016 16:45
Ripple Effect inside a selector
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/loyalty_button_reward_ripple">
<item android:id="@android:id/mask" android:drawable="@drawable/button_shopping_bag_loyalty_normal"/>
<item>
<selector>
<item android:state_enabled="true" android:drawable="@drawable/button_shopping_bag_loyalty_normal"/>
<item android:state_enabled="false" android:drawable="@drawable/button_shopping_bag_loyalty_rewards"/>
</selector>
</item>
@kingargyle
kingargyle / SelfReturningAnswer.java
Created February 1, 2017 15:15
Self Returning Answer for Mockito
public class SelfReturningAnswer implements Answer<Object> {
public Object answer(InvocationOnMock invocation) throws Throwable {
Object mock = invocation.getMock();
if( invocation.getMethod().getReturnType().isInstance( mock )){
return mock;
}
else{
return RETURNS_DEFAULTS.get().answer(invocation);
}
@kingargyle
kingargyle / RetrofitCachingExample.java
Created March 7, 2017 18:30 — forked from swankjesse/RetrofitCachingExample.java
Demonstrate HTTP caching with OkHttp and Retrofit.
/*
* Copyright (C) 2013 Square, 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
@kingargyle
kingargyle / Jackson1ConverterFactory
Created March 8, 2017 21:30 — forked from svendroid/Jackson1ConverterFactory
Retrofit Jackson1ConverterFactory
package retrofit;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.ResponseBody;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.ObjectReader;
import org.codehaus.jackson.map.ObjectWriter;
import org.codehaus.jackson.type.JavaType;
@kingargyle
kingargyle / AbstractNLSSupportTest.java
Created July 10, 2017 20:29
An proof of concept for adding NLS annotations to a test for testing nationalization of property files.
public abstract class AbstractNLSSupportTest {
@Rule
public TestName name = new TestName();
protected String propertiesFilename;
protected String brandId;
protected String language;
protected String country;