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( 59 sptr<IInputClient> client, int32_t requestKeyboardReason = 0) override; 60 int32_t HideInput(sptr<IInputClient> client) override; 61 int32_t StopInputSession() override; 62 int32_t ReleaseInput(sptr<IInputClient> client) override; 63 int32_t RequestShowInput() override; 64 int32_t RequestHideInput() override; 65 int32_t GetDefaultInputMethod(std::shared_ptr<Property> &prop, bool isBrief) override; 66 int32_t GetInputMethodConfig(OHOS::AppExecFwk::ElementName &inputMethodConfig) override; 67 std::shared_ptr<Property> GetCurrentInputMethod() override; 68 std::shared_ptr<SubProperty> GetCurrentInputMethodSubtype() override; 69 int32_t ListInputMethod(InputMethodStatus status, std::vector<Property> &props) override; 70 int32_t ListCurrentInputMethodSubtype(std::vector<SubProperty> &subProps) override; 71 int32_t ListInputMethodSubtype(const std::string &bundleName, std::vector<SubProperty> &subProps) override; 72 int32_t SwitchInputMethod( 73 const std::string &bundleName, const std::string &subName, SwitchTrigger trigger) override; 74 int32_t DisplayOptionalInputMethod() override; 75 int32_t SetCoreAndAgent(const sptr<IInputMethodCore> &core, const sptr<IRemoteObject> &agent) override; 76 int32_t InitConnect() override; 77 int32_t UnRegisteredProxyIme(UnRegisteredType type, const sptr<IInputMethodCore> &core) override; 78 int32_t PanelStatusChange(const InputWindowStatus &status, const ImeWindowInfo &info) override; 79 int32_t UpdateListenEventFlag(InputClientInfo &clientInfo, uint32_t eventFlag) override; 80 int32_t SetCallingWindow(uint32_t windowId, sptr<IInputClient> client) override; 81 int32_t GetInputStartInfo(bool& isInputStart, uint32_t& callingWndId, int32_t& requestKeyboardReason) override; 82 83 bool IsCurrentIme() override; 84 bool IsInputTypeSupported(InputType type) override; 85 bool IsCurrentImeByPid(int32_t pid) override; 86 int32_t StartInputType(InputType type) override; 87 int32_t ExitCurrentInputType() override; 88 int32_t IsPanelShown(const PanelInfo &panelInfo, bool &isShown) override; 89 int32_t GetSecurityMode(int32_t &security) override; 90 int32_t ConnectSystemCmd(const sptr<IRemoteObject> &channel, sptr<IRemoteObject> &agent) override; 91 // Deprecated because of no permission check, kept for compatibility 92 int32_t HideCurrentInputDeprecated() override; 93 int32_t ShowCurrentInputDeprecated() override; 94 int Dump(int fd, const std::vector<std::u16string> &args) override; 95 void DumpAllMethod(int fd); 96 int32_t IsDefaultIme() override; 97 bool IsDefaultImeSet() override; 98 bool EnableIme(const std::string &bundleName) override; 99 int32_t GetInputMethodState(EnabledStatus &status) override; 100 bool IsSystemApp() override; 101 102 protected: 103 void OnStart() override; 104 void OnStop() override; 105 int32_t OnExtension(const std::string &extension, MessageParcel &data, MessageParcel &reply) override; 106 107 private: 108 int32_t Init(); 109 void Initialize(); 110 111 std::thread workThreadHandler; /*!< thread handler of the WorkThread */ 112 int32_t GetUserId(int32_t uid); 113 int32_t GetCallingUserId(); 114 std::shared_ptr<IdentityChecker> identityChecker_ = nullptr; 115 int32_t PrepareInput(int32_t userId, InputClientInfo &clientInfo); 116 void WorkThread(); 117 int32_t OnUserStarted(const Message *msg); 118 int32_t OnUserRemoved(const Message *msg); 119 int32_t OnUserStop(const Message *msg); 120 int32_t OnHideKeyboardSelf(const Message *msg); 121 bool IsNeedSwitch(int32_t userId, const std::string &bundleName, const std::string &subName); 122 int32_t CheckEnableAndSwitchPermission(); 123 std::string SetSettingValues(const std::string &settingValue, const std::string &bundleName); 124 int32_t CheckSwitchPermission(int32_t userId, const SwitchInfo &switchInfo, SwitchTrigger trigger); 125 bool IsStartInputTypePermitted(int32_t userId); 126 int32_t OnSwitchInputMethod(int32_t userId, const SwitchInfo &switchInfo, SwitchTrigger trigger); 127 int32_t OnStartInputType(int32_t userId, const SwitchInfo &switchInfo, bool isCheckPermission); 128 int32_t HandlePackageEvent(const Message *msg); 129 int32_t OnPackageRemoved(int32_t userId, const std::string &packageName); 130 void OnUserUnlocked(const Message *msg); 131 int32_t OnDisplayOptionalInputMethod(); 132 static sptr<AAFwk::IAbilityManager> GetAbilityManagerService(); 133 void SubscribeCommonEvent(); 134 int32_t Switch(int32_t userId, const std::string &bundleName, const std::shared_ptr<ImeInfo> &info); 135 int32_t SwitchExtension(int32_t userId, const std::shared_ptr<ImeInfo> &info); 136 int32_t SwitchSubType(int32_t userId, const std::shared_ptr<ImeInfo> &info); 137 int32_t SwitchInputType(int32_t userId, const SwitchInfo &switchInfo); 138 void GetValidSubtype(const std::string &subName, const std::shared_ptr<ImeInfo> &info); 139 ServiceRunningState state_; 140 void InitServiceHandler(); 141 void UpdateUserInfo(int32_t userId); 142 void UpdateUserLockState(); 143 void HandleWmsConnected(int32_t userId, int32_t screenId); 144 void HandleWmsDisconnected(int32_t userId, int32_t screenId); 145 void HandleScbStarted(int32_t userId, int32_t screenId); 146 void HandleUserSwitched(int32_t userId); 147 void HandleWmsStarted(); 148 void HandleMemStarted(); 149 void HandleDataShareReady(); 150 void HandleOsAccountStarted(); 151 void HandleFocusChanged(bool isFocused, int32_t pid, int32_t uid); 152 void StopImeInBackground(); 153 int32_t InitAccountMonitor(); 154 static std::shared_ptr<AppExecFwk::EventHandler> serviceHandler_; 155 int32_t userId_; 156 static constexpr const char *SELECT_DIALOG_ACTION = "action.system.inputmethodchoose"; 157 static constexpr const char *SELECT_DIALOG_HAP = "com.ohos.inputmethodchoosedialog"; 158 static constexpr const char *SELECT_DIALOG_ABILITY = "InputMethod"; 159 bool stop_ = false; 160 void InitMonitors(); 161 int32_t InitKeyEventMonitor(); 162 bool InitWmsMonitor(); 163 void InitSystemLanguageMonitor(); 164 bool InitMemMgrMonitor(); 165 void InitWmsConnectionMonitor(); 166 void InitFocusChangedMonitor(); 167 int32_t SwitchByCombinationKey(uint32_t state); 168 int32_t SwitchMode(); 169 int32_t SwitchLanguage(); 170 int32_t SwitchType(); 171 int32_t GenerateClientInfo(int32_t userId, InputClientInfo &clientInfo); 172 void RegisterEnableImeObserver(); 173 void RegisterSecurityModeObserver(); 174 int32_t CheckInputTypeOption(int32_t userId, InputClientInfo &inputClientInfo); 175 int32_t IsDefaultImeFromTokenId(int32_t userId, uint32_t tokenId); 176 void DealSwitchRequest(); 177 void DealSecurityChange(); 178 void OnSecurityModeChange(); 179 bool IsCurrentIme(int32_t userId); 180 int32_t StartInputType(int32_t userId, InputType type); 181 // if switch input type need to switch ime, then no need to hide panel first. 182 void NeedHideWhenSwitchInputType(int32_t userId, bool &needHide); 183 int32_t GetInputMethodState(int32_t userId, const std::string &bundleName, EnabledStatus &status); 184 bool IsSecurityMode(int32_t userId, const std::string &bundleName); 185 int32_t GetImeEnablePattern(int32_t userId, const std::string &bundleName, EnabledStatus &status); 186 std::mutex checkMutex_; 187 void DatashareCallback(const std::string &key); 188 bool IsValidBundleName(const std::string &bundleName); 189 std::string GetRestoreBundleName(MessageParcel &data); 190 int32_t RestoreInputmethod(std::string &bundleName); 191 std::atomic<bool> enableImeOn_ = false; 192 std::atomic<bool> enableSecurityMode_ = false; 193 194 std::atomic<bool> isScbEnable_ = false; 195 std::mutex switchImeMutex_; 196 std::atomic<bool> switchTaskExecuting_ = false; 197 std::atomic<uint32_t> targetSwitchCount_ = 0; 198 199 std::mutex modeChangeMutex_; 200 bool isChangeHandling_ = false; 201 bool hasPendingChanges_ = false; 202 }; 203 } // namespace MiscServices 204 } // namespace OHOS 205 #endif // SERVICES_INCLUDE_INPUT_METHOD_SYSTEM_ABILITY_H 206