1 /*
2  * Copyright (c) 2023 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 SECURITY_COMPONENT_INFO_H
17 #define SECURITY_COMPONENT_INFO_H
18 
19 #include <cstdint>
20 #include <functional>
21 
22 namespace OHOS {
23 namespace Security {
24 namespace SecurityComponent {
25 static constexpr int32_t INVALID_SC_ID = -1;
26 using DimensionT = double; // unit is vp
27 static constexpr DimensionT DEFAULT_DIMENSION = 0.0;
28 static constexpr DimensionT MIN_FONT_SIZE_WITHOUT_ICON = 12.0;
29 static constexpr DimensionT MIN_FONT_SIZE_WITH_ICON = 10.0;
30 static constexpr DimensionT MIN_ICON_SIZE = 12.0;
31 static constexpr DimensionT MIN_PADDING_SIZE = 0.0;
32 static constexpr DimensionT MIN_PADDING_WITHOUT_BG = 4.0;
33 static constexpr uint32_t MAX_EXTRA_SIZE = 0x1000;
34 
35 static constexpr int32_t KEY_SPACE = 2050;
36 static constexpr int32_t KEY_ENTER = 2054;
37 static constexpr int32_t KEY_NUMPAD_ENTER = 2119;
38 using OnFirstUseDialogCloseFunc = std::function<void(int32_t)>;
39 
40 struct PaddingSize {
41     DimensionT top = DEFAULT_DIMENSION;
42     DimensionT right = DEFAULT_DIMENSION;
43     DimensionT bottom = DEFAULT_DIMENSION;
44     DimensionT left = DEFAULT_DIMENSION;
45 };
46 
47 enum SecCompType {
48     UNKNOWN_SC_TYPE = 0,
49     LOCATION_COMPONENT,
50     PASTE_COMPONENT,
51     SAVE_COMPONENT,
52     MAX_SC_TYPE
53 };
54 
55 union SecCompColor {
56     struct {
57         uint8_t blue;
58         uint8_t green;
59         uint8_t red;
60         uint8_t alpha;
61     } argb;
62     uint32_t value;
63 };
64 
IsComponentTypeValid(int32_t type)65 inline bool IsComponentTypeValid(int32_t type)
66 {
67     return (type > UNKNOWN_SC_TYPE && type < MAX_SC_TYPE);
68 }
69 
GreatOrEqual(double left,double right)70 inline bool GreatOrEqual(double left, double right)
71 {
72     constexpr double epsilon = -0.001;
73     return (left - right) > epsilon;
74 }
75 
GreatNotEqual(double left,double right)76 inline bool GreatNotEqual(double left, double right)
77 {
78     constexpr double epsilon = 0.001;
79     return (left - right) > epsilon;
80 }
81 
IsEqual(double left,double right)82 inline bool IsEqual(double left, double right)
83 {
84     constexpr double epsilon = 0.001;
85     if (left > right) {
86         return (left - right) < epsilon;
87     } else if (right > left) {
88         return (right - left) < epsilon;
89     } else {
90         return true;
91     }
92 }
93 
94 class SecCompRect {
95 public:
96     SecCompRect() = default;
97     ~SecCompRect() = default;
98 
IsInRect(double x,double y)99     bool IsInRect(double x, double y) const
100     {
101         return (GreatOrEqual(x, x_) && GreatOrEqual((x_ + width_), x) &&
102             GreatOrEqual(y, y_) && GreatOrEqual((y_ + height_), y));
103     };
104 
105     bool operator==(const SecCompRect& other) const
106     {
107         return (IsEqual(x_, other.x_)) && (IsEqual(y_, other.y_)) &&
108             (IsEqual(width_, other.width_)) && (IsEqual(height_, other.height_));
109     }
110 
111     DimensionT x_ = 0.0;
112     DimensionT y_ = 0.0;
113     DimensionT width_ = 0.0;
114     DimensionT height_ = 0.0;
115 };
116 
117 struct ExtraInfo {
118     uint32_t dataSize;
119     uint8_t* data;
120 };
121 
122 enum class ClickEventType : int32_t {
123     UNKNOWN_EVENT_TYPE,
124     POINT_EVENT_TYPE,
125     KEY_EVENT_TYPE
126 };
127 
128 struct SecCompPointEvent {
129     double touchX;
130     double touchY;
131     uint64_t timestamp;
132 };
133 
134 struct SecCompKeyEvent {
135     uint64_t timestamp;
136     int32_t keyCode;
137 };
138 
139 struct SecCompClickEvent {
140     ClickEventType type;
141     union {
142         SecCompPointEvent point;
143         SecCompKeyEvent key;
144     };
145     ExtraInfo extraInfo;
146 };
147 }  // namespace SecurityComponent
148 }  // namespace Security
149 }  // namespace OHOS
150 #endif  // SECURITY_COMPONENT_INFO_H
151