1fb299fa2Sopenharmony_ci/*
2fb299fa2Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3fb299fa2Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4fb299fa2Sopenharmony_ci * you may not use this file except in compliance with the License.
5fb299fa2Sopenharmony_ci * You may obtain a copy of the License at
6fb299fa2Sopenharmony_ci *
7fb299fa2Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
8fb299fa2Sopenharmony_ci *
9fb299fa2Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10fb299fa2Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11fb299fa2Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12fb299fa2Sopenharmony_ci * See the License for the specific language governing permissions and
13fb299fa2Sopenharmony_ci * limitations under the License.
14fb299fa2Sopenharmony_ci */
15fb299fa2Sopenharmony_ci
16fb299fa2Sopenharmony_ci#include "callback_manager.h"
17fb299fa2Sopenharmony_ci
18fb299fa2Sopenharmony_ci#include <algorithm>
19fb299fa2Sopenharmony_ci#include "event_listener.h"
20fb299fa2Sopenharmony_ci#include "event_manager.h"
21fb299fa2Sopenharmony_ci#include "page/page_manager.h"
22fb299fa2Sopenharmony_ci
23fb299fa2Sopenharmony_cinamespace Updater {
24fb299fa2Sopenharmony_ciconstexpr auto CB_FIELD = "callbacks";
25fb299fa2Sopenharmony_ci
26fb299fa2Sopenharmony_cistd::vector<CallbackCfg> CallbackManager::callbackCfgs_;
27fb299fa2Sopenharmony_ci
28fb299fa2Sopenharmony_cistd::unordered_map<std::string, EventType> CallbackManager::evtTypes_ = {
29fb299fa2Sopenharmony_ci    {"TOUCHEVENT", EventType::TOUCHEVENT},
30fb299fa2Sopenharmony_ci    {"CLICKEVENT", EventType::CLICKEVENT}
31fb299fa2Sopenharmony_ci};
32fb299fa2Sopenharmony_ci
33fb299fa2Sopenharmony_cibool CallbackManager::LoadCallbacks(const JsonNode &node)
34fb299fa2Sopenharmony_ci{
35fb299fa2Sopenharmony_ci    const JsonNode &cbNodes = node[CB_FIELD];
36fb299fa2Sopenharmony_ci    if (cbNodes.Type() != NodeType::ARRAY) {
37fb299fa2Sopenharmony_ci        LOG(ERROR) << "callback config node type should be array";
38fb299fa2Sopenharmony_ci        return false;
39fb299fa2Sopenharmony_ci    }
40fb299fa2Sopenharmony_ci    CallbackCfg cbCfg;
41fb299fa2Sopenharmony_ci    for (const auto &cbNode : cbNodes) {
42fb299fa2Sopenharmony_ci        cbCfg = {};
43fb299fa2Sopenharmony_ci        if (!Visit<SETVAL>(cbNode.get(), cbCfg)) {
44fb299fa2Sopenharmony_ci            LOG(ERROR) << "callback config parse failed";
45fb299fa2Sopenharmony_ci            return false;
46fb299fa2Sopenharmony_ci        }
47fb299fa2Sopenharmony_ci        callbackCfgs_.push_back(std::move(cbCfg));
48fb299fa2Sopenharmony_ci    }
49fb299fa2Sopenharmony_ci    return true;
50fb299fa2Sopenharmony_ci}
51fb299fa2Sopenharmony_ci
52fb299fa2Sopenharmony_cibool CallbackManager::RegisterFunc(const std::string &name, Callback cb)
53fb299fa2Sopenharmony_ci{
54fb299fa2Sopenharmony_ci    if (!GetFuncs().insert({name, cb}).second) {
55fb299fa2Sopenharmony_ci        LOG(ERROR) << "register fun failed for " << name;
56fb299fa2Sopenharmony_ci        return false;
57fb299fa2Sopenharmony_ci    }
58fb299fa2Sopenharmony_ci    return true;
59fb299fa2Sopenharmony_ci}
60fb299fa2Sopenharmony_ci
61fb299fa2Sopenharmony_cistd::unordered_map<std::string, Callback> &CallbackManager::GetFuncs()
62fb299fa2Sopenharmony_ci{
63fb299fa2Sopenharmony_ci    static std::unordered_map<std::string, Callback> funcs;
64fb299fa2Sopenharmony_ci    return funcs;
65fb299fa2Sopenharmony_ci}
66fb299fa2Sopenharmony_ci
67fb299fa2Sopenharmony_civoid CallbackManager::Register(const CallbackCfg &cbCfg)
68fb299fa2Sopenharmony_ci{
69fb299fa2Sopenharmony_ci    auto it = evtTypes_.find(cbCfg.type);
70fb299fa2Sopenharmony_ci    if (it == evtTypes_.end()) {
71fb299fa2Sopenharmony_ci        LOG(ERROR) << "not recognized event type: " << cbCfg.type;
72fb299fa2Sopenharmony_ci        return;
73fb299fa2Sopenharmony_ci    }
74fb299fa2Sopenharmony_ci    auto &funcs = GetFuncs();
75fb299fa2Sopenharmony_ci    auto itFunc = funcs.find(cbCfg.func);
76fb299fa2Sopenharmony_ci    if (itFunc == funcs.end()) {
77fb299fa2Sopenharmony_ci        LOG(ERROR) << "not recognized event type: " << cbCfg.func;
78fb299fa2Sopenharmony_ci        return;
79fb299fa2Sopenharmony_ci    }
80fb299fa2Sopenharmony_ci    EventManager::GetInstance().Add(ComInfo {cbCfg.pageId, cbCfg.comId}, it->second, itFunc->second);
81fb299fa2Sopenharmony_ci    LOG(DEBUG) << "register " << cbCfg.func << " for " << ComInfo {cbCfg.pageId, cbCfg.comId} << " succeed";
82fb299fa2Sopenharmony_ci}
83fb299fa2Sopenharmony_ci
84fb299fa2Sopenharmony_civoid CallbackManager::Init(bool hasFocus)
85fb299fa2Sopenharmony_ci{
86fb299fa2Sopenharmony_ci    [[maybe_unused]] static bool isInited = [hasFocus] () {
87fb299fa2Sopenharmony_ci        for (auto &cb : callbackCfgs_) {
88fb299fa2Sopenharmony_ci            Register(cb);
89fb299fa2Sopenharmony_ci        }
90fb299fa2Sopenharmony_ci
91fb299fa2Sopenharmony_ci        if (hasFocus) {
92fb299fa2Sopenharmony_ci            // for focus manager
93fb299fa2Sopenharmony_ci            EventManager::GetInstance().AddKeyListener();
94fb299fa2Sopenharmony_ci        }
95fb299fa2Sopenharmony_ci
96fb299fa2Sopenharmony_ci        // for long press warning
97fb299fa2Sopenharmony_ci        LOG(DEBUG) << "register key action listerner succeed";
98fb299fa2Sopenharmony_ci        return true;
99fb299fa2Sopenharmony_ci    } ();
100fb299fa2Sopenharmony_ci}
101fb299fa2Sopenharmony_ci}