~aleteoryx/muditaos

ref: d29ee76fa546a32e9e004de2833c25e818fb94f5 muditaos/module-apps/application-desktop/windows/PinLockWindow.cpp -rw-r--r-- 5.8 KiB
d29ee76f — Michał Kamoń [EGD-4168] passcodes for sim change to variable length (#924) 5 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
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

// application manager
#include "InputEvent.hpp"
#include "service-appmgr/Controller.hpp"

// module-gui
#include "gui/widgets/BottomBar.hpp"
#include "PinLockWindow.hpp"

#include "application-desktop/ApplicationDesktop.hpp"
#include "application-desktop/data/LockPhoneData.hpp"
#include "ScreenLockBox.hpp"
#include "SimLockBox.hpp"
#include "PukLockBox.hpp"
#include <application-phonebook/ApplicationPhonebook.hpp>

namespace gui
{
    PinLockWindow::PinLockWindow(app::Application *app, const std::string &window_name, PinLock &lock)
        : PinLockBaseWindow(app, window_name, lock), this_window_name(window_name)
    {
        buildInterface();
    }

    void PinLockWindow::rebuild()
    {
        // find which widget has focus
        destroyInterface();
        buildInterface();
        // set state
        focusItem = nullptr;
        setVisibleState(lock.getState());
    }
    void PinLockWindow::buildInterface()
    {
        AppWindow::buildInterface();
        PinLockBaseWindow::build();
        buildPinLockBox();
        LockBox->buildLockBox(lock.getMaxPinSize());
    }

    void PinLockWindow::destroyInterface()
    {
        erase();
        invalidate();
    }

    void PinLockWindow::invalidate() noexcept
    {
        titleLabel = nullptr;
        lockImage  = nullptr;
        infoImage  = nullptr;
        infoText   = nullptr;
        pinLabelsBox = nullptr;
    }

    void PinLockWindow::setVisibleState(const PinLock::State state)
    {
        if (state == PinLock::State::EnterPin) {
            LockBox->setVisibleStateEnterPin();
        }
        else if (state == PinLock::State::VerifiedPin) {
            LockBox->setVisibleStateVerifiedPin();
        }
        else if (state == PinLock::State::InvalidPin) {
            LockBox->setVisibleStateInvalidPin();
        }
        else if (state == PinLock::State::Blocked) {
            LockBox->setVisibleStateBlocked();
        }
        application->refreshWindow(RefreshModes::GUI_REFRESH_FAST);
    }

    void PinLockWindow::onBeforeShow(ShowMode mode, SwitchData *data)
    {
        if (auto lockData = dynamic_cast<LockPhoneData *>(data)) {
            lockTimeoutApplication = lockData->getPreviousApplication();
        }
        if (lock.unlock()) {
            setVisibleState(PinLock::State::VerifiedPin);
            application->switchWindow(gui::name::window::main_window);
        }
        setVisibleState(lock.getState());
    }

    bool PinLockWindow::onInput(const InputEvent &inputEvent)
    {
        auto state = lock.getState();
        if (!inputEvent.isShortPress() || state == PinLock::State::VerifiedPin) {
            return AppWindow::onInput(inputEvent);
        }
        // accept only LF, enter, RF, #, and numeric values;
        if (inputEvent.is(KeyCode::KEY_LF) && bottomBar->isActive(BottomBar::Side::LEFT)) {
            app::manager::Controller::switchApplication(
                application, app::name_phonebook, gui::window::name::ice_contacts, nullptr);
            return true;
        }
        else if (inputEvent.is(KeyCode::KEY_RF) && bottomBar->isActive(BottomBar::Side::RIGHT)) {
            if (state == PinLock::State::EnterPin) {
                lock.clearAttempt();
            }
            else if (state == PinLock::State::InvalidPin) {
                LockBox->setVisibleStateInvalidPin();
                lock.consumeInvalidPinState();
            }
            application->switchWindow(gui::name::window::main_window);
            return true;
        }
        else if (inputEvent.is(KeyCode::KEY_PND)) {
            if (state == PinLock::State::EnterPin) {
                lock.popChar();
                LockBox->popChar(lock.getCharCount());
                if (!lock.canVerify()) {
                    bottomBar->setActive(BottomBar::Side::CENTER, false);
                }
                return true;
            }
        }
        else if (0 <= gui::toNumeric(inputEvent.keyCode) && gui::toNumeric(inputEvent.keyCode) <= 9) {
            if (state == PinLock::State::EnterPin && lock.canPut()) {
                LockBox->putChar(lock.getCharCount());
                lock.putNextChar(gui::toNumeric(inputEvent.keyCode));
                if (!lock.canPut() && lock.getLockType() == PinLock::LockType::Screen) {
                    lock.verifyPin();
                }
                else if (lock.canVerify()) {
                    bottomBar->setActive(BottomBar::Side::CENTER, true);
                }
                return true;
            }
        }
        else if (inputEvent.is(KeyCode::KEY_ENTER) && bottomBar->isActive(BottomBar::Side::CENTER)) {
            if (state == PinLock::State::InvalidPin) {
                lock.consumeInvalidPinState();
                application->switchWindow(this_window_name);
            }
            else if (state == PinLock::State::EnterPin) {
                lock.verifyPin();
            }
            else if (state == PinLock::State::Blocked) {
                application->switchWindow(gui::name::window::main_window);
            }
            return true;
        }
        // check if any of the lower inheritance onInput methods catch the event
        return AppWindow::onInput(inputEvent);
    }

    void PinLockWindow::buildPinLockBox()
    {
        auto lockType = lock.getLockType();
        if (lockType == PinLock::LockType::Screen) {
            LockBox = std::make_unique<ScreenLockBox>(this);
        }
        else if (lockType == PinLock::LockType::PUK) {
            LockBox = std::make_unique<PukLockBox>(this);
        }
        else if (lockType == PinLock::LockType::SIM) {
            LockBox = std::make_unique<SimLockBox>(this);
        }
        assert(LockBox != nullptr);
    }
} /* namespace gui */