~aleteoryx/muditaos

ref: ca9579a252e7ea6186b2dedb4dd6f66309eb0d93 muditaos/module-apps/apps-common/locks/windows/LockInputWindow.cpp -rw-r--r-- 8.8 KiB
ca9579a2 — Dawid Wojtas [BH-1590] Light press ends the Power Nap 3 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "LockInputWindow.hpp"

#include <locks/data/LockStyle.hpp>
#include <locks/widgets/Lock.hpp>

#include <service-appmgr/Controller.hpp>
#include <header/IceAction.hpp>
#include <FontManager.hpp>
#include <i18n/i18n.hpp>

namespace lock_style = style::window::lock_input;

namespace gui
{
    void LockInputWindow::build()
    {
        buildBody();
        buildImage();
        buildInfoTexts();
        buildPinBody();
        buildIceBox();
        buildNavBar();

        body->resizeItems();
    }

    void LockInputWindow::buildBody()
    {
        body = new VBox(this,
                        style::window::default_left_margin,
                        style::window::default_vertical_pos,
                        style::window::default_body_width,
                        style::window::default_body_height);
        body->setEdges(RectangleEdge::None);
    }

    void LockInputWindow::buildImage()
    {
        using namespace style::window::lock_input;

        infoImage = new gui::ImageBox(body, 0, 0, 0, 0, new gui::Image(""));
        infoImage->setMinimumSize(image::wh, image::wh);
        infoImage->setMargins(Margins(0, image::image_top_margin, 0, image::image_bottom_margin));
        infoImage->showImage(false);
    }

    void LockInputWindow::buildInfoTexts()
    {
        using namespace style::window::lock_input;

        primaryText = new Text(body, 0, 0, 0, 0);
        primaryText->setMinimumSize(style::window::default_body_width, primary_text::h);
        primaryText->setFont(style::window::font::medium);
        primaryText->setAlignment(gui::Alignment(gui::Alignment::Horizontal::Center, gui::Alignment::Vertical::Top));

        secondaryText = new Text(body, 0, 0, 0, secondary_text::h);
        secondaryText->setMinimumSize(style::window::default_body_width, secondary_text::h);
        secondaryText->setMargins(Margins(0, secondary_text::top_margin, 0, 0));
        secondaryText->setFont(style::window::font::medium);
        secondaryText->setAlignment(gui::Alignment(gui::Alignment::Horizontal::Center, gui::Alignment::Vertical::Top));
    }

    void LockInputWindow::buildNavBar()
    {
        navBar->setText(nav_bar::Side::Left, utils::translate(style::strings::common::skip));
        navBar->setText(nav_bar::Side::Center, utils::translate(style::strings::common::confirm));
        navBar->setText(nav_bar::Side::Right, utils::translate(style::strings::common::back));
        setNavBarWidgetsActive(false, false, false);
    }

    void LockInputWindow::buildIceBox()
    {
        header->navigationIndicatorAdd(new gui::header::IceAction(), gui::header::BoxSelection::Left);
    }

    void LockInputWindow::buildPinBody()
    {
        pinLabelsBox = new gui::HBox(body, 0, 0, 0, 0);
        pinLabelsBox->setMinimumSize(lock_style::input_box::w, lock_style::input_box::h);
        pinLabelsBox->setAlignment(Alignment(gui::Alignment::Horizontal::Center, gui::Alignment::Vertical::Center));
        pinLabelsBox->setMargins(Margins(0, lock_style::input_box::top_margin, 0, 0));
        pinLabelsBox->setEdges(RectangleEdge::None);
    }

    void LockInputWindow::buildPinLabels(const std::function<Rect *()> &itemBuilder, unsigned int pinSize)
    {
        if (pinSize == 0) {
            return;
        }

        for (uint32_t i = 0; i < pinSize; i++) {
            auto label = itemBuilder();
            label->setFilled(false);
            label->setBorderColor(gui::ColorFullBlack);
            label->setPenWidth(2);
            label->setAlignment(gui::Alignment(gui::Alignment::Horizontal::Center, gui::Alignment::Vertical::Center));
            label->setVisible(true);
            label->activeItem = false;
            pinLabelsBox->addWidget(label);
        }
    }

    status_bar::Configuration LockInputWindow::configureStatusBar(status_bar::Configuration appConfiguration)
    {
        appConfiguration.disable(status_bar::Indicator::NetworkAccessTechnology);
        appConfiguration.enable(status_bar::Indicator::Time);
        appConfiguration.enable(status_bar::Indicator::PhoneMode);
        appConfiguration.enable(status_bar::Indicator::Battery);
        appConfiguration.enable(status_bar::Indicator::Signal);
        appConfiguration.enable(status_bar::Indicator::SimCard);
        appConfiguration.enable(status_bar::Indicator::Bluetooth);
        appConfiguration.enable(status_bar::Indicator::AlarmClock);
        return appConfiguration;
    }

    void LockInputWindow::restore() noexcept
    {
        infoImage->showImage(false);
        primaryText->setVisible(false);
        secondaryText->setVisible(false);
        pinLabelsBox->setVisible(false);
    }

    void LockInputWindow::setImage(const UTF8 &imageName)
    {
        infoImage->setImage(imageName);
        infoImage->showImage(true);
    }

    void LockInputWindow::setNavBarWidgetsActive(bool left, bool center, bool right)
    {
        navBar->setActive(nav_bar::Side::Left, left);
        navBar->setActive(nav_bar::Side::Center, center);
        navBar->setActive(nav_bar::Side::Right, right);
    }

    void LockInputWindow::setText(const std::string &value, TextType type, text::RichTextParser::TokenMap tokens)
    {
        switch (type) {
        case TextType::Title: {
            header->setTitleVisibility(true);
            if (!tokens.empty()) {
                setTitle(text::RichTextParser().parse(utils::translate(value), nullptr, std::move(tokens))->getText());
            }
            else {
                setTitle(utils::translate(value));
            }
            break;
        }
        case TextType::Primary:
            primaryText->setVisible(true);
            primaryText->setRichText(utils::translate(value), std::move(tokens));
            break;
        case TextType::Secondary:
            secondaryText->setVisible(true);
            secondaryText->setRichText(utils::translate(value), std::move(tokens));
            break;
        }
    }

    void LockInputWindow::setTitleBar(bool titleVisible, bool iceVisible)
    {
        header->setTitleVisibility(titleVisible);

        if (iceVisible) {
            header->navigationIndicatorAdd(new gui::header::IceAction(), gui::header::BoxSelection::Left);
        }
        else {
            header->navigationIndicatorRemove(gui::header::BoxSelection::Left);
        }
    }

    auto LockInputWindow::getToken(LockInputWindow::Token token) const -> std::string
    {
        if (token == Token::PinType) {
            return "$PINTYPE";
        }
        else if (token == Token::Sim) {
            return "$SIM";
        }
        else if (token == Token::Attempts) {
            return "$ATTEMPTS";
        }
        else if (token == Token::Time) {
            return "$TIME";
        }
        else if (token == Token::CmeCode) {
            return "$CMECODE";
        }
        return std::string{};
    }

    void LockInputWindow::setVisibleState()
    {
        restore();
        switch (lock->getState()) {
        case locks::Lock::LockState::InputRequired:
            lockBox->setVisibleStateInputRequired(LockBox::InputActionType::ProvideInput);
            break;
        case locks::Lock::LockState::InputInvalid:
            lockBox->setVisibleStateInputInvalid(LockBox::InputErrorType::InvalidInput, lock->getAttemptsLeft());
            break;
        case locks::Lock::LockState::Blocked:
            lockBox->setVisibleStateBlocked(lock->getNextUnlockAttemptFormattedTime());
            break;
        case locks::Lock::LockState::NewInputRequired:
            lockBox->setVisibleStateInputRequired(LockBox::InputActionType::ProvideNewInput);
            break;
        case locks::Lock::LockState::NewInputConfirmRequired:
            lockBox->setVisibleStateInputRequired(LockBox::InputActionType::ConfirmNewInput);
            break;
        case locks::Lock::LockState::NewInputInvalid:
            lockBox->setVisibleStateInputInvalid(LockBox::InputErrorType::NewInputConfirmFailed,
                                                 lock->getAttemptsLeft());
            break;
        default:
            break;
        }
    }

    auto LockInputWindow::isIceVisible() const noexcept -> bool
    {
        return header->navigationIndicatorVisible(header::BoxSelection::Left);
    }

    auto LockInputWindow::isInInputState() const noexcept -> bool
    {
        return lock && (lock->isState(locks::Lock::LockState::InputRequired) ||
                        lock->isState(locks::Lock::LockState::NewInputRequired) ||
                        lock->isState(locks::Lock::LockState::NewInputConfirmRequired));
    }

    auto LockInputWindow::isInInvalidInputState() const noexcept -> bool
    {
        return lock && (lock->isState(locks::Lock::LockState::InputInvalid) ||
                        lock->isState(locks::Lock::LockState::NewInputInvalid));
    }

} // namespace gui