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