1885b47fbSopenharmony_ci/*
2885b47fbSopenharmony_ci * Copyright (C) 2022 Huawei Device Co., Ltd.
3885b47fbSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4885b47fbSopenharmony_ci * you may not use this file except in compliance with the License.
5885b47fbSopenharmony_ci * You may obtain a copy of the License at
6885b47fbSopenharmony_ci *
7885b47fbSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8885b47fbSopenharmony_ci *
9885b47fbSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10885b47fbSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11885b47fbSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12885b47fbSopenharmony_ci * See the License for the specific language governing permissions and
13885b47fbSopenharmony_ci * limitations under the License.
14885b47fbSopenharmony_ci */
15885b47fbSopenharmony_ci
16885b47fbSopenharmony_ci#ifndef ACCESSIBILITY_COMMON_HELPER_H
17885b47fbSopenharmony_ci#define ACCESSIBILITY_COMMON_HELPER_H
18885b47fbSopenharmony_ci
19885b47fbSopenharmony_ci#include <atomic>
20885b47fbSopenharmony_ci#include <chrono>
21885b47fbSopenharmony_ci#include <functional>
22885b47fbSopenharmony_ci#include <thread>
23885b47fbSopenharmony_ci
24885b47fbSopenharmony_cinamespace OHOS {
25885b47fbSopenharmony_cinamespace Accessibility {
26885b47fbSopenharmony_cinamespace {
27885b47fbSopenharmony_ci    constexpr int32_t WAIT_PUBLISH_SLEEPTIME = 10;
28885b47fbSopenharmony_ci    constexpr int32_t WAIT_LOOP_SLEEPTIME = 100;
29885b47fbSopenharmony_ci} // namespace
30885b47fbSopenharmony_ci
31885b47fbSopenharmony_ciclass AccessibilityCommonHelper {
32885b47fbSopenharmony_cipublic:
33885b47fbSopenharmony_ci    static AccessibilityCommonHelper& GetInstance()
34885b47fbSopenharmony_ci    {
35885b47fbSopenharmony_ci        static AccessibilityCommonHelper helper;
36885b47fbSopenharmony_ci        return helper;
37885b47fbSopenharmony_ci    }
38885b47fbSopenharmony_ci
39885b47fbSopenharmony_ci    bool GetIsServicePublished()
40885b47fbSopenharmony_ci    {
41885b47fbSopenharmony_ci        return isServicePublished_;
42885b47fbSopenharmony_ci    }
43885b47fbSopenharmony_ci
44885b47fbSopenharmony_ci    void SetIsServicePublished(bool publish)
45885b47fbSopenharmony_ci    {
46885b47fbSopenharmony_ci        isServicePublished_ = publish;
47885b47fbSopenharmony_ci    }
48885b47fbSopenharmony_ci
49885b47fbSopenharmony_ci    void WaitForServicePublish()
50885b47fbSopenharmony_ci    {
51885b47fbSopenharmony_ci        while (1) {
52885b47fbSopenharmony_ci            std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_PUBLISH_SLEEPTIME));
53885b47fbSopenharmony_ci            if (isServicePublished_) {
54885b47fbSopenharmony_ci                return;
55885b47fbSopenharmony_ci            }
56885b47fbSopenharmony_ci        }
57885b47fbSopenharmony_ci    }
58885b47fbSopenharmony_ci
59885b47fbSopenharmony_ci    bool WaitForLoop(const std::function<bool()> &compare, int32_t timeout)
60885b47fbSopenharmony_ci    {
61885b47fbSopenharmony_ci        int32_t count = timeout * 1000 / WAIT_LOOP_SLEEPTIME;
62885b47fbSopenharmony_ci        while (count > 0) {
63885b47fbSopenharmony_ci            std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_LOOP_SLEEPTIME));
64885b47fbSopenharmony_ci            if (compare() == true) {
65885b47fbSopenharmony_ci                return true;
66885b47fbSopenharmony_ci            } else {
67885b47fbSopenharmony_ci                count--;
68885b47fbSopenharmony_ci            }
69885b47fbSopenharmony_ci        }
70885b47fbSopenharmony_ci        return false;
71885b47fbSopenharmony_ci    }
72885b47fbSopenharmony_ci
73885b47fbSopenharmony_ci    void SetRemoteObjectNotNullFlag(bool flag)
74885b47fbSopenharmony_ci    {
75885b47fbSopenharmony_ci        isRemoteObjectNotNulll_ = flag;
76885b47fbSopenharmony_ci    }
77885b47fbSopenharmony_ci
78885b47fbSopenharmony_ci    bool GetRemoteObjectNotNullFlag() const
79885b47fbSopenharmony_ci    {
80885b47fbSopenharmony_ci        return isRemoteObjectNotNulll_;
81885b47fbSopenharmony_ci    }
82885b47fbSopenharmony_ci
83885b47fbSopenharmony_ciprivate:
84885b47fbSopenharmony_ci    bool isServicePublished_ = false;
85885b47fbSopenharmony_ci    bool isRemoteObjectNotNulll_ = false;
86885b47fbSopenharmony_ci};
87885b47fbSopenharmony_ci} // namespace Accessibility
88885b47fbSopenharmony_ci} // namespace OHOS
89885b47fbSopenharmony_ci#endif // ACCESSIBILITY_COMMON_HELPER_H