Last active
December 11, 2015 15:08
-
-
Save futtetennista/4619065 to your computer and use it in GitHub Desktop.
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
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