Skip to content

Instantly share code, notes, and snippets.

@heruoxin
Created January 29, 2018 05:05
Show Gist options
  • Save heruoxin/804b112c28a34f43d7e0c1fbb8414544 to your computer and use it in GitHub Desktop.
Save heruoxin/804b112c28a34f43d7e0c1fbb8414544 to your computer and use it in GitHub Desktop.
读取 Device Owner 模式下的 ApplicationInfo
// 读列表
pm.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES)
// 读到之后需要过滤掉双开/访客账户的 app
boolean installed = (applicationInfo.flags | ApplicationInfo.FLAG_INSTALLED == applicationInfo.flags);
// 判断 app 的状态是否被免 root冻结(hidden)的工具类
private static final int PRIVATE_FLAG_HIDDEN = 1;
private static final int FLAG_HIDDEN = 1<<27;
@Nullable
private static Field AI_FIELD;
static {
try {
if (Sdk.lowerThan(Sdk.M)) throw new UnsupportedOperationException();
AI_FIELD = ApplicationInfo.class.getDeclaredField("privateFlags");
AI_FIELD.setAccessible(true);
} catch (Throwable ignored) {
AI_FIELD = null;
}
}
public static boolean isAppHidden(ApplicationInfo ai) {
if (Objects.isNull(ai)) return false;
if (Objects.nonNull(AI_FIELD)) {
try {
int flags = (int) AI_FIELD.get(ai);
return FlagUtil.contain(flags, PRIVATE_FLAG_HIDDEN);
} catch (Throwable e) {
return FlagUtil.contain(ai.flags, FLAG_HIDDEN);
}
} else {
return FlagUtil.contain(ai.flags, FLAG_HIDDEN);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment