• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
DataDetect(const TextDataDetectInfo & info,const TextDetectResultFunc & resultFunc)47 void DataDetectorMgr::DataDetect(const TextDataDetectInfo& info, const TextDetectResultFunc& resultFunc)
48 {
49     if (!IsDataDetectorSupported()) {
50         TextDataDetectResult result;
51         result.code = UNSUPPORTED_CODE;
52         resultFunc(result);
53         return;
54     }
55     if (engine_) {
56         engine_->DataDetect(info, resultFunc);
57     }
58 }
59 
AdjustCursorPosition(int32_t & caretPos,const std::string & content,TimeStamp & lastAiPosTimeStamp,const TimeStamp & lastClickTimeStamp)60 void DataDetectorMgr::AdjustCursorPosition(
61     int32_t& caretPos, const std::string& content, TimeStamp& lastAiPosTimeStamp, const TimeStamp& lastClickTimeStamp)
62 {
63     if (engine_) {
64         int32_t aiPos = GetCursorPosition(content, caretPos);
65         TAG_LOGI(AceLogTag::ACE_TEXTINPUT, "TryGetAiCursorPosition,aiPos is %{public}d", aiPos);
66 
67         if (aiPos < 0) {
68             return;
69         }
70         // aiPos should above zero;
71         caretPos = aiPos;
72         // record the ai position time
73         lastAiPosTimeStamp = lastClickTimeStamp;
74     }
75 }
76 
AdjustWordSelection(int32_t & caretPos,const std::string & content,int32_t & start,int32_t & end)77 void DataDetectorMgr::AdjustWordSelection(int32_t& caretPos, const std::string& content, int32_t& start, int32_t& end)
78 {
79     if (engine_) {
80         std::vector<int8_t> ret = GetWordSelection(content, caretPos);
81         start = ret[0];
82         end = ret[1];
83 
84         TAG_LOGI(AceLogTag::ACE_TEXTINPUT, "get ai selection:%{public}d--%{public}d", start, end);
85         if (start < 0 || end < 0) {
86             return;
87         }
88         caretPos = start;
89     }
90 }
91 
GetWordSelection(const std::string & text,int8_t offset)92 std::vector<int8_t> DataDetectorMgr::GetWordSelection(const std::string& text, int8_t offset)
93 {
94     if (engine_) {
95         return engine_->GetWordSelection(text, offset);
96     }
97 
98     return std::vector<int8_t> { -1, -1 };
99 }
100 
GetCursorPosition(const std::string & text,int8_t offset)101 int8_t DataDetectorMgr::GetCursorPosition(const std::string& text, int8_t offset)
102 {
103     if (engine_) {
104         return engine_->GetCursorPosition(text, offset);
105     }
106 
107     return -1;
108 }
109 } // namespace OHOS::Ace
110