Skip to content

Instantly share code, notes, and snippets.

View TechIsFun's full-sized avatar

Andrea Maglie TechIsFun

View GitHub Profile
@TechIsFun
TechIsFun / parcelable_read_boolean.java
Last active August 29, 2015 14:08
Read boolean from Parcelable
public static boolean readBoolean(Parcel in) {
if (in != null) {
return in.readInt() == 1 ? true : false;
}
return false;
}
@TechIsFun
TechIsFun / parcelabe_write_boolean.java
Last active August 29, 2015 14:08
Write boolean to parcelable
public static void writeBoolean(Parcel destination, boolean value) {
if (destination != null) {
destination.writeInt(value ? 0 : 1);
}
}
@TechIsFun
TechIsFun / bundle_to_string.java
Created January 7, 2015 15:42
Dump content of bundle to a String
public static String bundle2String(Bundle bundle) {
StringBuilder sb = new StringBuilder();
List<String> keys = new ArrayList<String>(bundle.keySet());
Collections.sort(keys);
for (String key : keys) {
Object value = bundle.get(key);
if (value == null) {
sb.append(String.format("%s: null", key));
@TechIsFun
TechIsFun / incrementVersionCode.groovy
Created March 8, 2015 08:47
Android, gradle: a task that auto-increments versionCode
task incrementVersionCode << {
println(":incrementVersionCode - Incrementing Version Code...")
def buildGradleFile = file("build.gradle")
def patternVersionCode = Pattern.compile("versionCode (\\d+)")
def buildGradleFileText = buildGradleFile.getText()
def matcherVersionCode = patternVersionCode.matcher(buildGradleFileText)
matcherVersionCode.find()
def mVersionCode = Integer.parseInt(matcherVersionCode.group(1))
def mNextVersionCode = mVersionCode + 1
def manifestContent = matcherVersionCode.replaceAll("versionCode " + mNextVersionCode)
public Observable<Intent> networkObservable() {
return Observable.create(new Observable.OnSubscribe<Intent>() {
@Override
public void call(Subscriber<? super Intent> subscriber) {
final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
subscriber.onNext(intent);
}
};
public class EspressoFriendlyExampleTest {
private static final String TEST_ITEM_TITLE = "A test title";
private static final String TEST_ITEM_DESCRIPTION = "A test description";
private static final String TEST_FLAVOUR_TITLE = "A test flavour";
private static final String TEST_ITEM_COPY_TITLE = "A test copy";
private static final String TEST_ITEM_COPY_DESCRIPTION = "A test copy description";
@Override
protected void tearDown() throws Exception {
public class MyDebugTree extends Timber.DebugTree {
@Override
protected String createStackElementTag(StackTraceElement element) {
return super.createStackElementTag(element) + ":" + element.getLineNumber();
}
}
public class CrashlyticsTree extends Timber.Tree {
@Override
protected void log(int priority, String tag, String message, Throwable t) {
if (priority == Log.VERBOSE || priority == Log.DEBUG) {
return;
}
Crashlytics.log(priority, tag, message);
package com.example;
import android.content.SharedPreferences;
import android.support.annotation.NonNull;
import com.example.networking.ApiService;
import com.example.networking.response.ExampleResponse;
import com.google.gson.Gson;
import java.util.HashMap;
@Test
public void testOnClickMapButton() throws Exception {
Context spyContext = spy(RuntimenEnvironment.application);
mActivity.onClickOnMapButton(spyContext);
verify(spyContext).startActivity(argThat(googleMapsIntentMatcher()));
}
private static Matcher<Intent> googleMapsIntentMatcher() {