/* * Copyright (C) 2021 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 SERVICES_INCLUDE_INPUT_METHOD_SYSTEM_ABILITY_H #define SERVICES_INCLUDE_INPUT_METHOD_SYSTEM_ABILITY_H #include "ability_manager_interface.h" #include "identity_checker_impl.h" #include "ime_info_inquirer.h" #include "input_method_system_ability_stub.h" #include "inputmethod_dump.h" #include "inputmethod_trace.h" #include "system_ability.h" #include "input_method_types.h" #include "user_session_manager.h" #include "input_type_manager.h" namespace OHOS { namespace MiscServices { enum class ServiceRunningState { STATE_NOT_START, STATE_RUNNING }; class InputMethodSystemAbility : public SystemAbility, public InputMethodSystemAbilityStub { DECLARE_SYSTEM_ABILITY(InputMethodSystemAbility); public: DISALLOW_COPY_AND_MOVE(InputMethodSystemAbility); InputMethodSystemAbility(int32_t systemAbilityId, bool runOnCreate); InputMethodSystemAbility(); ~InputMethodSystemAbility(); int32_t OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; ErrCode StartInput(const InputClientInfoInner &inputClientInfoInner, std::vector> &agents, std::vector &imeInfos) override; ErrCode ShowCurrentInput(uint32_t type = static_cast(ClientType::INNER_KIT)) override; ErrCode HideCurrentInput() override; ErrCode ShowInput(const sptr& client, uint32_t type = static_cast(ClientType::INNER_KIT), int32_t requestKeyboardReason = 0) override; ErrCode HideInput(const sptr& client) override; ErrCode StopInputSession() override; ErrCode ReleaseInput(const sptr& client, uint32_t sessionId) override; ErrCode RequestShowInput() override; ErrCode RequestHideInput(bool isFocusTriggered) override; ErrCode GetDefaultInputMethod(Property &prop, bool isBrief) override; ErrCode GetInputMethodConfig(ElementName &inputMethodConfig) override; ErrCode GetCurrentInputMethod(Property& resultValue) override; ErrCode GetCurrentInputMethodSubtype(SubProperty& resultValue) override; ErrCode ListInputMethod(uint32_t status, std::vector &props) override; ErrCode ListCurrentInputMethodSubtype(std::vector &subProps) override; ErrCode ListInputMethodSubtype(const std::string &bundleName, std::vector &subProps) override; ErrCode SwitchInputMethod( const std::string &bundleName, const std::string &subName, uint32_t trigger) override; ErrCode DisplayOptionalInputMethod() override; ErrCode SetCoreAndAgent(const sptr &core, const sptr &agent) override; ErrCode InitConnect() override; ErrCode UnRegisteredProxyIme(int32_t type, const sptr &core) override; ErrCode PanelStatusChange(uint32_t status, const ImeWindowInfo &info) override; ErrCode UpdateListenEventFlag(const InputClientInfoInner &clientInfoInner, uint32_t eventFlag) override; ErrCode SetCallingWindow(uint32_t windowId, const sptr& client) override; ErrCode GetInputStartInfo(bool& isInputStart, uint32_t& callingWndId, int32_t& requestKeyboardReason) override; ErrCode SendPrivateData(const Value &value) override; ErrCode IsCurrentIme(bool& resultValue) override; ErrCode IsInputTypeSupported(int32_t type, bool& resultValue) override; ErrCode IsCurrentImeByPid(int32_t pid, bool& resultValue) override; ErrCode StartInputType(int32_t type) override; ErrCode StartInputTypeAsync(int32_t type) override; ErrCode ExitCurrentInputType() override; ErrCode IsPanelShown(const PanelInfo &panelInfo, bool &isShown) override; ErrCode GetSecurityMode(int32_t &security) override; ErrCode ConnectSystemCmd(const sptr &channel, sptr &agent) override; // Deprecated because of no permission check, kept for compatibility ErrCode HideCurrentInputDeprecated() override; ErrCode ShowCurrentInputDeprecated() override; int Dump(int fd, const std::vector &args) override; void DumpAllMethod(int fd); ErrCode IsDefaultIme() override; ErrCode IsDefaultImeSet(bool& resultValue) override; ErrCode EnableIme(const std::string &bundleName, const std::string &extensionName, int32_t status) override; ErrCode GetInputMethodState(int32_t &status) override; ErrCode IsSystemApp(bool& resultValue) override; ErrCode RegisterProxyIme( uint64_t displayId, const sptr &core, const sptr &agent) override; ErrCode UnregisterProxyIme(uint64_t displayId) override; ErrCode IsRestrictedDefaultImeByDisplay(uint64_t displayId, bool &resultValue) override; ErrCode IsCapacitySupport(int32_t capacity, bool &isSupport) override; ErrCode BindImeMirror(const sptr &core, const sptr &agent) override; ErrCode UnbindImeMirror() override; int32_t GetCallingUserId(); protected: void OnStart() override; void OnStop() override; int32_t OnIdle(const SystemAbilityOnDemandReason &idleReason) override; int32_t OnExtension(const std::string &extension, MessageParcel &data, MessageParcel &reply) override; private: int32_t Init(); void Initialize(); std::thread workThreadHandler; /*!< thread handler of the WorkThread */ void RestartSessionIme(std::shared_ptr &session); std::shared_ptr GetSessionFromMsg(const Message *msg); int32_t PrepareForOperateKeyboard(std::shared_ptr &session); int32_t SwitchByCondition(const Condition &condition, const std::shared_ptr &info); int32_t GetUserId(int32_t uid); uint64_t GetCallingDisplayId(sptr abilityToken = nullptr); std::shared_ptr identityChecker_ = nullptr; int32_t PrepareInput(int32_t userId, InputClientInfo &clientInfo); void WorkThread(); int32_t OnUserStarted(const Message *msg); int32_t OnUserRemoved(const Message *msg); int32_t OnUserStop(const Message *msg); int32_t OnHideKeyboardSelf(const Message *msg); void OnSysMemChanged(); bool IsNeedSwitch(int32_t userId, const std::string &bundleName, const std::string &subName); int32_t CheckEnableAndSwitchPermission(); int32_t CheckSwitchPermission(int32_t userId, const SwitchInfo &switchInfo, SwitchTrigger trigger); bool IsStartInputTypePermitted(int32_t userId); int32_t OnSwitchInputMethod(int32_t userId, const SwitchInfo &switchInfo, SwitchTrigger trigger); int32_t StartSwitch(int32_t userId, const SwitchInfo &switchInfo, const std::shared_ptr &session); int32_t OnStartInputType(int32_t userId, const SwitchInfo &switchInfo, bool isCheckPermission); int32_t HandlePackageEvent(const Message *msg); int32_t HandleUpdateLargeMemoryState(const Message *msg); int32_t OnPackageRemoved(int32_t userId, const std::string &packageName); void OnScreenUnlock(const Message *msg); void OnScreenLock(const Message *msg); int32_t OnDisplayOptionalInputMethod(); static sptr GetAbilityManagerService(); void SubscribeCommonEvent(); int32_t Switch(int32_t userId, const std::string &bundleName, const std::shared_ptr &info); int32_t SwitchExtension(int32_t userId, const std::shared_ptr &info); int32_t SwitchSubType(int32_t userId, const std::shared_ptr &info); int32_t SwitchInputType(int32_t userId, const SwitchInfo &switchInfo); void GetValidSubtype(const std::string &subName, const std::shared_ptr &info); ServiceRunningState state_; void InitServiceHandler(); void UpdateUserInfo(int32_t userId); void HandleWmsConnected(int32_t userId, int32_t screenId); void HandleWmsDisconnected(int32_t userId, int32_t screenId); void HandleScbStarted(int32_t userId, int32_t screenId); void HandleUserSwitched(int32_t userId); void HandleWmsStarted(); void HandleMemStarted(); void HandleDataShareReady(); void HandleOsAccountStarted(); void HandleFocusChanged(bool isFocused, uint64_t displayId, int32_t pid, int32_t uid); void HandleImeCfgCapsState(); void HandlePasteboardStarted(); void StopImeInBackground(); int32_t InitAccountMonitor(); static std::shared_ptr serviceHandler_; int32_t userId_; bool stop_ = false; void InitMonitors(); int32_t InitKeyEventMonitor(); bool InitWmsMonitor(); void InitSystemLanguageMonitor(); bool InitMemMgrMonitor(); void InitWmsConnectionMonitor(); void InitFocusChangedMonitor(); void InitWindowDisplayChangedMonitor(); bool InitPasteboardMonitor(); int32_t SwitchByCombinationKey(uint32_t state); int32_t SwitchMode(); int32_t SwitchLanguage(); int32_t SwitchType(); int32_t GenerateClientInfo(int32_t userId, InputClientInfo &clientInfo); void RegisterSecurityModeObserver(); int32_t CheckInputTypeOption(int32_t userId, InputClientInfo &inputClientInfo); int32_t IsDefaultImeFromTokenId(int32_t userId, uint32_t tokenId); void DealSwitchRequest(); bool IsCurrentIme(int32_t userId, uint32_t tokenId); int32_t StartInputType(int32_t userId, InputType type); // if switch input type need to switch ime, then no need to hide panel first. void NeedHideWhenSwitchInputType(int32_t userId, InputType type, bool &needHide); bool GetDeviceFunctionKeyState(int32_t functionKey, bool &isEnable); bool ModifyImeCfgWithWrongCaps(); void HandleBundleScanFinished(); int32_t StartInputInner(InputClientInfo &inputClientInfo, std::vector> &agents, std::vector &imeInfos); int32_t ShowInputInner(sptr client, int32_t requestKeyboardReason = 0); int32_t ShowCurrentInputInner(); std::pair GetCurrentImeInfoForHiSysEvent(int32_t userId); static InputType GetSecurityInputType(const InputClientInfo &inputClientInfo); int32_t StartSecurityIme(int32_t &userId, InputClientInfo &inputClientInfo); int32_t GetScreenLockIme(int32_t userId, std::string &ime); int32_t GetAlternativeIme(int32_t userId, std::string &ime); #ifdef IMF_ON_DEMAND_START_STOP_SA_ENABLE int64_t GetTickCount(); void ResetDelayUnloadTask(uint32_t code = 0); bool IsImeInUse(); #endif std::mutex checkMutex_; int32_t EnableIme(int32_t userId, const std::string &bundleName, const std::string &extensionName = "", EnabledStatus status = EnabledStatus::BASIC_MODE); void OnCurrentImeStatusChanged(int32_t userId, const std::string &bundleName, EnabledStatus newStatus); void DataShareCallback(const std::string &key); bool IsValidBundleName(const std::string &bundleName); std::string GetRestoreBundleName(MessageParcel &data); int32_t RestoreInputmethod(std::string &bundleName); void IncreaseAttachCount(); void DecreaseAttachCount(); bool IsTmpIme(int32_t userId, uint32_t tokenId); bool IsTmpImeSwitchSubtype(int32_t userId, uint32_t tokenId, const SwitchInfo &switchInfo); class AttachStateGuard { public: explicit AttachStateGuard(InputMethodSystemAbility &ability) : ability_(ability) { ability_.IncreaseAttachCount(); } ~AttachStateGuard() { ability_.DecreaseAttachCount(); } private: InputMethodSystemAbility &ability_; }; std::atomic isBundleScanFinished_ = false; std::atomic isScbEnable_ = false; std::mutex switchImeMutex_; std::atomic switchTaskExecuting_ = false; std::atomic targetSwitchCount_ = 0; }; } // namespace MiscServices } // namespace OHOS #endif // SERVICES_INCLUDE_INPUT_METHOD_SYSTEM_ABILITY_H