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 #ifndef KEY_OPTION_H
17 #define KEY_OPTION_H
18 
19 #include <set>
20 #include "nocopyable.h"
21 #include "parcel.h"
22 
23 namespace OHOS {
24 namespace MMI {
25 class KeyOption {
26 public:
27     KeyOption() = default;
28     DISALLOW_COPY_AND_MOVE(KeyOption);
29 
30 public:
31     /**
32      * @brief Obtains previous keys.
33      * @return Returns previous keys.
34      * @since 9
35      */
36     std::set<int32_t> GetPreKeys() const;
37 
38     /**
39      * @brief Sets previous keys, that is, the keys that are pressed first in a combination key.
40      * There is no requirement on the sequence of previous keys.
41      * @param preKeys Indicates the previous keys to set.
42      * @return void
43      * @since 9
44      */
45     void SetPreKeys(const std::set<int32_t>& preKeys);
46 
47     /**
48      * @brief Obtains the final key.
49      * @return Returns the final key.
50      * @since 9
51      */
52     int32_t GetFinalKey() const;
53 
54     /**
55      * @brief Sets the final key, that is, the key that is last pressed or released in a combination key.
56      * @param finalKey Indicates the final key.
57      * @return void
58      * @since 9
59      */
60     void SetFinalKey(int32_t finalKey);
61 
62     /**
63      * @brief Checks whether the final key in a combination key is pressed or released.
64      * @return Returns <b>true</b> if the key is pressed; returns <b>false</b> if the key is released.
65      * @since 9
66      */
67     bool IsFinalKeyDown() const;
68 
69     /**
70      * @brief Sets whether the final key in a combination key is pressed or released.
71      * @param pressed Indicates whether the key is pressed. The value <b>true</b> means that the key
72      * is pressed, and the value <b>false</b> means that the key is released.
73      * @return void
74      * @since 9
75      */
76     void SetFinalKeyDown(bool pressed);
77 
78     /**
79      * @brief Obtains the duration when the final key is held down or the maximum duration between
80      * when the key is pressed and when the key is released.
81      * If the final key is pressed, this parameter indicates the duration when the final key is held down.
82      * If the last key is released, this parameter indicates the maximum duration between when the key
83      * is pressed and when the key is released.
84      * @return Returns the duration when the final key is held down or the maximum duration between
85      * when the key is pressed and when the key is released.
86      * @since 9
87      */
88     int32_t GetFinalKeyDownDuration() const;
89 
90     /**
91      * @brief Get the delay time of lifting the last key. When the last key is lifted, the subscription
92      * will be delayed and triggered.
93      * @return Return to the delay time of lifting the last key.
94      * @since 9
95      */
96     int32_t GetFinalKeyUpDelay() const;
97 
98     /**
99      * @brief Sets the duration when the final key is held down or the maximum duration between when
100      * the key is pressed and when the key is released.
101      * If the final key is pressed, this parameter indicates the duration when the final key is held down.
102      * If the last key is released, this parameter indicates the maximum duration between when the key
103      * is pressed and when the key is released.
104      * @param duration Indicates the duration when the final key is held down or the maximum duration
105      * between when the key is pressed and when the key is released.
106      * @return void
107      * @since 9
108      */
109     void SetFinalKeyDownDuration(int32_t duration);
110 
111     /**
112      * @brief Set the delay time for lifting the last key.
113      * @param delay Delay time for lifting the last key.
114      * @return void
115      * @since 9
116      */
117     void SetFinalKeyUpDelay(int32_t delay);
118 
119 public:
120     /**
121      * @brief Writes data to a <b>Parcel</b> object.
122      * @param out Indicates the object into which data will be written.
123      * @return Returns <b>true</b> if the data is successfully written; returns <b>false</b> otherwise.
124      * @since 9
125      */
126     bool WriteToParcel(Parcel &out) const;
127 
128     /**
129      * @brief Reads data from a <b>Parcel</b> object.
130      * @param in Indicates the object from which data will be read.
131      * @return Returns <b>true</b> if the data is successfully read; returns <b>false</b> otherwise.
132      * @since 9
133      */
134     bool ReadFromParcel(Parcel &in);
135 
136 private:
137     std::set<int32_t> preKeys_ {};
138     int32_t finalKey_ { -1 };
139     bool isFinalKeyDown_ { false };
140     int32_t finalKeyDownDuration_ { 0 };
141     int32_t finalKeyUpDelay_ { 0 };
142 };
143 } // namespace MMI
144 } // namespace OHOS
145 #endif // KEY_OPTION_H
146