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