1 /*
2 * Copyright (c) 2021-2025 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 "mmi_log.h"
19
20 #undef MMI_LOG_TAG
21 #define MMI_LOG_TAG "KeyOption"
22
23 namespace OHOS {
24 namespace MMI {
25 namespace {
26 constexpr int32_t PRE_KEYS_MAX_SIZE { 4 };
27 }
GetPreKeys() const28 std::set<int32_t> KeyOption::GetPreKeys() const
29 {
30 return preKeys_;
31 }
32
SetPreKeys(const std::set<int32_t> & preKeys)33 void KeyOption::SetPreKeys(const std::set<int32_t> &preKeys)
34 {
35 preKeys_ = preKeys;
36 }
37
GetFinalKey() const38 int32_t KeyOption::GetFinalKey() const
39 {
40 return finalKey_;
41 }
42
SetFinalKey(int32_t finalKey)43 void KeyOption::SetFinalKey(int32_t finalKey)
44 {
45 finalKey_ = finalKey;
46 }
47
IsFinalKeyDown() const48 bool KeyOption::IsFinalKeyDown() const
49 {
50 return isFinalKeyDown_;
51 }
SetFinalKeyDown(bool pressed)52 void KeyOption::SetFinalKeyDown(bool pressed)
53 {
54 isFinalKeyDown_ = pressed;
55 }
56
GetFinalKeyDownDuration() const57 int32_t KeyOption::GetFinalKeyDownDuration() const
58 {
59 return finalKeyDownDuration_;
60 }
61
GetFinalKeyUpDelay() const62 int32_t KeyOption::GetFinalKeyUpDelay() const
63 {
64 return finalKeyUpDelay_;
65 }
66
SetFinalKeyDownDuration(int32_t duration)67 void KeyOption::SetFinalKeyDownDuration(int32_t duration)
68 {
69 finalKeyDownDuration_ = duration;
70 }
71
SetFinalKeyUpDelay(int32_t delay)72 void KeyOption::SetFinalKeyUpDelay(int32_t delay)
73 {
74 finalKeyUpDelay_ = delay;
75 }
76
IsRepeat() const77 bool KeyOption::IsRepeat() const
78 {
79 return isRepeat_;
80 }
81
SetRepeat(bool repeat)82 void KeyOption::SetRepeat(bool repeat)
83 {
84 isRepeat_ = repeat;
85 }
86
GetPriority() const87 int32_t KeyOption::GetPriority() const
88 {
89 return priority_;
90 }
91
SetPriority(int32_t priority)92 void KeyOption::SetPriority(int32_t priority)
93 {
94 priority_ = priority;
95 }
96
ReadFromParcel(Parcel & in)97 bool KeyOption::ReadFromParcel(Parcel &in)
98 {
99 int32_t preKeysSize = 0;
100 READINT32(in, preKeysSize);
101 if (preKeysSize < 0) {
102 return false;
103 }
104 if (preKeysSize > PRE_KEYS_MAX_SIZE) {
105 MMI_HILOGE("The preKeys size:%{public}d, exceeds maximum allowed size:%{public}d", preKeysSize,
106 PRE_KEYS_MAX_SIZE);
107 return false;
108 }
109 for (auto i = 0; i < preKeysSize; ++i) {
110 int32_t keyValue = 0;
111 READINT32(in, keyValue);
112 preKeys_.insert(keyValue);
113 }
114 return (
115 in.ReadInt32(finalKey_) &&
116 in.ReadBool(isFinalKeyDown_) &&
117 in.ReadInt32(finalKeyDownDuration_) &&
118 in.ReadInt32(finalKeyUpDelay_) &&
119 in.ReadBool(isRepeat_) &&
120 in.ReadInt32(priority_)
121 );
122 }
123
WriteToParcel(Parcel & out) const124 bool KeyOption::WriteToParcel(Parcel &out) const
125 {
126 if (preKeys_.size() > PRE_KEYS_MAX_SIZE) {
127 MMI_HILOGE("The preKeys size:%{public}zu, exceeds maximum allowed size:%{public}d", preKeys_.size(),
128 PRE_KEYS_MAX_SIZE);
129 return false;
130 }
131 int32_t preKeysSize = static_cast<int32_t>(preKeys_.size());
132 WRITEINT32(out, preKeysSize);
133 for (const auto &i : preKeys_) {
134 WRITEINT32(out, i);
135 }
136 return (
137 out.WriteInt32(finalKey_) &&
138 out.WriteBool(isFinalKeyDown_) &&
139 out.WriteInt32(finalKeyDownDuration_) &&
140 out.WriteInt32(finalKeyUpDelay_) &&
141 out.WriteBool(isRepeat_) &&
142 out.WriteInt32(priority_)
143 );
144 }
145
GetKey() const146 int32_t KeyMonitorOption::GetKey() const
147 {
148 return key_;
149 }
150
GetAction() const151 int32_t KeyMonitorOption::GetAction() const
152 {
153 return action_;
154 }
155
IsRepeat() const156 bool KeyMonitorOption::IsRepeat() const
157 {
158 return isRepeat_;
159 }
160
SetKey(int32_t key)161 void KeyMonitorOption::SetKey(int32_t key)
162 {
163 key_ = key;
164 }
165
SetAction(int32_t action)166 void KeyMonitorOption::SetAction(int32_t action)
167 {
168 action_ = action;
169 }
170
SetRepeat(bool repeat)171 void KeyMonitorOption::SetRepeat(bool repeat)
172 {
173 isRepeat_ = repeat;
174 }
175 } // namespace MMI
176 } // namespace OHOS
177