问题:android长按power,出现关机、重启、飞行模式界面,长按关机会提示是否进入安全模式,现在需要去除这个功能。
在识别长按的是否返回可以解决该问题。
现在说一下我的改法:
在首次开机后,会在/data/system/users/目录下面生成0.xml(本机)和userlist.xml(游客)两个文件,这两个xml文件就会有一些UserManager里面的功能限制,安全模式就在这其中。
/**
* Specifies if the user is not allowed to reboot the device into safe boot mode.
* This can only be set by device owners and profile owners on the primary user.
* The default value is false
.
*
* Key for user restrictions.
*
Type: Boolean
* @see DevicePolicyManager#addUserRestriction(ComponentName, String)
* @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
* @see #getUserRestrictions()
*/
public static final String DISALLOW_SAFE_BOOT = "no_safe_boot";
开机初始化UserManagerService时会生成并写入限制功能到这两个xml文件中:
private UserManagerService(Context context, PackageManagerService pm,
Object packagesLock, File dataDir) {
mContext = context;
mPm = pm;
mPackagesLock = packagesLock;
mHandler = new MainHandler();
synchronized (mPackagesLock) {
mUsersDir = new File(dataDir, USER_INFO_DIR);
mUsersDir.mkdirs();
// Make zeroth user directory, for services to migrate their files to that location
File userZeroDir = new File(mUsersDir, String.valueOf(UserHandle.USER_SYSTEM));
userZeroDir.mkdirs();
FileUtils.setPermissions(mUsersDir.toString(),
FileUtils.S_IRWXU | FileUtils.S_IRWXG | FileUtils.S_IROTH | FileUtils.S_IXOTH,
-1, -1);
mUserListFile = new File(mUsersDir, USER_LIST_FILENAME);
initDefaultGuestRestrictions(); // 初始化游客权限
readUserListLP(); // 初始化管理员权限
sInstance = this;
}
mLocalService = new LocalService();
LocalServices.addService(UserManagerInternal.class, mLocalService);
mLockPatternUtils = new LockPatternUtils(mContext);
mUserStates.put(UserHandle.USER_SYSTEM, UserState.STATE_BOOTING);
}
initDefaultGuestRestrictions()很简单明了,不多说:
/**
* If default guest restrictions haven't been initialized yet, add the basic
* restrictions.
*/
private void initDefaultGuestRestrictions() {
synchronized (mGuestRestrictions) {
if (mGuestRestrictions.isEmpty()) {
mGuestRestrictions.putBoolean(UserManager.DISALLOW_CONFIG_WIFI, true);
mGuestRestrictions.putBoolean(UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES, true);
mGuestRestrictions.putBoolean(UserManager.DISALLOW_OUTGOING_CALLS, true);
mGuestRestrictions.putBoolean(UserManager.DISALLOW_SMS, true);
}
}
}
readUserListLP首先会查看是否存在该文件,如果存在就不初始化了,所以之前调试抓log的时候,里面的log永远打不出来,需要恢复出厂设置才会打。
if (!mUserListFile.exists()) {
fallbackToSingleUserLP();
return;
}
之后关键的代码在fallbackToSingleUserLP
Bundle restrictions = new Bundle();
try {
final String[] defaultFirstUserRestrictions = mContext.getResources().getStringArray(
com.android.internal.R.array.config_defaultFirstUserRestrictions);
for (String userRestriction : defaultFirstUserRestrictions) {
if (UserRestrictionsUtils.isValidRestriction(userRestriction)) {
restrictions.putBoolean(userRestriction, true);
}
}
} catch (Resources.NotFoundException e) {
Log.e(LOG_TAG, "Couldn't find resource: config_defaultFirstUserRestrictions", e);
}
config_defaultFirstUserRestrictions文件配置需要限制的功能,然后初始化的时候一一设置为true。
想要限制安全模式功能,按如下修改即可:
- no_safe_boot
注意overlay