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 #include "core/common/ai/data_detector_mgr.h"
17
18 #include "core/common/ai/data_detector_default.h"
19
20 namespace OHOS::Ace {
21
GetInstance()22 DataDetectorMgr& DataDetectorMgr::GetInstance()
23 {
24 static DataDetectorMgr instance;
25 return instance;
26 }
27
DataDetectorMgr()28 DataDetectorMgr::DataDetectorMgr()
29 {
30 auto lib = DataDetectorLoader::Load();
31 if (lib == nullptr || (engine_ = lib->CreateDataDetector()) == nullptr) {
32 engine_ = DataDetectorInstance(new DataDetectorDefault, [](DataDetectorInterface* e) {
33 auto* p = reinterpret_cast<DataDetectorDefault*>(e);
34 delete p;
35 });
36 }
37 }
38
IsDataDetectorSupported()39 bool DataDetectorMgr::IsDataDetectorSupported()
40 {
41 if (engine_) {
42 return engine_->IsDataDetectorSupported();
43 }
44 return false;
45 }
46
GetAIEntityMenu(TextDataDetectResult & textDataDetectResult)47 void DataDetectorMgr::GetAIEntityMenu(TextDataDetectResult& textDataDetectResult)
48 {
49 if (engine_) {
50 engine_->GetAIEntityMenu(textDataDetectResult);
51 }
52 }
53
DataDetect(const TextDataDetectInfo & info,const TextDetectResultFunc & resultFunc)54 void DataDetectorMgr::DataDetect(const TextDataDetectInfo& info, const TextDetectResultFunc& resultFunc)
55 {
56 if (!IsDataDetectorSupported()) {
57 TextDataDetectResult result;
58 result.code = UNSUPPORTED_CODE;
59 resultFunc(result);
60 return;
61 }
62 if (engine_) {
63 engine_->DataDetect(info, resultFunc);
64 }
65 }
66
AdjustCursorPosition(int32_t & caretPos,const std::string & content,TimeStamp & lastAiPosTimeStamp,const TimeStamp & lastClickTimeStamp)67 void DataDetectorMgr::AdjustCursorPosition(
68 int32_t& caretPos, const std::string& content, TimeStamp& lastAiPosTimeStamp, const TimeStamp& lastClickTimeStamp)
69 {
70 if (engine_) {
71 int32_t aiPos = GetCursorPosition(content, caretPos);
72 TAG_LOGI(AceLogTag::ACE_TEXTINPUT, "TryGetAiCursorPosition,aiPos is %{public}d", aiPos);
73
74 if (aiPos < 0) {
75 return;
76 }
77 // aiPos should above zero;
78 caretPos = aiPos;
79 // record the ai position time
80 lastAiPosTimeStamp = lastClickTimeStamp;
81 }
82 }
83
AdjustWordSelection(int32_t & caretPos,const std::string & content,int32_t & start,int32_t & end)84 void DataDetectorMgr::AdjustWordSelection(int32_t& caretPos, const std::string& content, int32_t& start, int32_t& end)
85 {
86 if (engine_) {
87 std::vector<int8_t> ret = GetWordSelection(content, caretPos);
88 start = ret[0];
89 end = ret[1];
90
91 TAG_LOGI(AceLogTag::ACE_TEXTINPUT, "get ai selection:%{public}d--%{public}d", start, end);
92 if (start < 0 || end < 0) {
93 return;
94 }
95 caretPos = start;
96 }
97 }
98
GetWordSelection(const std::string & text,int8_t offset)99 std::vector<int8_t> DataDetectorMgr::GetWordSelection(const std::string& text, int8_t offset)
100 {
101 if (engine_) {
102 return engine_->GetWordSelection(text, offset);
103 }
104
105 return std::vector<int8_t> { -1, -1 };
106 }
107
GetCursorPosition(const std::string & text,int8_t offset)108 int8_t DataDetectorMgr::GetCursorPosition(const std::string& text, int8_t offset)
109 {
110 if (engine_) {
111 return engine_->GetCursorPosition(text, offset);
112 }
113
114 return -1;
115 }
116 } // namespace OHOS::Ace
117