• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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_ABILITYRUNTIME_OHOS_JS_INPUTMETHOD_EXTENSION_H
17 #define FOUNDATION_ABILITYRUNTIME_OHOS_JS_INPUTMETHOD_EXTENSION_H
18 
19 #include "configuration.h"
20 #include "display_manager.h"
21 #include "inputmethod_extension.h"
22 #include "js_runtime.h"
23 
24 namespace OHOS {
25 namespace AbilityRuntime {
26 /**
27  * @brief Basic inputmethod components.
28  */
29 class JsInputMethodExtension : public InputMethodExtension {
30 struct CacheDisplay {
31     int32_t displayWidth = 0;
32     int32_t displayHeight = 0;
33     Rosen::Rotation displayRotation = Rosen::Rotation::ROTATION_0;
34     Rosen::FoldStatus displayFoldStatus = Rosen::FoldStatus::UNKNOWN;
IsEmptyCacheDisplay35     bool IsEmpty()
36     {
37         return displayWidth == 0 && displayHeight == 0 && displayRotation == Rosen::Rotation::ROTATION_0 &&
38             displayFoldStatus == Rosen::FoldStatus::UNKNOWN;
39     };
SetCacheDisplayCacheDisplay40     void SetCacheDisplay(int32_t width, int32_t height, Rosen::Rotation rotation, Rosen::FoldStatus foldStatus)
41     {
42         displayWidth = width;
43         displayHeight = height;
44         displayRotation = rotation;
45         displayFoldStatus = foldStatus;
46     };
47 };
48 
49 public:
50     JsInputMethodExtension(JsRuntime &jsRuntime);
51     virtual ~JsInputMethodExtension() override;
52     static JsInputMethodExtension *jsInputMethodExtension;
53     /**
54      * @brief Create JsInputMethodExtension.
55      *
56      * @param runtime The runtime.
57      * @return The JsInputMethodExtension instance.
58      */
59     static JsInputMethodExtension *Create(const std::unique_ptr<Runtime> &runtime);
60 
61     /**
62      * @brief Init the extension.
63      *
64      * @param record the extension record.
65      * @param application the application info.
66      * @param handler the extension handler.
67      * @param token the remote token.
68      */
69     virtual void Init(const std::shared_ptr<AppExecFwk::AbilityLocalRecord> &record,
70         const std::shared_ptr<AppExecFwk::OHOSApplication> &application,
71         std::shared_ptr<AppExecFwk::AbilityHandler> &handler, const sptr<IRemoteObject> &token) override;
72 
73     /**
74      * @brief Called when this extension is started. You must override this function if you want to perform some
75      *        initialization operations during extension startup.
76      *
77      * This function can be called only once in the entire lifecycle of an extension.
78      * @param Want Indicates the {@link Want} structure containing startup information about the extension.
79      */
80     virtual void OnStart(const AAFwk::Want &want) override;
81 
82     /**
83      * @brief Called when this InputMethod extension is connected for the first time.
84      *
85      * You can override this function to implement your own processing logic.
86      *
87      * @param want Indicates the {@link Want} structure containing connection information about the InputMethod
88      *        extension.
89      *
90      * @return Returns a pointer to the <b>sid</b> of the connected InputMethod extension.
91      */
92     virtual sptr<IRemoteObject> OnConnect(const AAFwk::Want &want) override;
93 
94     /**
95      * @brief Called when all abilities connected to this InputMethod extension are disconnected.
96      *
97      * You can override this function to implement your own processing logic.
98      *
99      */
100     virtual void OnDisconnect(const AAFwk::Want &want) override;
101 
102     /**
103      * @brief Called back when InputMethod is started.
104      * This method can be called only by InputMethod. You can use the StartAbility(ohos.aafwk.content.Want) method
105      *        to start InputMethod. Then the system calls back the current method to use the transferred want parameter
106      *        to execute its own logic.
107      *
108      * @param want Indicates the want of InputMethod to start.
109      * @param restart Indicates the startup mode. The value true indicates that InputMethod is restarted after being
110      *        destroyed, and the value false indicates a normal startup.
111      * @param startId Indicates the number of times the InputMethod extension has been started. The startId is
112      *        incremented.
113      * by 1 every time the extension is started. For example, if the extension has been started for six times, the
114      * value of startId is 6.
115      */
116     virtual void OnCommand(const AAFwk::Want &want, bool restart, int startId) override;
117 
118     /**
119      * @brief Called when this extension enters the <b>STATE_STOP</b> state.
120      *
121      * The extension in the <b>STATE_STOP</b> is being destroyed.
122      * You can override this function to implement your own processing logic.
123      */
124     virtual void OnStop() override;
125 
126     /**
127      * @brief Called when the system configuration is updated.
128      *
129      * @param configuration Indicates the updated configuration information.
130      */
131     virtual void OnConfigurationUpdated(const AppExecFwk::Configuration &config) override;
132 
133     /**
134      * @brief Called when configuration changed, including system configuration and window configuration.
135      *
136      */
137     void ConfigurationUpdated();
138 
139 private:
140     napi_value CallObjectMethod(const char *name, const napi_value *argv = nullptr, size_t argc = 0);
141 
142     void BindContext(napi_env env, napi_value obj);
143 
144     void GetSrcPath(std::string &srcPath);
145 
146     void ListenWindowManager();
147 
148     void InitDisplayCache();
149 
150     JsRuntime &jsRuntime_;
151     std::unique_ptr<NativeReference> jsObj_;
152     std::shared_ptr<NativeReference> shellContextRef_ = nullptr;
153     std::shared_ptr<AbilityHandler> handler_ = nullptr;
154     CacheDisplay cacheDisplay_;
155 
156 protected:
157     class JsInputMethodExtensionDisplayListener : public Rosen::DisplayManager::IDisplayListener {
158     public:
JsInputMethodExtensionDisplayListener(const std::weak_ptr<JsInputMethodExtension> & extension)159         explicit JsInputMethodExtensionDisplayListener(const std::weak_ptr<JsInputMethodExtension> &extension)
160         {
161             jsInputMethodExtension_ = extension;
162         }
163 
OnCreate(Rosen::DisplayId displayId)164         void OnCreate(Rosen::DisplayId displayId) override
165         {
166             auto inputMethodSptr = jsInputMethodExtension_.lock();
167             if (inputMethodSptr != nullptr) {
168                 inputMethodSptr->OnCreate(displayId);
169             }
170         }
171 
OnDestroy(Rosen::DisplayId displayId)172         void OnDestroy(Rosen::DisplayId displayId) override
173         {
174             auto inputMethodSptr = jsInputMethodExtension_.lock();
175             if (inputMethodSptr != nullptr) {
176                 inputMethodSptr->OnDestroy(displayId);
177             }
178         }
179 
OnChange(Rosen::DisplayId displayId)180         void OnChange(Rosen::DisplayId displayId) override
181         {
182             auto inputMethodSptr = jsInputMethodExtension_.lock();
183             if (inputMethodSptr != nullptr) {
184                 inputMethodSptr->CheckNeedAdjustKeyboard(displayId);
185                 inputMethodSptr->OnChange(displayId);
186             }
187         }
188 
189     private:
190         std::weak_ptr<JsInputMethodExtension> jsInputMethodExtension_;
191     };
192 
193     void OnCreate(Rosen::DisplayId displayId);
194     void OnDestroy(Rosen::DisplayId displayId);
195     void OnChange(Rosen::DisplayId displayId);
196     void CheckNeedAdjustKeyboard(Rosen::DisplayId displayId);
197 
198 private:
199     sptr<JsInputMethodExtensionDisplayListener> displayListener_ = nullptr;
200 };
201 } // namespace AbilityRuntime
202 } // namespace OHOS
203 #endif // FOUNDATION_ABILITYRUNTIME_OHOS_JS_INPUTMETHOD_EXTENSION_H