鸿蒙 gnss 开关使能流程

news/2024/7/21 10:30:12 标签: harmonyos, 华为

先WiFi,后 定位,再从蓝牙到NFC,这个就是我大致熟悉开源鸿蒙代码的一个顺序流程,WiFi 的年前差不多基本流程熟悉了,当然还有很多细节和内容没有写到,后续都会慢慢的丰富起来,这一篇将开启GNSS的篇章,先从GNSS使能开始,代码还是选取开源鸿蒙HarmonyOS 4.0的代码基线。
界面部分代码省略,直接JS看调用哪个接口,往下梳理
代码位置:base/location/frameworks/native/source/locator.cpp —> locator.cpp 的实现是 LocatorImpl

void LocatorImpl::EnableAbility(bool enable)
{
    if (!Init()) {
        return;
    }
    sptr<LocatorProxy> proxy = GetProxy();
    if (proxy == nullptr) {
        LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
        return;
    }
    LocationErrCode errCode = proxy->EnableAbilityV9(enable);       ---> 使能,继续看这个
    // cache the value
    if (errCode == ERRCODE_SUCCESS) {               ---> 使能成功,保存现在的状态
        if (locationDataManager_ != nullptr) {
            locationDataManager_->SetCachedSwitchState(enable ? ENABLED : DISABLED);
        }
    }
}

// base/location/frameworks/native/source/locator_proxy.cpp
void LocatorProxy::EnableAbility(bool isEnabled)
{
    MessageParcel data;
    MessageParcel reply;
    if (!data.WriteInterfaceToken(GetDescriptor())) {
        return;
    }
    data.WriteBool(isEnabled);
    int error = SendMsgWithDataReply(static_cast<int>(LocatorInterfaceCode::ENABLE_ABILITY), data, reply);
    LBSLOGD(LOCATOR_STANDARD, "Proxy::EnableAbility Transact ErrCodes = %{public}d", error);
}
//处理这个消息 ENABLE_ABILITY
// base/location/services/location_locator/locator/source/locator_skeleton.cpp
int LocatorAbilityStub::PreEnableAbility(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
{
    if (!CommonUtils::CheckSystemPermission(identity.GetTokenId(), identity.GetTokenIdEx())) {
        LBSLOGE(LOCATOR, "CheckSystemPermission return false, [%{public}s]",
            identity.ToString().c_str());
        reply.WriteInt32(ERRCODE_SYSTEM_PERMISSION_DENIED);
        return ERRCODE_SYSTEM_PERMISSION_DENIED;
    }
    if (!CheckSettingsPermission(reply, identity)) {
        return ERRCODE_PERMISSION_DENIED;
    }
    auto locatorAbility = DelayedSingleton<LocatorAbility>::GetInstance();
    if (locatorAbility == nullptr) {
        LBSLOGE(LOCATOR, "PreEnableAbility: LocatorAbility is nullptr.");
        reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
        return ERRCODE_SERVICE_UNAVAILABLE;
    }
    bool isEnabled = data.ReadBool();
    // 上面主要是权限的check,这里我们看下面这句
    reply.WriteInt32(locatorAbility->EnableAbility(isEnabled)); 
    return ERRCODE_SUCCESS;
}

// base/location/services/location_locator/locator/source/locator_ability.cpp
LocationErrCode LocatorAbility::EnableAbility(bool isEnabled)
{
    LBSLOGI(LOCATOR, "EnableAbility %{public}d", isEnabled);
    int modeValue = isEnabled ? 1 : 0;
    if (modeValue == QuerySwitchState()) {
        LBSLOGD(LOCATOR, "no need to set location ability, enable:%{public}d", modeValue);
        return ERRCODE_SUCCESS;
    }
    // 更新 value 值
    Uri locationDataEnableUri(LOCATION_DATA_URI);
    LocationErrCode errCode = DelayedSingleton<LocationDataRdbHelper>::GetInstance()->
        SetValue(locationDataEnableUri, LOCATION_DATA_COLUMN_ENABLE, modeValue);
    if (errCode != ERRCODE_SUCCESS) {
        LBSLOGE(LOCATOR, "%{public}s: can not set state to db", __func__);
        return ERRCODE_SERVICE_UNAVAILABLE;
    }
    UpdateSaAbility();   ---> 主要看下这个方法
    std::string state = isEnabled ? "enable" : "disable";
    WriteLocationSwitchStateEvent(state);
    return ERRCODE_SUCCESS;
}

继续看 UpdateSaAbility 方法干个啥。

LocationErrCode LocatorAbility::UpdateSaAbility()
{
    auto event = AppExecFwk::InnerEvent::Get(EVENT_UPDATE_SA, 0);
    if (locatorHandler_ != nullptr) {
        locatorHandler_->SendHighPriorityEvent(event);    ---> 发送EVENT_UPDATE_SA 事件
    }
    return ERRCODE_SUCCESS;
}

// 处理 EVENT_UPDATE_SA 这个事件的地方:
void LocatorHandler::ProcessEvent(const AppExecFwk::InnerEvent::Pointer& event)
{
	……… ………… ………
    LBSLOGI(LOCATOR, "ProcessEvent event:%{public}d", eventId);
    switch (eventId) {
        case EVENT_UPDATE_SA: {
            if (locatorAbility != nullptr) {
                locatorAbility->UpdateSaAbilityHandler();    ---> 看这个方法
            }
            break;
	……… ………… ………
}

void LocatorAbility::UpdateSaAbilityHandler()
{
    int state = QuerySwitchState();
    LBSLOGI(LOCATOR, "update location subability enable state, switch state=%{public}d, action registered=%{public}d",
        state, isActionRegistered);
    auto locatorBackgroundProxy = DelayedSingleton<LocatorBackgroundProxy>::GetInstance();
    if (locatorBackgroundProxy == nullptr) {
        LBSLOGE(LOCATOR, "UpdateSaAbilityHandler: LocatorBackgroundProxy is nullptr");
        return;
    }
    locatorBackgroundProxy.get()->OnSaStateChange(state == ENABLED);
}

// base/location/services/location_locator/locator/source/locator_background_proxy.cpp
void LocatorBackgroundProxy::OnSaStateChange(bool enable)
{
    if (proxySwtich_ == enable || !featureSwitch_) {
        return;
    }
    LBSLOGD(LOCATOR_BACKGROUND_PROXY, "OnSaStateChange %{public}d", enable);
    proxySwtich_ = enable;
    if (enable && !requestsList_->empty()) {    ---> 位置打开,如果没有请求就不会Start Locator
        StartLocator();
    } else {
        StopLocator();
    }
}

开源鸿蒙打开location开关使能比较简单,主要是状态上的处理和更新,下一篇章继续记录发起定位的流程。


http://www.niftyadmin.cn/n/5392323.html

相关文章

C++ 的左值右值和移动语义到底是什么?

移动语义是C11之后提出来的新概念&#xff0c;提供了一个构造对象时的优化点。 移动语义提出来之后&#xff0c;对于很多人来说&#xff0c;想要利用好移动语义的优化&#xff0c;左值右值的概念就需要从之前的字面化理解&#xff08;C 98&#xff09;变成现在的 “显学” &am…

C# 使用fo-dicom操作dicom文件

Dicom 数据集中的像素数据非常特别。它不能作为单个标签轻松读取或写入。 读取标签代码&#xff1a; var dcmFile DicomFile.Open(path); var dcmDataSet dcmFile.Dataset;string strPatientName dcmDataSet.GetString(DicomTag.PatientName); string strPatientSex dcmD…

电脑开机蓝屏错误代码c000021a怎么办 电脑蓝屏报错c000021a的解决办法

很多小伙伴在电脑开机的时候出现蓝屏代码c000021a都不知道该怎么去解决&#xff0c;所以今天就给你们带来了c000021a蓝屏解救方法&#xff0c;如果你还没解决的话就快来看看吧。 解决办法&#xff1a; 原因&#xff1a; c000021a蓝屏的原因有很多&#xff0c;主要有以下几种…

人工智能 — 相机模型和镜头畸变

目录 一、相机模型1、相机与图像2、坐标系1、世界坐标系2、相机坐标系3、图像物理坐标系4、图像像素坐标系 3、相机成像4、世界坐标系到摄像机坐标系5、欧氏变换6、齐次坐标7、摄像机坐标系到图像物理坐标系8、图像物理坐标系到图像像素坐标系9、摄像机坐标系到图像像素坐标系1…

移动端自动化常用的元素定位工具 介绍

在移动端自动化测试和开发中&#xff0c;元素定位是非常关键的一步。以下是一些常用的工具和技术来帮助开发者或测试工程师在移动设备上定位元素&#xff1a; 1. **UiAutomator**: - **UiAutomator** 是 Android 官方提供的自动化测试框架。它可以用来编写测试脚本&…

ubuntu22.04使用阿里云Docker镜像源安装Docker

更新包 sudo apt update 安装依赖包 sudo apt install apt-transport-https ca-certificates curl gnupg lsb-release添加阿里云Docker镜像源GPG秘钥 curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/dock…

android密集架移动动画效果开发

机缘 因公司需要开发密集架相关项目&#xff0c;涉及相关项目需求设计&#xff0c;市场上并未有相关动画效果流出&#xff0c;基于设计开发相关需求 多列密集架情况&#xff1a; 密集架固定列在最左侧密集架固定列在最右侧密集架固定列在最中间 收获 最终完成初步效果 实例…

挑战30天学完Python:Day22 爬虫

&#x1f389; 本系列为Python基础学习&#xff0c;原稿来源于 30-Days-Of-Python 英文项目&#xff0c;大奇主要是对其本地化翻译、逐条验证和补充&#xff0c;想通过30天完成正儿八经的系统化实践。此系列适合零基础同学&#xff0c;或仅了解Python一点知识&#xff0c;但又没…