diff options
Diffstat (limited to 'android/os/StrictMode.java')
-rw-r--r-- | android/os/StrictMode.java | 44 |
1 files changed, 43 insertions, 1 deletions
diff --git a/android/os/StrictMode.java b/android/os/StrictMode.java index a93e25aa..59380fd3 100644 --- a/android/os/StrictMode.java +++ b/android/os/StrictMode.java @@ -39,6 +39,7 @@ import android.os.strictmode.InstanceCountViolation; import android.os.strictmode.IntentReceiverLeakedViolation; import android.os.strictmode.LeakedClosableViolation; import android.os.strictmode.NetworkViolation; +import android.os.strictmode.NonSdkApiUsedViolation; import android.os.strictmode.ResourceMismatchViolation; import android.os.strictmode.ServiceConnectionLeakedViolation; import android.os.strictmode.SqliteObjectLeakedViolation; @@ -76,6 +77,7 @@ import java.util.HashMap; import java.util.concurrent.Executor; import java.util.concurrent.RejectedExecutionException; import java.util.concurrent.atomic.AtomicInteger; +import java.util.function.Consumer; /** * StrictMode is a developer tool which detects things you might be doing by accident and brings @@ -262,6 +264,9 @@ public final class StrictMode { /** @hide */ @TestApi public static final int DETECT_VM_UNTAGGED_SOCKET = 0x80 << 24; // for VmPolicy + /** @hide */ + @TestApi public static final int DETECT_VM_NON_SDK_API_USAGE = 0x40 << 24; // for VmPolicy + private static final int ALL_VM_DETECT_BITS = DETECT_VM_CURSOR_LEAKS | DETECT_VM_CLOSABLE_LEAKS @@ -271,7 +276,9 @@ public final class StrictMode { | DETECT_VM_FILE_URI_EXPOSURE | DETECT_VM_CLEARTEXT_NETWORK | DETECT_VM_CONTENT_URI_WITHOUT_PERMISSION - | DETECT_VM_UNTAGGED_SOCKET; + | DETECT_VM_UNTAGGED_SOCKET + | DETECT_VM_NON_SDK_API_USAGE; + // Byte 3: Penalty @@ -413,6 +420,13 @@ public final class StrictMode { */ private static final AtomicInteger sDropboxCallsInFlight = new AtomicInteger(0); + /** + * Callback supplied to dalvik / libcore to get informed of usages of java API that are not + * a part of the public SDK. + */ + private static final Consumer<String> sNonSdkApiUsageConsumer = + message -> onVmPolicyViolation(new NonSdkApiUsedViolation(message)); + private StrictMode() {} /** @@ -796,6 +810,23 @@ public final class StrictMode { } /** + * Detect reflective usage of APIs that are not part of the public Android SDK. + */ + public Builder detectNonSdkApiUsage() { + return enable(DETECT_VM_NON_SDK_API_USAGE); + } + + /** + * Permit reflective usage of APIs that are not part of the public Android SDK. Note + * that this <b>only</b> affects {@code StrictMode}, the underlying runtime may + * continue to restrict or warn on access to methods that are not part of the + * public SDK. + */ + public Builder permitNonSdkApiUsage() { + return disable(DETECT_VM_NON_SDK_API_USAGE); + } + + /** * Detect everything that's potentially suspect. * * <p>In the Honeycomb release this includes leaks of SQLite cursors, Activities, and @@ -826,6 +857,8 @@ public final class StrictMode { detectContentUriWithoutPermission(); detectUntaggedSockets(); } + + // TODO: Decide whether to detect non SDK API usage beyond a certain API level. return this; } @@ -1848,6 +1881,13 @@ public final class StrictMode { } else if (networkPolicy != NETWORK_POLICY_ACCEPT) { Log.w(TAG, "Dropping requested network policy due to missing service!"); } + + + if ((sVmPolicy.mask & DETECT_VM_NON_SDK_API_USAGE) != 0) { + VMRuntime.setNonSdkApiUsageConsumer(sNonSdkApiUsageConsumer); + } else { + VMRuntime.setNonSdkApiUsageConsumer(null); + } } } @@ -2576,6 +2616,8 @@ public final class StrictMode { return DETECT_VM_CONTENT_URI_WITHOUT_PERMISSION; } else if (mViolation instanceof UntaggedSocketViolation) { return DETECT_VM_UNTAGGED_SOCKET; + } else if (mViolation instanceof NonSdkApiUsedViolation) { + return DETECT_VM_NON_SDK_API_USAGE; } throw new IllegalStateException("missing violation bit"); } |