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 "access_token.h" 24 #include "application_info.h" 25 #include "bundle_mgr_proxy.h" 26 #include "event_handler.h" 27 #include "ime_info_inquirer.h" 28 #include "input_method_status.h" 29 #include "input_method_system_ability_stub.h" 30 #include "inputmethod_dump.h" 31 #include "inputmethod_trace.h" 32 #include "peruser_session.h" 33 #include "system_ability.h" 34 #include "block_queue.h" 35 36 namespace OHOS { 37 namespace MiscServices { 38 using AbilityType = AppExecFwk::ExtensionAbilityType; 39 using namespace AppExecFwk; 40 using namespace Security::AccessToken; 41 enum class ServiceRunningState { STATE_NOT_START, STATE_RUNNING }; 42 43 struct SwitchInfo { 44 std::chrono::system_clock::time_point timestamp{}; 45 std::string bundleName; 46 std::string subName; 47 bool operator==(const SwitchInfo &info) const 48 { 49 return (timestamp == info.timestamp && bundleName == info.bundleName && subName == info.subName); 50 } 51 }; 52 53 class InputMethodSystemAbility : public SystemAbility, public InputMethodSystemAbilityStub { 54 DECLARE_SYSTEM_ABILITY(InputMethodSystemAbility); 55 56 public: 57 DISALLOW_COPY_AND_MOVE(InputMethodSystemAbility); 58 InputMethodSystemAbility(int32_t systemAbilityId, bool runOnCreate); 59 InputMethodSystemAbility(); 60 ~InputMethodSystemAbility(); 61 62 int32_t PrepareInput(InputClientInfo &clientInfo) override; 63 int32_t StartInput(sptr<IInputClient> client, bool isShowKeyboard, bool attachFlag) override; 64 int32_t ShowCurrentInput() override; 65 int32_t HideCurrentInput() override; 66 int32_t StopInput(sptr<IInputClient> client) override; 67 int32_t StopInputSession() override; 68 int32_t ReleaseInput(sptr<IInputClient> client) override; 69 std::shared_ptr<Property> GetCurrentInputMethod() override; 70 std::shared_ptr<SubProperty> GetCurrentInputMethodSubtype() override; 71 int32_t ListInputMethod(InputMethodStatus status, std::vector<Property> &props) override; 72 int32_t ListCurrentInputMethodSubtype(std::vector<SubProperty> &subProps) override; 73 int32_t ListInputMethodSubtype(const std::string &bundleName, std::vector<SubProperty> &subProps) override; 74 int32_t SwitchInputMethod(const std::string &bundleName, const std::string &subName) override; 75 int32_t DisplayOptionalInputMethod() override; 76 int32_t SetCoreAndAgent(sptr<IInputMethodCore> core, sptr<IInputMethodAgent> agent) override; 77 int32_t PanelStatusChange(const InputWindowStatus &status, const InputWindowInfo &windowInfo) override; 78 int32_t UpdateListenEventFlag(InputClientInfo &clientInfo, EventType eventType) override; 79 80 // Deprecated because of no permission check, kept for compatibility 81 int32_t HideCurrentInputDeprecated() override; 82 int32_t ShowCurrentInputDeprecated() override; 83 int32_t DisplayOptionalInputMethodDeprecated() override; 84 int Dump(int fd, const std::vector<std::u16string> &args) override; 85 void DumpAllMethod(int fd); 86 87 protected: 88 void OnStart() override; 89 void OnStop() override; 90 91 private: 92 int32_t Init(); 93 void Initialize(); 94 95 std::thread workThreadHandler; /*!< thread handler of the WorkThread */ 96 std::shared_ptr<PerUserSession> userSession_ = nullptr; 97 void WorkThread(); 98 bool StartInputService(const std::string &imeId); 99 void StopInputService(const std::string &imeId); 100 int32_t OnUserStarted(const Message *msg); 101 int32_t OnUserRemoved(const Message *msg); 102 int32_t OnPackageRemoved(const Message *msg); 103 int32_t OnDisplayOptionalInputMethod(); 104 void StartUserIdListener(); 105 bool IsNeedSwitch(const std::string &bundleName, const std::string &subName); 106 int32_t OnSwitchInputMethod(const SwitchInfo &switchInfo, bool isCheckPermission); 107 int32_t Switch(const std::string &bundleName, const ImeInfo &info); 108 int32_t SwitchExtension(const ImeInfo &info); 109 int32_t SwitchSubType(const ImeInfo &info); 110 ServiceRunningState state_; 111 void InitServiceHandler(); 112 static std::shared_ptr<AppExecFwk::EventHandler> serviceHandler_; 113 int32_t userId_; 114 static constexpr const char *SELECT_DIALOG_ACTION = "action.system.inputmethodchoose"; 115 static constexpr const char *SELECT_DIALOG_HAP = "cn.openharmony.inputmethodchoosedialog"; 116 static constexpr const char *SELECT_DIALOG_ABILITY = "InputMethod"; 117 static constexpr int32_t MAX_WAIT_TIME = 5000; 118 BlockQueue<SwitchInfo> switchQueue_{ MAX_WAIT_TIME }; 119 int32_t InitKeyEventMonitor(); 120 bool InitFocusChangeMonitor(); 121 int32_t SwitchByCombinationKey(uint32_t state); 122 int32_t SwitchMode(); 123 int32_t SwitchLanguage(); 124 int32_t SwitchType(); 125 int32_t GenerateClientInfo(InputClientInfo &clientInfo); 126 bool CheckBrokerTokenID(AccessTokenID tokenId); 127 }; 128 } // namespace MiscServices 129 } // namespace OHOS 130 #endif // SERVICES_INCLUDE_INPUT_METHOD_SYSTEM_ABILITY_H 131