最近在做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了