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::lock_guard 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::lock_guard 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::lock_guard 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::lock_guard 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 for (auto request : *requestsList_) {
320 if (request->GetLocatorCallBack() == callback) {
321 return true;
322 }
323 }
324 return false;
325 }
326
GetUserId(int32_t uid) const327 int32_t LocatorBackgroundProxy::GetUserId(int32_t uid) const
328 {
329 int userId = 0;
330 AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(uid, userId);
331 return userId;
332 }
333
OnUserSwitch(int32_t userId)334 void LocatorBackgroundProxy::OnUserSwitch(int32_t userId)
335 {
336 UpdateListOnUserSwitch(userId);
337 if (!requestsList_->empty()) {
338 StartLocator();
339 } else {
340 LBSLOGD(LOCATOR_BACKGROUND_PROXY, "OnUserSwitch stoplocator");
341 StopLocator();
342 }
343 }
344
OnUserRemove(int32_t userId)345 void LocatorBackgroundProxy::OnUserRemove(int32_t userId)
346 {
347 // if user is removed, remove the requestList from the user in requestsMap
348 std::lock_guard lock(requestListMutex_);
349 auto iter = requestsMap_->find(userId);
350 if (iter != requestsMap_->end()) {
351 requestsMap_->erase(iter);
352 LBSLOGD(LOCATOR_BACKGROUND_PROXY, "erase requsetlist on user:%{public}d", userId);
353 }
354 }
355
356 // limit the number of requests per app
CheckMaxRequestNum(pid_t uid,const std::string & packageName) const357 bool LocatorBackgroundProxy::CheckMaxRequestNum(pid_t uid, const std::string& packageName) const
358 {
359 int32_t num = 0;
360 auto iter = requestsMap_->find(GetUserId(uid));
361 if (iter == requestsMap_->end()) {
362 return false;
363 }
364 for (auto request : *(iter->second)) {
365 if (request->GetUid() == uid && packageName.compare(request->GetPackageName()) == 0) {
366 if (++num >= REQUESTS_NUM_MAX) {
367 return false;
368 }
369 }
370 }
371 return true;
372 }
373
OnLocationReport(const std::unique_ptr<Location> & location)374 void LocatorBackgroundProxy::mLocatorCallback::OnLocationReport(const std::unique_ptr<Location>& location)
375 {
376 LBSLOGD(LOCATOR_BACKGROUND_PROXY, "locator background OnLocationReport");
377 auto locatorBackgroundProxy = DelayedSingleton<LocatorBackgroundProxy>::GetInstance();
378 if (locatorBackgroundProxy == nullptr) {
379 LBSLOGE(LOCATOR_BACKGROUND_PROXY, "OnLocationReport: LocatorBackgroundProxy is nullptr.");
380 return;
381 }
382 auto requestsList = locatorBackgroundProxy.get()->GetRequestsInProxy();
383 if (requestsList.empty()) {
384 locatorBackgroundProxy->StopLocator();
385 return;
386 }
387 // call the callback of each proxy app
388 for (auto request : requestsList) {
389 request->GetLocatorCallBack()->OnLocationReport(location);
390 }
391 }
392
OnLocatingStatusChange(const int status)393 void LocatorBackgroundProxy::mLocatorCallback::OnLocatingStatusChange(const int status)
394 {
395 }
396
OnErrorReport(const int errorCode)397 void LocatorBackgroundProxy::mLocatorCallback::OnErrorReport(const int errorCode)
398 {
399 }
400
UserSwitchSubscriber(const OHOS::EventFwk::CommonEventSubscribeInfo & info)401 LocatorBackgroundProxy::UserSwitchSubscriber::UserSwitchSubscriber(
402 const OHOS::EventFwk::CommonEventSubscribeInfo &info)
403 : CommonEventSubscriber(info)
404 {
405 LBSLOGD(LOCATOR_BACKGROUND_PROXY, "create UserSwitchEventSubscriber");
406 }
407
OnReceiveEvent(const OHOS::EventFwk::CommonEventData & event)408 void LocatorBackgroundProxy::UserSwitchSubscriber::OnReceiveEvent(const OHOS::EventFwk::CommonEventData& event)
409 {
410 int32_t userId = event.GetCode();
411 const auto action = event.GetWant().GetAction();
412 auto locatorProxy = DelayedSingleton<LocatorBackgroundProxy>::GetInstance();
413 if (locatorProxy == nullptr) {
414 LBSLOGE(LOCATOR_BACKGROUND_PROXY, "OnReceiveEvent: LocatorBackgroundProxy is nullptr.");
415 return;
416 }
417 LBSLOGD(LOCATOR_BACKGROUND_PROXY, "action = %{public}s, userId = %{public}d", action.c_str(), userId);
418 if (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED) {
419 locatorProxy.get()->OnUserSwitch(userId);
420 } else if (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED) {
421 locatorProxy.get()->OnUserRemove(userId);
422 }
423 }
424
Subscribe()425 bool LocatorBackgroundProxy::UserSwitchSubscriber::Subscribe()
426 {
427 LBSLOGD(LOCATOR_BACKGROUND_PROXY, "subscribe common event");
428 OHOS::EventFwk::MatchingSkills matchingSkills;
429 matchingSkills.AddEvent(OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED);
430 OHOS::EventFwk::CommonEventSubscribeInfo subscriberInfo(matchingSkills);
431 std::shared_ptr<UserSwitchSubscriber> subscriber = std::make_shared<UserSwitchSubscriber>(subscriberInfo);
432 bool result = OHOS::EventFwk::CommonEventManager::SubscribeCommonEvent(subscriber);
433 if (result) {
434 } else {
435 LBSLOGE(LOCATOR_BACKGROUND_PROXY, "Subscribe service event error.");
436 }
437 return result;
438 }
439
SystemAbilityStatusChangeListener(std::shared_ptr<UserSwitchSubscriber> & subscriber)440 LocatorBackgroundProxy::SystemAbilityStatusChangeListener::SystemAbilityStatusChangeListener(
441 std::shared_ptr<UserSwitchSubscriber> &subscriber) : subscriber_(subscriber)
442 {}
443
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)444 void LocatorBackgroundProxy::SystemAbilityStatusChangeListener::OnAddSystemAbility(
445 int32_t systemAbilityId, const std::string& deviceId)
446 {
447 if (systemAbilityId != COMMON_EVENT_SERVICE_ID) {
448 LBSLOGE(LOCATOR_BACKGROUND_PROXY, "systemAbilityId is not COMMON_EVENT_SERVICE_ID");
449 return;
450 }
451 if (subscriber_ == nullptr) {
452 LBSLOGE(LOCATOR_BACKGROUND_PROXY, "OnAddSystemAbility subscribeer is nullptr");
453 return;
454 }
455 bool result = OHOS::EventFwk::CommonEventManager::SubscribeCommonEvent(subscriber_);
456 LBSLOGI(LOCATOR_BACKGROUND_PROXY, "SubscribeCommonEvent subscriber_ result = %{public}d", result);
457 }
458
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)459 void LocatorBackgroundProxy::SystemAbilityStatusChangeListener::OnRemoveSystemAbility(
460 int32_t systemAbilityId, const std::string& deviceId)
461 {
462 if (systemAbilityId != COMMON_EVENT_SERVICE_ID) {
463 LBSLOGE(LOCATOR_BACKGROUND_PROXY, "systemAbilityId is not COMMON_EVENT_SERVICE_ID");
464 return;
465 }
466 if (subscriber_ == nullptr) {
467 LBSLOGE(LOCATOR_BACKGROUND_PROXY, "OnRemoveSystemAbility subscribeer is nullptr");
468 return;
469 }
470 bool result = OHOS::EventFwk::CommonEventManager::UnSubscribeCommonEvent(subscriber_);
471 LBSLOGE(LOCATOR_BACKGROUND_PROXY, "UnSubscribeCommonEvent subscriber_ result = %{public}d", result);
472 }
473
IsAppBackground(std::string bundleName)474 bool LocatorBackgroundProxy::IsAppBackground(std::string bundleName)
475 {
476 sptr<ISystemAbilityManager> samgrClient = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
477 if (samgrClient == nullptr) {
478 LBSLOGE(REQUEST_MANAGER, "Get system ability manager failed.");
479 return false;
480 }
481 sptr<AppExecFwk::IAppMgr> iAppManager =
482 iface_cast<AppExecFwk::IAppMgr>(samgrClient->GetSystemAbility(APP_MGR_SERVICE_ID));
483 if (iAppManager == nullptr) {
484 LBSLOGE(REQUEST_MANAGER, "Failed to get ability manager service.");
485 return false;
486 }
487 std::vector<AppExecFwk::AppStateData> foregroundAppList;
488 iAppManager->GetForegroundApplications(foregroundAppList);
489 auto it = std::find_if(foregroundAppList.begin(), foregroundAppList.end(), [bundleName] (auto foregroundApp) {
490 return bundleName.compare(foregroundApp.bundleName) == 0;
491 });
492 if (it != foregroundAppList.end()) {
493 LBSLOGE(REQUEST_MANAGER, "app : %{public}s is foreground.", bundleName.c_str());
494 return false;
495 }
496 return true;
497 }
498
RegisterAppStateObserver()499 bool LocatorBackgroundProxy::RegisterAppStateObserver()
500 {
501 if (appStateObserver_ != nullptr) {
502 LBSLOGI(REQUEST_MANAGER, "app state observer exist.");
503 return true;
504 }
505 appStateObserver_ = sptr<AppStateChangeCallback>(new (std::nothrow) AppStateChangeCallback());
506 sptr<ISystemAbilityManager> samgrClient = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
507 if (samgrClient == nullptr) {
508 LBSLOGE(REQUEST_MANAGER, "Get system ability manager failed.");
509 appStateObserver_ = nullptr;
510 return false;
511 }
512 iAppMgr_ = iface_cast<AppExecFwk::IAppMgr>(samgrClient->GetSystemAbility(APP_MGR_SERVICE_ID));
513 if (iAppMgr_ == nullptr) {
514 LBSLOGE(REQUEST_MANAGER, "Failed to get ability manager service.");
515 appStateObserver_ = nullptr;
516 return false;
517 }
518 int32_t result = iAppMgr_->RegisterApplicationStateObserver(appStateObserver_);
519 if (result != 0) {
520 LBSLOGE(REQUEST_MANAGER, "Failed to Register app state observer.");
521 iAppMgr_ = nullptr;
522 appStateObserver_ = nullptr;
523 return false;
524 }
525 return true;
526 }
527
UnregisterAppStateObserver()528 bool LocatorBackgroundProxy::UnregisterAppStateObserver()
529 {
530 if (iAppMgr_ != nullptr && appStateObserver_ != nullptr) {
531 iAppMgr_->UnregisterApplicationStateObserver(appStateObserver_);
532 }
533 iAppMgr_ = nullptr;
534 appStateObserver_ = nullptr;
535 return true;
536 }
537
AppStateChangeCallback()538 AppStateChangeCallback::AppStateChangeCallback()
539 {
540 }
541
~AppStateChangeCallback()542 AppStateChangeCallback::~AppStateChangeCallback()
543 {
544 }
545
OnForegroundApplicationChanged(const AppExecFwk::AppStateData & appStateData)546 void AppStateChangeCallback::OnForegroundApplicationChanged(const AppExecFwk::AppStateData& appStateData)
547 {
548 auto requestManager = DelayedSingleton<RequestManager>::GetInstance();
549 if (requestManager == nullptr) {
550 LBSLOGE(REQUEST_MANAGER, "OnForegroundApplicationChanged: RequestManager is nullptr.");
551 return;
552 }
553 int32_t pid = appStateData.pid;
554 int32_t uid = appStateData.uid;
555 int32_t state = appStateData.state;
556 LBSLOGI(REQUEST_MANAGER,
557 "The state of App changed, uid = %{public}d, pid = %{public}d, state = %{public}d", uid, pid, state);
558 requestManager->HandlePowerSuspendChanged(pid, uid, state);
559 }
560 } // namespace OHOS
561 } // namespace Location
562