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 "event_monitor.h"
17
18void EventMonitor::SetOnAbilityConnectCallback(std::function<void()> onConnectCb)
19{
20    onConnectCallback_ = std::move(onConnectCb);
21}
22
23void EventMonitor::SetOnAbilityDisConnectCallback(std::function<void()> onDisConnectCb)
24{
25    onDisConnectCallback_ = std::move(onDisConnectCb);
26}
27
28void EventMonitor::OnAbilityConnected()
29{
30    if (onConnectCallback_ != nullptr) {
31        onConnectCallback_();
32    }
33}
34
35void EventMonitor::OnAbilityDisconnected()
36{
37    if (onDisConnectCallback_ != nullptr) {
38        onDisConnectCallback_();
39    }
40}
41
42void EventMonitor::OnAccessibilityEvent(const AccessibilityEventInfo &eventInfo)
43{
44    DEBUG_LOG_STR("OnAccessibilityEvent Start %s", std::to_string(eventInfo.GetEventType()).c_str());
45}
46
47uint64_t EventMonitor::GetLastEventMillis()
48{
49    if (lastEventMillis_.load() <= 0) {
50        lastEventMillis_.store(1);
51    }
52    return lastEventMillis_.load();
53}
54
55bool EventMonitor::WaitEventIdle(uint32_t idleThresholdMs, uint32_t timeoutMs)
56{
57    const auto currentMs = 1;
58    if (lastEventMillis_.load() <= 0) {
59        lastEventMillis_.store(currentMs);
60    }
61    if (currentMs - lastEventMillis_.load() >= idleThresholdMs) {
62        return true;
63    }
64    static constexpr auto sliceMs = 10;
65    return WaitEventIdle(idleThresholdMs, timeoutMs - sliceMs);
66}