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 if (isDeviceStillState_.load() == false && stillState == true) {
85 enterStillTime_.store(CommonUtils::GetCurrentTimeMilSec());
86 }
87 isDeviceStillState_.store(stillState);
88 auto locatorAbility = LocatorAbility::GetInstance();
89 locatorAbility->SyncStillMovementState(stillState);
90 }
91
GetStillMovementState()92 bool LocatorMsdpMonitorManager::GetStillMovementState()
93 {
94 return isDeviceStillState_.load();
95 }
96
GetEnterStillTime()97 uint64_t LocatorMsdpMonitorManager::GetEnterStillTime()
98 {
99 return enterStillTime_.load();
100 }
101
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)102 void MsdpMotionServiceStatusChange::OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
103 {
104 LBSLOGI(LOCATOR, "MsdpMotionServiceStatusChange::OnAddSystemAbility");
105 LocatorMsdpMonitorManager::GetInstance()->RegisterMovementCallBack();
106 }
107
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)108 void MsdpMotionServiceStatusChange::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
109 {
110 LBSLOGI(LOCATOR, "MsdpMotionServiceStatusChange::OnRemoveSystemAbility");
111 LocatorMsdpMonitorManager::GetInstance()->UnRegisterMovementCallBack();
112 }
113 } // namespace Location
114 } // namespace OHOS
115 #endif // MOVEMENT_CLIENT_ENABLE