• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "identity_checker_impl.h"
20 #include "ime_info_inquirer.h"
21 #include "input_method_system_ability_stub.h"
22 #include "inputmethod_dump.h"
23 #include "inputmethod_trace.h"
24 #include "security_mode_parser.h"
25 #include "system_ability.h"
26 #include "input_method_types.h"
27 #include "user_session_manager.h"
28 
29 namespace OHOS {
30 namespace MiscServices {
31 enum class ServiceRunningState { STATE_NOT_START, STATE_RUNNING };
32 class InputMethodSystemAbility : public SystemAbility, public InputMethodSystemAbilityStub {
33     DECLARE_SYSTEM_ABILITY(InputMethodSystemAbility);
34 
35 public:
36     DISALLOW_COPY_AND_MOVE(InputMethodSystemAbility);
37     InputMethodSystemAbility(int32_t systemAbilityId, bool runOnCreate);
38     InputMethodSystemAbility();
39     ~InputMethodSystemAbility();
40     int32_t OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override;
41 
42     int32_t StartInput(InputClientInfo &inputClientInfo, sptr<IRemoteObject> &agent,
43         std::pair<int64_t, std::string> &imeInfo) override;
44     int32_t ShowCurrentInput(ClientType type = ClientType::INNER_KIT) override;
45     int32_t HideCurrentInput() override;
46     int32_t ShowInput(
47         sptr<IInputClient> client, ClientType type = ClientType::INNER_KIT, int32_t requestKeyboardReason = 0) override;
48     int32_t HideInput(sptr<IInputClient> client) override;
49     int32_t StopInputSession() override;
50     int32_t ReleaseInput(sptr<IInputClient> client) override;
51     int32_t RequestShowInput() override;
52     int32_t RequestHideInput(bool isFocusTriggered) override;
53     int32_t GetDefaultInputMethod(std::shared_ptr<Property> &prop, bool isBrief) override;
54     int32_t GetInputMethodConfig(OHOS::AppExecFwk::ElementName &inputMethodConfig) override;
55     std::shared_ptr<Property> GetCurrentInputMethod() override;
56     std::shared_ptr<SubProperty> GetCurrentInputMethodSubtype() override;
57     int32_t ListInputMethod(InputMethodStatus status, std::vector<Property> &props) override;
58     int32_t ListCurrentInputMethodSubtype(std::vector<SubProperty> &subProps) override;
59     int32_t ListInputMethodSubtype(const std::string &bundleName, std::vector<SubProperty> &subProps) override;
60     int32_t SwitchInputMethod(
61         const std::string &bundleName, const std::string &subName, SwitchTrigger trigger) override;
62     int32_t DisplayOptionalInputMethod() override;
63     int32_t SetCoreAndAgent(const sptr<IInputMethodCore> &core, const sptr<IRemoteObject> &agent) override;
64     int32_t InitConnect() override;
65     int32_t UnRegisteredProxyIme(UnRegisteredType type, const sptr<IInputMethodCore> &core) override;
66     int32_t PanelStatusChange(const InputWindowStatus &status, const ImeWindowInfo &info) override;
67     int32_t UpdateListenEventFlag(InputClientInfo &clientInfo, uint32_t eventFlag) override;
68     int32_t SetCallingWindow(uint32_t windowId, sptr<IInputClient> client) override;
69     int32_t GetInputStartInfo(bool& isInputStart, uint32_t& callingWndId, int32_t& requestKeyboardReason) override;
70 
71     bool IsCurrentIme() override;
72     bool IsInputTypeSupported(InputType type) override;
73     bool IsCurrentImeByPid(int32_t pid) override;
74     int32_t StartInputType(InputType type) override;
75     int32_t ExitCurrentInputType() override;
76     int32_t IsPanelShown(const PanelInfo &panelInfo, bool &isShown) override;
77     int32_t GetSecurityMode(int32_t &security) override;
78     int32_t ConnectSystemCmd(const sptr<IRemoteObject> &channel, sptr<IRemoteObject> &agent) override;
79     // Deprecated because of no permission check, kept for compatibility
80     int32_t HideCurrentInputDeprecated() override;
81     int32_t ShowCurrentInputDeprecated() override;
82     int Dump(int fd, const std::vector<std::u16string> &args) override;
83     void DumpAllMethod(int fd);
84     int32_t IsDefaultIme() override;
85     bool IsDefaultImeSet() override;
86     bool EnableIme(const std::string &bundleName) override;
87     int32_t GetInputMethodState(EnabledStatus &status) override;
88     bool IsSystemApp() override;
89 
90 protected:
91     void OnStart() override;
92     void OnStop() override;
93     int32_t OnIdle(const SystemAbilityOnDemandReason &idleReason) override;
94     int32_t OnExtension(const std::string &extension, MessageParcel &data, MessageParcel &reply) override;
95 
96 private:
97     int32_t Init();
98     void Initialize();
99 
100     std::thread workThreadHandler; /*!< thread handler of the WorkThread */
101     void RestartSessionIme(std::shared_ptr<PerUserSession> &session);
102     std::shared_ptr<PerUserSession> GetSessionFromMsg(const Message *msg);
103     int32_t PrepareForOperateKeyboard(std::shared_ptr<PerUserSession> &session);
104     int32_t SwitchByCondition(const Condition &condition,
105         const std::shared_ptr<ImeInfo> &info);
106     int32_t GetUserId(int32_t uid);
107     int32_t GetCallingUserId();
108     std::shared_ptr<IdentityChecker> identityChecker_ = nullptr;
109     int32_t PrepareInput(int32_t userId, InputClientInfo &clientInfo);
110     void WorkThread();
111     int32_t OnUserStarted(const Message *msg);
112     int32_t OnUserRemoved(const Message *msg);
113     int32_t OnUserStop(const Message *msg);
114     int32_t OnHideKeyboardSelf(const Message *msg);
115     bool IsNeedSwitch(int32_t userId, const std::string &bundleName, const std::string &subName);
116     int32_t CheckEnableAndSwitchPermission();
117     std::string SetSettingValues(const std::string &settingValue, const std::string &bundleName);
118     int32_t CheckSwitchPermission(int32_t userId, const SwitchInfo &switchInfo, SwitchTrigger trigger);
119     bool IsStartInputTypePermitted(int32_t userId);
120     int32_t OnSwitchInputMethod(int32_t userId, const SwitchInfo &switchInfo, SwitchTrigger trigger);
121     int32_t OnStartInputType(int32_t userId, const SwitchInfo &switchInfo, bool isCheckPermission);
122     int32_t HandlePackageEvent(const Message *msg);
123     int32_t OnPackageRemoved(int32_t userId, const std::string &packageName);
124     void OnUserUnlocked(const Message *msg);
125     int32_t OnDisplayOptionalInputMethod();
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 HandleImeCfgCapsState();
146     void StopImeInBackground();
147     int32_t InitAccountMonitor();
148     static std::shared_ptr<AppExecFwk::EventHandler> serviceHandler_;
149     int32_t userId_;
150     bool stop_ = false;
151     void InitMonitors();
152     int32_t InitKeyEventMonitor();
153     bool InitWmsMonitor();
154     void InitSystemLanguageMonitor();
155     bool InitMemMgrMonitor();
156     void InitWmsConnectionMonitor();
157     void InitFocusChangedMonitor();
158     void InitWindowDisplayChangedMonitor();
159     int32_t SwitchByCombinationKey(uint32_t state);
160     int32_t SwitchMode();
161     int32_t SwitchLanguage();
162     int32_t SwitchType();
163     int32_t GenerateClientInfo(int32_t userId, InputClientInfo &clientInfo);
164     void RegisterEnableImeObserver();
165     void RegisterSecurityModeObserver();
166     int32_t CheckInputTypeOption(int32_t userId, InputClientInfo &inputClientInfo);
167     int32_t IsDefaultImeFromTokenId(int32_t userId, uint32_t tokenId);
168     void DealSwitchRequest();
169     void DealSecurityChange();
170     void OnSecurityModeChange();
171     bool IsCurrentIme(int32_t userId);
172     int32_t StartInputType(int32_t userId, InputType type);
173     // if switch input type need to switch ime, then no need to hide panel first.
174     void NeedHideWhenSwitchInputType(int32_t userId, bool &needHide);
175     bool GetDeviceFunctionKeyState(int32_t functionKey, bool &isEnable);
176     bool ModifyImeCfgWithWrongCaps();
177     void HandleBundleScanFinished();
178     int32_t GetInputMethodState(int32_t userId, const std::string &bundleName, EnabledStatus &status);
179     bool IsSecurityMode(int32_t userId, const std::string &bundleName);
180     int32_t GetImeEnablePattern(int32_t userId, const std::string &bundleName, EnabledStatus &status);
181     int32_t StartInputInner(
182         InputClientInfo &inputClientInfo, sptr<IRemoteObject> &agent, std::pair<int64_t, std::string> &imeInfo);
183     int32_t ShowInputInner(sptr<IInputClient> client, int32_t requestKeyboardReason = 0);
184     int32_t ShowCurrentInputInner();
185     std::pair<int64_t, std::string> GetCurrentImeInfoForHiSysEvent(int32_t userId);
186 #ifdef IMF_ON_DEMAND_START_STOP_SA_ENABLE
187     int64_t GetTickCount();
188     void ResetDelayUnloadTask(uint32_t code = 0);
189     bool IsImeInUse();
190 #endif
191     std::mutex checkMutex_;
192     void DatashareCallback(const std::string &key);
193     bool IsValidBundleName(const std::string &bundleName);
194     std::string GetRestoreBundleName(MessageParcel &data);
195     int32_t RestoreInputmethod(std::string &bundleName);
196     std::atomic<bool> enableImeOn_ = false;
197     std::atomic<bool> enableSecurityMode_ = false;
198     std::atomic<bool> isBundleScanFinished_ = false;
199     std::atomic<bool> isScbEnable_ = false;
200     std::mutex switchImeMutex_;
201     std::atomic<bool> switchTaskExecuting_ = false;
202     std::atomic<uint32_t> targetSwitchCount_ = 0;
203 
204     std::mutex modeChangeMutex_;
205     bool isChangeHandling_ = false;
206     bool hasPendingChanges_ = false;
207 };
208 } // namespace MiscServices
209 } // namespace OHOS
210 #endif // SERVICES_INCLUDE_INPUT_METHOD_SYSTEM_ABILITY_H
211