122736c2fSopenharmony_ci/*
222736c2fSopenharmony_ci * Copyright (C) 2021 Huawei Device Co., Ltd.
322736c2fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
422736c2fSopenharmony_ci * you may not use this file except in compliance with the License.
522736c2fSopenharmony_ci * You may obtain a copy of the License at
622736c2fSopenharmony_ci *
722736c2fSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
822736c2fSopenharmony_ci *
922736c2fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1022736c2fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1122736c2fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1222736c2fSopenharmony_ci * See the License for the specific language governing permissions and
1322736c2fSopenharmony_ci * limitations under the License.
1422736c2fSopenharmony_ci */
1522736c2fSopenharmony_ci
1622736c2fSopenharmony_ci#ifndef SERVICES_INCLUDE_INPUT_METHOD_SYSTEM_ABILITY_H
1722736c2fSopenharmony_ci#define SERVICES_INCLUDE_INPUT_METHOD_SYSTEM_ABILITY_H
1822736c2fSopenharmony_ci
1922736c2fSopenharmony_ci#include <atomic>
2022736c2fSopenharmony_ci#include <map>
2122736c2fSopenharmony_ci#include <thread>
2222736c2fSopenharmony_ci
2322736c2fSopenharmony_ci#include "application_info.h"
2422736c2fSopenharmony_ci#include "block_queue.h"
2522736c2fSopenharmony_ci#include "bundle_mgr_proxy.h"
2622736c2fSopenharmony_ci#include "element_name.h"
2722736c2fSopenharmony_ci#include "enable_ime_data_parser.h"
2822736c2fSopenharmony_ci#include "event_handler.h"
2922736c2fSopenharmony_ci#include "identity_checker_impl.h"
3022736c2fSopenharmony_ci#include "ime_info_inquirer.h"
3122736c2fSopenharmony_ci#include "input_method_status.h"
3222736c2fSopenharmony_ci#include "input_method_system_ability_stub.h"
3322736c2fSopenharmony_ci#include "inputmethod_dump.h"
3422736c2fSopenharmony_ci#include "inputmethod_trace.h"
3522736c2fSopenharmony_ci#include "message.h"
3622736c2fSopenharmony_ci#include "security_mode_parser.h"
3722736c2fSopenharmony_ci#include "settings_data_utils.h"
3822736c2fSopenharmony_ci#include "system_ability.h"
3922736c2fSopenharmony_ci#include "input_method_types.h"
4022736c2fSopenharmony_ci
4122736c2fSopenharmony_cinamespace OHOS {
4222736c2fSopenharmony_cinamespace MiscServices {
4322736c2fSopenharmony_cienum class ServiceRunningState { STATE_NOT_START, STATE_RUNNING };
4422736c2fSopenharmony_ciclass InputMethodSystemAbility : public SystemAbility, public InputMethodSystemAbilityStub {
4522736c2fSopenharmony_ci    DECLARE_SYSTEM_ABILITY(InputMethodSystemAbility);
4622736c2fSopenharmony_ci
4722736c2fSopenharmony_cipublic:
4822736c2fSopenharmony_ci    DISALLOW_COPY_AND_MOVE(InputMethodSystemAbility);
4922736c2fSopenharmony_ci    InputMethodSystemAbility(int32_t systemAbilityId, bool runOnCreate);
5022736c2fSopenharmony_ci    InputMethodSystemAbility();
5122736c2fSopenharmony_ci    ~InputMethodSystemAbility();
5222736c2fSopenharmony_ci
5322736c2fSopenharmony_ci    int32_t StartInput(InputClientInfo &inputClientInfo, sptr<IRemoteObject> &agent) override;
5422736c2fSopenharmony_ci    int32_t ShowCurrentInput() override;
5522736c2fSopenharmony_ci    int32_t HideCurrentInput() override;
5622736c2fSopenharmony_ci    int32_t ShowInput(sptr<IInputClient> client) override;
5722736c2fSopenharmony_ci    int32_t HideInput(sptr<IInputClient> client) override;
5822736c2fSopenharmony_ci    int32_t StopInputSession() override;
5922736c2fSopenharmony_ci    int32_t ReleaseInput(sptr<IInputClient> client) override;
6022736c2fSopenharmony_ci    int32_t RequestShowInput() override;
6122736c2fSopenharmony_ci    int32_t RequestHideInput() override;
6222736c2fSopenharmony_ci    int32_t GetDefaultInputMethod(std::shared_ptr<Property> &prop, bool isBrief) override;
6322736c2fSopenharmony_ci    int32_t GetInputMethodConfig(OHOS::AppExecFwk::ElementName &inputMethodConfig) override;
6422736c2fSopenharmony_ci    std::shared_ptr<Property> GetCurrentInputMethod() override;
6522736c2fSopenharmony_ci    std::shared_ptr<SubProperty> GetCurrentInputMethodSubtype() override;
6622736c2fSopenharmony_ci    int32_t ListInputMethod(InputMethodStatus status, std::vector<Property> &props) override;
6722736c2fSopenharmony_ci    int32_t ListCurrentInputMethodSubtype(std::vector<SubProperty> &subProps) override;
6822736c2fSopenharmony_ci    int32_t ListInputMethodSubtype(const std::string &bundleName, std::vector<SubProperty> &subProps) override;
6922736c2fSopenharmony_ci    int32_t SwitchInputMethod(
7022736c2fSopenharmony_ci        const std::string &bundleName, const std::string &subName, SwitchTrigger trigger) override;
7122736c2fSopenharmony_ci    int32_t DisplayOptionalInputMethod() override;
7222736c2fSopenharmony_ci    int32_t SetCoreAndAgent(const sptr<IInputMethodCore> &core, const sptr<IRemoteObject> &agent) override;
7322736c2fSopenharmony_ci    int32_t InitConnect() override;
7422736c2fSopenharmony_ci    int32_t UnRegisteredProxyIme(UnRegisteredType type, const sptr<IInputMethodCore> &core) override;
7522736c2fSopenharmony_ci    int32_t PanelStatusChange(const InputWindowStatus &status, const ImeWindowInfo &info) override;
7622736c2fSopenharmony_ci    int32_t UpdateListenEventFlag(InputClientInfo &clientInfo, uint32_t eventFlag) override;
7722736c2fSopenharmony_ci    bool IsCurrentIme() override;
7822736c2fSopenharmony_ci    bool IsInputTypeSupported(InputType type) override;
7922736c2fSopenharmony_ci    bool IsCurrentImeByPid(int32_t pid) override;
8022736c2fSopenharmony_ci    int32_t StartInputType(InputType type) override;
8122736c2fSopenharmony_ci    int32_t ExitCurrentInputType() override;
8222736c2fSopenharmony_ci    int32_t IsPanelShown(const PanelInfo &panelInfo, bool &isShown) override;
8322736c2fSopenharmony_ci    int32_t GetSecurityMode(int32_t &security) override;
8422736c2fSopenharmony_ci    int32_t ConnectSystemCmd(const sptr<IRemoteObject> &channel, sptr<IRemoteObject> &agent) override;
8522736c2fSopenharmony_ci    // Deprecated because of no permission check, kept for compatibility
8622736c2fSopenharmony_ci    int32_t HideCurrentInputDeprecated() override;
8722736c2fSopenharmony_ci    int32_t ShowCurrentInputDeprecated() override;
8822736c2fSopenharmony_ci    int Dump(int fd, const std::vector<std::u16string> &args) override;
8922736c2fSopenharmony_ci    void DumpAllMethod(int fd);
9022736c2fSopenharmony_ci    int32_t IsDefaultIme() override;
9122736c2fSopenharmony_ci    bool IsDefaultImeSet() override;
9222736c2fSopenharmony_ci    bool EnableIme(const std::string &bundleName) override;
9322736c2fSopenharmony_ci
9422736c2fSopenharmony_ciprotected:
9522736c2fSopenharmony_ci    void OnStart() override;
9622736c2fSopenharmony_ci    void OnStop() override;
9722736c2fSopenharmony_ci
9822736c2fSopenharmony_ciprivate:
9922736c2fSopenharmony_ci    int32_t Init();
10022736c2fSopenharmony_ci    void Initialize();
10122736c2fSopenharmony_ci
10222736c2fSopenharmony_ci    std::thread workThreadHandler; /*!< thread handler of the WorkThread */
10322736c2fSopenharmony_ci    int32_t GetUserId(int32_t uid);
10422736c2fSopenharmony_ci    int32_t GetCallingUserId();
10522736c2fSopenharmony_ci    std::shared_ptr<IdentityChecker> identityChecker_ = nullptr;
10622736c2fSopenharmony_ci    int32_t PrepareInput(int32_t userId, InputClientInfo &clientInfo);
10722736c2fSopenharmony_ci    void WorkThread();
10822736c2fSopenharmony_ci    int32_t OnUserStarted(const Message *msg);
10922736c2fSopenharmony_ci    int32_t OnUserRemoved(const Message *msg);
11022736c2fSopenharmony_ci    int32_t OnUserStop(const Message *msg);
11122736c2fSopenharmony_ci    int32_t OnHideKeyboardSelf(const Message *msg);
11222736c2fSopenharmony_ci    bool IsNeedSwitch(int32_t userId, const std::string &bundleName, const std::string &subName);
11322736c2fSopenharmony_ci    int32_t CheckEnableAndSwitchPermission();
11422736c2fSopenharmony_ci    std::string SetSettingValues(const std::string &settingValue, const std::string &bundleName);
11522736c2fSopenharmony_ci    int32_t CheckSwitchPermission(int32_t userId, const SwitchInfo &switchInfo, SwitchTrigger trigger);
11622736c2fSopenharmony_ci    bool IsStartInputTypePermitted(int32_t userId);
11722736c2fSopenharmony_ci    int32_t OnSwitchInputMethod(int32_t userId, const SwitchInfo &switchInfo, SwitchTrigger trigger);
11822736c2fSopenharmony_ci    int32_t OnStartInputType(int32_t userId, const SwitchInfo &switchInfo, bool isCheckPermission);
11922736c2fSopenharmony_ci    int32_t HandlePackageEvent(const Message *msg);
12022736c2fSopenharmony_ci    int32_t OnPackageRemoved(int32_t userId, const std::string &packageName);
12122736c2fSopenharmony_ci    int32_t OnDisplayOptionalInputMethod();
12222736c2fSopenharmony_ci    void SubscribeCommonEvent();
12322736c2fSopenharmony_ci    int32_t Switch(int32_t userId, const std::string &bundleName, const std::shared_ptr<ImeInfo> &info);
12422736c2fSopenharmony_ci    int32_t SwitchExtension(int32_t userId, const std::shared_ptr<ImeInfo> &info);
12522736c2fSopenharmony_ci    int32_t SwitchSubType(int32_t userId, const std::shared_ptr<ImeInfo> &info);
12622736c2fSopenharmony_ci    int32_t SwitchInputType(int32_t userId, const SwitchInfo &switchInfo);
12722736c2fSopenharmony_ci    void GetValidSubtype(const std::string &subName, const std::shared_ptr<ImeInfo> &info);
12822736c2fSopenharmony_ci    ServiceRunningState state_;
12922736c2fSopenharmony_ci    void InitServiceHandler();
13022736c2fSopenharmony_ci    void UpdateUserInfo(int32_t userId);
13122736c2fSopenharmony_ci    void HandleWmsConnected(int32_t userId, int32_t screenId);
13222736c2fSopenharmony_ci    void HandleWmsDisconnected(int32_t userId, int32_t screenId);
13322736c2fSopenharmony_ci    void HandleScbStarted(int32_t userId, int32_t screenId);
13422736c2fSopenharmony_ci    void HandleUserSwitched(int32_t userId);
13522736c2fSopenharmony_ci    void HandleWmsStarted();
13622736c2fSopenharmony_ci    void HandleMemStarted();
13722736c2fSopenharmony_ci    void HandleOsAccountStarted();
13822736c2fSopenharmony_ci    void HandleFocusChanged(bool isFocused, int32_t pid, int32_t uid);
13922736c2fSopenharmony_ci    void StopImeInBackground();
14022736c2fSopenharmony_ci    int32_t InitAccountMonitor();
14122736c2fSopenharmony_ci    int32_t RegisterDataShareObserver();
14222736c2fSopenharmony_ci    static std::shared_ptr<AppExecFwk::EventHandler> serviceHandler_;
14322736c2fSopenharmony_ci    int32_t userId_;
14422736c2fSopenharmony_ci    static constexpr const char *SELECT_DIALOG_ACTION = "action.system.inputmethodchoose";
14522736c2fSopenharmony_ci    static constexpr const char *SELECT_DIALOG_HAP = "com.ohos.inputmethodchoosedialog";
14622736c2fSopenharmony_ci    static constexpr const char *SELECT_DIALOG_ABILITY = "InputMethod";
14722736c2fSopenharmony_ci    bool stop_ = false;
14822736c2fSopenharmony_ci    void InitMonitors();
14922736c2fSopenharmony_ci    int32_t InitKeyEventMonitor();
15022736c2fSopenharmony_ci    bool InitWmsMonitor();
15122736c2fSopenharmony_ci    void InitSystemLanguageMonitor();
15222736c2fSopenharmony_ci    bool InitMemMgrMonitor();
15322736c2fSopenharmony_ci    void InitWmsConnectionMonitor();
15422736c2fSopenharmony_ci    void InitFocusChangedMonitor();
15522736c2fSopenharmony_ci    int32_t SwitchByCombinationKey(uint32_t state);
15622736c2fSopenharmony_ci    int32_t SwitchMode();
15722736c2fSopenharmony_ci    int32_t SwitchLanguage();
15822736c2fSopenharmony_ci    int32_t SwitchType();
15922736c2fSopenharmony_ci    int32_t GenerateClientInfo(int32_t userId, InputClientInfo &clientInfo);
16022736c2fSopenharmony_ci    void RegisterEnableImeObserver();
16122736c2fSopenharmony_ci    void RegisterSecurityModeObserver();
16222736c2fSopenharmony_ci    int32_t CheckInputTypeOption(int32_t userId, InputClientInfo &inputClientInfo);
16322736c2fSopenharmony_ci    int32_t IsDefaultImeFromTokenId(int32_t userId, uint32_t tokenId);
16422736c2fSopenharmony_ci    void DealSwitchRequest();
16522736c2fSopenharmony_ci    void DealSecurityChange();
16622736c2fSopenharmony_ci    void OnSecurityModeChange();
16722736c2fSopenharmony_ci    bool IsCurrentIme(int32_t userId);
16822736c2fSopenharmony_ci    int32_t StartInputType(int32_t userId, InputType type);
16922736c2fSopenharmony_ci    // if switch input type need to switch ime, then no need to hide panel first.
17022736c2fSopenharmony_ci    void NeedHideWhenSwitchInputType(int32_t userId, bool &needHide);
17122736c2fSopenharmony_ci
17222736c2fSopenharmony_ci    std::mutex checkMutex_;
17322736c2fSopenharmony_ci    void DatashareCallback(const std::string &key);
17422736c2fSopenharmony_ci    std::atomic<bool> enableImeOn_ = false;
17522736c2fSopenharmony_ci    std::atomic<bool> enableSecurityMode_ = false;
17622736c2fSopenharmony_ci
17722736c2fSopenharmony_ci    std::atomic<bool> isScbEnable_ = false;
17822736c2fSopenharmony_ci    std::mutex switchImeMutex_;
17922736c2fSopenharmony_ci    std::atomic<bool> switchTaskExecuting_ = false;
18022736c2fSopenharmony_ci    std::atomic<uint32_t> targetSwitchCount_ = 0;
18122736c2fSopenharmony_ci
18222736c2fSopenharmony_ci    std::mutex modeChangeMutex_;
18322736c2fSopenharmony_ci    bool isChangeHandling_ = false;
18422736c2fSopenharmony_ci    bool hasPendingChanges_ = false;
18522736c2fSopenharmony_ci};
18622736c2fSopenharmony_ci} // namespace MiscServices
18722736c2fSopenharmony_ci} // namespace OHOS
18822736c2fSopenharmony_ci#endif // SERVICES_INCLUDE_INPUT_METHOD_SYSTEM_ABILITY_H
189