1 /*
2  * Copyright (c) 2021-2022 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 
24 namespace OHOS {
25 namespace MMI {
26 namespace {
27 constexpr int32_t PRE_KEYS_MAX_SIZE { 4 };
28 }
GetPreKeys() const29 std::set<int32_t> KeyOption::GetPreKeys() const
30 {
31     return preKeys_;
32 }
33 
SetPreKeys(const std::set<int32_t> & preKeys)34 void KeyOption::SetPreKeys(const std::set<int32_t> &preKeys)
35 {
36     preKeys_ = preKeys;
37 }
38 
GetFinalKey() const39 int32_t KeyOption::GetFinalKey() const
40 {
41     return finalKey_;
42 }
43 
SetFinalKey(int32_t finalKey)44 void KeyOption::SetFinalKey(int32_t finalKey)
45 {
46     finalKey_ = finalKey;
47 }
48 
IsFinalKeyDown() const49 bool KeyOption::IsFinalKeyDown() const
50 {
51     return isFinalKeyDown_;
52 }
SetFinalKeyDown(bool pressed)53 void KeyOption::SetFinalKeyDown(bool pressed)
54 {
55     isFinalKeyDown_ = pressed;
56 }
57 
GetFinalKeyDownDuration() const58 int32_t KeyOption::GetFinalKeyDownDuration() const
59 {
60     return finalKeyDownDuration_;
61 }
62 
GetFinalKeyUpDelay() const63 int32_t KeyOption::GetFinalKeyUpDelay() const
64 {
65     return finalKeyUpDelay_;
66 }
67 
SetFinalKeyDownDuration(int32_t duration)68 void KeyOption::SetFinalKeyDownDuration(int32_t duration)
69 {
70     finalKeyDownDuration_ = duration;
71 }
72 
SetFinalKeyUpDelay(int32_t delay)73 void KeyOption::SetFinalKeyUpDelay(int32_t delay)
74 {
75     finalKeyUpDelay_ = delay;
76 }
77 
ReadFromParcel(Parcel & in)78 bool KeyOption::ReadFromParcel(Parcel &in)
79 {
80     int32_t preKeysSize = 0;
81     READINT32(in, preKeysSize);
82     if (preKeysSize < 0) {
83         return false;
84     }
85     if (preKeysSize > PRE_KEYS_MAX_SIZE) {
86         MMI_HILOGE("The preKeys size:%{public}d, exceeds maximum allowed size:%{public}d", preKeysSize,
87             PRE_KEYS_MAX_SIZE);
88         return false;
89     }
90     for (auto i = 0; i < preKeysSize; ++i) {
91         int32_t keyValue = 0;
92         READINT32(in, keyValue);
93         preKeys_.insert(keyValue);
94     }
95     return (
96         in.ReadInt32(finalKey_) &&
97         in.ReadBool(isFinalKeyDown_) &&
98         in.ReadInt32(finalKeyDownDuration_) &&
99         in.ReadInt32(finalKeyUpDelay_)
100     );
101 }
102 
WriteToParcel(Parcel & out) const103 bool KeyOption::WriteToParcel(Parcel &out) const
104 {
105     if (preKeys_.size() > PRE_KEYS_MAX_SIZE) {
106         MMI_HILOGE("The preKeys size:%{public}zu, exceeds maximum allowed size:%{public}d", preKeys_.size(),
107             PRE_KEYS_MAX_SIZE);
108         return false;
109     }
110     int32_t preKeysSize = static_cast<int32_t>(preKeys_.size());
111     WRITEINT32(out, preKeysSize);
112     for (const auto &i : preKeys_) {
113         WRITEINT32(out, i);
114     }
115     return (
116         out.WriteInt32(finalKey_) &&
117         out.WriteBool(isFinalKeyDown_) &&
118         out.WriteInt32(finalKeyDownDuration_) &&
119         out.WriteInt32(finalKeyUpDelay_)
120     );
121 }
122 } // namespace MMI
123 } // namespace OHOS