Skip to content

Commit

Permalink
Refactor: minor fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
tiann committed Mar 16, 2019
1 parent c089d6a commit cef2cdd
Showing 1 changed file with 36 additions and 14 deletions.
50 changes: 36 additions & 14 deletions library/src/main/java/me/weishu/reflection/Reflection.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.os.Build;
import android.util.Log;

import java.lang.reflect.Method;

import static android.os.Build.VERSION.PREVIEW_SDK_INT;
import static android.os.Build.VERSION.SDK_INT;
import static android.os.Build.VERSION_CODES.P;

/**
* @author weishu
* @date 2018/6/7.
Expand Down Expand Up @@ -44,11 +47,16 @@ public class Reflection {
private static int unsealed = UNKNOWN;

public static int unseal(Context context) {
if (Build.VERSION.SDK_INT < 28) {
if (SDK_INT < 28) {
// Below Android P, ignore
return 0;
}

// try exempt API first.
if (exemptAll()) {
return 0;
}

if (context == null) {
return -10;
}
Expand All @@ -57,25 +65,37 @@ public static int unseal(Context context) {
int targetSdkVersion = applicationInfo.targetSdkVersion;

synchronized (Reflection.class) {
if (unsealed == UNKNOWN) {
unsealed = unsealNative(targetSdkVersion);
if (unsealed >= 0) {
try {
@SuppressLint("PrivateApi") Method setHiddenApiEnforcementPolicy = ApplicationInfo.class
.getDeclaredMethod("setHiddenApiEnforcementPolicy", int.class);
setHiddenApiEnforcementPolicy.invoke(applicationInfo, 0);
} catch (Throwable e) {
e.printStackTrace();
unsealed = ERROR_SET_APPLICATION_FAILED;
}
}
if (unsealed != UNKNOWN) {
return unsealed;
}

unsealed = unsealNative(targetSdkVersion);
if (unsealed < 0) {
return unsealed;
}

if ((SDK_INT == P && PREVIEW_SDK_INT > 0) || SDK_INT > P) {
return unsealed;
}

// Android P, we need to sync the flags with ApplicationInfo
// We needn't to this on Android Q.
try {
@SuppressLint("PrivateApi") Method setHiddenApiEnforcementPolicy = ApplicationInfo.class
.getDeclaredMethod("setHiddenApiEnforcementPolicy", int.class);
setHiddenApiEnforcementPolicy.invoke(applicationInfo, 0);
} catch (Throwable e) {
e.printStackTrace();
unsealed = ERROR_SET_APPLICATION_FAILED;
}
}

return unsealed;
}

/**
* make the method exempted from hidden API check.
*
* @param method the method signature prefix.
* @return true if success.
*/
Expand All @@ -85,6 +105,7 @@ public static boolean exempt(String method) {

/**
* make specific methods exempted from hidden API check.
*
* @param methods the method signature prefix, such as "Ldalvik/system", "Landroid" or even "L"
* @return true if success
*/
Expand All @@ -103,6 +124,7 @@ public static boolean exempt(String... methods) {

/**
* Make all hidden API exempted.
*
* @return true if success.
*/
public static boolean exemptAll() {
Expand Down

0 comments on commit cef2cdd

Please sign in to comment.