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     }
67 }
68 
UnRegisterMovementCallBack()69 void LocatorMsdpMonitorManager::UnRegisterMovementCallBack()
70 {
71     LBSLOGI(LOCATOR, "UnRegisterMovementCallBack");
72     std::unique_lock<std::mutex> lock(deviceMovementEventMutex);
73     if (deviceMovementCallback_ == nullptr) {
74         return;
75     }
76     Msdp::MovementClient::GetInstance().UnSubscribeCallback(
77         Msdp::MovementDataUtils::MovementType::TYPE_STILL, deviceMovementCallback_);
78     deviceMovementCallback_ = nullptr;
79 }
80 
UpdateStillMovementState(bool stillState)81 void LocatorMsdpMonitorManager::UpdateStillMovementState(bool stillState)
82 {
83     isDeviceStillState_.store(stillState);
84     LBSLOGI(LOCATOR, "device movement state change, isDeviceStillState_ %{public}d",
85         isDeviceStillState_.load());
86     auto locatorAbility = LocatorAbility::GetInstance();
87     if (locatorAbility == nullptr) {
88         LBSLOGE(LOCATOR, "LocatorMsdpMonitorManager::UpdateStillMovementState LocatorAbility is nullptr.");
89     }
90     locatorAbility->SyncStillMovementState(stillState);
91 }
92 
GetStillMovementState()93 bool LocatorMsdpMonitorManager::GetStillMovementState()
94 {
95     return isDeviceStillState_.load();
96 }
97 
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)98 void MsdpMotionServiceStatusChange::OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
99 {
100     LBSLOGI(LOCATOR, "MsdpMotionServiceStatusChange::OnAddSystemAbility");
101     LocatorMsdpMonitorManager::GetInstance()->RegisterMovementCallBack();
102 }
103 
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)104 void MsdpMotionServiceStatusChange::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
105 {
106     LBSLOGI(LOCATOR, "MsdpMotionServiceStatusChange::OnRemoveSystemAbility");
107     LocatorMsdpMonitorManager::GetInstance()->UnRegisterMovementCallBack();
108 }
109 } // namespace Location
110 } // namespace OHOS
111 #endif // MOVEMENT_CLIENT_ENABLE