1 /*
2 * Copyright (C) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "locator_background_proxy.h"
17
18 #include <thread>
19
20 #include "common_event_manager.h"
21 #include "common_event_support.h"
22 #include "iservice_registry.h"
23 #include "os_account_manager.h"
24 #include "system_ability_definition.h"
25
26 #include "common_utils.h"
27 #include "constant_definition.h"
28 #include "location_config_manager.h"
29 #include "location_log.h"
30 #include "locator_ability.h"
31 #include "request_manager.h"
32
33 namespace OHOS {
34 namespace Location {
35 std::mutex LocatorBackgroundProxy::requestListMutex_;
36 std::mutex LocatorBackgroundProxy::locatorMutex_;
LocatorBackgroundProxy()37 LocatorBackgroundProxy::LocatorBackgroundProxy()
38 {
39 InitArgsFromProp();
40 if (!featureSwitch_) {
41 return;
42 }
43 requestsMap_ = std::make_shared<std::map<int32_t, std::shared_ptr<std::list<std::shared_ptr<Request>>>>>();
44 requestsList_ = std::make_shared<std::list<std::shared_ptr<Request>>>();
45 CommonUtils::GetCurrentUserId(curUserId_);
46 requestsMap_->insert(make_pair(curUserId_, requestsList_));
47
48 auto requestConfig = std::make_unique<RequestConfig>();
49 requestConfig->SetPriority(PRIORITY_LOW_POWER);
50 requestConfig->SetTimeInterval(timeInterval_);
51 callback_ = sptr<mLocatorCallback>(new (std::nothrow) LocatorBackgroundProxy::mLocatorCallback());
52 if (callback_ == nullptr) {
53 return;
54 }
55 request_ = std::make_shared<Request>();
56 if (request_ == nullptr) {
57 return;
58 }
59 request_->SetUid(SYSTEM_UID);
60 request_->SetPid(getpid());
61 request_->SetPackageName(PROC_NAME);
62 request_->SetRequestConfig(*requestConfig);
63 request_->SetLocatorCallBack(callback_);
64 SubscribeSaStatusChangeListerner();
65 isUserSwitchSubscribed_ = LocatorBackgroundProxy::UserSwitchSubscriber::Subscribe();
66 proxySwtich_ = (LocationConfigManager::GetInstance().GetLocationSwitchState() == ENABLED);
67 RegisterAppStateObserver();
68 }
69
~LocatorBackgroundProxy()70 LocatorBackgroundProxy::~LocatorBackgroundProxy()
71 {
72 UnregisterAppStateObserver();
73 }
74
75 // modify the parameters, in order to make the test easier
InitArgsFromProp()76 void LocatorBackgroundProxy::InitArgsFromProp()
77 {
78 featureSwitch_ = 1;
79 timeInterval_ = DEFAULT_TIME_INTERVAL;
80 }
81
SubscribeSaStatusChangeListerner()82 void LocatorBackgroundProxy::SubscribeSaStatusChangeListerner()
83 {
84 OHOS::EventFwk::MatchingSkills matchingSkills;
85 matchingSkills.AddEvent(OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED);
86 OHOS::EventFwk::CommonEventSubscribeInfo subscriberInfo(matchingSkills);
87 if (subscriber_ == nullptr) {
88 subscriber_ = std::make_shared<UserSwitchSubscriber>(subscriberInfo);
89 }
90 auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
91 statusChangeListener_ = new (std::nothrow) SystemAbilityStatusChangeListener(subscriber_);
92 if (samgrProxy == nullptr || statusChangeListener_ == nullptr) {
93 LBSLOGE(LOCATOR_BACKGROUND_PROXY,
94 "SubscribeSaStatusChangeListerner samgrProxy or statusChangeListener_ is nullptr");
95 return;
96 }
97 int32_t ret = samgrProxy->SubscribeSystemAbility(COMMON_EVENT_SERVICE_ID, statusChangeListener_);
98 LBSLOGI(LOCATOR_BACKGROUND_PROXY,
99 "SubscribeSaStatusChangeListerner SubscribeSystemAbility COMMON_EVENT_SERVICE_ID result:%{public}d", ret);
100 }
101
StartLocatorThread()102 void LocatorBackgroundProxy::StartLocatorThread()
103 {
104 auto requestManager = DelayedSingleton<RequestManager>::GetInstance();
105 auto locatorAbility = DelayedSingleton<LocatorAbility>::GetInstance();
106 if (requestManager == nullptr || locatorAbility == nullptr) {
107 LBSLOGE(LOCATOR, "StartLocatorThread: RequestManager or LocatorAbility is nullptr");
108 return;
109 }
110 std::this_thread::sleep_for(std::chrono::seconds(timeInterval_));
111 std::unique_lock<std::mutex> lock(locatorMutex_, std::defer_lock);
112 lock.lock();
113 isWating_ = false;
114 if (isLocating_ || !proxySwtich_ || requestsList_->empty()) {
115 LBSLOGD(LOCATOR_BACKGROUND_PROXY, "cancel locating");
116 lock.unlock();
117 return;
118 }
119 isLocating_ = true;
120 lock.unlock();
121 LBSLOGI(LOCATOR_BACKGROUND_PROXY, "real start locating");
122 requestManager.get()->HandleStartLocating(request_);
123 locatorAbility.get()->ReportLocationStatus(callback_, SESSION_START);
124 }
125
StopLocatorThread()126 void LocatorBackgroundProxy::StopLocatorThread()
127 {
128 auto locatorAbility = DelayedSingleton<LocatorAbility>::GetInstance();
129 if (locatorAbility == nullptr) {
130 LBSLOGE(LOCATOR, "StopLocatorThread: LocatorAbility is nullptr.");
131 return;
132 }
133 std::unique_lock<std::mutex> lock(locatorMutex_, std::defer_lock);
134 lock.lock();
135 if (!isLocating_) {
136 lock.unlock();
137 return;
138 }
139 isLocating_ = false;
140 lock.unlock();
141 locatorAbility.get()->StopLocating(callback_);
142 LBSLOGI(LOCATOR_BACKGROUND_PROXY, "end locating");
143 }
144
StopLocator()145 void LocatorBackgroundProxy::StopLocator()
146 {
147 std::unique_lock lock(locatorMutex_);
148 if (!isLocating_) {
149 return;
150 }
151 std::thread th(&LocatorBackgroundProxy::StopLocatorThread, this);
152 th.detach();
153 }
154
StartLocator()155 void LocatorBackgroundProxy::StartLocator()
156 {
157 std::unique_lock lock(locatorMutex_);
158 if (isLocating_ || !proxySwtich_ || isWating_) {
159 return;
160 }
161 isWating_ = true;
162 LBSLOGI(LOCATOR_BACKGROUND_PROXY, "start locating");
163 std::thread th(&LocatorBackgroundProxy::StartLocatorThread, this);
164 th.detach();
165 }
166
UpdateListOnRequestChange(const std::shared_ptr<Request> & request)167 void LocatorBackgroundProxy::UpdateListOnRequestChange(const std::shared_ptr<Request>& request)
168 {
169 if (request == nullptr) {
170 LBSLOGE(LOCATOR_BACKGROUND_PROXY, "request is null");
171 return;
172 }
173 int32_t uid = request->GetUid();
174 std::string bundleName;
175 if (!CommonUtils::GetBundleNameByUid(uid, bundleName)) {
176 LBSLOGE(LOCATOR_BACKGROUND_PROXY, "Fail to Get bundle name: uid = %{public}d.", uid);
177 return;
178 }
179 UpdateListOnSuspend(request, !IsAppBackground(bundleName));
180 if (requestsList_->empty()) {
181 StopLocator();
182 } else {
183 StartLocator();
184 }
185 }
186
187 // called when the app freezes or wakes up
188 // When the app enters frozen state, start proxy
189 // when the app wakes up, stop proxy
OnSuspend(const std::shared_ptr<Request> & request,bool active)190 void LocatorBackgroundProxy::OnSuspend(const std::shared_ptr<Request>& request, bool active)
191 {
192 if (!featureSwitch_) {
193 return;
194 }
195 if (!isUserSwitchSubscribed_) {
196 isUserSwitchSubscribed_ = LocatorBackgroundProxy::UserSwitchSubscriber::Subscribe();
197 }
198 UpdateListOnSuspend(request, active);
199 if (requestsList_->empty()) {
200 StopLocator();
201 } else {
202 StartLocator();
203 }
204 }
205
206 // called when SA switch on or switch off
207 // when switch on, start proxy
208 // when switch off, stop proxy
OnSaStateChange(bool enable)209 void LocatorBackgroundProxy::OnSaStateChange(bool enable)
210 {
211 if (proxySwtich_ == enable || !featureSwitch_) {
212 return;
213 }
214 LBSLOGD(LOCATOR_BACKGROUND_PROXY, "OnSaStateChange %{public}d", enable);
215 proxySwtich_ = enable;
216 if (enable && !requestsList_->empty()) {
217 StartLocator();
218 } else {
219 StopLocator();
220 }
221 }
222
223 // called when deleteRequest called from locator ability (e.g. app stop locating)
OnDeleteRequestRecord(const std::shared_ptr<Request> & request)224 void LocatorBackgroundProxy::OnDeleteRequestRecord(const std::shared_ptr<Request>& request)
225 {
226 if (!featureSwitch_) {
227 return;
228 }
229 bool listEmpty = false;
230 std::unique_lock<std::mutex> lock(requestListMutex_, std::defer_lock);
231 lock.lock();
232 auto it = find(requestsList_->begin(), requestsList_->end(), request);
233 if (it != requestsList_->end()) {
234 requestsList_->remove(request);
235 if (requestsList_->empty()) {
236 listEmpty = true;
237 }
238 }
239 lock.unlock();
240 if (listEmpty) {
241 StopLocator();
242 }
243 }
244
CheckPermission(const std::shared_ptr<Request> & request) const245 bool LocatorBackgroundProxy::CheckPermission(const std::shared_ptr<Request>& request) const
246 {
247 uint32_t tokenId = request->GetTokenId();
248 uint32_t firstTokenId = request->GetFirstTokenId();
249 return ((CommonUtils::CheckLocationPermission(tokenId, firstTokenId) ||
250 CommonUtils::CheckApproximatelyPermission(tokenId, firstTokenId)) &&
251 CommonUtils::CheckBackgroundPermission(tokenId, firstTokenId));
252 }
253
UpdateListOnSuspend(const std::shared_ptr<Request> & request,bool active)254 void LocatorBackgroundProxy::UpdateListOnSuspend(const std::shared_ptr<Request>& request, bool active)
255 {
256 if (request == nullptr) {
257 return;
258 }
259 auto requestManager = DelayedSingleton<RequestManager>::GetInstance();
260 if (requestManager == nullptr) {
261 LBSLOGE(LOCATOR_BACKGROUND_PROXY, "UpdateListOnSuspend: RequestManager is nullptr.");
262 return;
263 }
264 requestManager->UpdateUsingPermission(request);
265 std::unique_lock lock(requestListMutex_);
266 auto userId = GetUserId(request->GetUid());
267 auto iter = requestsMap_->find(userId);
268 if (iter == requestsMap_->end()) {
269 return;
270 }
271 auto requestsList = iter->second;
272 auto it = find(requestsList->begin(), requestsList->end(), request);
273 if (it != requestsList->end()) {
274 if (active || !CheckPermission(request)) {
275 LBSLOGD(LOCATOR_BACKGROUND_PROXY, "remove request:%{public}s from User:%{public}d",
276 request->ToString().c_str(), userId);
277 requestsList->remove(request);
278 }
279 } else {
280 if (request->GetRequestConfig() == nullptr) {
281 return;
282 }
283 if (!active && CheckPermission(request) && request->GetRequestConfig()->GetFixNumber() == 0
284 && CheckMaxRequestNum(request->GetUid(), request->GetPackageName())) {
285 LBSLOGD(LOCATOR_BACKGROUND_PROXY, "add request:%{public}s from User:%{public}d",
286 request->ToString().c_str(), userId);
287 requestsList->push_back(request);
288 }
289 }
290 }
291
UpdateListOnUserSwitch(int32_t userId)292 void LocatorBackgroundProxy::UpdateListOnUserSwitch(int32_t userId)
293 {
294 std::unique_lock lock(requestListMutex_);
295 auto iter = requestsMap_->find(userId);
296 if (iter == requestsMap_->end()) {
297 auto mRequestsList = std::make_shared<std::list<std::shared_ptr<Request>>>();
298 requestsMap_->insert(make_pair(userId, mRequestsList));
299 LBSLOGD(LOCATOR_BACKGROUND_PROXY, "add requsetlist on user:%{public}d", userId);
300 }
301 // if change to another user, proxy requestList should change
302 requestsList_ = (*requestsMap_)[userId];
303 curUserId_ = userId;
304 }
305
306
GetRequestsInProxy() const307 const std::list<std::shared_ptr<Request>>& LocatorBackgroundProxy::GetRequestsInProxy() const
308 {
309 return *requestsList_;
310 }
311
312 // called in LocatorCallbackProxy::OnLocationReport
313 // check if callback is from proxy
IsCallbackInProxy(const sptr<ILocatorCallback> & callback) const314 bool LocatorBackgroundProxy::IsCallbackInProxy(const sptr<ILocatorCallback>& callback) const
315 {
316 if (!featureSwitch_) {
317 return false;
318 }
319 std::unique_lock lock(requestListMutex_);
320 for (auto request : *requestsList_) {
321 if (request->GetLocatorCallBack() == callback) {
322 return true;
323 }
324 }
325 return false;
326 }
327
GetUserId(int32_t uid) const328 int32_t LocatorBackgroundProxy::GetUserId(int32_t uid) const
329 {
330 int userId = 0;
331 AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(uid, userId);
332 return userId;
333 }
334
OnUserSwitch(int32_t userId)335 void LocatorBackgroundProxy::OnUserSwitch(int32_t userId)
336 {
337 UpdateListOnUserSwitch(userId);
338 if (!requestsList_->empty()) {
339 StartLocator();
340 } else {
341 LBSLOGD(LOCATOR_BACKGROUND_PROXY, "OnUserSwitch stoplocator");
342 StopLocator();
343 }
344 }
345
OnUserRemove(int32_t userId)346 void LocatorBackgroundProxy::OnUserRemove(int32_t userId)
347 {
348 // if user is removed, remove the requestList from the user in requestsMap
349 std::unique_lock lock(requestListMutex_);
350 auto iter = requestsMap_->find(userId);
351 if (iter != requestsMap_->end()) {
352 requestsMap_->erase(iter);
353 LBSLOGD(LOCATOR_BACKGROUND_PROXY, "erase requsetlist on user:%{public}d", userId);
354 }
355 }
356
357 // limit the number of requests per app
CheckMaxRequestNum(pid_t uid,const std::string & packageName) const358 bool LocatorBackgroundProxy::CheckMaxRequestNum(pid_t uid, const std::string& packageName) const
359 {
360 int32_t num = 0;
361 auto iter = requestsMap_->find(GetUserId(uid));
362 if (iter == requestsMap_->end()) {
363 return false;
364 }
365 for (auto request : *(iter->second)) {
366 if (request->GetUid() == uid && packageName.compare(request->GetPackageName()) == 0) {
367 if (++num >= REQUESTS_NUM_MAX) {
368 return false;
369 }
370 }
371 }
372 return true;
373 }
374
OnLocationReport(const std::unique_ptr<Location> & location)375 void LocatorBackgroundProxy::mLocatorCallback::OnLocationReport(const std::unique_ptr<Location>& location)
376 {
377 LBSLOGD(LOCATOR_BACKGROUND_PROXY, "locator background OnLocationReport");
378 auto locatorBackgroundProxy = DelayedSingleton<LocatorBackgroundProxy>::GetInstance();
379 if (locatorBackgroundProxy == nullptr) {
380 LBSLOGE(LOCATOR_BACKGROUND_PROXY, "OnLocationReport: LocatorBackgroundProxy is nullptr.");
381 return;
382 }
383 auto requestsList = locatorBackgroundProxy.get()->GetRequestsInProxy();
384 if (requestsList.empty()) {
385 locatorBackgroundProxy->StopLocator();
386 return;
387 }
388 // call the callback of each proxy app
389 for (auto request : requestsList) {
390 request->GetLocatorCallBack()->OnLocationReport(location);
391 }
392 }
393
OnLocatingStatusChange(const int status)394 void LocatorBackgroundProxy::mLocatorCallback::OnLocatingStatusChange(const int status)
395 {
396 }
397
OnErrorReport(const int errorCode)398 void LocatorBackgroundProxy::mLocatorCallback::OnErrorReport(const int errorCode)
399 {
400 }
401
UserSwitchSubscriber(const OHOS::EventFwk::CommonEventSubscribeInfo & info)402 LocatorBackgroundProxy::UserSwitchSubscriber::UserSwitchSubscriber(
403 const OHOS::EventFwk::CommonEventSubscribeInfo &info)
404 : CommonEventSubscriber(info)
405 {
406 LBSLOGD(LOCATOR_BACKGROUND_PROXY, "create UserSwitchEventSubscriber");
407 }
408
OnReceiveEvent(const OHOS::EventFwk::CommonEventData & event)409 void LocatorBackgroundProxy::UserSwitchSubscriber::OnReceiveEvent(const OHOS::EventFwk::CommonEventData& event)
410 {
411 int32_t userId = event.GetCode();
412 const auto action = event.GetWant().GetAction();
413 auto locatorProxy = DelayedSingleton<LocatorBackgroundProxy>::GetInstance();
414 if (locatorProxy == nullptr) {
415 LBSLOGE(LOCATOR_BACKGROUND_PROXY, "OnReceiveEvent: LocatorBackgroundProxy is nullptr.");
416 return;
417 }
418 LBSLOGD(LOCATOR_BACKGROUND_PROXY, "action = %{public}s, userId = %{public}d", action.c_str(), userId);
419 if (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED) {
420 locatorProxy.get()->OnUserSwitch(userId);
421 } else if (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED) {
422 locatorProxy.get()->OnUserRemove(userId);
423 }
424 }
425
Subscribe()426 bool LocatorBackgroundProxy::UserSwitchSubscriber::Subscribe()
427 {
428 LBSLOGD(LOCATOR_BACKGROUND_PROXY, "subscribe common event");
429 OHOS::EventFwk::MatchingSkills matchingSkills;
430 matchingSkills.AddEvent(OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED);
431 OHOS::EventFwk::CommonEventSubscribeInfo subscriberInfo(matchingSkills);
432 std::shared_ptr<UserSwitchSubscriber> subscriber = std::make_shared<UserSwitchSubscriber>(subscriberInfo);
433 bool result = OHOS::EventFwk::CommonEventManager::SubscribeCommonEvent(subscriber);
434 if (result) {
435 } else {
436 LBSLOGE(LOCATOR_BACKGROUND_PROXY, "Subscribe service event error.");
437 }
438 return result;
439 }
440
SystemAbilityStatusChangeListener(std::shared_ptr<UserSwitchSubscriber> & subscriber)441 LocatorBackgroundProxy::SystemAbilityStatusChangeListener::SystemAbilityStatusChangeListener(
442 std::shared_ptr<UserSwitchSubscriber> &subscriber) : subscriber_(subscriber)
443 {}
444
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)445 void LocatorBackgroundProxy::SystemAbilityStatusChangeListener::OnAddSystemAbility(
446 int32_t systemAbilityId, const std::string& deviceId)
447 {
448 if (systemAbilityId != COMMON_EVENT_SERVICE_ID) {
449 LBSLOGE(LOCATOR_BACKGROUND_PROXY, "systemAbilityId is not COMMON_EVENT_SERVICE_ID");
450 return;
451 }
452 if (subscriber_ == nullptr) {
453 LBSLOGE(LOCATOR_BACKGROUND_PROXY, "OnAddSystemAbility subscribeer is nullptr");
454 return;
455 }
456 bool result = OHOS::EventFwk::CommonEventManager::SubscribeCommonEvent(subscriber_);
457 LBSLOGI(LOCATOR_BACKGROUND_PROXY, "SubscribeCommonEvent subscriber_ result = %{public}d", result);
458 }
459
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)460 void LocatorBackgroundProxy::SystemAbilityStatusChangeListener::OnRemoveSystemAbility(
461 int32_t systemAbilityId, const std::string& deviceId)
462 {
463 if (systemAbilityId != COMMON_EVENT_SERVICE_ID) {
464 LBSLOGE(LOCATOR_BACKGROUND_PROXY, "systemAbilityId is not COMMON_EVENT_SERVICE_ID");
465 return;
466 }
467 if (subscriber_ == nullptr) {
468 LBSLOGE(LOCATOR_BACKGROUND_PROXY, "OnRemoveSystemAbility subscribeer is nullptr");
469 return;
470 }
471 bool result = OHOS::EventFwk::CommonEventManager::UnSubscribeCommonEvent(subscriber_);
472 LBSLOGE(LOCATOR_BACKGROUND_PROXY, "UnSubscribeCommonEvent subscriber_ result = %{public}d", result);
473 }
474
IsAppBackground(std::string bundleName)475 bool LocatorBackgroundProxy::IsAppBackground(std::string bundleName)
476 {
477 sptr<ISystemAbilityManager> samgrClient = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
478 if (samgrClient == nullptr) {
479 LBSLOGE(LOCATOR_BACKGROUND_PROXY, "Get system ability manager failed.");
480 return false;
481 }
482 sptr<AppExecFwk::IAppMgr> iAppManager =
483 iface_cast<AppExecFwk::IAppMgr>(samgrClient->GetSystemAbility(APP_MGR_SERVICE_ID));
484 if (iAppManager == nullptr) {
485 LBSLOGE(LOCATOR_BACKGROUND_PROXY, "Failed to get ability manager service.");
486 return false;
487 }
488 std::vector<AppExecFwk::AppStateData> foregroundAppList;
489 iAppManager->GetForegroundApplications(foregroundAppList);
490 auto it = std::find_if(foregroundAppList.begin(), foregroundAppList.end(), [bundleName] (auto foregroundApp) {
491 return bundleName.compare(foregroundApp.bundleName) == 0;
492 });
493 if (it != foregroundAppList.end()) {
494 LBSLOGE(LOCATOR_BACKGROUND_PROXY, "app : %{public}s is foreground.", bundleName.c_str());
495 return false;
496 }
497 return true;
498 }
499
RegisterAppStateObserver()500 bool LocatorBackgroundProxy::RegisterAppStateObserver()
501 {
502 if (appStateObserver_ != nullptr) {
503 LBSLOGI(REQUEST_MANAGER, "app state observer exist.");
504 return true;
505 }
506 appStateObserver_ = sptr<AppStateChangeCallback>(new (std::nothrow) AppStateChangeCallback());
507 sptr<ISystemAbilityManager> samgrClient = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
508 if (samgrClient == nullptr) {
509 LBSLOGE(REQUEST_MANAGER, "Get system ability manager failed.");
510 appStateObserver_ = nullptr;
511 return false;
512 }
513 iAppMgr_ = iface_cast<AppExecFwk::IAppMgr>(samgrClient->GetSystemAbility(APP_MGR_SERVICE_ID));
514 if (iAppMgr_ == nullptr) {
515 LBSLOGE(REQUEST_MANAGER, "Failed to get ability manager service.");
516 appStateObserver_ = nullptr;
517 return false;
518 }
519 int32_t result = iAppMgr_->RegisterApplicationStateObserver(appStateObserver_);
520 if (result != 0) {
521 LBSLOGE(REQUEST_MANAGER, "Failed to Register app state observer.");
522 iAppMgr_ = nullptr;
523 appStateObserver_ = nullptr;
524 return false;
525 }
526 return true;
527 }
528
UnregisterAppStateObserver()529 bool LocatorBackgroundProxy::UnregisterAppStateObserver()
530 {
531 if (iAppMgr_ != nullptr && appStateObserver_ != nullptr) {
532 iAppMgr_->UnregisterApplicationStateObserver(appStateObserver_);
533 }
534 iAppMgr_ = nullptr;
535 appStateObserver_ = nullptr;
536 return true;
537 }
538
AppStateChangeCallback()539 AppStateChangeCallback::AppStateChangeCallback()
540 {
541 }
542
~AppStateChangeCallback()543 AppStateChangeCallback::~AppStateChangeCallback()
544 {
545 }
546
OnForegroundApplicationChanged(const AppExecFwk::AppStateData & appStateData)547 void AppStateChangeCallback::OnForegroundApplicationChanged(const AppExecFwk::AppStateData& appStateData)
548 {
549 auto requestManager = DelayedSingleton<RequestManager>::GetInstance();
550 if (requestManager == nullptr) {
551 LBSLOGE(REQUEST_MANAGER, "OnForegroundApplicationChanged: RequestManager is nullptr.");
552 return;
553 }
554 int32_t pid = appStateData.pid;
555 int32_t uid = appStateData.uid;
556 int32_t state = appStateData.state;
557 LBSLOGI(REQUEST_MANAGER,
558 "The state of App changed, uid = %{public}d, pid = %{public}d, state = %{public}d", uid, pid, state);
559 requestManager->HandlePowerSuspendChanged(pid, uid, state);
560 }
561 } // namespace OHOS
562 } // namespace Location
563