Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0b558701 authored by Jeff Sharkey's avatar Jeff Sharkey
Browse files

StrictMode detectAll() based on target API level.

Some apps are asking StrictMode to detectAll(), but we should only
trigger the violation types that they would expect based on their
target SDK level.  (This prevents us from starting to yell about
violations in new API levels that a developer never would have known
about.)

Test: builds, boots, older apps aren't triggered
Bug: 34735225
Change-Id: Idff1bfdf57c635b828236e34a24d55350b9fa556
parent 9e77aefe
Loading
Loading
Loading
Loading
+40 −12
Original line number Diff line number Diff line
@@ -40,6 +40,7 @@ import com.android.internal.util.HexDump;
import dalvik.system.BlockGuard;
import dalvik.system.CloseGuard;
import dalvik.system.VMDebug;
import dalvik.system.VMRuntime;

import java.io.PrintWriter;
import java.io.StringWriter;
@@ -423,7 +424,21 @@ public final class StrictMode {
             * disk operations but will likely expand in future releases.
             */
            public Builder detectAll() {
                return enable(ALL_THREAD_DETECT_BITS);
                detectDiskReads();
                detectDiskWrites();
                detectNetwork();

                final int targetSdk = VMRuntime.getRuntime().getTargetSdkVersion();
                if (targetSdk >= Build.VERSION_CODES.HONEYCOMB) {
                    detectCustomSlowCalls();
                }
                if (targetSdk >= Build.VERSION_CODES.M) {
                    detectResourceMismatches();
                }
                if (targetSdk >= Build.VERSION_CODES.O) {
                    detectUnbufferedIo();
                }
                return this;
            }

            /**
@@ -722,18 +737,31 @@ public final class StrictMode {
             * but will likely expand in future releases.
             */
            public Builder detectAll() {
                int flags = DETECT_VM_ACTIVITY_LEAKS | DETECT_VM_CURSOR_LEAKS
                        | DETECT_VM_CLOSABLE_LEAKS | DETECT_VM_REGISTRATION_LEAKS
                        | DETECT_VM_FILE_URI_EXPOSURE | DETECT_VM_CONTENT_URI_WITHOUT_PERMISSION
                        | DETECT_VM_UNTAGGED_SOCKET;
                detectLeakedSqlLiteObjects();

                // TODO: always add DETECT_VM_CLEARTEXT_NETWORK once we have facility
                // for apps to mark sockets that should be ignored
                final int targetSdk = VMRuntime.getRuntime().getTargetSdkVersion();
                if (targetSdk >= Build.VERSION_CODES.HONEYCOMB) {
                    detectActivityLeaks();
                    detectLeakedClosableObjects();
                }
                if (targetSdk >= Build.VERSION_CODES.JELLY_BEAN) {
                    detectLeakedRegistrationObjects();
                }
                if (targetSdk >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
                    detectFileUriExposure();
                }
                if (targetSdk >= Build.VERSION_CODES.M) {
                    // TODO: always add DETECT_VM_CLEARTEXT_NETWORK once we have
                    // facility for apps to mark sockets that should be ignored
                    if (SystemProperties.getBoolean(CLEARTEXT_PROPERTY, false)) {
                    flags |= DETECT_VM_CLEARTEXT_NETWORK;
                        detectCleartextNetwork();
                    }

                return enable(flags);
                }
                if (targetSdk >= Build.VERSION_CODES.O) {
                    detectContentUriWithoutPermission();
                    detectUntaggedSockets();
                }
                return this;
            }

            /**