1 /*
2 * Copyright (c) 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 "universal_drag_wrapper.h"
17
18 #include <dlfcn.h>
19
20 #include "devicestatus_define.h"
21 #include "fi_log.h"
22
23 #undef LOG_TAG
24 #define LOG_TAG "UniversalDragWrapper"
25
26 namespace OHOS {
27 namespace Msdp {
28 namespace DeviceStatus {
29 namespace {
30 const std::string UNIVERSAL_DRAG_MANAGER_SO_PATH = "system/lib64/libuniversal_drag.z.so";
31 }
32
InitUniversalDrag()33 bool UniversalDragWrapper::InitUniversalDrag()
34 {
35 FI_HILOGI("Enter InitUniversalDrag");
36 universalDragHandle_ = dlopen(UNIVERSAL_DRAG_MANAGER_SO_PATH.c_str(), RTLD_LAZY);
37 char *error = nullptr;
38 if (((error = dlerror()) != nullptr) || (universalDragHandle_ == nullptr)) {
39 FI_HILOGE("Couldn't load universal drag handler library with dlopen(). Error: %{public}s", error);
40 return false;
41 }
42 if (initUniversalDragHandle_ == nullptr) {
43 initUniversalDragHandle_ =
44 reinterpret_cast<InitFunc>(dlsym(universalDragHandle_, "Init"));
45 if ((error = dlerror()) != nullptr) {
46 FI_HILOGE("Symbol InitUniversalDrag error: %{public}s", error);
47 return false;
48 }
49 }
50 if (initUniversalDragHandle_ == nullptr) {
51 FI_HILOGE("initUniversalDragHandle_ is null");
52 return false;
53 }
54 return initUniversalDragHandle_(env_);
55 }
56
RmoveUniversalDrag()57 void UniversalDragWrapper::RmoveUniversalDrag()
58 {
59 FI_HILOGI("Enter RmoveUniversalDrag");
60 if (!universalDragHandle_) {
61 FI_HILOGE("universalDragHandle_ is null");
62 return;
63 }
64 if (removeUniversalDragHandle_ == nullptr) {
65 removeUniversalDragHandle_ =
66 reinterpret_cast<RemoveUniversalDragFunc>(dlsym(universalDragHandle_, "RmoveUniversalDrag"));
67 char *error = nullptr;
68 if ((error = dlerror()) != nullptr) {
69 FI_HILOGE("Symbol RmoveUniversalDrag error: %{public}s", error);
70 return;
71 }
72 }
73 if (removeUniversalDragHandle_ != nullptr) {
74 removeUniversalDragHandle_();
75 FI_HILOGI("RmoveUniversalDrag success");
76 }
77 }
78
SetDragableState(bool state)79 void UniversalDragWrapper::SetDragableState(bool state)
80 {
81 CALL_DEBUG_ENTER;
82 if (!universalDragHandle_) {
83 FI_HILOGE("universalDragHandle_ is null");
84 return;
85 }
86 if (setDragableStateHandle_ == nullptr) {
87 setDragableStateHandle_ =
88 reinterpret_cast<SetDragableStateFunc>(dlsym(universalDragHandle_, "SetDragableState"));
89 char *error = nullptr;
90 if ((error = dlerror()) != nullptr) {
91 FI_HILOGE("Symbol SetDragableStateHandle error: %{public}s", error);
92 return;
93 }
94 }
95 if (setDragableStateHandle_ != nullptr) {
96 setDragableStateHandle_(state);
97 }
98 }
99
GetAppDragSwitchState(const std::string & pkgName,bool & state)100 int32_t UniversalDragWrapper::GetAppDragSwitchState(const std::string &pkgName, bool &state)
101 {
102 CALL_DEBUG_ENTER;
103 if (!universalDragHandle_) {
104 FI_HILOGE("universalDragHandle_ is null");
105 return RET_ERR;
106 }
107 if (getAppDragSwitchStateHandle_ == nullptr) {
108 getAppDragSwitchStateHandle_ =
109 reinterpret_cast<GetAppDragSwitchStateFunc>(dlsym(universalDragHandle_, "GetAppDragSwitchState"));
110 char *error = nullptr;
111 if ((error = dlerror()) != nullptr) {
112 FI_HILOGE("Symbol GetAppDragSwitchStateHandle error: %{public}s", error);
113 return RET_ERR;
114 }
115 }
116
117 if (getAppDragSwitchStateHandle_ == nullptr) {
118 FI_HILOGE("getAppDragSwitchStateHandle_ is null");
119 return RET_ERR;
120 }
121 return getAppDragSwitchStateHandle_(pkgName.c_str(), state);
122 }
123
SetDraggableStateAsync(bool state,int64_t downTime)124 void UniversalDragWrapper::SetDraggableStateAsync(bool state, int64_t downTime)
125 {
126 CALL_DEBUG_ENTER;
127 if (!universalDragHandle_) {
128 FI_HILOGE("universalDragHandle_ is null");
129 return;
130 }
131 if (setDraggableStateAsyncHandle_ == nullptr) {
132 setDraggableStateAsyncHandle_ =
133 reinterpret_cast<SetDraggableStateAsyncFunc>(dlsym(universalDragHandle_, "SetDraggableStateAsync"));
134 char *error = nullptr;
135 if ((error = dlerror()) != nullptr) {
136 FI_HILOGE("Symbol SetDraggableStateAsyncHandle error: %{public}s", error);
137 return;
138 }
139 }
140
141 if (setDraggableStateAsyncHandle_ == nullptr) {
142 FI_HILOGE("setDraggableStateAsyncHandle_ is null");
143 return;
144 }
145 setDraggableStateAsyncHandle_(state, downTime);
146 }
147
~UniversalDragWrapper()148 UniversalDragWrapper::~UniversalDragWrapper()
149 {
150 FI_HILOGI("Destructor success");
151 if (universalDragHandle_ != nullptr) {
152 dlclose(universalDragHandle_);
153 universalDragHandle_ = nullptr;
154 FI_HILOGW("Remove universalDragHandle success");
155 }
156 initUniversalDragHandle_ = nullptr;
157 removeUniversalDragHandle_ = nullptr;
158 setDragableStateHandle_ = nullptr;
159 setDragSwitchStateHandle_ = nullptr;
160 setAppDragSwitchStateHandle_ = nullptr;
161 }
162
SetDragSwitchState(bool enable)163 void UniversalDragWrapper::SetDragSwitchState(bool enable)
164 {
165 CALL_DEBUG_ENTER;
166 if (!universalDragHandle_) {
167 FI_HILOGE("universalDragHandle_ is null");
168 return;
169 }
170 if (setDragSwitchStateHandle_ == nullptr) {
171 setDragSwitchStateHandle_ =
172 reinterpret_cast<SetDragSwitchStateFunc>(dlsym(universalDragHandle_, "SetDragSwitchState"));
173 char *error = nullptr;
174 if ((error = dlerror()) != nullptr) {
175 FI_HILOGE("Symbol setDragSwitchStateHandle error: %{public}s", error);
176 return;
177 }
178 }
179 if (setDragSwitchStateHandle_ != nullptr) {
180 setDragSwitchStateHandle_(enable);
181 }
182 }
183
SetAppDragSwitchState(const std::string & pkgName,bool enable)184 void UniversalDragWrapper::SetAppDragSwitchState(const std::string &pkgName, bool enable)
185 {
186 CALL_DEBUG_ENTER;
187 if (!universalDragHandle_) {
188 FI_HILOGE("universalDragHandle_ is null");
189 return;
190 }
191 if (setAppDragSwitchStateHandle_ == nullptr) {
192 setAppDragSwitchStateHandle_ =
193 reinterpret_cast<SetAppDragSwitchStateFunc>(dlsym(universalDragHandle_, "SetAppDragSwitchState"));
194 char *error = nullptr;
195 if ((error = dlerror()) != nullptr) {
196 FI_HILOGE("Symbol setAppDragSwitchStateHandle error: %{public}s", error);
197 return;
198 }
199 }
200 if (setAppDragSwitchStateHandle_ != nullptr) {
201 setAppDragSwitchStateHandle_(pkgName.c_str(), enable);
202 }
203 }
204
StopLongPressDrag()205 void UniversalDragWrapper::StopLongPressDrag()
206 {
207 CALL_DEBUG_ENTER;
208 if (!universalDragHandle_) {
209 FI_HILOGE("universalDragHandle_ is null");
210 return;
211 }
212 if (StopLongPressDragHandle_ == nullptr) {
213 StopLongPressDragHandle_ =
214 reinterpret_cast<StopLongPressDragFunc>(dlsym(universalDragHandle_, "StopLongPressDrag"));
215 char *error = nullptr;
216 if ((error = dlerror()) != nullptr) {
217 FI_HILOGE("Symbol setAppDragSwitchStateHandle error: %{public}s", error);
218 return;
219 }
220 }
221 if (StopLongPressDragHandle_ != nullptr) {
222 StopLongPressDragHandle_();
223 }
224 }
225 } // namespace DeviceStatus
226 } // namespace Msdp
227 } // namespace OHOS