/**@class android.os.StrictMode @extends java.lang.Object <p>StrictMode is a developer tool which detects things you might be doing by accident and brings them to your attention so you can fix them. <p>StrictMode is most commonly used to catch accidental disk or network access on the application's main thread, where UI operations are received and animations take place. Keeping disk and network operations off the main thread makes for much smoother, more responsive applications. By keeping your application's main thread responsive, you also prevent <a href="{@docRoot}guide/practices/design/responsiveness.html">ANR dialogs</a> from being shown to users. <p class="note">Note that even though an Android device's disk is often on flash memory, many devices run a filesystem on top of that memory with very limited concurrency. It's often the case that almost all disk accesses are fast, but may in individual cases be dramatically slower when certain I/O is happening in the background from other processes. If possible, it's best to assume that such things are not fast.</p> <p>Example code to enable from early in your {@link android.app.Application}, {@link android.app.Activity}, or other application component's {@link android.app.Application#onCreate} method: <pre> public void onCreate() { if (DEVELOPER_MODE) { StrictMode.setThreadPolicy(new {@link ThreadPolicy.Builder android.os.StrictMode.ThreadPolicy.Builder}() .detectDiskReads() .detectDiskWrites() .detectNetwork() // or .detectAll() for all detectable problems .penaltyLog() .build()); StrictMode.setVmPolicy(new {@link android.os.StrictMode.VmPolicy.Builder StrictMode.android.os.StrictMode.VmPolicy.Builder}() .detectLeakedSqlLiteObjects() .detectLeakedClosableObjects() .penaltyLog() .penaltyDeath() .build()); } super.onCreate(); } </pre> <p>You can decide what should happen when a violation is detected. For example, using {@link android.os.StrictMode.ThreadPolicy.Builder#penaltyLog} you can watch the output of <code>adb logcat</code> while you use your application to see the violations as they happen. <p>If you find violations that you feel are problematic, there are a variety of tools to help solve them: threads, {@link android.os.Handler}, {@link android.os.AsyncTask}, {@link android.app.IntentService}, etc. But don't feel compelled to fix everything that StrictMode finds. In particular, many cases of disk access are often necessary during the normal activity lifecycle. Use StrictMode to find things you did by accident. Network requests on the UI thread are almost always a problem, though. <p class="note">StrictMode is not a security mechanism and is not guaranteed to find all disk or network accesses. While it does propagate its state across process boundaries when doing {@link android.os.Binder} calls, it's still ultimately a best effort mechanism. Notably, disk or network access from JNI calls won't necessarily trigger it. Future versions of Android may catch more (or fewer) operations, so you should never leave StrictMode enabled in applications distributed on Google Play. */ var StrictMode = { /** Boolean system property to disable strict mode checks outright. Set this to 'true' to force disable; 'false' has no effect on other enable/disable policy. @hide */ DISABLE_PROPERTY : "persist.sys.strictmode.disable", /** The boolean system property to control screen flashes on violations. @hide */ VISUAL_PROPERTY : "persist.sys.strictmode.visual", /** @hide */ DETECT_DISK_WRITE : "1", /** @hide */ DETECT_DISK_READ : "2", /** @hide */ DETECT_NETWORK : "4", /** For StrictMode.noteSlowCall() @hide */ DETECT_CUSTOM : "8", /** For StrictMode.noteResourceMismatch() @hide */ DETECT_RESOURCE_MISMATCH : "16", /** Note, a "VM_" bit, not thread. @hide */ DETECT_VM_CURSOR_LEAKS : "256", /** Note, a "VM_" bit, not thread. @hide */ DETECT_VM_CLOSABLE_LEAKS : "512", /** Note, a "VM_" bit, not thread. @hide */ DETECT_VM_ACTIVITY_LEAKS : "1024", /** @hide */ DETECT_VM_REGISTRATION_LEAKS : "4096", /** @hide */ PENALTY_LOG : "65536", /** @hide */ PENALTY_DIALOG : "131072", /** Death on any detected violation. @hide */ PENALTY_DEATH : "262144", /** Death just for detected network usage. @hide */ PENALTY_DEATH_ON_NETWORK : "524288", /** Flash the screen during violations. @hide */ PENALTY_FLASH : "1048576", /** @hide */ PENALTY_DROPBOX : "2097152", /** Non-public penalty mode which overrides all the other penalty bits and signals that we're in a Binder call and we should ignore the other penalty bits and instead serialize back all our offending stack traces to the caller to ultimately handle in the originating process. This must be kept in sync with the constant in libs/binder/Parcel.cpp @hide */ PENALTY_GATHER : "4194304", /** Death when cleartext network traffic is detected. @hide */ PENALTY_DEATH_ON_CLEARTEXT_NETWORK : "8388608", /**{@hide} */ NETWORK_POLICY_ACCEPT : "0", /**{@hide} */ NETWORK_POLICY_LOG : "1", /**{@hide} */ NETWORK_POLICY_REJECT : "2", /**Sets the policy for what actions on the current thread should be detected, as well as the penalty if such actions occur. <p>Internally this sets a thread-local variable which is propagated across cross-process IPC calls, meaning you can catch violations when a system service or another process accesses the disk or network on your behalf. @param {Object {StrictMode.ThreadPolicy}} policy the policy to put into place */ setThreadPolicy : function( ) {}, /**Returns the bitmask of the current thread's policy. @return {Number} the bitmask of all the DETECT_* and PENALTY_* bits currently enabled @hide */ getThreadPolicyMask : function( ) {}, /**Returns the current thread's policy. */ getThreadPolicy : function( ) {}, /**A convenience wrapper that takes the current {@link android.os.StrictMode.ThreadPolicy} from {@link #getThreadPolicy}, modifies it to permit both disk reads & writes, and sets the new policy with {@link #setThreadPolicy}, returning the old policy so you can restore it at the end of a block. @return {Object {android.os.StrictMode.ThreadPolicy}} the old policy, to be passed to {@link #setThreadPolicy} to restore the policy at the end of a block */ allowThreadDiskWrites : function( ) {}, /**A convenience wrapper that takes the current {@link android.os.StrictMode.ThreadPolicy} from {@link #getThreadPolicy}, modifies it to permit disk reads, and sets the new policy with {@link #setThreadPolicy}, returning the old policy so you can restore it at the end of a block. @return {Object {android.os.StrictMode.ThreadPolicy}} the old policy, to be passed to setThreadPolicy to restore the policy. */ allowThreadDiskReads : function( ) {}, /**Enable DropBox logging for debug phone builds. @hide */ conditionallyEnableDebugLogging : function( ) {}, /**Used by the framework to make network usage on the main thread a fatal error. @hide */ enableDeathOnNetwork : function( ) {}, /** @hide */ conditionallyCheckInstanceCounts : function( ) {}, /**Sets the policy for what actions in the VM process (on any thread) should be detected, as well as the penalty if such actions occur. @param {Object {StrictMode.VmPolicy}} policy the policy to put into place */ setVmPolicy : function( ) {}, /**Gets the current VM policy. */ getVmPolicy : function( ) {}, /**Enable the recommended StrictMode defaults, with violations just being logged. <p>This catches disk and network access on the main thread, as well as leaked SQLite cursors and unclosed resources. This is simply a wrapper around {@link #setVmPolicy} and {@link #setThreadPolicy}. */ enableDefaults : function( ) {}, /** @hide */ vmSqliteObjectLeaksEnabled : function( ) {}, /** @hide */ vmClosableObjectLeaksEnabled : function( ) {}, /** @hide */ vmRegistrationLeaksEnabled : function( ) {}, /** @hide */ vmFileUriExposureEnabled : function( ) {}, /** @hide */ vmCleartextNetworkEnabled : function( ) {}, /** @hide */ onSqliteObjectLeaked : function( ) {}, /** @hide */ onWebViewMethodCalledOnWrongThread : function( ) {}, /** @hide */ onIntentReceiverLeaked : function( ) {}, /** @hide */ onServiceConnectionLeaked : function( ) {}, /** @hide */ onFileUriExposed : function( ) {}, /** @hide */ onCleartextNetworkDetected : function( ) {}, /** @hide */ onVmPolicyViolation : function( ) {}, /** @hide */ onVmPolicyViolation : function( ) {}, /**Enter a named critical span (e.g. an animation) <p>The name is an arbitary label (or tag) that will be applied to any strictmode violation that happens while this span is active. You must call finish() on the span when done. <p>This will never return null, but on devices without debugging enabled, this may return a dummy object on which the finish() method is a no-op. <p>TODO: add CloseGuard to this, verifying callers call finish. @hide */ enterCriticalSpan : function( ) {}, /**For code to note that it's slow. This is a no-op unless the current thread's {@link android.os.StrictMode.ThreadPolicy} has {@link android.os.StrictMode.ThreadPolicy.Builder#detectCustomSlowCalls} enabled. @param {String} name a short string for the exception stack trace that's built if when this fires. */ noteSlowCall : function( ) {}, /**For code to note that a resource was obtained using a type other than its defined type. This is a no-op unless the current thread's {@link android.os.StrictMode.ThreadPolicy} has {@link android.os.StrictMode.ThreadPolicy.Builder#detectResourceMismatches()} enabled. @param {Object {Object}} tag an object for the exception stack trace that's built if when this fires. @hide */ noteResourceMismatch : function( ) {}, /** @hide */ noteDiskRead : function( ) {}, /** @hide */ noteDiskWrite : function( ) {}, /**Returns an object that is used to track instances of activites. The activity should store a reference to the tracker object in one of its fields. @hide */ trackActivity : function( ) {}, /** @hide */ incrementExpectedActivityCount : function( ) {}, /** @hide */ decrementExpectedActivityCount : function( ) {}, };