最近在做faceunlock与多用户交互的一些东西,我的录入入口是写在Settings中的,Settings是会随着用户的切换更换进程的持有者就像其他普通用户一样,但我的比对是写在SystemUI的keyguard中的,SystemUI去请求Camera.open的时候就会报错
CameraService: CameraService::connect X (PID 1334) rejected (cannot connect from device user 0, currently allowed device users: 10)
CameraBase: An error occurred while connecting to camera 1: Status(-8): '1: validateClientPermissionsLocked:933: Callers from device user 0 are not currently allowed to connect to camera "1"'
主要是这句
cannot connect from device user 0, currently allowed device users: 10
意思就是说此进程请求的用户是0也就是主用户,但是当前设备的用户是10也就是其他用户,换句话说就是无论怎样更换用户SystemUI始终被主用户持有,因此其他用户在SystemUI中就无法打开Camera
往底层追究此逻辑在
/frameworks/av/services/camera/libcameraservice/CameraService.cpp
中
// Only allow clients who are being used by the current foreground device user, unless calling
// from our own process.
if (callingPid != getpid() && (mAllowedUsers.find(clientUserId) == mAllowedUsers.end())) { //在这里比对了进程的pid和持有的userId
ALOGE("CameraService::connect X (PID %d) rejected (cannot connect from "
"device user %d, currently allowed device users: %s)", callingPid, clientUserId,
toString(mAllowedUsers).string());
return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
"Callers from device user %d are not currently allowed to connect to camera \"%s\"",
clientUserId, cameraId.string());
}
所以我们只要将这句改成
if (callingPid != getpid()&& clientUserId != 0 && (mAllowedUsers.find(clientUserId) == mAllowedUsers.end()))
就可以了,加入了进程持有者也可以是主用户的条件,这样一般用户也可以在SystemUI中打开Camera了