1 /*
2 * Copyright (C) 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 "accessibility_window_info_parcel.h"
17 #include "accessibility_element_info_parcel.h"
18 #include "hilog_wrapper.h"
19 #include "parcel_util.h"
20
21 namespace OHOS {
22 namespace Accessibility {
23
24 namespace {
25 constexpr int32_t TOUCH_HOT_AREA_MAX_SIZE = 1024;
26 }
27
AccessibilityWindowInfoParcel(const AccessibilityWindowInfo & accessibilityWindowInfo)28 AccessibilityWindowInfoParcel::AccessibilityWindowInfoParcel(const AccessibilityWindowInfo &accessibilityWindowInfo)
29 : AccessibilityWindowInfo(accessibilityWindowInfo)
30 {
31 }
32
ReadFromParcel(Parcel & parcel)33 bool AccessibilityWindowInfoParcel::ReadFromParcel(Parcel &parcel)
34 {
35 int32_t accessibilityWindowType = TYPE_WINDOW_INVALID;
36 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, accessibilityWindowType);
37 accessibilityWindowType_ = static_cast<AccessibilityWindowType>(accessibilityWindowType);
38 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint32, parcel, windowType_);
39 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint32, parcel, windowMode_);
40 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, windowLayer_);
41 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, windowId_);
42 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, active_);
43 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, focused_);
44 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, accessibilityFocused_);
45 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, isDecorEnable_);
46 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, mainWindowId_);
47 sptr<RectParcel> boundsInScreen = parcel.ReadStrongParcelable<RectParcel>();
48 if (boundsInScreen == nullptr) {
49 HILOG_ERROR("ReadStrongParcelable boundsInScreen failed.");
50 return false;
51 }
52 boundsInScreen_ = *boundsInScreen;
53
54 int32_t touchHotAreasSize = 0;
55 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, touchHotAreasSize);
56 if (touchHotAreasSize > TOUCH_HOT_AREA_MAX_SIZE) {
57 return false;
58 }
59 std::vector<Rect> touchHotAreas {};
60 for (int i = 0; i < touchHotAreasSize; i++) {
61 sptr<RectParcel> hotAreaRectParcel = parcel.ReadStrongParcelable<RectParcel>();
62 if (hotAreaRectParcel == nullptr) {
63 HILOG_ERROR("ReadStrongParcelable hotAreaRect failed.");
64 return false;
65 }
66 touchHotAreas.push_back(*hotAreaRectParcel);
67 }
68 touchHotAreas_ = touchHotAreas;
69 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint64, parcel, displayId_);
70
71 return true;
72 }
73
Marshalling(Parcel & parcel) const74 bool AccessibilityWindowInfoParcel::Marshalling(Parcel &parcel) const
75 {
76 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(accessibilityWindowType_));
77 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint32, parcel, windowType_);
78 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint32, parcel, windowMode_);
79 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, windowLayer_);
80 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, windowId_);
81 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, active_);
82 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, focused_);
83 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, accessibilityFocused_);
84 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, isDecorEnable_);
85 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, mainWindowId_);
86 RectParcel rectParcel(boundsInScreen_);
87 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Parcelable, parcel, &rectParcel);
88 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, touchHotAreas_.size());
89 for (const auto &hotArea : touchHotAreas_) {
90 RectParcel hotAreaRectParcel(hotArea);
91 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Parcelable, parcel, &hotAreaRectParcel);
92 }
93 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint64, parcel, displayId_);
94
95 return true;
96 };
97
Unmarshalling(Parcel & parcel)98 sptr<AccessibilityWindowInfoParcel> AccessibilityWindowInfoParcel::Unmarshalling(Parcel &parcel)
99 {
100 sptr<AccessibilityWindowInfoParcel> info = new(std::nothrow) AccessibilityWindowInfoParcel();
101 if (info == nullptr) {
102 HILOG_ERROR("Failed to create info.");
103 return nullptr;
104 }
105 if (!info->ReadFromParcel(parcel)) {
106 HILOG_ERROR("ReadFromParcel failed.");
107 info = nullptr;
108 return nullptr;
109 }
110 return info;
111 }
112 } // namespace Accessibility
113 } // namespace OHOS