1/*
2 * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include "key_option.h"
17
18#include "config_multimodal.h"
19#include "mmi_log.h"
20
21#undef MMI_LOG_TAG
22#define MMI_LOG_TAG "KeyOption"
23
24namespace OHOS {
25namespace MMI {
26namespace {
27constexpr int32_t PRE_KEYS_MAX_SIZE { 4 };
28}
29std::set<int32_t> KeyOption::GetPreKeys() const
30{
31    return preKeys_;
32}
33
34void KeyOption::SetPreKeys(const std::set<int32_t> &preKeys)
35{
36    preKeys_ = preKeys;
37}
38
39int32_t KeyOption::GetFinalKey() const
40{
41    return finalKey_;
42}
43
44void KeyOption::SetFinalKey(int32_t finalKey)
45{
46    finalKey_ = finalKey;
47}
48
49bool KeyOption::IsFinalKeyDown() const
50{
51    return isFinalKeyDown_;
52}
53void KeyOption::SetFinalKeyDown(bool pressed)
54{
55    isFinalKeyDown_ = pressed;
56}
57
58int32_t KeyOption::GetFinalKeyDownDuration() const
59{
60    return finalKeyDownDuration_;
61}
62
63int32_t KeyOption::GetFinalKeyUpDelay() const
64{
65    return finalKeyUpDelay_;
66}
67
68void KeyOption::SetFinalKeyDownDuration(int32_t duration)
69{
70    finalKeyDownDuration_ = duration;
71}
72
73void KeyOption::SetFinalKeyUpDelay(int32_t delay)
74{
75    finalKeyUpDelay_ = delay;
76}
77
78bool KeyOption::IsRepeat() const
79{
80    return isRepeat_;
81}
82
83void KeyOption::SetRepeat(bool repeat)
84{
85    isRepeat_ = repeat;
86}
87
88bool KeyOption::ReadFromParcel(Parcel &in)
89{
90    int32_t preKeysSize = 0;
91    READINT32(in, preKeysSize);
92    if (preKeysSize < 0) {
93        return false;
94    }
95    if (preKeysSize > PRE_KEYS_MAX_SIZE) {
96        MMI_HILOGE("The preKeys size:%{public}d, exceeds maximum allowed size:%{public}d", preKeysSize,
97            PRE_KEYS_MAX_SIZE);
98        return false;
99    }
100    for (auto i = 0; i < preKeysSize; ++i) {
101        int32_t keyValue = 0;
102        READINT32(in, keyValue);
103        preKeys_.insert(keyValue);
104    }
105    return (
106        in.ReadInt32(finalKey_) &&
107        in.ReadBool(isFinalKeyDown_) &&
108        in.ReadInt32(finalKeyDownDuration_) &&
109        in.ReadInt32(finalKeyUpDelay_) &&
110        in.ReadBool(isRepeat_)
111    );
112}
113
114bool KeyOption::WriteToParcel(Parcel &out) const
115{
116    if (preKeys_.size() > PRE_KEYS_MAX_SIZE) {
117        MMI_HILOGE("The preKeys size:%{public}zu, exceeds maximum allowed size:%{public}d", preKeys_.size(),
118            PRE_KEYS_MAX_SIZE);
119        return false;
120    }
121    int32_t preKeysSize = static_cast<int32_t>(preKeys_.size());
122    WRITEINT32(out, preKeysSize);
123    for (const auto &i : preKeys_) {
124        WRITEINT32(out, i);
125    }
126    return (
127        out.WriteInt32(finalKey_) &&
128        out.WriteBool(isFinalKeyDown_) &&
129        out.WriteInt32(finalKeyDownDuration_) &&
130        out.WriteInt32(finalKeyUpDelay_) &&
131        out.WriteBool(isRepeat_)
132    );
133}
134} // namespace MMI
135} // namespace OHOS