Android系统设置中,"隐私"里面,有一项是 关于"摄像头使用权限",如果关闭了,则所有的app都没法正常去打开相机。
我们如果需要做类似禁用相机的功能,也是可以参考该部分的代码流程逻辑。

这一功能涉及到的相关代码,如下图所示。
CameraService.cpp
php
--------- CameraService.cppSet camera muting behaviorbool isCameraPrivacyEnabled = mSensorPrivacyPolicy->isCameraPrivacyEnabled(multiuser_get_user_id(clientUid)); if (client->supportsCameraMute()) { client->setCameraMute( mOverrideCameraMuteMode || isCameraPrivacyEnabled); } else if (isCameraPrivacyEnabled) { // no camera mute supported, but privacy is on! => disconnect ALOGI("Camera mute not supported for package: %s, camera id: %s", String8(client->getPackageName()).string(), cameraId.string()); // Do not hold mServiceLock while disconnecting clients, but // retain the condition blocking other clients from connecting // in mServiceLockWrapper if held. mServiceLock.unlock(); // Clear caller identity temporarily so client disconnect PID // checks work correctly int64_t token = CameraThreadState::clearCallingIdentity(); // Note AppOp to trigger the "Unblock" dialog client->noteAppOp(); client->disconnect(); CameraThreadState::restoreCallingIdentity(token); // Reacquire mServiceLock mServiceLock.lock(); return STATUS_ERROR_FMT(ERROR_DISABLED, "Camera \"%s\" disabled due to camera mute", cameraId.string()); }
php
bool isCameraPrivacyEnabled = mSensorPrivacyPolicy->isCameraPrivacyEnabled(multiuser_get_user_id(clientUid));
if (isCameraPrivacyEnabled) { // no camera mute supported, but privacy is on! => disconnect ALOGI("Camera mute not supported for package: %s, camera id: %s", String8(client->getPackageName()).string(), cameraId.string());bool CameraService::SensorPrivacyPolicy::isCameraPrivacyEnabled(userid_t userId) { if (!hasCameraPrivacyFeature()) { return false; } return mSpm.isIndividualSensorPrivacyEnabled(userId, SensorPrivacyManager::INDIVIDUAL_SENSOR_CAMERA); }
----- SensorPrivacyService.java isIndividualSensorPrivacyEnabled()
《小驰行动派的知识星球》

推荐阅读: