Files
MuditaOS/module-apps/application-desktop/widgets/PinLock.hpp
Pawel Olejniczak 4b35b13fe9 [EGD-4734] Add PIN settings window
This window displays switch button for
disabling/enabling pin code for sim card.
Its also starting point for changing pin code.
Added actions for enabling and disabling
pin code for sim card.
Added method for checking if pin code is
set for sim card.
2021-04-07 19:09:20 +02:00

142 lines
4.2 KiB
C++

// Copyright (c) 2017-2021, 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,
PinLockDisableRequired,
PinLockEnableRequired,
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;
}
[[nodiscard]] const std::string &getPasscodeName() const noexcept
{
return passcodeName;
}
[[nodiscard]] Store::GSM::SIM getSim() const noexcept
{
return sim;
}
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:
std::string passcodeName;
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