• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "callback_manager.h"
17 
18 #include <algorithm>
19 #include "event_listener.h"
20 #include "event_manager.h"
21 #include "page/page_manager.h"
22 #include "updater_ui.h"
23 
24 namespace Updater {
25 constexpr auto CB_FIELD = "callbacks";
26 
27 std::vector<CallbackCfg> CallbackManager::callbackCfgs_;
28 
29 std::unordered_map<std::string, EventType> CallbackManager::evtTypes_ = {
30     {"TOUCHEVENT", EventType::TOUCHEVENT},
31     {"CLICKEVENT", EventType::CLICKEVENT}
32 };
33 
34 std::unordered_map<std::string, Callback> CallbackManager::funcs_ = {
35     {"OnLabelCancelEvt", &OnLabelCancelEvt},
36     {"OnLabelOkEvt", &OnLabelOkEvt},
37     {"OnRebootEvt", &OnRebootEvt},
38     {"OnLabelResetEvt", &OnLabelResetEvt},
39     {"OnMenuShutdownEvt", &OnMenuShutdownEvt},
40     {"OnLabelSDCardEvt", &OnLabelSDCardEvt},
41     {"OnLabelSDCardNoDelayEvt", &OnLabelSDCardNoDelayEvt},
42     {"OnConfirmRstEvt", &OnConfirmRstEvt},
43     {"OnMenuClearCacheEvt", &OnMenuClearCacheEvt}
44 };
45 
LoadCallbacks(const JsonNode & node)46 bool CallbackManager::LoadCallbacks(const JsonNode &node)
47 {
48     const JsonNode &cbNodes = node[CB_FIELD];
49     if (cbNodes.Type() != NodeType::ARRAY) {
50         LOG(ERROR) << "callback config node type should be array";
51         return false;
52     }
53     CallbackCfg cbCfg;
54     for (const auto &cbNode : cbNodes) {
55         cbCfg = {};
56         if (!Visit<SETVAL>(cbNode.get(), cbCfg)) {
57             LOG(ERROR) << "callback config parse failed";
58             return false;
59         }
60         callbackCfgs_.push_back(std::move(cbCfg));
61     }
62     return true;
63 }
64 
Register(const CallbackCfg & cbCfg)65 void CallbackManager::Register(const CallbackCfg &cbCfg)
66 {
67     auto it = evtTypes_.find(cbCfg.type);
68     if (it == evtTypes_.end()) {
69         LOG(ERROR) << "not recognized event type: " << cbCfg.type;
70         return;
71     }
72     auto itFunc = funcs_.find(cbCfg.func);
73     if (itFunc == funcs_.end()) {
74         LOG(ERROR) << "not recognized event type: " << cbCfg.func;
75         return;
76     }
77     EventManager::GetInstance().Add(ComInfo {cbCfg.pageId, cbCfg.comId}, it->second, itFunc->second);
78     LOG(DEBUG) << "register " << cbCfg.func << " for " << ComInfo {cbCfg.pageId, cbCfg.comId} << " succeed";
79 }
80 
Init(bool hasFocus)81 void CallbackManager::Init(bool hasFocus)
82 {
83     [[maybe_unused]] static bool isInited = [hasFocus] () {
84         for (auto &cb : callbackCfgs_) {
85             Register(cb);
86         }
87 
88         if (hasFocus) {
89             // for focus manager
90             EventManager::GetInstance().AddKeyListener();
91         }
92 
93         // for long press warning
94         LOG(DEBUG) << "register key action listerner succeed";
95         return true;
96     } ();
97 }
98 }