~aleteoryx/muditaos

ref: e8f7a57219ea7c881cf53322fe6a3c6b0c5e7c56 muditaos/module-apps/application-settings-new/windows/ChangePasscodeWindow.cpp -rw-r--r-- 6.8 KiB
e8f7a572 — Tomasz Langowski [EGD-5758] Restore auto locking timer in ApplicationManager 4 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
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "application-desktop/data/AppDesktopStyle.hpp"
#include "application-settings-new/ApplicationSettings.hpp"
#include "ChangePasscodeWindow.hpp"
#include "DialogMetadata.hpp"
#include "DialogMetadataMessage.hpp"
#include "windows/Dialog.hpp"

namespace lock_style        = style::window::pin_lock;
namespace screen_lock_style = style::window::screen_pin_lock;

namespace gui
{
    ChangePasscodeWindow::ChangePasscodeWindow(app::Application *app)
        : LockWindow(app, gui::window::name::change_passcode)
    {
        buildInterface();
    }

    auto ChangePasscodeWindow::onInput(const InputEvent &inputEvent) -> bool
    {
        auto &lock = lockHandler.getLock();
        if (lock.isState(PinLock::LockState::Unlocked) && inputEvent.isShortPress()) {
            application->returnToPreviousWindow();
        }
        if (!inputEvent.isShortPress()) {
            return AppWindow::onInput(inputEvent);
        }

        auto keyCodeNum = gui::toNumeric(inputEvent.keyCode);
        if (0 <= keyCodeNum && keyCodeNum <= 9 && lock.canPut()) {
            lockBox->putChar(lock.getCharCount());
            lock.putNextChar(keyCodeNum);
            return true;
        }
        else if (inputEvent.is(KeyCode::KEY_PND)) {
            lock.popChar();
            lockBox->popChar(lock.getCharCount());
            return true;
        }
        else if (inputEvent.is(KeyCode::KEY_ENTER)) {
            processPasscode();
            setVisibleState();
            return true;
        }
        return AppWindow::onInput(inputEvent);
    }

    void ChangePasscodeWindow::rebuild()
    {
        destroyInterface();
        buildInterface();
    }

    void ChangePasscodeWindow::buildBottomBar()
    {
        LockWindow::buildBottomBar();
        setBottomBarWidgetsActive(false, true, true);
    }

    void ChangePasscodeWindow::buildInterface()
    {
        AppWindow::buildInterface();
        LockWindow::build();

        lockBox = std::make_unique<ScreenLockBaseBox>(this);
        lockBox->buildLockBox(lockHandler.getLock().getMaxPinSize());
        primaryText->setPosition(screen_lock_style::primary_text::y, gui::Axis::Y);
        pinLabelsBox->setPosition(screen_lock_style::pin_label::y, gui::Axis::Y);
    }

    void ChangePasscodeWindow::buildTitleBar()
    {
        setTitle(utils::localize.get("app_settings_security_change_passcode"));
    }

    void ChangePasscodeWindow::destroyInterface()
    {
        erase();
    }

    void ChangePasscodeWindow::onBeforeShow(ShowMode mode, SwitchData *data)
    {
        if (data != nullptr) {
            auto changePasscodeData = static_cast<ChangePasscodeData *>(data);
            changePasscodeAction    = changePasscodeData->changePasscodeAction;
            if (ChangePasscodeAction::OnlyProvideNewPasscode == changePasscodeAction) {
                lockState = PinLock::LockState::NewPasscodeRequired;
            }
        }
        setVisibleState();
    }

    void ChangePasscodeWindow::processPasscode()
    {
        switch (lockState) {
        case PinLock::LockState::PasscodeRequired:
        case PinLock::LockState::PasscodeInvalidRetryRequired: {
            auto app  = static_cast<app::ApplicationSettingsNew *>(application);
            lockState = lockHandler.checkPasscode(app->getLockPassHash());
            if (ChangePasscodeAction::OnlyCheckCurrentPasscode == changePasscodeAction &&
                lockState == PinLock::LockState::NewPasscodeRequired) {
                DialogMetadata meta;
                meta.icon   = "big_circle_placeholder";
                meta.text   = utils::localize.get("app_settings_security_passcode_disabled");
                meta.title  = utils::localize.get("app_settings_security_change_passcode");
                meta.action = [this]() {
                    application->switchWindow(gui::window::name::security);
                    return true;
                };
                application->setLockScreenPasscodeOn(false);
                application->switchWindow(gui::window::name::dialog_confirm,
                                          std::make_unique<gui::DialogMetadataMessage>(meta));
                return;
            }
            break;
        }
        case PinLock::LockState::NewPasscodeRequired:
        case PinLock::LockState::NewPasscodeInvalidRetryRequired: {
            lockState = lockHandler.newPasscodeProvided();
            break;
        }
        case PinLock::LockState::NewPasscodeConfirmRequired:
        case PinLock::LockState::NewPasscodeInvalid: {
            lockState = lockHandler.newPasscodeConfirmed();
            if (lockState == PinLock::LockState::Unlocked) {
                auto app = static_cast<app::ApplicationSettingsNew *>(application);
                app->setLockPassHash(lockHandler.getNewPasscodeHash());
            }
            break;
        }
        default:
            break;
        }
    }

    void ChangePasscodeWindow::setVisibleState()
    {
        lockBox->clear();
        switch (lockState) {
        case PinLock::LockState::PasscodeRequired: {
            setText("app_settings_security_type_current_passcode", LockWindow::TextType::Primary);
            secondaryText->setVisible(false);
            break;
        }
        case PinLock::LockState::NewPasscodeRequired: {
            setText("app_settings_security_enter_new_passcode", LockWindow::TextType::Primary);
            secondaryText->setVisible(false);
            break;
        }
        case PinLock::LockState::NewPasscodeConfirmRequired: {
            setText("app_settings_security_confirm_new_passcode", LockWindow::TextType::Primary);
            secondaryText->setVisible(false);
            break;
        }
        case PinLock::LockState::PasscodeInvalidRetryRequired:
        case PinLock::LockState::NewPasscodeInvalidRetryRequired:
        case PinLock::LockState::NewPasscodeInvalid: {
            setText("app_settings_security_wrong_passcode", LockWindow::TextType::Secondary);
            break;
        }
        case PinLock::LockState::Unlocked: {
            DialogMetadata meta;
            meta.icon   = "big_circle_placeholder";
            meta.text   = utils::localize.get("app_settings_security_passcode_changed_successfully");
            meta.title  = utils::localize.get("app_settings_security_change_passcode");
            meta.action = [this]() {
                application->switchWindow(gui::window::name::security);
                return true;
            };
            application->setLockScreenPasscodeOn(true);
            application->switchWindow(gui::window::name::dialog_confirm,
                                      std::make_unique<gui::DialogMetadataMessage>(meta));
            break;
        }
        default: {
            break;
        }
        }
    }
} // namespace gui