Skip to content

Instantly share code, notes, and snippets.

@LouisCAD
Last active September 10, 2016 00:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LouisCAD/3327a3d7a31d019a076a54c9a2bb33fd to your computer and use it in GitHub Desktop.
Save LouisCAD/3327a3d7a31d019a076a54c9a2bb33fd to your computer and use it in GitHub Desktop.
Allows one-liner GoogleApiClient Callbacks logging
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import timber.log.Timber;
import xyz.louiscad.common.Constants;
public enum DebugConnectionCallbacks {
;
@NonNull
public static GoogleApiClient.ConnectionCallbacks
logIt(@NonNull final GoogleApiClient.ConnectionCallbacks callbacks) {
if (!Constants.DEBUG) return callbacks;
else return new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(@Nullable Bundle connectionHint) {
Timber.d("onConnected(%s)", connectionHint);
callbacks.onConnected(connectionHint);
}
@Override
public void onConnectionSuspended(int cause) {
final String causeStr = cause == CAUSE_SERVICE_DISCONNECTED ?
"CAUSE_SERVICE_DISCONNECTED" : "CAUSE_NETWORK_LOST";
Timber.d("onConnectionSuspended(%s)", causeStr);
callbacks.onConnectionSuspended(cause);
}
};
}
@NonNull
public static GoogleApiClient.OnConnectionFailedListener
logFails(@NonNull final GoogleApiClient.OnConnectionFailedListener listener) {
if (!Constants.DEBUG) return listener;
else return new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Timber.d("onConnectionFailed(%s)", connectionResult);
listener.onConnectionFailed(connectionResult);
}
};
}
}
public interface DebugConstants {
/**
* Workaround for the always false BuildConfig.DEBUG value in libraries.
* Must be manually changed to false when build release apk.<br/>
* For non library modules, use BuildConfig.DEBUG instead, which value
* changes following buildType.
*/
boolean DEBUG = true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment