diff --git a/frameworks/js/napi/source/location_napi_event.cpp b/frameworks/js/napi/source/location_napi_event.cpp index f3307059..299829d3 100644 --- a/frameworks/js/napi/source/location_napi_event.cpp +++ b/frameworks/js/napi/source/location_napi_event.cpp @@ -435,7 +435,17 @@ void GenerateExecuteContext(SingleLocationAsyncContext* context) } g_locatorProxy->StopLocating(callbackPtr); if (callbackHost->GetCount() != 0 && callbackHost->GetSingleLocation() == nullptr) { - context->errCode = ERRCODE_LOCATING_FAIL; + std::unique_ptr location = nullptr; +#ifdef ENABLE_NAPI_MANAGER + g_locatorProxy->GetCachedLocationV9(location); +#else + location = g_locatorProxy->GetCachedLocation(); +#endif + if (location != nullptr) { + callbackHost->SetSingleLocation(location); + } else { + context->errCode = ERRCODE_LOCATING_FAIL; + } } callbackHost->SetCount(1); #ifndef ENABLE_NAPI_MANAGER diff --git a/frameworks/location_common/common/liblbsservice_common_version_script.txt b/frameworks/location_common/common/liblbsservice_common_version_script.txt index c8a2fa03..e1290688 100644 --- a/frameworks/location_common/common/liblbsservice_common_version_script.txt +++ b/frameworks/location_common/common/liblbsservice_common_version_script.txt @@ -23,7 +23,7 @@ "*Location*WriteAppLocatingStateEvent*"; "*ExecuteHookWhenAddWorkRecord*"; "*LOCATOR_SA_LOCATION_PERMISSION_CHECK*"; - + "*ExecuteHookWhenCheckAppForCacheTime*"; local: *; }; \ No newline at end of file diff --git a/frameworks/location_common/common/source/common_utils.cpp b/frameworks/location_common/common/source/common_utils.cpp index 3f760363..ebe1cbdf 100644 --- a/frameworks/location_common/common/source/common_utils.cpp +++ b/frameworks/location_common/common/source/common_utils.cpp @@ -317,6 +317,13 @@ int64_t CommonUtils::GetCurrentTimeStamp() return static_cast(currentTime.tv_sec); } +int64_t CommonUtils::GetCurrentTimeStampMs() +{ + struct timeval currentTime; + gettimeofday(¤tTime, nullptr); + return static_cast(currentTime.tv_sec * MILLI_PER_SEC + currentTime.tv_usec / MICRO_PER_MILLI); +} + std::vector CommonUtils::Split(std::string str, std::string pattern) { std::vector result; diff --git a/frameworks/location_common/common/source/hook_utils.cpp b/frameworks/location_common/common/source/hook_utils.cpp index bcc64174..670ce80d 100644 --- a/frameworks/location_common/common/source/hook_utils.cpp +++ b/frameworks/location_common/common/source/hook_utils.cpp @@ -135,5 +135,15 @@ bool HookUtils::CheckGnssLocationValidity(const std::unique_ptr& locat LocationProcessStage::CHECK_GNSS_LOCATION_VALIDITY, (void *)&gnssLocationValidStruct, nullptr); return gnssLocationValidStruct.result; } + +bool HookUtils::ExecuteHookWhenCheckAppForCacheTime(std::string packageName) +{ + LocatorRequestStruct locatorRequestStruct; + locatorRequestStruct.bundleName = packageName; + locatorRequestStruct.result = false; + ExecuteHook( + LocationProcessStage::LOCATOR_SA_LOCATION_CACHE_TIME_CHECK, (void *)&locatorRequestStruct, nullptr); + return locatorRequestStruct.result; +} } // namespace Location } // namespace OHOS diff --git a/interfaces/inner_api/include/common_utils.h b/interfaces/inner_api/include/common_utils.h index 4e7a7328..5f85d1b1 100644 --- a/interfaces/inner_api/include/common_utils.h +++ b/interfaces/inner_api/include/common_utils.h @@ -172,6 +172,7 @@ public: static bool CheckAppInstalled(const std::string& bundleName); static int64_t GetCurrentTime(); static int64_t GetCurrentTimeStamp(); + static int64_t GetCurrentTimeStampMs(); static std::vector Split(std::string str, std::string pattern); static errno_t GetMacArray(const std::string& strMac, uint8_t mac[MAC_LEN]); static unsigned char ConvertStringToDigit(std::string str); diff --git a/interfaces/inner_api/include/hook_utils.h b/interfaces/inner_api/include/hook_utils.h index 0dcddbf6..696d2b25 100644 --- a/interfaces/inner_api/include/hook_utils.h +++ b/interfaces/inner_api/include/hook_utils.h @@ -43,6 +43,7 @@ enum class LocationProcessStage { ADD_REQUEST_TO_WORK_RECORD, LOCATOR_SA_LOCATION_PERMISSION_CHECK, LOCATOR_SA_COMMAND_PROCESS, + LOCATOR_SA_LOCATION_CACHE_TIME_CHECK, }; typedef struct { @@ -98,6 +99,7 @@ public: std::string bundleName); static bool CheckGnssLocationValidity(const std::unique_ptr& location); static bool ExecuteHookWhenCheckAppForUser(std::string packageName); + static bool ExecuteHookWhenCheckAppForCacheTime(std::string packageName); }; } // namespace Location } // namespace OHOS diff --git a/services/location_locator/locator/include/locator_background_proxy.h b/services/location_locator/locator/include/locator_background_proxy.h index 32ea628a..42af94c1 100644 --- a/services/location_locator/locator/include/locator_background_proxy.h +++ b/services/location_locator/locator/include/locator_background_proxy.h @@ -24,6 +24,8 @@ #include "app_state_data.h" #include "application_state_observer_stub.h" #include "common_event_subscriber.h" +#include "event_handler.h" +#include "event_runner.h" #include "system_ability_status_change_stub.h" #include "i_locator_callback.h" @@ -39,6 +41,13 @@ public: void OnForegroundApplicationChanged(const AppExecFwk::AppStateData& appStateData) override; }; +class LocatorBackgroundHandler : public AppExecFwk::EventHandler { +public: + explicit LocatorBackgroundHandler(const std::shared_ptr& runner); + ~LocatorBackgroundHandler() override; + void ProcessEvent(const AppExecFwk::InnerEvent::Pointer& event) override; +}; + class LocatorBackgroundProxy { public: static LocatorBackgroundProxy* GetInstance(); @@ -54,11 +63,11 @@ public: bool UnregisterAppStateObserver(); bool IsAppInLocationContinuousTasks(pid_t uid, pid_t pid); bool IsAppHasFormVisible(uint32_t tokenId, uint64_t tokenIdEx); -private: void StartLocator(); void StopLocator(); void StartLocatorThread(); void StopLocatorThread(); +private: void OnUserSwitch(int32_t userId); void OnUserRemove(int32_t userId); void UpdateListOnSuspend(const std::shared_ptr& request, bool active); @@ -116,6 +125,7 @@ private: static std::mutex locatorMutex_; sptr iAppMgr_ = nullptr; sptr appStateObserver_ = nullptr; + std::shared_ptr locatorBackgroundHandler_; }; } // namespace Location } // namespace OHOS diff --git a/services/location_locator/locator/include/locator_required_data_manager.h b/services/location_locator/locator/include/locator_required_data_manager.h index f29caab7..446d0742 100644 --- a/services/location_locator/locator/include/locator_required_data_manager.h +++ b/services/location_locator/locator/include/locator_required_data_manager.h @@ -200,6 +200,12 @@ public: __attribute__((no_sanitize("cfi"))) void StartWifiScan(int fixNumber, bool flag); bool IsConnecting(); static LocatorRequiredDataManager* GetInstance(); + void SyncStillMovementState(bool state); + void SendWifiScanEvent(); + void SendGetWifiListEvent(int timeout); + void SetToObtainCellinfo(); + void UpdateWifiScanCompleteTimestamp(); + int64_t GetWifiScanCompleteTimestamp(); private: int timeInterval_ = 0; @@ -215,7 +221,6 @@ private: void WifiInfoInit(); bool IsWifiCallbackRegistered(); void SetIsWifiCallbackRegistered(bool isWifiCallbackRegistered); - std::shared_ptr wifiScanPtr_; bool isWifiCallbackRegistered_ = false; std::mutex wifiRegisteredMutex_; WifiEvent wifiScanEventCallback_ = {0}; @@ -224,6 +229,13 @@ private: std::map, AppIdentity> callbacksMap_; std::shared_ptr scanHandler_; std::shared_ptr wifiSdkHandler_; + int64_t wifiScanTimestamp_ = 0; + std::mutex wifiScanCompleteTimestampMutex_; + int64_t wifiScanCompleteTimestamp_ = 0; + int64_t getWifiScanInfoTimestamp_ = 0; + std::mutex lastStillTimeMutex_; + int64_t lastStillTime_ = 0; + std::vector wifiScanInfo_; }; } // namespace Location } // namespace OHOS diff --git a/services/location_locator/locator/source/locator_ability.cpp b/services/location_locator/locator/source/locator_ability.cpp index 809eb9a6..5755ff75 100644 --- a/services/location_locator/locator/source/locator_ability.cpp +++ b/services/location_locator/locator/source/locator_ability.cpp @@ -96,7 +96,7 @@ const uint32_t EVENT_SET_SWITCH_STATE_TO_DB_BY_USERID = 0x0026; const uint32_t RETRY_INTERVAL_UNITE = 1000; const uint32_t RETRY_INTERVAL_OF_INIT_REQUEST_MANAGER = 5 * RETRY_INTERVAL_UNITE; -const uint32_t RETRY_INTERVAL_OF_UNLOAD_SA = 30 * RETRY_INTERVAL_UNITE; +const uint32_t RETRY_INTERVAL_OF_UNLOAD_SA = 30 * 60 * RETRY_INTERVAL_UNITE; const int COMMON_SA_ID = 4353; const int COMMON_SWITCH_STATE_ID = 30; const std::u16string COMMON_DESCRIPTION = u"location.IHifenceAbility"; @@ -171,6 +171,7 @@ void LocatorAbility::OnStop() LBSLOGD(LOCATOR, "LocatorAbility::reset LocationWorkingState failed."); } SetLocationhubStateToSyspara(LOCATIONHUB_STATE_UNLOAD); + LocatorRequiredDataManager::GetInstance()->UnregisterWifiCallBack(); LBSLOGI(LOCATOR, "LocatorAbility::OnStop ability stopped."); } @@ -958,13 +959,14 @@ LocationErrCode LocatorAbility::StartLocating(std::unique_ptr& re Security::AccessToken::AccessTokenKit::GetPermissionUsedType(request->GetTokenId(), ACCESS_APPROXIMATELY_LOCATION); request->SetPermUsedType(static_cast(type)); - + LocatorRequiredDataManager::GetInstance()->SendWifiScanEvent(); #ifdef EMULATOR_ENABLED // for emulator, report cache location is unnecessary HandleStartLocating(request, callback); #else if (NeedReportCacheLocation(request, callback)) { LBSLOGI(LOCATOR, "report cache location to %{public}s", identity.GetBundleName().c_str()); + LocatorBackgroundProxy::GetInstance()->StartLocator(); callback->AsObject()->RemoveDeathRecipient(death); } else { HandleStartLocating(request, callback); @@ -1096,6 +1098,7 @@ LocationErrCode LocatorAbility::StopLocating(sptr& callback) LocationErrCode LocatorAbility::GetCacheLocation(std::unique_ptr& loc, AppIdentity &identity) { + LocatorBackgroundProxy::GetInstance()->StartLocator(); if (locatorHandler_ == nullptr) { return ERRCODE_SERVICE_UNAVAILABLE; } diff --git a/services/location_locator/locator/source/locator_background_proxy.cpp b/services/location_locator/locator/source/locator_background_proxy.cpp index 088cdb45..c73f07a8 100644 --- a/services/location_locator/locator/source/locator_background_proxy.cpp +++ b/services/location_locator/locator/source/locator_background_proxy.cpp @@ -34,6 +34,7 @@ #include "accesstoken_kit.h" #include "tokenid_kit.h" +#include "ipc_skeleton.h" #ifdef BGTASKMGR_SUPPORT #include "background_mode.h" @@ -48,6 +49,9 @@ namespace OHOS { namespace Location { std::mutex LocatorBackgroundProxy::requestListMutex_; std::mutex LocatorBackgroundProxy::locatorMutex_; +const uint32_t EVENT_STARTLOCATING = 0x0100; +const uint32_t EVENT_STOPLOCATING = 0x0200; + LocatorBackgroundProxy* LocatorBackgroundProxy::GetInstance() { static LocatorBackgroundProxy data; @@ -57,6 +61,8 @@ LocatorBackgroundProxy* LocatorBackgroundProxy::GetInstance() LocatorBackgroundProxy::LocatorBackgroundProxy() { InitArgsFromProp(); + locatorBackgroundHandler_ = std::make_shared(AppExecFwk::EventRunner::Create(true, + AppExecFwk::ThreadMode::FFRT)); if (!featureSwitch_) { return; } @@ -66,7 +72,7 @@ LocatorBackgroundProxy::LocatorBackgroundProxy() requestsMap_->insert(make_pair(curUserId_, requestsList_)); auto requestConfig = std::make_unique(); - requestConfig->SetPriority(PRIORITY_LOW_POWER); + requestConfig->SetPriority(PRIORITY_FAST_FIRST_FIX); requestConfig->SetTimeInterval(timeInterval_); callback_ = sptr(new (std::nothrow) LocatorBackgroundProxy::mLocatorCallback()); if (callback_ == nullptr) { @@ -81,9 +87,12 @@ LocatorBackgroundProxy::LocatorBackgroundProxy() request_->SetPackageName(PROC_NAME); request_->SetRequestConfig(*requestConfig); request_->SetLocatorCallBack(callback_); + request_->SetUuid(PROC_NAME); + request_->SetTokenId(IPCSkeleton::GetCallingTokenID()); + request_->SetTokenIdEx(IPCSkeleton::GetCallingFullTokenID()); SubscribeSaStatusChangeListerner(); isUserSwitchSubscribed_ = LocatorBackgroundProxy::UserSwitchSubscriber::Subscribe(); - proxySwtich_ = (LocationConfigManager::GetInstance()->GetLocationSwitchState() == ENABLED); + proxySwtich_ = (LocationDataRdbManager::QuerySwitchState() == ENABLED); RegisterAppStateObserver(); } @@ -122,12 +131,9 @@ void LocatorBackgroundProxy::SubscribeSaStatusChangeListerner() void LocatorBackgroundProxy::StartLocatorThread() { auto requestManager = RequestManager::GetInstance(); - auto locatorAbility = LocatorAbility::GetInstance(); - std::this_thread::sleep_for(std::chrono::seconds(timeInterval_)); std::unique_lock lock(locatorMutex_, std::defer_lock); lock.lock(); - isWating_ = false; - if (isLocating_ || !proxySwtich_ || requestsList_->empty()) { + if (isLocating_ || !proxySwtich_) { LBSLOGD(LOCATOR_BACKGROUND_PROXY, "cancel locating"); lock.unlock(); return; @@ -136,7 +142,6 @@ void LocatorBackgroundProxy::StartLocatorThread() lock.unlock(); LBSLOGI(LOCATOR_BACKGROUND_PROXY, "real start locating"); requestManager->HandleStartLocating(request_); - locatorAbility->ReportLocationStatus(callback_, SESSION_START); } void LocatorBackgroundProxy::StopLocatorThread() @@ -156,10 +161,13 @@ void LocatorBackgroundProxy::StopLocatorThread() void LocatorBackgroundProxy::StopLocator() { + locatorBackgroundHandler_->SendHighPriorityEvent(EVENT_STOPLOCATING, 0, 0); } void LocatorBackgroundProxy::StartLocator() { + locatorBackgroundHandler_->SendHighPriorityEvent(EVENT_STARTLOCATING, 0, 0); + locatorBackgroundHandler_->SendHighPriorityEvent(EVENT_STOPLOCATING, 0, DEFAULT_TIMEOUT_5S); } void LocatorBackgroundProxy::UpdateListOnRequestChange(const std::shared_ptr& request) @@ -178,38 +186,11 @@ void LocatorBackgroundProxy::OnSuspend(const std::shared_ptr& request, // when switch off, stop proxy void LocatorBackgroundProxy::OnSaStateChange(bool enable) { - if (proxySwtich_ == enable || !featureSwitch_) { - return; - } - LBSLOGD(LOCATOR_BACKGROUND_PROXY, "OnSaStateChange %{public}d", enable); - proxySwtich_ = enable; - if (enable && !requestsList_->empty()) { - StartLocator(); - } else { - StopLocator(); - } } // called when deleteRequest called from locator ability (e.g. app stop locating) void LocatorBackgroundProxy::OnDeleteRequestRecord(const std::shared_ptr& request) { - if (!featureSwitch_) { - return; - } - bool listEmpty = false; - std::unique_lock lock(requestListMutex_, std::defer_lock); - lock.lock(); - auto it = find(requestsList_->begin(), requestsList_->end(), request); - if (it != requestsList_->end()) { - requestsList_->remove(request); - if (requestsList_->empty()) { - listEmpty = true; - } - } - lock.unlock(); - if (listEmpty) { - StopLocator(); - } } bool LocatorBackgroundProxy::CheckPermission(const std::shared_ptr& request) const @@ -275,12 +256,6 @@ void LocatorBackgroundProxy::OnUserSwitch(int32_t userId) if (locatorAbility != nullptr) { locatorAbility->ApplyRequests(0); } - if (!requestsList_->empty()) { - StartLocator(); - } else { - LBSLOGD(LOCATOR_BACKGROUND_PROXY, "OnUserSwitch stoplocator"); - StopLocator(); - } } void LocatorBackgroundProxy::OnUserRemove(int32_t userId) @@ -315,16 +290,7 @@ bool LocatorBackgroundProxy::CheckMaxRequestNum(pid_t uid, const std::string& pa void LocatorBackgroundProxy::mLocatorCallback::OnLocationReport(const std::unique_ptr& location) { LBSLOGD(LOCATOR_BACKGROUND_PROXY, "locator background OnLocationReport"); - auto locatorBackgroundProxy = LocatorBackgroundProxy::GetInstance(); - auto requestsList = locatorBackgroundProxy->GetRequestsInProxy(); - if (requestsList.empty()) { - locatorBackgroundProxy->StopLocator(); - return; - } - // call the callback of each proxy app - for (auto request : requestsList) { - request->GetLocatorCallBack()->OnLocationReport(location); - } + LocatorBackgroundProxy::GetInstance()->StopLocator(); } void LocatorBackgroundProxy::mLocatorCallback::OnLocatingStatusChange(const int status) @@ -526,5 +492,28 @@ void AppStateChangeCallback::OnForegroundApplicationChanged(const AppExecFwk::Ap "The state of App changed, uid = %{public}d, pid = %{public}d, state = %{public}d", uid, pid, state); requestManager->HandlePowerSuspendChanged(pid, uid, state); } + +LocatorBackgroundHandler::LocatorBackgroundHandler( + const std::shared_ptr& runner) : EventHandler(runner) {} + +LocatorBackgroundHandler::~LocatorBackgroundHandler() {} + +void LocatorBackgroundHandler::ProcessEvent(const AppExecFwk::InnerEvent::Pointer& event) +{ + auto locatorBackgroundProxy = LocatorBackgroundProxy::GetInstance(); + uint32_t eventId = event->GetInnerEventId(); + switch (eventId) { + case EVENT_STARTLOCATING: { + locatorBackgroundProxy->StartLocatorThread(); + break; + } + case EVENT_STOPLOCATING: { + locatorBackgroundProxy->StopLocatorThread(); + break; + } + default: + break; + } +} } // namespace OHOS } // namespace Location diff --git a/services/location_locator/locator/source/locator_required_data_manager.cpp b/services/location_locator/locator/source/locator_required_data_manager.cpp index c92f56df..94d47856 100644 --- a/services/location_locator/locator/source/locator_required_data_manager.cpp +++ b/services/location_locator/locator/source/locator_required_data_manager.cpp @@ -29,14 +29,15 @@ const uint32_t EVENT_GET_WIFI_LIST = 0x0300; const uint32_t EVENT_REGISTER_WIFI_CALLBACK = 0x0400; const uint32_t EVENT_UNREGISTER_WIFI_CALLBACK = 0x0500; const int32_t DEFAULT_TIMEOUT_4S = 4000; +const int32_t DEFAULT_TIMEOUT_MS = 1500; LocatorRequiredDataManager::LocatorRequiredDataManager() { -#ifdef WIFI_ENABLE - WifiInfoInit(); -#endif scanHandler_ = std::make_shared(AppExecFwk::EventRunner::Create(true, AppExecFwk::ThreadMode::FFRT)); wifiSdkHandler_ = std::make_shared(AppExecFwk::EventRunner::Create(true, AppExecFwk::ThreadMode::FFRT)); + if (wifiSdkHandler_ != nullptr) { + wifiSdkHandler_->SendEvent(EVENT_REGISTER_WIFI_CALLBACK, 0, 0); + } } LocatorRequiredDataManager* LocatorRequiredDataManager::GetInstance() @@ -47,6 +48,44 @@ LocatorRequiredDataManager* LocatorRequiredDataManager::GetInstance() LocatorRequiredDataManager::~LocatorRequiredDataManager() { + if (wifiSdkHandler_ != nullptr) { + wifiSdkHandler_->SendEvent(EVENT_UNREGISTER_WIFI_CALLBACK, 0, 0); + } +} + +void LocatorRequiredDataManager::SyncStillMovementState(bool state) +{ + std::unique_lock lock(lastStillTimeMutex_); + if (state) { + lastStillTime_ = CommonUtils::GetCurrentTimeStampMs(); + SendWifiScanEvent(); + } else { + lastStillTime_ = 0; + } +} + +void LocatorRequiredDataManager::SetToObtainCellinfo() +{ + bool needScan = false; + wifiScanTimestamp_ = CommonUtils::GetCurrentTimeStampMs(); + std::unique_lock lock(wifiScanCompleteTimestampMutex_); + wifiScanCompleteTimestamp_ = wifiScanTimestamp_ + 1; +} + +void LocatorRequiredDataManager::SendWifiScanEvent() +{ + if (scanHandler_ != nullptr) { + AppExecFwk::InnerEvent::Pointer event = AppExecFwk::InnerEvent::Get( + EVENT_START_SCAN, 1); + scanHandler_->SendEvent(event); + } +} + +void LocatorRequiredDataManager::SendGetWifiListEvent(int timeout) +{ + if (wifiSdkHandler_ != nullptr) { + wifiSdkHandler_->SendHighPriorityEvent(EVENT_GET_WIFI_LIST, 0, timeout); + } } __attribute__((no_sanitize("cfi"))) LocationErrCode LocatorRequiredDataManager::RegisterCallback( @@ -59,21 +98,20 @@ __attribute__((no_sanitize("cfi"))) LocationErrCode LocatorRequiredDataManager:: callbacksMap_[callback] = identity; LBSLOGD(LOCATOR, "after RegisterCallback, callback size:%{public}s", std::to_string(callbacksMap_.size()).c_str()); + if (!IsWifiCallbackRegistered() && wifiSdkHandler_ != nullptr) { + wifiSdkHandler_->SendEvent(EVENT_REGISTER_WIFI_CALLBACK, 0, 0); + } bool needScan = false; - if (config->GetNeedStartScan() && (callbacksMap_.size() == 1 || !IsWifiCallbackRegistered())) { + if (config->GetNeedStartScan()) { needScan = true; } lock.unlock(); if (needScan) { - if (wifiSdkHandler_ != nullptr) { - wifiSdkHandler_->SendEvent(EVENT_REGISTER_WIFI_CALLBACK, 0, 0); - } - timeInterval_ = config->GetScanIntervalMs(); - if (scanHandler_ != nullptr) { - AppExecFwk::InnerEvent::Pointer event = AppExecFwk::InnerEvent::Get( - EVENT_START_SCAN, config->GetFixNumber()); - scanHandler_->SendEvent(event); - } + SendWifiScanEvent(); + SendGetWifiListEvent(DEFAULT_TIMEOUT_4S >= config->GetScanTimeoutMs() ? + config->GetScanTimeoutMs() : DEFAULT_TIMEOUT_4S); + } else { + SendGetWifiListEvent(0); } #endif } else if (config->GetType() == LocatingRequiredDataType::BLUE_TOOTH) { @@ -97,12 +135,6 @@ LocationErrCode LocatorRequiredDataManager::UnregisterCallback(const sptrSendEvent(EVENT_UNREGISTER_WIFI_CALLBACK, 0, 0); - } - if (scanHandler_ != nullptr) { - scanHandler_->SendEvent(EVENT_STOP_SCAN, 0, 0); - } #endif return ERRCODE_SUCCESS; } @@ -178,11 +210,6 @@ void LocatorBleCallbackWapper::OnNotifyMsgReportFromLpDevice(const Bluetooth::UU #endif #ifdef WIFI_ENABLE -void LocatorRequiredDataManager::WifiInfoInit() -{ - wifiScanPtr_ = Wifi::WifiScan::GetInstance(WIFI_SCAN_ABILITY_ID); -} - bool LocatorRequiredDataManager::IsWifiCallbackRegistered() { std::unique_lock lock(wifiRegisteredMutex_); @@ -237,6 +264,8 @@ __attribute__((no_sanitize("cfi"))) void LocatorRequiredDataManager::GetWifiScan if (wifiSdkHandler_ != nullptr) { wifiSdkHandler_->RemoveEvent(EVENT_GET_WIFI_LIST); } + wifiScanInfo_ = wifiScanInfo; + getWifiScanInfoTimestamp_ = CommonUtils::GetCurrentTimeStampMs(); } std::vector> LocatorRequiredDataManager::GetLocatingRequiredDataByWifi( @@ -258,6 +287,18 @@ std::vector> LocatorRequiredDataManager::G return res; } +void LocatorRequiredDataManager::UpdateWifiScanCompleteTimestamp() +{ + std::unique_lock lock(wifiScanCompleteTimestampMutex_); + wifiScanCompleteTimestamp_ = CommonUtils::GetCurrentTimeStampMs(); +} + +int64_t LocatorRequiredDataManager::GetWifiScanCompleteTimestamp() +{ + std::unique_lock lock(wifiScanCompleteTimestampMutex_); + return wifiScanCompleteTimestamp_; +} + void LocatorWifiScanEventCallback::OnWifiScanStateChanged(int state, int size) { LBSLOGD(LOCATOR, "OnWifiScanStateChanged state=%{public}d", state); @@ -265,6 +306,7 @@ void LocatorWifiScanEventCallback::OnWifiScanStateChanged(int state, int size) LBSLOGE(LOCATOR, "OnWifiScanStateChanged false"); } auto dataManager = LocatorRequiredDataManager::GetInstance(); + dataManager->UpdateWifiScanCompleteTimestamp(); if (!dataManager->IsConnecting()) { LBSLOGE(LOCATOR, "%{public}s no valid callback, return", __func__); return; @@ -307,38 +349,27 @@ __attribute__((no_sanitize("cfi"))) void LocatorRequiredDataManager::StartWifiSc LBSLOGE(LOCATOR, "%{public}s no valid callback, return", __func__); return; } - if (!IsConnecting()) { - if (scanHandler_ != nullptr) { - scanHandler_->RemoveEvent(EVENT_START_SCAN); - } - if (wifiSdkHandler_ != nullptr) { - wifiSdkHandler_->RemoveEvent(EVENT_GET_WIFI_LIST); - } - return; - } #ifdef WIFI_ENABLE - if (wifiScanPtr_ == nullptr) { + bool needScan = false; + // �����ϴ�ɨ�賬��1.5�룬�����Ѿ�ɨ����� + int64_t time = CommonUtils::GetCurrentTimeStampMs(); + if ((time - wifiScanTimestamp_ > DEFAULT_TIMEOUT_MS && + GetWifiScanCompleteTimestamp() > wifiScanTimestamp_)) { + needScan = true; + } + if (!needScan) { + if (GetWifiScanCompleteTimestamp() > wifiScanTimestamp_) { + SendGetWifiListEvent(0); + } return; } - int ret = wifiScanPtr_->Scan(); + wifiScanTimestamp_ = CommonUtils::GetCurrentTimeStampMs(); + int ret = Wifi::WifiScan::GetInstance(WIFI_SCAN_ABILITY_ID)->Scan(); if (ret != Wifi::WIFI_OPT_SUCCESS) { LBSLOGE(LOCATOR, "%{public}s WifiScan failed, ret=%{public}d", __func__, ret); - if (wifiSdkHandler_ != nullptr) { - wifiSdkHandler_->SendHighPriorityEvent(EVENT_GET_WIFI_LIST, 0, 0); - } - } else { - if (wifiSdkHandler_ != nullptr) { - wifiSdkHandler_->SendHighPriorityEvent(EVENT_GET_WIFI_LIST, 0, DEFAULT_TIMEOUT_4S); - } + SendGetWifiListEvent(0); } #endif - if (fixNumber == 1) { - return; - } - LBSLOGD(LOCATOR, "StartWifiScan timeInterval_=%{public}d", timeInterval_); - if (scanHandler_ != nullptr) { - scanHandler_->SendHighPriorityEvent(EVENT_START_SCAN, 0, timeInterval_); - } } bool LocatorRequiredDataManager::IsConnecting() diff --git a/services/location_locator/locator/source/report_manager.cpp b/services/location_locator/locator/source/report_manager.cpp index 36760375..e5dd017b 100644 --- a/services/location_locator/locator/source/report_manager.cpp +++ b/services/location_locator/locator/source/report_manager.cpp @@ -37,6 +37,7 @@ static constexpr double MAXIMUM_FUZZY_LOCATION_DISTANCE = 4000.0; // Unit m static constexpr double MINIMUM_FUZZY_LOCATION_DISTANCE = 3000.0; // Unit m static constexpr int GNSS_FIX_CACHED_TIME = 25; static constexpr int NLP_FIX_CACHED_TIME = 25; +static constexpr int WHITE_FIX_CACHED_TIME = 60; ReportManager* ReportManager::GetInstance() { @@ -343,12 +344,23 @@ std::unique_ptr ReportManager::GetCacheLocation(const std::shared_ptr< { int64_t curTime = CommonUtils::GetCurrentTimeStamp(); std::unique_ptr cacheLocation = nullptr; - if (!CommonUtils::DoubleEqual(cacheGnssLocation_.GetLatitude(), MIN_LATITUDE - 1) && - (curTime - cacheGnssLocation_.GetTimeStamp() / MILLI_PER_SEC) <= GNSS_FIX_CACHED_TIME) { - cacheLocation = std::make_unique(cacheGnssLocation_); - } else if (!CommonUtils::DoubleEqual(cacheNlpLocation_.GetLatitude(), MIN_LATITUDE - 1) && - (curTime - cacheNlpLocation_.GetTimeStamp() / MILLI_PER_SEC) <= NLP_FIX_CACHED_TIME) { - cacheLocation = std::make_unique(cacheNlpLocation_); + std::string packageName = request->GetPackageName(); + if (HookUtils::ExecuteHookWhenCheckAppForCacheTime(packageName)) { + if (!CommonUtils::DoubleEqual(cacheGnssLocation_.GetLatitude(), MIN_LATITUDE - 1) && + (curTime - cacheGnssLocation_.GetTimeStamp() / MILLI_PER_SEC) <= WHITE_FIX_CACHED_TIME) { + cacheLocation = std::make_unique(cacheGnssLocation_); + } else if (!CommonUtils::DoubleEqual(cacheNlpLocation_.GetLatitude(), MIN_LATITUDE - 1) && + (curTime - cacheNlpLocation_.GetTimeStamp() / MILLI_PER_SEC) <= WHITE_FIX_CACHED_TIME) { + cacheLocation = std::make_unique(cacheNlpLocation_); + } + } else { + if (!CommonUtils::DoubleEqual(cacheGnssLocation_.GetLatitude(), MIN_LATITUDE - 1) && + (curTime - cacheGnssLocation_.GetTimeStamp() / MILLI_PER_SEC) <= GNSS_FIX_CACHED_TIME) { + cacheLocation = std::make_unique(cacheGnssLocation_); + } else if (!CommonUtils::DoubleEqual(cacheNlpLocation_.GetLatitude(), MIN_LATITUDE - 1) && + (curTime - cacheNlpLocation_.GetTimeStamp() / MILLI_PER_SEC) <= NLP_FIX_CACHED_TIME) { + cacheLocation = std::make_unique(cacheNlpLocation_); + } } std::unique_ptr finalLocation = GetPermittedLocation(request, cacheLocation); if (!ResultCheck(finalLocation, request)) {