Skip to content

Instantly share code, notes, and snippets.

@inazaruk
Created August 6, 2012 22:25
Show Gist options
  • Select an option

  • Save inazaruk/3279042 to your computer and use it in GitHub Desktop.

Select an option

Save inazaruk/3279042 to your computer and use it in GitHub Desktop.
Usual way of managing StrictMode configuraitons
public class App extends Application {
private final static String TAG = Tags.getTag(App.class);
@Override
public void onCreate() {
if(BuildConfig.DEBUG) {
enableStrictMode();
}
super.onCreate();
LoaderManager.enableDebugLogging(BuildConfig.DEBUG);
FragmentManager.enableDebugLogging(BuildConfig.DEBUG);
}
private static void enableStrictMode() {
try {
StrictModeHelper.enableUniqueViolations(true);
doEnableStrictMode();
} catch (Throwable ex) {
Log.d(TAG, "Failed to enable StrictMode.", ex);
}
}
@TargetApi(9)
private static void doEnableStrictMode() {
ThreadPolicy.Builder threadBuilder;
VmPolicy.Builder vmBuilder;
threadBuilder = new ThreadPolicy.Builder();
vmBuilder = new VmPolicy.Builder();
try {
updateStrictModePolicies_v9(threadBuilder, vmBuilder);
} catch (Exception ex) {
Log.d(TAG, "Failed to update StrictMode policies for v9.");
}
try {
updateStrictModePolicies_v11(threadBuilder, vmBuilder);
} catch (Throwable ex) {
Log.d(TAG, "Failed to update StrictMode policies for v11.");
}
StrictMode.setThreadPolicy(threadBuilder.build());
StrictMode.setVmPolicy(vmBuilder.build());
}
@TargetApi(9)
private static void updateStrictModePolicies_v9(ThreadPolicy.Builder threadBuilder,
VmPolicy.Builder vmBuilder) {
threadBuilder.detectAll().penaltyLog().penaltyDropBox();
vmBuilder.detectAll().penaltyLog().penaltyDropBox();
}
@TargetApi(11)
private static void updateStrictModePolicies_v11(ThreadPolicy.Builder threadBuilder,
VmPolicy.Builder vmBuilder) {
threadBuilder.penaltyFlashScreen();
vmBuilder.detectLeakedClosableObjects().detectActivityLeaks();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment