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 "core/common/stylus/stylus_detector_default.h"
17
18 #include "base/utils/string_utils.h"
19 #include "frameworks/base/log/log_wrapper.h"
20 namespace OHOS::Ace {
21
22 namespace {
23 const int32_t MAX_COMMAND_INDEX = 5;
24 const int32_t MAX_PARAMS_SIZE = 3;
25 const int32_t PARAMS_ENABLE_INDEX = 2;
26 } // namespace
GetInstance()27 StylusDetectorDefault* StylusDetectorDefault::GetInstance()
28 {
29 static StylusDetectorDefault instance;
30 return &instance;
31 }
32
IsEnable()33 bool StylusDetectorDefault::IsEnable()
34 {
35 return isEnable_;
36 }
37
RegisterStylusInteractionListener(const std::string & bundleName,const std::shared_ptr<IStylusDetectorCallback> & callback)38 bool StylusDetectorDefault::RegisterStylusInteractionListener(
39 const std::string& bundleName, const std::shared_ptr<IStylusDetectorCallback>& callback)
40 {
41 defaultCallback_ = std::move(callback);
42 return true;
43 }
44
UnRegisterStylusInteractionListener(const std::string & bundleName)45 void StylusDetectorDefault::UnRegisterStylusInteractionListener(const std::string& bundleName)
46 {
47 defaultCallback_ = nullptr;
48 }
49
Notify(const NotifyInfo & notifyInfo)50 bool StylusDetectorDefault::Notify(const NotifyInfo& notifyInfo)
51 {
52 defaultNodeId_ = notifyInfo.componentId;
53 LOGI("Stylus default notify receviecis (%{public}d, %{public}d). TargetNode id is %{public}d", notifyInfo.x,
54 notifyInfo.y, notifyInfo.componentId);
55 return true;
56 }
57
ExecuteCommand(const std::vector<std::string> & params)58 void StylusDetectorDefault::ExecuteCommand(const std::vector<std::string>& params)
59 {
60 if (params.size() > MAX_PARAMS_SIZE || params.size() < PARAMS_ENABLE_INDEX) {
61 return;
62 }
63 bool isEnable = true;
64 auto commandId = StringUtils::StringToInt(params[1]);
65 if (params.size() == MAX_PARAMS_SIZE) {
66 isEnable = static_cast<bool>(StringUtils::StringToInt(params[PARAMS_ENABLE_INDEX]));
67 }
68 if (commandId < 0 || commandId >= MAX_COMMAND_INDEX) {
69 return;
70 }
71 isEnable_ = isEnable;
72 auto command = static_cast<CommandType>(commandId);
73 if (defaultCallback_) {
74 defaultCallback_->OnDetector(command, static_cast<void*>(&defaultText_), nullptr);
75 }
76 }
77 } // namespace OHOS::Ace