package com.novoda.notils.logger.analyse;

import android.os.Build;
import android.os.StrictMode;

/**
 * Enables StrictMode with defaults to detect all wrong doing.
 * <p/>
 * See http://developer.android.com/reference/android/os/StrictMode.html for more
 */
public final class StrictModeManager {

    /**
     * Flag to enable or disable StrictMode
     * Recommended you set this to BuildConfig.DEBUG in your class that extends Application
     */
    public static boolean ON = true;

    private StrictModeManager() {
        throw new IllegalStateException("This class should not be instantiated");
    }

    /**
     * Initializes StrictMode to close activity and log to console when a violation occurs.
     */
    public static void initializeStrictMode() {
        initializeStrictMode(newThreadPolicyBuilderWithDefaultViolations(), newVmPolicyBuilderWithDefaultViolations());
    }

    /**
     * Initializes StrictMode to close activity and log to console when a violation occurs.
     *
     * Allows specification of violations via parameters. Note, penaltyLog and penaltyDeath will be applied.
     */
    public static void initializeStrictMode(StrictMode.ThreadPolicy.Builder threadPolicyBuilder, StrictMode.VmPolicy.Builder vmPolicyBuilder) {
        if (ON) {
            StrictMode.setThreadPolicy(threadPolicyBuilder.penaltyLog().penaltyDeath().build());
            StrictMode.setVmPolicy(vmPolicyBuilder.penaltyLog().penaltyDeath().build());
        }
    }

    /**
     * Initializes StrictMode to log to console when a violation occurs.
     */
    public static void initializeStrictModeLogOnly() {
        initializeStrictModeWithPenaltyLog(newThreadPolicyBuilderWithDefaultViolations(), newVmPolicyBuilderWithDefaultViolations());
    }

    /**
     * Initializes StrictMode to log to console when a violation occurs.
     * <p/>
     * Allows specification of violations via parameters. Note, penaltyLog will be applied.
     */
    public static void initializeStrictModeWithPenaltyLog(StrictMode.ThreadPolicy.Builder threadPolicyBuilder, StrictMode.VmPolicy.Builder vmPolicyBuilder) {
        if (ON) {
            StrictMode.setThreadPolicy(threadPolicyBuilder.penaltyLog().build());
            StrictMode.setVmPolicy(vmPolicyBuilder.penaltyLog().build());
        }
    }

    private static StrictMode.VmPolicy.Builder newVmPolicyBuilderWithDefaultViolations() {
        StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder().detectLeakedSqlLiteObjects();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            builder.detectLeakedRegistrationObjects();
        }
        return builder;
    }

    private static StrictMode.ThreadPolicy.Builder newThreadPolicyBuilderWithDefaultViolations() {
        return new StrictMode.ThreadPolicy.Builder()
                .detectCustomSlowCalls();
    }

}

@@ -40,33 +40,8 @@
 e;%0A%0A
-import android.os.Build;%0A
 impo
@@ -750,405 +750,43 @@
    i
-nitializeStrictMode(newThreadPolicyBuilderWithDefaultViolations(), newVmPolicyBuilderWithDefaultViolations());%0A    %7D%0A
+f (ON) %7B
 %0A    
-/**%0A
      
-* Initializes StrictMode to close activity and log to console when a violation occurs.%0A     *%0A     * Allows specification of violations via parameters. Note, penaltyLog and penaltyDeath will be applied.%0A     */%0A    public static void initializeStrictMode(StrictMode.
+   StrictMode.set
 Thre
@@ -789,34 +789,29 @@
 ThreadPolicy
-.Builder t
+(newT
 hreadPolicyB
@@ -820,144 +820,20 @@
 lder
-, StrictMode.VmPolicy.Builder vmPolicyBuilder) %7B%0A        if (ON) %7B%0A            StrictMode.setThreadPolicy(threadPolicyBuilder.penaltyLog
+WithDefaults
 ().p
@@ -883,33 +883,36 @@
 ode.setVmPolicy(
-v
+newV
 mPolicyBuilder.p
@@ -909,27 +909,28 @@
 yBuilder
-.penaltyLog
+WithDefaults
 ().penal
@@ -1114,500 +1114,8 @@
 ) %7B%0A
-        initializeStrictModeWithPenaltyLog(newThreadPolicyBuilderWithDefaultViolations(), newVmPolicyBuilderWithDefaultViolations());%0A    %7D%0A%0A    /**%0A     * Initializes StrictMode to log to console when a violation occurs.%0A     * %3Cp/%3E%0A     * Allows specification of violations via parameters. Note, penaltyLog will be applied.%0A     */%0A    public static void initializeStrictModeWithPenaltyLog(StrictMode.ThreadPolicy.Builder threadPolicyBuilder, StrictMode.VmPolicy.Builder vmPolicyBuilder) %7B%0A
     
@@ -1167,17 +1167,20 @@
 dPolicy(
-t
+newT
 hreadPol
@@ -1185,35 +1185,36 @@
 olicyBuilder
-.penaltyLog
+WithDefaults
 ().build());
@@ -1249,17 +1249,20 @@
 mPolicy(
-v
+newV
 mPolicyB
@@ -1271,19 +1271,20 @@
 lder
-.penaltyLog
+WithDefaults
 ().b
@@ -1381,33 +1381,24 @@
 rWithDefault
-Violation
 s() %7B%0A      
@@ -1403,45 +1403,14 @@
     
-StrictMode.VmPolicy.Builder builder =
+return
  new
@@ -1450,190 +1450,26 @@
 tect
-LeakedSqlLiteObjects();%0A        if (Build.VERSION.SDK_INT %3E= Build.VERSION_CODES.JELLY_BEAN) %7B%0A            builder.detectLeakedRegistrationObjects();%0A        %7D%0A        return builder
+All().penaltyLog()
 ;%0A  
@@ -1561,17 +1561,8 @@
 ault
-Violation
 s() 
@@ -1619,47 +1619,31 @@
 er()
-%0A                .detectCustomSlowCalls
+.detectAll().penaltyLog
 ();%0A
@@ -1650,9 +1650,8 @@
     %7D%0A%0A%7D
-%0A

Revert changes to StrictModeManager