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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_AI_WRITE_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_AI_WRITE_H
18 
19 #include <functional>
20 #include <string>
21 
22 #include "base/memory/ace_type.h"
23 #include "core/common/ace_application_info.h"
24 #include "core/components_ng/base/frame_node.h"
25 #include "core/components_ng/pattern/text_field/text_selector.h"
26 #include "interfaces/inner_api/ace/modal_ui_extension_config.h"
27 
28 namespace OHOS::AAFwk {
29 class Want;
30 class WantParams;
31 } // namespace OHOS::AAFwk
32 
33 namespace OHOS::Ace {
34 struct AIWriteInfo {
35     // The position relative to the beginning and end of all text.
36     int32_t selectStart = 0;
37     int32_t selectEnd = 0;
38     int32_t selectLength = -1;
39     int32_t maxLength = -1;
40     // The beginning and end positions relative to the sentence-level text.
41     int32_t start = 0;
42     int32_t end = 0;
43 
44     std::string componentType;
45     std::string firstHandle;
46     std::string secondHandle;
47     PlatformVersion apiVersion = PlatformVersion::VERSION_TWELVE;
48     std::vector<uint8_t> selectBuffer;
49     std::vector<uint8_t> sentenceBuffer;
50 };
51 
52 class AIWriteAdapter : public AceType {
53     DECLARE_ACE_TYPE(AIWriteAdapter, AceType);
54 
55 public:
56     AIWriteAdapter() = default;
57     ~AIWriteAdapter() override = default;
58 
59     bool IsSentenceBoundary(const wchar_t value);
60     void CloseModalUIExtension();
61     void ShowModalUIExtension(const AIWriteInfo& info,
62         std::function<void(std::vector<uint8_t>&)> resultCallback);
63     std::vector<uint8_t> GetBufferParam(const std::string& key, const AAFwk::WantParams& wantParams);
64     bool GetBoolParam(const std::string& key, const AAFwk::WantParams& wantParams);
65     uint32_t GetSelectLengthOnlyText(const std::wstring& content);
SetPipelineContext(const WeakPtr<NG::PipelineContext> & pipelineContext)66     void SetPipelineContext(const WeakPtr<NG::PipelineContext>& pipelineContext)
67     {
68         pipelineContext_ = pipelineContext;
69     }
GetPipelineContext()70     WeakPtr<NG::PipelineContext> GetPipelineContext() const
71     {
72         return pipelineContext_;
73     }
SetBundleName(const std::string & bundleName)74     void SetBundleName(const std::string& bundleName)
75     {
76         bundleName_ = bundleName;
77     }
SetAbilityName(const std::string & abilityName)78     void SetAbilityName(const std::string& abilityName)
79     {
80         abilityName_ = abilityName;
81     }
GetBundleName()82     const std::string& GetBundleName() const
83     {
84         return bundleName_;
85     }
GetAbilityName()86     const std::string& GetAbilityName() const
87     {
88         return abilityName_;
89     }
SetAIWrite(const bool clickAIWrite)90     void SetAIWrite(const bool clickAIWrite)
91     {
92         clickAIWrite_ = clickAIWrite;
93     }
GetAIWrite()94     bool GetAIWrite()
95     {
96         return clickAIWrite_;
97     }
98 private:
99     void SetWantParams(const AIWriteInfo& info, AAFwk::Want& want);
100     void SendData();
101     void SetArrayParam(AAFwk::WantParams& wantParams, const std::string& key, const std::vector<uint8_t>& value);
102     void BindModalUIExtensionCallback(
103         Ace::ModalUIExtensionCallbacks& callbacks, std::function<void(std::vector<uint8_t>&)> resultCallback);
SetModalUIExtensionProxy(const std::shared_ptr<Ace::ModalUIExtensionProxy> & proxy)104     void SetModalUIExtensionProxy(const std::shared_ptr<Ace::ModalUIExtensionProxy>& proxy)
105     {
106         modalUIExtensionProxy_ = proxy;
107     }
GetModalUIExtensionProxy()108     std::shared_ptr<Ace::ModalUIExtensionProxy> GetModalUIExtensionProxy()
109     {
110         return modalUIExtensionProxy_;
111     }
112     int32_t sessionId_ = -1;
113     std::string bundleName_;
114     std::string abilityName_;
115     WeakPtr<NG::PipelineContext> pipelineContext_;
116     bool clickAIWrite_ = false;
117     AIWriteInfo aiWriteInfo_;
118     std::shared_ptr<Ace::ModalUIExtensionProxy> modalUIExtensionProxy_;
119 };
120 } // namespace OHOS::Ace
121 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_AI_WRITE_H