Created
January 14, 2016 15:17
-
-
Save axemorgan/7f641b0420b021befc7d to your computer and use it in GitHub Desktop.
A wrapper for Android's Trace utility that checks the SDK version so you don't have to before each call.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.os.Build; | |
import android.os.Trace; | |
import android.support.annotation.NonNull; | |
/** | |
* A wrapper for {@link Trace}, which is unavailable on SDK versions under 18 (Jelly Bean MR2). | |
* Using any methods of this class on versions below 18 will result in a runtime exception. | |
*/ | |
public class Tracer { | |
private static final int REQUIRED_SDK = Build.VERSION_CODES.JELLY_BEAN_MR2; | |
/** | |
* {@link Trace#beginSection(String)} | |
* | |
* @param name A String used to label the section in Systrace | |
*/ | |
public static void beginSection(@NonNull String name) { | |
Tracer.throwIfNotDebug(); | |
if (Build.VERSION.SDK_INT >= REQUIRED_SDK) { | |
Trace.beginSection(name); | |
} else { | |
throw new RuntimeException("Tracing requires a minimum SDK of " + REQUIRED_SDK); | |
} | |
} | |
/** | |
* {@link Trace#endSection()} | |
*/ | |
public static void endSection() { | |
Tracer.throwIfNotDebug(); | |
if (Build.VERSION.SDK_INT >= REQUIRED_SDK) { | |
Trace.endSection(); | |
} else { | |
throw new RuntimeException("Tracing requires a minimum SDK of " + REQUIRED_SDK); | |
} | |
} | |
private static void throwIfNotDebug() { | |
if (!BuildConfig.DEBUG) { | |
throw new RuntimeException("Trace code must not be used in release builds."); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment