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 "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
ReadFromParcel(Parcel & in)87 bool KeyOption::ReadFromParcel(Parcel &in)
88 {
89 int32_t preKeysSize = 0;
90 READINT32(in, preKeysSize);
91 if (preKeysSize < 0) {
92 return false;
93 }
94 if (preKeysSize > PRE_KEYS_MAX_SIZE) {
95 MMI_HILOGE("The preKeys size:%{public}d, exceeds maximum allowed size:%{public}d", preKeysSize,
96 PRE_KEYS_MAX_SIZE);
97 return false;
98 }
99 for (auto i = 0; i < preKeysSize; ++i) {
100 int32_t keyValue = 0;
101 READINT32(in, keyValue);
102 preKeys_.insert(keyValue);
103 }
104 return (
105 in.ReadInt32(finalKey_) &&
106 in.ReadBool(isFinalKeyDown_) &&
107 in.ReadInt32(finalKeyDownDuration_) &&
108 in.ReadInt32(finalKeyUpDelay_) &&
109 in.ReadBool(isRepeat_)
110 );
111 }
112
WriteToParcel(Parcel & out) const113 bool KeyOption::WriteToParcel(Parcel &out) const
114 {
115 if (preKeys_.size() > PRE_KEYS_MAX_SIZE) {
116 MMI_HILOGE("The preKeys size:%{public}zu, exceeds maximum allowed size:%{public}d", preKeys_.size(),
117 PRE_KEYS_MAX_SIZE);
118 return false;
119 }
120 int32_t preKeysSize = static_cast<int32_t>(preKeys_.size());
121 WRITEINT32(out, preKeysSize);
122 for (const auto &i : preKeys_) {
123 WRITEINT32(out, i);
124 }
125 return (
126 out.WriteInt32(finalKey_) &&
127 out.WriteBool(isFinalKeyDown_) &&
128 out.WriteInt32(finalKeyDownDuration_) &&
129 out.WriteInt32(finalKeyUpDelay_) &&
130 out.WriteBool(isRepeat_)
131 );
132 }
133
GetKey() const134 int32_t KeyMonitorOption::GetKey() const
135 {
136 return key_;
137 }
138
GetAction() const139 int32_t KeyMonitorOption::GetAction() const
140 {
141 return action_;
142 }
143
IsRepeat() const144 bool KeyMonitorOption::IsRepeat() const
145 {
146 return isRepeat_;
147 }
148
SetKey(int32_t key)149 void KeyMonitorOption::SetKey(int32_t key)
150 {
151 key_ = key;
152 }
153
SetAction(int32_t action)154 void KeyMonitorOption::SetAction(int32_t action)
155 {
156 action_ = action;
157 }
158
SetRepeat(bool repeat)159 void KeyMonitorOption::SetRepeat(bool repeat)
160 {
161 isRepeat_ = repeat;
162 }
163
Marshalling(Parcel & parcel) const164 bool KeyMonitorOption::Marshalling(Parcel &parcel) const
165 {
166 return (parcel.WriteInt32(key_) &&
167 parcel.WriteInt32(action_) &&
168 parcel.WriteBool(isRepeat_));
169 }
170
Unmarshalling(Parcel & parcel)171 bool KeyMonitorOption::Unmarshalling(Parcel &parcel)
172 {
173 return (
174 parcel.ReadInt32(key_) &&
175 parcel.ReadInt32(action_) &&
176 parcel.ReadBool(isRepeat_)
177 );
178 }
179 } // namespace MMI
180 } // namespace OHOS
181