~aleteoryx/muditaos

ref: a607871f76a87b141d0c1db65cb87bc783055874 muditaos/module-apps/application-desktop/widgets/PinLock.hpp -rw-r--r-- 3.0 KiB
a607871f — Hubert Chrzaniuk [EGD-3839] Cellular - get time zone (#903) 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
// 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 "PhoneNumber.hpp"

namespace gui
{
    class PinLockHandler;

    class PinLock
    {
      public:
        enum class LockType
        {
            Screen,
            SIM,
            PUK,
            Unknown
        };
        enum class InfoName
        {
            LockName,
            PhoneNum
        };
        enum class State
        {
            EnterPin,
            InvalidPin,
            VerifiedPin,
            Blocked,
            Unlocked
        };

        [[nodiscard]] State getState() const noexcept
        {
            return state;
        }
        [[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]] unsigned int getRemainingAttempts() const noexcept
        {
            return remainingAttempts;
        }
        [[nodiscard]] bool canPut() const noexcept
        {
            return getCharCount() != getMaxPinSize();
        }
        [[nodiscard]] bool canVerify() const noexcept
        {
            return getCharCount() >= minPinSize;
        }
        void putNextChar(unsigned int c);
        void verifyPin();
        /// 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;
        /// if Lock is in the State::InvalidPin state, changes it's state to the State::EnterPin
        void consumeInvalidPinState() noexcept;

        [[nodiscard]] bool isLocked() const noexcept;
        bool unlock() noexcept;
        void lock() noexcept;

        [[nodiscard]] std::string getLockInfo(const InfoName name) const;
        [[nodiscard]] LockType getLockType() const noexcept
        {
            return type;
        }
        PinLock(gui::PinLockHandler *);

      private:
        /// for PIN verification purposes as PIN storage and management is out of scope of PinLock class
        gui::PinLockHandler *handler;

        LockType type                  = LockType::Unknown;
        State state                    = State::EnterPin;
        unsigned int remainingAttempts = 0;
        /// code of the entered character on specified position
        std::vector<unsigned int> pinValue;
        unsigned int maxPinSize = 0;
        unsigned int minPinSize = 0;
        std::map<InfoName, std::string> additionalLockInfo;

        void reset(LockType _type,
                   State _state,
                   unsigned int _remainingAttempts,
                   unsigned int _maxPinSize,
                   unsigned int _minPinSize) noexcept;

        friend class gui::PinLockHandler;
    };

} // namespace gui