~aleteoryx/muditaos

ref: 8856a6564170dcb8714e2865f56d7fa93950d8d2 muditaos/module-apps/application-desktop/widgets/PinLock.hpp -rw-r--r-- 3.9 KiB
8856a656 — Mateusz Grzegorzek [EGD-4995] Add changing passcode windows flow 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
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#pragma once
#include <vector>
#include <string>
#include <functional>
#include <limits>

#include <module-utils/common_data/EventStore.hpp>

namespace gui
{
    class PinLockHandler;

    class PinLock
    {
      public:
        enum class LockType
        {
            SimPin,
            SimPuk,
            Screen
        };
        enum class LockState
        {
            Unlocked,
            PasscodeRequired,
            PasscodeInvalidRetryRequired,
            Blocked,
            NewPasscodeRequired,
            NewPasscodeInvalidRetryRequired,
            NewPasscodeConfirmRequired,
            NewPasscodeInvalid,
            ErrorOccurred
        };

        [[nodiscard]] LockState getState() const noexcept
        {
            return lockState;
        }
        [[nodiscard]] LockType getLockType() const noexcept
        {
            return lockType;
        }
        [[nodiscard]] unsigned int getMaxPinSize() const noexcept
        {
            return maxPinSize;
        }
        /// returns current position of a PIN character to be inserted
        [[nodiscard]] unsigned int getCharCount() const noexcept
        {
            return pinValue.size();
        }
        [[nodiscard]] bool canPut() const noexcept
        {
            return getCharCount() < getMaxPinSize();
        }
        [[nodiscard]] bool canVerify() const noexcept
        {
            return getCharCount() >= minPinSize;
        }
        [[nodiscard]] std::vector<unsigned int> getPin() const
        {
            return pinValue;
        }
        [[nodiscard]] unsigned int getValue() const noexcept
        {
            return value;
        }
        [[nodiscard]] bool isSim(Store::GSM::SIM _sim) const noexcept
        {
            return sim == _sim;
        }
        [[nodiscard]] bool isState(LockState state) const noexcept
        {
            return lockState == state;
        }
        [[nodiscard]] bool isType(LockType type) const noexcept
        {
            return lockType == type;
        }

        void putNextChar(unsigned int c);
        /// removes a last character passed to Lock via putNextChar. The last character can not be popped
        void popChar();
        /// clear all characters passed to the Lock
        void clearAttempt() noexcept;
        /// consumes LockState::PasscodeInvalidRetryRequired state and LockState::NewPasscodeInvalid)
        void consumeState() noexcept;
        void setNewPasscodeInvalidState() noexcept;
        /// calls
        void activate();

        PinLock(Store::GSM::SIM sim, LockState state, LockType type, unsigned int value = unlimitedNumOfAttempts)
            : sim{sim}, lockState{state}, lockType{type}, value{value}
        {}
        PinLock(const PinLock &other) = default;

        std::function<void(LockType type, const std::vector<unsigned int> &)> onActivatedCallback = nullptr;

      private:
        Store::GSM::SIM sim = Store::GSM::SIM::NONE;
        LockState lockState = LockState::Unlocked;
        LockType lockType   = LockType::Screen;
        unsigned int value  = 0;

        std::vector<unsigned int> pinValue;
        unsigned int maxPinSize = defaultPasscodeSize;
        unsigned int minPinSize = defaultPasscodeSize;
        bool autoActivate       = false;

        static constexpr unsigned int defaultPasscodeSize = 4;
        static constexpr unsigned int unlimitedNumOfAttempts = std::numeric_limits<unsigned int>::max();

        void setAutoActivate(bool _autoActivate)
        {
            autoActivate = _autoActivate;
        }
        void setPinSizeBounds(unsigned int _minPinSize, unsigned int _maxPinSize)
        {
            minPinSize = _minPinSize;
            maxPinSize = _maxPinSize;
        }

        friend class PinLockHandler;
        friend class ChangePasscodeLockHandler;
    };

} // namespace gui