1 /* 2 * Copyright (C) 2021 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 SERVICES_INCLUDE_INPUT_METHOD_SYSTEM_ABILITY_H 17 #define SERVICES_INCLUDE_INPUT_METHOD_SYSTEM_ABILITY_H 18 19 #include <atomic> 20 #include <map> 21 #include <thread> 22 23 #include "ability_manager_interface.h" 24 #include "application_info.h" 25 #include "block_queue.h" 26 #include "bundle_mgr_proxy.h" 27 #include "element_name.h" 28 #include "enable_ime_data_parser.h" 29 #include "event_handler.h" 30 #include "identity_checker_impl.h" 31 #include "ime_info_inquirer.h" 32 #include "input_method_status.h" 33 #include "input_method_system_ability_stub.h" 34 #include "inputmethod_dump.h" 35 #include "inputmethod_trace.h" 36 #include "message.h" 37 #include "security_mode_parser.h" 38 #include "settings_data_utils.h" 39 #include "system_ability.h" 40 #include "input_method_types.h" 41 42 namespace OHOS { 43 namespace MiscServices { 44 enum class ServiceRunningState { STATE_NOT_START, STATE_RUNNING }; 45 46 class InputMethodSystemAbility : public SystemAbility, public InputMethodSystemAbilityStub { 47 DECLARE_SYSTEM_ABILITY(InputMethodSystemAbility); 48 49 public: 50 DISALLOW_COPY_AND_MOVE(InputMethodSystemAbility); 51 InputMethodSystemAbility(int32_t systemAbilityId, bool runOnCreate); 52 InputMethodSystemAbility(); 53 ~InputMethodSystemAbility(); 54 55 int32_t StartInput(InputClientInfo &inputClientInfo, sptr<IRemoteObject> &agent) override; 56 int32_t ShowCurrentInput() override; 57 int32_t HideCurrentInput() override; 58 int32_t ShowInput(sptr<IInputClient> client) override; 59 int32_t HideInput(sptr<IInputClient> client) override; 60 int32_t StopInputSession() override; 61 int32_t ReleaseInput(sptr<IInputClient> client) override; 62 int32_t RequestShowInput() override; 63 int32_t RequestHideInput() override; 64 int32_t GetDefaultInputMethod(std::shared_ptr<Property> &prop, bool isBrief) override; 65 int32_t GetInputMethodConfig(OHOS::AppExecFwk::ElementName &inputMethodConfig) override; 66 std::shared_ptr<Property> GetCurrentInputMethod() override; 67 std::shared_ptr<SubProperty> GetCurrentInputMethodSubtype() override; 68 int32_t ListInputMethod(InputMethodStatus status, std::vector<Property> &props) override; 69 int32_t ListCurrentInputMethodSubtype(std::vector<SubProperty> &subProps) override; 70 int32_t ListInputMethodSubtype(const std::string &bundleName, std::vector<SubProperty> &subProps) override; 71 int32_t SwitchInputMethod( 72 const std::string &bundleName, const std::string &subName, SwitchTrigger trigger) override; 73 int32_t DisplayOptionalInputMethod() override; 74 int32_t SetCoreAndAgent(const sptr<IInputMethodCore> &core, const sptr<IRemoteObject> &agent) override; 75 int32_t InitConnect() override; 76 int32_t UnRegisteredProxyIme(UnRegisteredType type, const sptr<IInputMethodCore> &core) override; 77 int32_t PanelStatusChange(const InputWindowStatus &status, const ImeWindowInfo &info) override; 78 int32_t UpdateListenEventFlag(InputClientInfo &clientInfo, uint32_t eventFlag) override; 79 bool IsCurrentIme() override; 80 bool IsInputTypeSupported(InputType type) override; 81 bool IsCurrentImeByPid(int32_t pid) override; 82 int32_t StartInputType(InputType type) override; 83 int32_t ExitCurrentInputType() override; 84 int32_t IsPanelShown(const PanelInfo &panelInfo, bool &isShown) override; 85 int32_t GetSecurityMode(int32_t &security) override; 86 int32_t ConnectSystemCmd(const sptr<IRemoteObject> &channel, sptr<IRemoteObject> &agent) override; 87 // Deprecated because of no permission check, kept for compatibility 88 int32_t HideCurrentInputDeprecated() override; 89 int32_t ShowCurrentInputDeprecated() override; 90 int Dump(int fd, const std::vector<std::u16string> &args) override; 91 void DumpAllMethod(int fd); 92 int32_t IsDefaultIme() override; 93 bool IsDefaultImeSet() override; 94 bool EnableIme(const std::string &bundleName) override; 95 96 protected: 97 void OnStart() override; 98 void OnStop() override; 99 100 private: 101 int32_t Init(); 102 void Initialize(); 103 104 std::thread workThreadHandler; /*!< thread handler of the WorkThread */ 105 int32_t GetUserId(int32_t uid); 106 int32_t GetCallingUserId(); 107 std::shared_ptr<IdentityChecker> identityChecker_ = nullptr; 108 int32_t PrepareInput(int32_t userId, InputClientInfo &clientInfo); 109 void WorkThread(); 110 int32_t OnUserStarted(const Message *msg); 111 int32_t OnUserRemoved(const Message *msg); 112 int32_t OnUserStop(const Message *msg); 113 int32_t OnHideKeyboardSelf(const Message *msg); 114 bool IsNeedSwitch(int32_t userId, const std::string &bundleName, const std::string &subName); 115 int32_t CheckEnableAndSwitchPermission(); 116 std::string SetSettingValues(const std::string &settingValue, const std::string &bundleName); 117 int32_t CheckSwitchPermission(int32_t userId, const SwitchInfo &switchInfo, SwitchTrigger trigger); 118 bool IsStartInputTypePermitted(int32_t userId); 119 int32_t OnSwitchInputMethod(int32_t userId, const SwitchInfo &switchInfo, SwitchTrigger trigger); 120 int32_t OnStartInputType(int32_t userId, const SwitchInfo &switchInfo, bool isCheckPermission); 121 int32_t HandlePackageEvent(const Message *msg); 122 int32_t OnPackageRemoved(int32_t userId, const std::string &packageName); 123 void OnUserUnlocked(const Message *msg); 124 int32_t OnDisplayOptionalInputMethod(); 125 static sptr<AAFwk::IAbilityManager> GetAbilityManagerService(); 126 void SubscribeCommonEvent(); 127 int32_t Switch(int32_t userId, const std::string &bundleName, const std::shared_ptr<ImeInfo> &info); 128 int32_t SwitchExtension(int32_t userId, const std::shared_ptr<ImeInfo> &info); 129 int32_t SwitchSubType(int32_t userId, const std::shared_ptr<ImeInfo> &info); 130 int32_t SwitchInputType(int32_t userId, const SwitchInfo &switchInfo); 131 void GetValidSubtype(const std::string &subName, const std::shared_ptr<ImeInfo> &info); 132 ServiceRunningState state_; 133 void InitServiceHandler(); 134 void UpdateUserInfo(int32_t userId); 135 void UpdateUserLockState(); 136 void HandleWmsConnected(int32_t userId, int32_t screenId); 137 void HandleWmsDisconnected(int32_t userId, int32_t screenId); 138 void HandleScbStarted(int32_t userId, int32_t screenId); 139 void HandleUserSwitched(int32_t userId); 140 void HandleWmsStarted(); 141 void HandleMemStarted(); 142 void HandleDataShareReady(); 143 void HandleOsAccountStarted(); 144 void HandleFocusChanged(bool isFocused, int32_t pid, int32_t uid); 145 void StopImeInBackground(); 146 int32_t InitAccountMonitor(); 147 static std::shared_ptr<AppExecFwk::EventHandler> serviceHandler_; 148 int32_t userId_; 149 static constexpr const char *SELECT_DIALOG_ACTION = "action.system.inputmethodchoose"; 150 static constexpr const char *SELECT_DIALOG_HAP = "com.ohos.inputmethodchoosedialog"; 151 static constexpr const char *SELECT_DIALOG_ABILITY = "InputMethod"; 152 bool stop_ = false; 153 void InitMonitors(); 154 int32_t InitKeyEventMonitor(); 155 bool InitWmsMonitor(); 156 void InitSystemLanguageMonitor(); 157 bool InitMemMgrMonitor(); 158 void InitWmsConnectionMonitor(); 159 void InitFocusChangedMonitor(); 160 int32_t SwitchByCombinationKey(uint32_t state); 161 int32_t SwitchMode(); 162 int32_t SwitchLanguage(); 163 int32_t SwitchType(); 164 int32_t GenerateClientInfo(int32_t userId, InputClientInfo &clientInfo); 165 void RegisterEnableImeObserver(); 166 void RegisterSecurityModeObserver(); 167 int32_t CheckInputTypeOption(int32_t userId, InputClientInfo &inputClientInfo); 168 int32_t IsDefaultImeFromTokenId(int32_t userId, uint32_t tokenId); 169 void DealSwitchRequest(); 170 void DealSecurityChange(); 171 void OnSecurityModeChange(); 172 bool IsCurrentIme(int32_t userId); 173 int32_t StartInputType(int32_t userId, InputType type); 174 // if switch input type need to switch ime, then no need to hide panel first. 175 void NeedHideWhenSwitchInputType(int32_t userId, bool &needHide); 176 177 std::mutex checkMutex_; 178 void DatashareCallback(const std::string &key); 179 std::atomic<bool> enableImeOn_ = false; 180 std::atomic<bool> enableSecurityMode_ = false; 181 182 std::atomic<bool> isScbEnable_ = false; 183 std::mutex switchImeMutex_; 184 std::atomic<bool> switchTaskExecuting_ = false; 185 std::atomic<uint32_t> targetSwitchCount_ = 0; 186 187 std::mutex modeChangeMutex_; 188 bool isChangeHandling_ = false; 189 bool hasPendingChanges_ = false; 190 }; 191 } // namespace MiscServices 192 } // namespace OHOS 193 #endif // SERVICES_INCLUDE_INPUT_METHOD_SYSTEM_ABILITY_H 194