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 #ifndef FOUNDATION_ACE_INTERFACE_INNERKITS_DATA_DETECTOR_INTERFACE_H
17 #define FOUNDATION_ACE_INTERFACE_INNERKITS_DATA_DETECTOR_INTERFACE_H
18 
19 #include <functional>
20 #include <string>
21 #include <unordered_map>
22 #include <variant>
23 #include <vector>
24 
25 #include "commonlibrary/c_utils/base/include/refbase.h"
26 
27 namespace OHOS {
28 class IRemoteObject;
29 }
30 
31 namespace OHOS::Ace {
32 constexpr int32_t UNSUPPORTED_CODE = 801;
33 using FuncVariant = std::variant<std::function<std::string()>, std::function<void(sptr<IRemoteObject>, std::string)>,
34     std::function<void(int32_t, std::string)>,
35     std::function<void(int32_t, std::string, std::string, int32_t, std::string)>>;
36 
37 struct TextDataDetectInfo {
38     std::string text;
39     std::string module;
40     bool isWordPosEnabled = false;
41 };
42 
43 struct TextDataDetectResult {
44     int32_t code = UNSUPPORTED_CODE;
45     std::string entity;
46     std::string wordPos;
47     std::unordered_map<std::string, std::vector<std::pair<std::string, FuncVariant>>> menuOptionAndAction;
48 };
49 
50 using TextDetectResultFunc = std::function<void(const TextDataDetectResult)>;
51 
52 class DataDetectorInterface {
53 public:
54     virtual bool IsDataDetectorSupported() = 0;
55     virtual void GetAIEntityMenu(TextDataDetectResult& textDataDetectResult) = 0;
56     virtual void DataDetect(const TextDataDetectInfo& info, const TextDetectResultFunc& resultFunc) = 0;
57 
GetCursorPosition(const std::string & text,int8_t offset)58     virtual int8_t GetCursorPosition(const std::string& text, int8_t offset)
59     {
60         return -1;
61     }
62 
GetWordSelection(const std::string & text,int8_t offset)63     virtual std::vector<int8_t> GetWordSelection(const std::string& text, int8_t offset)
64     {
65         return std::vector<int8_t> { -1, -1 };
66     }
67 
68 protected:
~DataDetectorInterface()69     virtual ~DataDetectorInterface() {}
70 };
71 } // namespace OHOS::Ace
72 #endif // FOUNDATION_ACE_INTERFACE_INNERKITS_DATA_DETECTOR_INTERFACE_H
73