下面列出了android.app.ActivityManager#isUserAMonkey ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void setOemUnlockEnabled(boolean enabled) throws SecurityException {
// do not allow monkey to flip the flag
if (ActivityManager.isUserAMonkey()) {
return;
}
enforceOemUnlockWritePermission();
enforceIsAdmin();
if (enabled) {
// Do not allow oem unlock to be enabled if it's disallowed by a user restriction.
enforceUserRestriction(UserManager.DISALLOW_OEM_UNLOCK);
enforceUserRestriction(UserManager.DISALLOW_FACTORY_RESET);
}
synchronized (mLock) {
doSetOemUnlockEnabledLocked(enabled);
computeAndWriteDigestLocked();
}
}
@Override
public void onPress() {
// don't actually trigger the bugreport if we are running stability
// tests via monkey
if (ActivityManager.isUserAMonkey()) {
return;
}
// Add a little delay before executing, to give the
// dialog a chance to go away before it takes a
// screenshot.
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
try {
// Take an "interactive" bugreport.
MetricsLogger.action(mContext,
MetricsEvent.ACTION_BUGREPORT_FROM_POWER_MENU_INTERACTIVE);
ActivityManager.getService().requestBugReport(
ActivityManager.BUGREPORT_OPTION_INTERACTIVE);
} catch (RemoteException e) {
}
}
}, 500);
}
@Override
public boolean onLongPress() {
// don't actually trigger the bugreport if we are running stability
// tests via monkey
if (ActivityManager.isUserAMonkey()) {
return false;
}
try {
// Take a "full" bugreport.
MetricsLogger.action(mContext, MetricsEvent.ACTION_BUGREPORT_FROM_POWER_MENU_FULL);
ActivityManager.getService().requestBugReport(
ActivityManager.BUGREPORT_OPTION_FULL);
} catch (RemoteException e) {
}
return false;
}
@Override
public void setOemUnlockAllowedByUser(boolean allowedByUser) {
if (ActivityManager.isUserAMonkey()) {
// Prevent a monkey from changing this
return;
}
enforceManageUserOemUnlockPermission();
enforceUserIsAdmin();
final long token = Binder.clearCallingIdentity();
try {
if (!isOemUnlockAllowedByAdmin()) {
throw new SecurityException("Admin does not allow OEM unlock");
}
if (!mOemLock.isOemUnlockAllowedByCarrier()) {
throw new SecurityException("Carrier does not allow OEM unlock");
}
mOemLock.setOemUnlockAllowedByDevice(allowedByUser);
setPersistentDataBlockOemUnlockAllowedBit(allowedByUser);
} finally {
Binder.restoreCallingIdentity(token);
}
}
/**
* If the "user" is a monkey, post an alert and notify the caller. This prevents automated
* test frameworks from stumbling into annoying or dangerous operations.
*/
private static boolean alertIfMonkey(Context context, int stringId) {
if (ActivityManager.isUserAMonkey()) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage(stringId);
builder.setPositiveButton(R.string.monkey_ok, null);
builder.show();
return true;
} else {
return false;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new DebugLog("MainActivity.onCreate", DebugLog.LogLevel.V);
//不支持旧版本的Android TV,理由:仅支持打开系统电源菜单意义不大
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P && SpecialSupport.isAndroidTV(this)) {
UIUtils.visibleHint(this, R.string.hint_android_tv_error);
startActivity(new Intent(Intent.ACTION_UNINSTALL_PACKAGE, Uri.parse("package:" + BuildConfig.APPLICATION_ID)).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
finish();
return;
}
//对于本应用的性质而言,Monkey Test没有必要,而且也容易造成测试机器电源状态改变
if (ActivityManager.isUserAMonkey()) {
new TextToast(this, true, getString(R.string.monkey_test_notice));
ShellUtils.killShKillProcess("monkey");
finish();
return;
}
//配置选项
String configView = getString(R.string.loading);
final String PREFIX = "\n√ ";
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT)
configView += getString(R.string.kitkat_compat);
if (ConfigManager.get(ConfigManager.WHITE_THEME))
configView += PREFIX + getString(R.string.r_whitetheme);
if (ConfigManager.get(ConfigManager.CANCELABLE))
configView += PREFIX + getString(R.string.r_cancelable);
if (ConfigManager.get(ConfigManager.NO_NEED_TO_CONFIRM))
configView += PREFIX + getString(R.string.r_normal_do);
if (ConfigManager.get(ConfigManager.DO_NOT_CHECK_ROOT)) {
configView += PREFIX + getString(R.string.r_no_root_check);
if (ConfigManager.get(ConfigManager.UNROOT_MODE))
configView += PREFIX + getString(R.string.r_unroot_mode);
}
cfgStat();
new TextToast(this, configView);
if (!ConfigManager.get(ConfigManager.DO_NOT_CHECK_ROOT))
activitySwitch(ShellUtils.isRoot());
else //如果不检查root权限,则检查“手动免root模式”配置
activitySwitch(!ConfigManager.get(ConfigManager.UNROOT_MODE));
}
/**
* Check if the normal method of "isUserAMonkey"
* returns a quick win of who the user is.
*
* @return {@code true} if the user is a monkey
* or {@code false} if not.
*/
public static boolean isUserAMonkey() {
return ActivityManager.isUserAMonkey();
}