1 /*
2 * Copyright (C) 2024 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 #ifdef MOVEMENT_CLIENT_ENABLE
17 #include "locator_msdp_monitor_manager.h"
18 #include "locator_ability.h"
19
20 namespace OHOS {
21 namespace Location {
LocatorMsdpMonitorManager()22 LocatorMsdpMonitorManager::LocatorMsdpMonitorManager()
23 {
24 Init();
25 }
26
GetInstance()27 LocatorMsdpMonitorManager* LocatorMsdpMonitorManager::GetInstance()
28 {
29 static LocatorMsdpMonitorManager data;
30 return &data;
31 }
32
~LocatorMsdpMonitorManager()33 LocatorMsdpMonitorManager::~LocatorMsdpMonitorManager()
34 {
35 saStatusListener_ = nullptr;
36 }
37
Init()38 void LocatorMsdpMonitorManager::Init()
39 {
40 int32_t result;
41 auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
42 if (samgrProxy == nullptr) {
43 LBSLOGE(LOCATOR, "%{public}s samgrProxy is nullptr!", __func__);
44 return;
45 }
46 saStatusListener_ = sptr<MsdpMotionServiceStatusChange>(new MsdpMotionServiceStatusChange());
47 if (saStatusListener_ == nullptr) {
48 LBSLOGE(LOCATOR, "%{public}s saStatusListener_ is nullptr!", __func__);
49 return;
50 }
51 result = samgrProxy->SubscribeSystemAbility(static_cast<int32_t>(MSDP_MOTION_SERVICE_ID),
52 saStatusListener_);
53 LBSLOGI(LOCATOR, "%{public}s SubcribeSystemAbility result is %{public}d!", __func__, result);
54 }
55
RegisterMovementCallBack()56 void LocatorMsdpMonitorManager::RegisterMovementCallBack()
57 {
58 LBSLOGI(LOCATOR, "RegisterMovementCallBack");
59 std::unique_lock<std::mutex> lock(deviceMovementEventMutex);
60 if (deviceMovementCallback_ == nullptr) {
61 deviceMovementCallback_ = sptr<DeviceMovementCallback>(new DeviceMovementCallback());
62 }
63 if (Msdp::MovementClient::GetInstance().SubscribeCallback(
64 Msdp::MovementDataUtils::MovementType::TYPE_STILL, deviceMovementCallback_) != ERR_OK) {
65 LBSLOGI(LOCATOR, "Register a device movement observer failed!");
66 deviceMovementCallback_ = nullptr;
67 }
68 }
69
UnRegisterMovementCallBack()70 void LocatorMsdpMonitorManager::UnRegisterMovementCallBack()
71 {
72 LBSLOGI(LOCATOR, "UnRegisterMovementCallBack");
73 std::unique_lock<std::mutex> lock(deviceMovementEventMutex);
74 if (deviceMovementCallback_ == nullptr) {
75 return;
76 }
77 Msdp::MovementClient::GetInstance().UnSubscribeCallback(
78 Msdp::MovementDataUtils::MovementType::TYPE_STILL, deviceMovementCallback_);
79 deviceMovementCallback_ = nullptr;
80 }
81
UpdateStillMovementState(bool stillState)82 void LocatorMsdpMonitorManager::UpdateStillMovementState(bool stillState)
83 {
84 isDeviceStillState_.store(stillState);
85 LBSLOGI(LOCATOR, "device movement state change, isDeviceStillState_ %{public}d",
86 isDeviceStillState_.load());
87 auto locatorAbility = LocatorAbility::GetInstance();
88 locatorAbility->SyncStillMovementState(stillState);
89 }
90
GetStillMovementState()91 bool LocatorMsdpMonitorManager::GetStillMovementState()
92 {
93 return isDeviceStillState_.load();
94 }
95
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)96 void MsdpMotionServiceStatusChange::OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
97 {
98 LBSLOGI(LOCATOR, "MsdpMotionServiceStatusChange::OnAddSystemAbility");
99 LocatorMsdpMonitorManager::GetInstance()->RegisterMovementCallBack();
100 }
101
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)102 void MsdpMotionServiceStatusChange::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
103 {
104 LBSLOGI(LOCATOR, "MsdpMotionServiceStatusChange::OnRemoveSystemAbility");
105 LocatorMsdpMonitorManager::GetInstance()->UnRegisterMovementCallBack();
106 }
107 } // namespace Location
108 } // namespace OHOS
109 #endif // MOVEMENT_CLIENT_ENABLE