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 "event_manager.h"
17fb299fa2Sopenharmony_ci
18fb299fa2Sopenharmony_ci#include "log/log.h"
19fb299fa2Sopenharmony_ci
20fb299fa2Sopenharmony_cinamespace Updater {
21fb299fa2Sopenharmony_ciextern "C" __attribute__((constructor)) void RegisterEventManagerHelper(void)
22fb299fa2Sopenharmony_ci{
23fb299fa2Sopenharmony_ci    EventManager::GetInstance().RegisterEventManagerHelper(std::make_unique<KeyListener>());
24fb299fa2Sopenharmony_ci}
25fb299fa2Sopenharmony_ci
26fb299fa2Sopenharmony_civoid EventManager::RegisterEventManagerHelper(std::unique_ptr<KeyListener> ptr)
27fb299fa2Sopenharmony_ci{
28fb299fa2Sopenharmony_ci    helper_ = std::move(ptr);
29fb299fa2Sopenharmony_ci}
30fb299fa2Sopenharmony_ci
31fb299fa2Sopenharmony_ciEventManager::EventManager() : pgMgr_(PageManager::GetInstance())
32fb299fa2Sopenharmony_ci{
33fb299fa2Sopenharmony_ci}
34fb299fa2Sopenharmony_ci
35fb299fa2Sopenharmony_ciEventManager &EventManager::GetInstance()
36fb299fa2Sopenharmony_ci{
37fb299fa2Sopenharmony_ci    static EventManager instance;
38fb299fa2Sopenharmony_ci    return instance;
39fb299fa2Sopenharmony_ci}
40fb299fa2Sopenharmony_ci
41fb299fa2Sopenharmony_civoid EventManager::Add(const ComInfo &viewId, std::unique_ptr<LabelOnTouchListener> listener)
42fb299fa2Sopenharmony_ci{
43fb299fa2Sopenharmony_ci    if (!pgMgr_.IsValidCom(viewId)) {
44fb299fa2Sopenharmony_ci        LOG(ERROR) << "not an valid view " << viewId;
45fb299fa2Sopenharmony_ci        return;
46fb299fa2Sopenharmony_ci    }
47fb299fa2Sopenharmony_ci    auto com = pgMgr_[viewId.pageId][viewId.comId].As();
48fb299fa2Sopenharmony_ci    labelOnTouchListener_.push_back(std::move(listener));
49fb299fa2Sopenharmony_ci    com->SetTouchable(true);
50fb299fa2Sopenharmony_ci    com->SetOnTouchListener(labelOnTouchListener_.back().get());
51fb299fa2Sopenharmony_ci}
52fb299fa2Sopenharmony_ci
53fb299fa2Sopenharmony_civoid EventManager::Add(const ComInfo &viewId, std::unique_ptr<BtnOnEventListener> listener)
54fb299fa2Sopenharmony_ci{
55fb299fa2Sopenharmony_ci    if (!pgMgr_.IsValidCom(viewId)) {
56fb299fa2Sopenharmony_ci        LOG(ERROR) << "not an valid view " << viewId;
57fb299fa2Sopenharmony_ci        return;
58fb299fa2Sopenharmony_ci    }
59fb299fa2Sopenharmony_ci    auto com = pgMgr_[viewId.pageId][viewId.comId].As();
60fb299fa2Sopenharmony_ci    // both click and touch listener
61fb299fa2Sopenharmony_ci    BtnOnEventListener *btnListener = listener.get();
62fb299fa2Sopenharmony_ci    // save this listener
63fb299fa2Sopenharmony_ci    btnOnClickListener_.push_back(std::move(listener));
64fb299fa2Sopenharmony_ci    com->SetOnClickListener(btnListener);
65fb299fa2Sopenharmony_ci    // this touch listener used to disable volume key when pressing button
66fb299fa2Sopenharmony_ci    com->SetOnTouchListener(btnListener);
67fb299fa2Sopenharmony_ci}
68fb299fa2Sopenharmony_ci
69fb299fa2Sopenharmony_civoid EventManager::Add(const ComInfo &viewId, std::unique_ptr<BtnOnDragListener> listener)
70fb299fa2Sopenharmony_ci{
71fb299fa2Sopenharmony_ci    if (!pgMgr_.IsValidCom(viewId)) {
72fb299fa2Sopenharmony_ci        LOG(ERROR) << "not an valid view " << viewId;
73fb299fa2Sopenharmony_ci        return;
74fb299fa2Sopenharmony_ci    }
75fb299fa2Sopenharmony_ci    auto com = pgMgr_[viewId.pageId][viewId.comId].As();
76fb299fa2Sopenharmony_ci    btnOnDragListener.push_back(std::move(listener));
77fb299fa2Sopenharmony_ci    com->SetDraggable(true);
78fb299fa2Sopenharmony_ci    com->SetOnDragListener(btnOnDragListener.back().get());
79fb299fa2Sopenharmony_ci}
80fb299fa2Sopenharmony_ci
81fb299fa2Sopenharmony_ci// key listener is registered at root view, because key event don't has position info and is globally responded
82fb299fa2Sopenharmony_civoid EventManager::AddKeyListener()
83fb299fa2Sopenharmony_ci{
84fb299fa2Sopenharmony_ci    OHOS::RootView::GetInstance()->SetOnKeyActListener(helper_.get());
85fb299fa2Sopenharmony_ci}
86fb299fa2Sopenharmony_ci
87fb299fa2Sopenharmony_civoid EventManager::Add(const ComInfo &viewId, EventType evt, Callback cb)
88fb299fa2Sopenharmony_ci{
89fb299fa2Sopenharmony_ci    switch (evt) {
90fb299fa2Sopenharmony_ci        case EventType::CLICKEVENT:
91fb299fa2Sopenharmony_ci            Add(viewId, std::make_unique<BtnOnEventListener>(cb, true));
92fb299fa2Sopenharmony_ci            break;
93fb299fa2Sopenharmony_ci        case EventType::TOUCHEVENT:
94fb299fa2Sopenharmony_ci            Add(viewId, std::make_unique<LabelOnTouchListener>(cb, true));
95fb299fa2Sopenharmony_ci            break;
96fb299fa2Sopenharmony_ci        case EventType::DRAGEVENT:
97fb299fa2Sopenharmony_ci            Add(viewId, std::make_unique<BtnOnDragListener>(cb, true));
98fb299fa2Sopenharmony_ci            break;
99fb299fa2Sopenharmony_ci        default:
100fb299fa2Sopenharmony_ci            LOG(WARNING) << "event type not support";
101fb299fa2Sopenharmony_ci            break;
102fb299fa2Sopenharmony_ci    }
103fb299fa2Sopenharmony_ci}
104fb299fa2Sopenharmony_ci} // namespace Updater
105