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 #include "dh_data_sync_trigger_listener.h"
17 
18 #include <cinttypes>
19 
20 #include "anonymous_string.h"
21 #include "component_manager.h"
22 #include "dh_utils_tool.h"
23 #include "distributed_hardware_log.h"
24 #include "event_handler.h"
25 
26 class ComponentManager;
27 namespace OHOS {
28 namespace DistributedHardware {
29 #undef DH_LOG_TAG
30 #define DH_LOG_TAG "DHDataSyncTriggerListener"
DHDataSyncTriggerListener()31 DHDataSyncTriggerListener::DHDataSyncTriggerListener()
32 {
33     DHLOGI("Ctor DHDataSyncTriggerListener");
34 }
35 
~DHDataSyncTriggerListener()36 DHDataSyncTriggerListener::~DHDataSyncTriggerListener()
37 {
38     DHLOGI("Dtor DHDataSyncTriggerListener");
39 }
40 
OnDataSyncTrigger(const std::string & networkId)41 void DHDataSyncTriggerListener::OnDataSyncTrigger(const std::string &networkId)
42 {
43     if (!IsIdLengthValid(networkId)) {
44         return;
45     }
46     DHLOGI("Receive data sync trigger, networkId: %{public}s", GetAnonyString(networkId).c_str());
47     if (networkId.empty()) {
48         DHLOGE("OnDataSyncTrigger networkId is empty");
49         return;
50     }
51     std::shared_ptr<std::string> networkIdPtr = std::make_shared<std::string>(networkId);
52     AppExecFwk::InnerEvent::Pointer msgEvent = AppExecFwk::InnerEvent::Get(EVENT_DATA_SYNC_MANUAL, networkIdPtr);
53     if (ComponentManager::GetInstance().GetEventHandler() == nullptr) {
54         DHLOGE("Can not get eventHandler");
55         return;
56     }
57     ComponentManager::GetInstance().GetEventHandler()->SendEvent(msgEvent,
58         0, AppExecFwk::EventQueue::Priority::IMMEDIATE);
59 }
60 } // namespace DistributedHardware
61 } // namespace OHOS