当前位置: 首页 > news >正文

android闪光灯源码分析

     

目录

一、APP层源码分析

                  二,framework层代码分析

        ​​​​​​​2.1 binder溯源


        这几天撸了android11 aosp闪光灯源码,本着前人栽树后人乘凉的原则,有志于android系统开发的新同学们提供一盏明灯,照亮你们前行。

        本人撸代码风格,喜欢从app撸到kernel,启航出发。

一、APP层源码分析

        下拉状态栏,可见“手电筒”快捷开关,点击后开启手电筒,UI见下图,

这部分代码,在目录:frameworks/base/packages/SystemUI/src/com/android/systemui/qs/tiles/FlashlightTile.java,点击按钮回调,

    protected void handleClick() {if (ActivityManager.isUserAMonkey()) {return;}boolean newState = !mState.value;refreshState(newState);//点击“手电筒”核心核数mFlashlightController.setFlashlight(newState);}

通过 mFlashlightController.setFlashlight(newState)走到文件

frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/policy/FlashlightControllerImpl.java,

    public void setFlashlight(boolean enabled) {boolean pendingError = false;synchronized (this) {if (mCameraId == null) return;if (mFlashlightEnabled != enabled) {mFlashlightEnabled = enabled;try {//核心调用mCameraManager.setTorchMode(mCameraId, enabled);} catch (CameraAccessException e) {Log.e(TAG, "Couldn't set torch mode", e);mFlashlightEnabled = false;pendingError = true;}}}dispatchModeChanged(mFlashlightEnabled);if (pendingError) {dispatchError();}}

通过mCameraManager.setTorchMode(mCameraId, enabled)走到frameworks/base/core/java/android/hardware/camera2/CameraManager.java,

    public void setTorchMode(@NonNull String cameraId, boolean enabled)throws CameraAccessException {if (CameraManagerGlobal.sCameraServiceDisabled) {throw new IllegalArgumentException("No cameras available on device");}CameraManagerGlobal.get().setTorchMode(cameraId, enabled);}

CameraManagerGlobal.get().setTorchMode(cameraId, enabled)走到,

        public void setTorchMode(String cameraId, boolean enabled) throws CameraAccessException {synchronized(mLock) {if (cameraId == null) {throw new IllegalArgumentException("cameraId was null");}ICameraService cameraService = getCameraService();if (cameraService == null) {throw new CameraAccessException(CameraAccessException.CAMERA_DISCONNECTED,"Camera service is currently unavailable");}try {cameraService.setTorchMode(cameraId, enabled, mTorchClientBinder);} catch(ServiceSpecificException e) {throwAsPublicException(e);} catch (RemoteException e) {throw new CameraAccessException(CameraAccessException.CAMERA_DISCONNECTED,"Camera service is currently unavailable");}}}
        ICameraService cameraService = getCameraService();看到这句是不是很熟悉,android获取系统服务,跨进程调用。只是和我们app开发常用的 不同,这里直接拿到camera服务的proxy。对下面这行代码不理解的,可以学习下android binder机制,本质就是拿到camera服务的proxy
ICameraService cameraService = ICameraService.Stub.asInterface(cameraServiceBinder);

        原来操作“手电筒”的不是单独模块,而是通过camera模块来控制的,666。

二,framework层代码分析

2.1 binder溯源

        由第一部分分析可知,“手电筒”通过cameraservice来操作,结合binder知识,可以在ICameraService.aidl接口定义中找到,

void setTorchMode(String cameraId, boolean enabled, IBinder clientBinder);

aidl文件会自动生成java文件,再通过jni调用到cpp文件,最终在ICameraService.cpp中实现了client的调用,

::android::binder::Status BpCameraService::setTorchMode(const ::android::String16& cameraId, bool enabled, const ::android::sp<::android::IBinder>& clientBinder) {::android::Parcel _aidl_data;::android::Parcel _aidl_reply;::android::status_t _aidl_ret_status = ::android::OK;::android::binder::Status _aidl_status;_aidl_ret_status = _aidl_data.writeInterfaceToken(getInterfaceDescriptor());if (((_aidl_ret_status) != (::android::OK))) {goto _aidl_error;}_aidl_ret_status = _aidl_data.writeString16(cameraId);if (((_aidl_ret_status) != (::android::OK))) {goto _aidl_error;}_aidl_ret_status = _aidl_data.writeBool(enabled);if (((_aidl_ret_status) != (::android::OK))) {goto _aidl_error;}_aidl_ret_status = _aidl_data.writeStrongBinder(clientBinder);if (((_aidl_ret_status) != (::android::OK))) {goto _aidl_error;}_aidl_ret_status = remote()->transact(::android::IBinder::FIRST_CALL_TRANSACTION + 15 /* setTorchMode */, _aidl_data, &_aidl_reply);if (UNLIKELY(_aidl_ret_status == ::android::UNKNOWN_TRANSACTION && ICameraService::getDefaultImpl())) {return ICameraService::getDefaultImpl()->setTorchMode(cameraId, enabled, clientBinder);}if (((_aidl_ret_status) != (::android::OK))) {goto _aidl_error;}_aidl_ret_status = _aidl_status.readFromParcel(_aidl_reply);if (((_aidl_ret_status) != (::android::OK))) {goto _aidl_error;}if (!_aidl_status.isOk()) {return _aidl_status;}_aidl_error:_aidl_status.setFromStatusT(_aidl_ret_status);return _aidl_status;
}

通过remote()->transact()发起IPC请求,事务码FIRST_CALL_TRANSACTION + 15对应setTorchMode方法,数据通过_aidl_dataParcel传递,执行到CameraService.cpp文件,

Status CameraService::setTorchMode(const String16& cameraId, bool enabled,const sp<IBinder>& clientBinder) {Mutex::Autolock lock(mServiceLock);ATRACE_CALL();if (enabled && clientBinder == nullptr) {ALOGE("%s: torch client binder is NULL", __FUNCTION__);return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT,"Torch client Binder is null");}String8 id = String8(cameraId.string());int uid = CameraThreadState::getCallingUid();if (shouldRejectSystemCameraConnection(id)) {return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, "Unable to set torch mode"" for system only device %s: ", id.string());}// verify id is valid.auto state = getCameraState(id);if (state == nullptr) {ALOGE("%s: camera id is invalid %s", __FUNCTION__, id.string());return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,"Camera ID \"%s\" is a not valid camera ID", id.string());}StatusInternal cameraStatus = state->getStatus();if (cameraStatus != StatusInternal::PRESENT &&cameraStatus != StatusInternal::NOT_AVAILABLE) {ALOGE("%s: camera id is invalid %s, status %d", __FUNCTION__, id.string(), (int)cameraStatus);return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,"Camera ID \"%s\" is a not valid camera ID", id.string());}{Mutex::Autolock al(mTorchStatusMutex);TorchModeStatus status;status_t err = getTorchStatusLocked(id, &status);if (err != OK) {if (err == NAME_NOT_FOUND) {return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,"Camera \"%s\" does not have a flash unit", id.string());}ALOGE("%s: getting current torch status failed for camera %s",__FUNCTION__, id.string());return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,"Error updating torch status for camera \"%s\": %s (%d)", id.string(),strerror(-err), err);}if (status == TorchModeStatus::NOT_AVAILABLE) {if (cameraStatus == StatusInternal::NOT_AVAILABLE) {ALOGE("%s: torch mode of camera %s is not available because ""camera is in use", __FUNCTION__, id.string());return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE,"Torch for camera \"%s\" is not available due to an existing camera user",id.string());} else {ALOGE("%s: torch mode of camera %s is not available due to ""insufficient resources", __FUNCTION__, id.string());return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE,"Torch for camera \"%s\" is not available due to insufficient resources",id.string());}}}{// Update UID map - this is used in the torch status changed callbacks, so must be done// before setTorchModeMutex::Autolock al(mTorchUidMapMutex);if (mTorchUidMap.find(id) == mTorchUidMap.end()) {mTorchUidMap[id].first = uid;mTorchUidMap[id].second = uid;} else {// Set the pending UIDmTorchUidMap[id].first = uid;}}status_t err = mFlashlight->setTorchMode(id, enabled);if (err != OK) {int32_t errorCode;String8 msg;switch (err) {case -ENOSYS:msg = String8::format("Camera \"%s\" has no flashlight",id.string());errorCode = ERROR_ILLEGAL_ARGUMENT;break;default:msg = String8::format("Setting torch mode of camera \"%s\" to %d failed: %s (%d)",id.string(), enabled, strerror(-err), err);errorCode = ERROR_INVALID_OPERATION;}ALOGE("%s: %s", __FUNCTION__, msg.string());return STATUS_ERROR(errorCode, msg.string());}{// update the link to client's deathMutex::Autolock al(mTorchClientMapMutex);ssize_t index = mTorchClientMap.indexOfKey(id);if (enabled) {if (index == NAME_NOT_FOUND) {mTorchClientMap.add(id, clientBinder);} else {mTorchClientMap.valueAt(index)->unlinkToDeath(this);mTorchClientMap.replaceValueAt(index, clientBinder);}clientBinder->linkToDeath(this);} else if (index != NAME_NOT_FOUND) {mTorchClientMap.valueAt(index)->unlinkToDeath(this);}}int clientPid = CameraThreadState::getCallingPid();const char *id_cstr = id.c_str();const char *torchState = enabled ? "on" : "off";ALOGI("Torch for camera id %s turned %s for client PID %d", id_cstr, torchState, clientPid);logTorchEvent(id_cstr, torchState , clientPid);return Status::ok();
}

其中,关键句status_t err = mFlashlight->setTorchMode(id, enabled);再由hal层交互可知,‌
最终通过CameraProviderManager查询HAL实现的setTorchMode()接口来控制device。

status_t CameraProviderManager::setTorchMode(const std::string &id, bool enabled) {std::lock_guard<std::mutex> lock(mInterfaceMutex);auto deviceInfo = findDeviceInfoLocked(id);if (deviceInfo == nullptr) return NAME_NOT_FOUND;// Pass the camera ID to start interface so that it will save it to the map of ICameraProviders// that are currently in use.sp<ProviderInfo> parentProvider = deviceInfo->mParentProvider.promote();if (parentProvider == nullptr) {return DEAD_OBJECT;}const sp<provider::V2_4::ICameraProvider> interface = parentProvider->startProviderInterface();if (interface == nullptr) {return DEAD_OBJECT;}saveRef(DeviceMode::TORCH, deviceInfo->mId, interface);return deviceInfo->setTorchMode(enabled);
}

最终,通过deviceInfo->setTorchMode(enabled)来实现对硬件的操作。

http://www.dtcms.com/a/272982.html

相关文章:

  • Android 插件化实现原理详解
  • 【读书笔记】如何画好架构图:架构思维的三大底层逻辑
  • 遥感影像图像分割-地物提取模型训练与大图直接推理流程
  • 突破传统局限:60G 3D毫米波雷达如何实现精准人体全状态检测?
  • Vue3基础知识
  • 论文笔记(LLM distillation):Distilling Step-by-Step!
  • 5、Vue中使用Cesium实现交互式折线绘制详解
  • 电脑被突然重启后,再每次打开excel文件,都会记录之前的位置窗口大小,第一次无法全屏显示。
  • imx6ul Qt运行qml报错This plugin does not support createPlatformOpenGLContext!
  • 无人机抗风模块运行与技术难点分析
  • Flowable22变量监听器---------------持续更新中
  • OneFileLLM:一键聚合多源信息流
  • 股指期货交割交易日到期没平仓盈亏以哪个价格计算?
  • RP2040使用存储系统
  • 2025年7月10日泛财经要闻精选
  • ACPU正式启动全球化布局,重构AI时代的中心化算力基础施设
  • 基于cornerstone3D的dicom影像浏览器 第三十二章 文件夹做pacs服务端,fake-pacs-server
  • 专题 数字(Number)基础
  • pytorch深度学习-Lenet-Minist
  • (LeetCode 每日一题) 3440. 重新安排会议得到最多空余时间 II (贪心)
  • RabbitMQ消息队列——三个核心特性
  • LeetCode 1652. 拆炸弹
  • AI时代的接口调试与文档生成:Apipost 与 Apifox 的表现对比
  • Leetcode刷题营第十九题:对链表进行插入排序
  • Python 网络爬虫中 robots 协议使用的常见问题及解决方法
  • 图解 BFS 路径搜索:LeetCode1971
  • 芯片I/O脚先于电源脚上电会导致Latch-up(闩锁效应)吗?
  • Logback日志框架配置实战指南
  • 5种使用USB数据线将文件从安卓设备传输到电脑的方法
  • 【JavaScript 函数、闭包与 this 绑定机制深度解析】