• 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 #include "js_get_input_method_textchange_listener.h"
16 
17 #include "js_get_input_method_controller.h"
18 namespace OHOS {
19 namespace MiscServices {
20 std::mutex JsGetInputMethodTextChangedListener::listenerMutex_;
21 sptr<JsGetInputMethodTextChangedListener> JsGetInputMethodTextChangedListener::inputMethodListener_{ nullptr };
GetTextListener(const std::shared_ptr<AppExecFwk::EventHandler> & handler,bool newEditBox)22 sptr<JsGetInputMethodTextChangedListener> JsGetInputMethodTextChangedListener::GetTextListener(
23     const std::shared_ptr<AppExecFwk::EventHandler> &handler, bool newEditBox)
24 {
25     IMSA_HILOGD("newEditBox is %{public}d.", newEditBox);
26     if (newEditBox) {
27         std::lock_guard<std::mutex> lock(listenerMutex_);
28         inputMethodListener_ = new (std::nothrow) JsGetInputMethodTextChangedListener(handler);
29     } else {
30         if (inputMethodListener_ == nullptr) {
31             std::lock_guard<std::mutex> lock(listenerMutex_);
32             if (inputMethodListener_ == nullptr) {
33                 inputMethodListener_ = new (std::nothrow) JsGetInputMethodTextChangedListener(handler);
34             }
35         }
36     }
37     return inputMethodListener_;
38 }
39 
InsertText(const std::u16string & text)40 void JsGetInputMethodTextChangedListener::InsertText(const std::u16string &text)
41 {
42     auto controller = JsGetInputMethodController::GetInstance();
43     if (controller == nullptr) {
44         IMSA_HILOGE("controller is nullptr!");
45         return;
46     }
47     controller->InsertText(text);
48 }
49 
DeleteForward(int32_t length)50 void JsGetInputMethodTextChangedListener::DeleteForward(int32_t length)
51 {
52     auto controller = JsGetInputMethodController::GetInstance();
53     if (controller == nullptr) {
54         IMSA_HILOGE("controller is nullptr!");
55         return;
56     }
57     controller->DeleteRight(length);
58 }
59 
DeleteBackward(int32_t length)60 void JsGetInputMethodTextChangedListener::DeleteBackward(int32_t length)
61 {
62     auto controller = JsGetInputMethodController::GetInstance();
63     if (controller == nullptr) {
64         IMSA_HILOGE("controller is nullptr!");
65         return;
66     }
67     controller->DeleteLeft(length);
68 }
69 
SendKeyboardStatus(const KeyboardStatus & status)70 void JsGetInputMethodTextChangedListener::SendKeyboardStatus(const KeyboardStatus &status)
71 {
72     auto controller = JsGetInputMethodController::GetInstance();
73     if (controller == nullptr) {
74         IMSA_HILOGE("controller is nullptr!");
75         return;
76     }
77     controller->SendKeyboardStatus(status);
78 }
79 
SendFunctionKey(const FunctionKey & functionKey)80 void JsGetInputMethodTextChangedListener::SendFunctionKey(const FunctionKey &functionKey)
81 {
82     auto controller = JsGetInputMethodController::GetInstance();
83     if (controller == nullptr) {
84         IMSA_HILOGE("controller is nullptr!");
85         return;
86     }
87     controller->SendFunctionKey(functionKey);
88 }
89 
MoveCursor(const Direction direction)90 void JsGetInputMethodTextChangedListener::MoveCursor(const Direction direction)
91 {
92     auto controller = JsGetInputMethodController::GetInstance();
93     if (controller == nullptr) {
94         IMSA_HILOGE("controller is nullptr!");
95         return;
96     }
97     controller->MoveCursor(direction);
98 }
99 
HandleExtendAction(int32_t action)100 void JsGetInputMethodTextChangedListener::HandleExtendAction(int32_t action)
101 {
102     auto controller = JsGetInputMethodController::GetInstance();
103     if (controller == nullptr) {
104         IMSA_HILOGE("controller is nullptr!");
105         return;
106     }
107     controller->HandleExtendAction(action);
108 }
109 
GetLeftTextOfCursor(int32_t number)110 std::u16string JsGetInputMethodTextChangedListener::GetLeftTextOfCursor(int32_t number)
111 {
112     auto controller = JsGetInputMethodController::GetInstance();
113     if (controller == nullptr) {
114         IMSA_HILOGE("controller is nullptr!");
115         return std::u16string();
116     }
117     return controller->GetText("getLeftTextOfCursor", number);
118 }
119 
GetRightTextOfCursor(int32_t number)120 std::u16string JsGetInputMethodTextChangedListener::GetRightTextOfCursor(int32_t number)
121 {
122     auto controller = JsGetInputMethodController::GetInstance();
123     if (controller == nullptr) {
124         IMSA_HILOGE("controller is nullptr!");
125         return std::u16string();
126     }
127     return controller->GetText("getRightTextOfCursor", number);
128 }
129 
GetTextIndexAtCursor()130 int32_t JsGetInputMethodTextChangedListener::GetTextIndexAtCursor()
131 {
132     auto controller = JsGetInputMethodController::GetInstance();
133     if (controller == nullptr) {
134         IMSA_HILOGE("controller is nullptr!");
135         int32_t index = -1;
136         return index;
137     }
138     return controller->GetTextIndexAtCursor();
139 }
140 
ReceivePrivateCommand(const std::unordered_map<std::string,PrivateDataValue> & privateCommand)141 int32_t JsGetInputMethodTextChangedListener::ReceivePrivateCommand(
142     const std::unordered_map<std::string, PrivateDataValue> &privateCommand)
143 {
144     return ErrorCode::NO_ERROR;
145 }
146 
IsFromTs()147 bool JsGetInputMethodTextChangedListener::IsFromTs()
148 {
149     return true;
150 }
151 
SetPreviewText(const std::u16string & text,const Range & range)152 int32_t JsGetInputMethodTextChangedListener::SetPreviewText(const std::u16string &text, const Range &range)
153 {
154     auto controller = JsGetInputMethodController::GetInstance();
155     if (controller == nullptr) {
156         IMSA_HILOGE("controller is nullptr!");
157         return ErrorCode::ERROR_NULL_POINTER;
158     }
159     return controller->SetPreviewText(text, range);
160 }
161 
FinishTextPreview()162 void JsGetInputMethodTextChangedListener::FinishTextPreview()
163 {
164     auto controller = JsGetInputMethodController::GetInstance();
165     if (controller == nullptr) {
166         IMSA_HILOGE("controller is nullptr!");
167         return;
168     }
169     return controller->FinishTextPreview();
170 }
171 
GetEventHandler()172 std::shared_ptr<AppExecFwk::EventHandler> JsGetInputMethodTextChangedListener::GetEventHandler()
173 {
174     std::lock_guard<std::mutex> lock(handlerMutex_);
175     return handler_;
176 }
177 
JsGetInputMethodTextChangedListener(const std::shared_ptr<AppExecFwk::EventHandler> & handler)178 JsGetInputMethodTextChangedListener::JsGetInputMethodTextChangedListener(
179     const std::shared_ptr<AppExecFwk::EventHandler> &handler)
180 {
181     handler_ = handler;
182 }
~JsGetInputMethodTextChangedListener()183 JsGetInputMethodTextChangedListener::~JsGetInputMethodTextChangedListener()
184 {
185 }
186 } // namespace MiscServices
187 } // namespace OHOS