Skip to content

Instantly share code, notes, and snippets.

@futtetennista
Last active December 11, 2015 15:08
Show Gist options
  • Save futtetennista/4619065 to your computer and use it in GitHub Desktop.
Save futtetennista/4619065 to your computer and use it in GitHub Desktop.
public class CustomUncaughtExceptionHandlerProxy implements Thread.UncaughtExceptionHandler {
private static CustomUncaughtExceptionHandlerProxy proxy;
private final boolean enabled;
private Thread.UncaughtExceptionHandler customUncaughtExceptionHandler;
private CustomUncaughtExceptionHandlerProxy(CustomApplication application) {
enabled = !application.inDevMode();
if (enabled) {
// Save a reference to the current default handler.
customUncaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();
// Set our custom handler as the default one, so that it's first notified when something ugly happens.
Thread.setDefaultUncaughtExceptionHandler(this);
}
}
public static void initialize(CustomApplication application) {
if (proxy == null) {
proxy = new CustomUncaughtExceptionHandlerProxy(application);
}
}
public static CustomUncaughtExceptionHandlerProxy getInstance() {
return proxy;
}
@Override
public void uncaughtException(Thread thread, Throwable ex) {
if (enabled) {
if (!preferences.rateAppDialogAlreadyShown()) {
// reset count only if the 'rate app' dialog hasn't been already shown.
preferences.resetShowRateAppDialogCountdown();
}
// Let the default uncaught exception handler do its job.
customUncaughtExceptionHandler.uncaughtException(thread, ex);
} else {
Thread.getDefaultUncaughtExceptionHandler().uncaughtException(thread, ex);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment