/* * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef FOUNDATION_ABILITYRUNTIME_OHOS_JS_INPUTMETHOD_EXTENSION_H #define FOUNDATION_ABILITYRUNTIME_OHOS_JS_INPUTMETHOD_EXTENSION_H #include "configuration.h" #include "display_manager.h" #include "inputmethod_extension.h" #include "js_runtime.h" #include "system_ability_status_change_stub.h" namespace OHOS { namespace AbilityRuntime { /** * @brief Basic inputmethod components. */ class JsInputMethodExtension : public InputMethodExtension { public: JsInputMethodExtension(JsRuntime &jsRuntime); virtual ~JsInputMethodExtension() override; static JsInputMethodExtension *jsInputMethodExtension; /** * @brief Create JsInputMethodExtension. * * @param runtime The runtime. * @return The JsInputMethodExtension instance. */ static JsInputMethodExtension *Create(const std::unique_ptr &runtime); /** * @brief Init the extension. * * @param record the extension record. * @param application the application info. * @param handler the extension handler. * @param token the remote token. */ virtual void Init(const std::shared_ptr &record, const std::shared_ptr &application, std::shared_ptr &handler, const sptr &token) override; /** * @brief Called when this extension is started. You must override this function if you want to perform some * initialization operations during extension startup. * * This function can be called only once in the entire lifecycle of an extension. * @param Want Indicates the {@link Want} structure containing startup information about the extension. */ virtual void OnStart(const AAFwk::Want &want) override; /** * @brief Called when this InputMethod extension is connected for the first time. * * You can override this function to implement your own processing logic. * * @param want Indicates the {@link Want} structure containing connection information about the InputMethod * extension. * * @return Returns a pointer to the sid of the connected InputMethod extension. */ virtual sptr OnConnect(const AAFwk::Want &want) override; /** * @brief Called when all abilities connected to this InputMethod extension are disconnected. * * You can override this function to implement your own processing logic. * */ virtual void OnDisconnect(const AAFwk::Want &want) override; /** * @brief Called back when InputMethod is started. * This method can be called only by InputMethod. You can use the StartAbility(ohos.aafwk.content.Want) method * to start InputMethod. Then the system calls back the current method to use the transferred want parameter * to execute its own logic. * * @param want Indicates the want of InputMethod to start. * @param restart Indicates the startup mode. The value true indicates that InputMethod is restarted after being * destroyed, and the value false indicates a normal startup. * @param startId Indicates the number of times the InputMethod extension has been started. The startId is * incremented. * by 1 every time the extension is started. For example, if the extension has been started for six times, the * value of startId is 6. */ virtual void OnCommand(const AAFwk::Want &want, bool restart, int startId) override; /** * @brief Called when this extension enters the STATE_STOP state. * * The extension in the STATE_STOP is being destroyed. * You can override this function to implement your own processing logic. */ virtual void OnStop() override; /** * @brief Called when the system configuration is updated. * * @param configuration Indicates the updated configuration information. */ virtual void OnConfigurationUpdated(const AppExecFwk::Configuration& config) override; /** * @brief Called when configuration changed, including system configuration and window configuration. * */ void ConfigurationUpdated(); private: napi_value CallObjectMethod(const char *name, const napi_value *argv = nullptr, size_t argc = 0); void BindContext(napi_env env, napi_value obj); void GetSrcPath(std::string &srcPath); void ListenWindowManager(); JsRuntime &jsRuntime_; std::unique_ptr jsObj_; std::shared_ptr shellContextRef_ = nullptr; std::shared_ptr handler_ = nullptr; protected: class JsInputMethodExtensionDisplayListener : public Rosen::DisplayManager::IDisplayListener { public: explicit JsInputMethodExtensionDisplayListener(const std::weak_ptr &extension) { jsInputMethodExtension_ = extension; } void OnCreate(Rosen::DisplayId displayId) override { auto inputMethodSptr = jsInputMethodExtension_.lock(); if (inputMethodSptr != nullptr) { inputMethodSptr->OnCreate(displayId); } } void OnDestroy(Rosen::DisplayId displayId) override { auto inputMethodSptr = jsInputMethodExtension_.lock(); if (inputMethodSptr != nullptr) { inputMethodSptr->OnDestroy(displayId); } } void OnChange(Rosen::DisplayId displayId) override { auto inputMethodSptr = jsInputMethodExtension_.lock(); if (inputMethodSptr != nullptr) { inputMethodSptr->OnChange(displayId); } } private: std::weak_ptr jsInputMethodExtension_; }; void OnCreate(Rosen::DisplayId displayId); void OnDestroy(Rosen::DisplayId displayId); void OnChange(Rosen::DisplayId displayId); private: class SystemAbilityStatusChangeListener : public OHOS::SystemAbilityStatusChangeStub { public: SystemAbilityStatusChangeListener(sptr displayListener) : listener_(displayListener) {}; virtual void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; virtual void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override {} private: sptr listener_ = nullptr; }; sptr displayListener_ = nullptr; }; } // namespace AbilityRuntime } // namespace OHOS #endif // FOUNDATION_ABILITYRUNTIME_OHOS_JS_INPUTMETHOD_EXTENSION_H