Merge "StrictMode: Add support for warning on non SDK API usage." into pi-dev
This commit is contained in:
committed by
Android (Google) Code Review
commit
c8f5480981
@@ -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() {}
|
||||
|
||||
/**
|
||||
@@ -795,6 +809,23 @@ public final class StrictMode {
|
||||
return disable(DETECT_VM_ACTIVITY_LEAKS);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
@@ -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");
|
||||
}
|
||||
|
||||
28
core/java/android/os/strictmode/NonSdkApiUsedViolation.java
Normal file
28
core/java/android/os/strictmode/NonSdkApiUsedViolation.java
Normal file
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* Copyright (C) 2018 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package android.os.strictmode;
|
||||
|
||||
/**
|
||||
* Subclass of {@code Violation} that is used when a process accesses
|
||||
* a non SDK API.
|
||||
*/
|
||||
public final class NonSdkApiUsedViolation extends Violation {
|
||||
/** @hide */
|
||||
public NonSdkApiUsedViolation(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user