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_event_subscriber.h"
17 #include "location_log.h"
18 #include "locator_ability.h"
19 #include "location_config_manager.h"
20
21 namespace OHOS {
22 namespace Location {
23 constexpr const char* LOCATOR_STANDBY_NAP = "napped";
24 constexpr const char* LOCATOR_STANDBY_SLEEPING = "sleeping";
25
LocatorEventSubscriber(const OHOS::EventFwk::CommonEventSubscribeInfo & info)26 LocatorEventSubscriber::LocatorEventSubscriber(const OHOS::EventFwk::CommonEventSubscribeInfo &info)
27 : CommonEventSubscriber(info) {}
28
~LocatorEventSubscriber()29 LocatorEventSubscriber::~LocatorEventSubscriber() {}
30
OnReceiveEvent(const OHOS::EventFwk::CommonEventData & event)31 void LocatorEventSubscriber::OnReceiveEvent(const OHOS::EventFwk::CommonEventData& event)
32 {
33 std::string action = event.GetWant().GetAction();
34 LBSLOGI(LOCATOR_EVENT, "received action = %{public}s", action.c_str());
35 if (OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_DEVICE_IDLE_MODE_CHANGED.compare(action) == 0) {
36 auto locatorAbility = LocatorAbility::GetInstance();
37 if (locatorAbility == nullptr) {
38 LBSLOGE(LOCATOR_EVENT, "OnReceiveEvent: LocatorAbility is nullptr.");
39 return;
40 }
41 const bool napped = event.GetWant().GetBoolParam(LOCATOR_STANDBY_NAP, 0);
42 const bool sleeping = event.GetWant().GetBoolParam(LOCATOR_STANDBY_SLEEPING, 0);
43 LBSLOGD(LOCATOR_EVENT, "device idle napped: %{public}d, sleeping: %{public}d", napped, sleeping);
44 locatorAbility->SyncIdleState(sleeping);
45 return;
46 }
47 auto locatorAbility = LocatorAbility::GetInstance();
48 if (locatorAbility == nullptr) {
49 LBSLOGE(LOCATOR_EVENT, "OnReceiveEvent: LocatorAbility is nullptr.");
50 return;
51 }
52 if (std::string(MODE_CHANGED_EVENT).compare(action) == 0) {
53 locatorAbility->UpdateSaAbility();
54 } else if (std::string(LOCATION_PRIVACY_ACCEPT_EVENT).compare(action) == 0) {
55 LocationConfigManager::GetInstance()->SetPrivacyTypeState(PRIVACY_TYPE_STARTUP, true);
56 locatorAbility->EnableAbility(true);
57 } else if (std::string(LOCATION_PRIVACY_REJECT_EVENT).compare(action) == 0) {
58 locatorAbility->EnableAbility(false);
59 }
60 }
61 } // namespace Location
62 } // namespace OHOS
63