1 /*
2  * Copyright (c) 2022-2022 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 INPUTMETHOD_SYSEVENT_H
17 #define INPUTMETHOD_SYSEVENT_H
18 
19 #include <map>
20 #include <mutex>
21 #include <string>
22 #include <unordered_map>
23 
24 #include "global.h"
25 #include "timer.h"
26 
27 namespace OHOS {
28 namespace MiscServices {
29 enum class OperateIMEInfoCode : int32_t {
30     IME_SHOW_ATTACH = 0,
31     IME_SHOW_ENEDITABLE,
32     IME_SHOW_NORMAL,
33     IME_UNBIND,
34     IME_HIDE_UNBIND,
35     IME_HIDE_UNEDITABLE,
36     IME_HIDE_NORMAL,
37     IME_HIDE_UNFOCUSED,
38     IME_HIDE_SELF,
39     IME_HIDE_FORCE,
40 };
41 
42 enum class IMEBehaviour : int32_t {
43     START_IME = 0,
44     CHANGE_IME,
45 };
46 
47 enum class ImeState : int32_t { UNBIND = 0, BIND };
48 
49 class InputMethodSysEvent {
50 public:
51     static InputMethodSysEvent &GetInstance();
52     void ServiceFaultReporter(const std::string &componentName, int32_t errCode);
53     void InputmethodFaultReporter(int32_t errCode, const std::string &name, const std::string &info);
54     void RecordEvent(IMEBehaviour behaviour);
55     void OperateSoftkeyboardBehaviour(OperateIMEInfoCode infoCode);
56     void ReportImeState(ImeState state, pid_t pid, const std::string &bundleName);
57     bool StartTimerForReport();
58     void SetUserId(int32_t userId);
59 
60 private:
61     InputMethodSysEvent() = default;
62     ~InputMethodSysEvent();
63     using TimerCallback = std::function<void()>;
64     void ImeUsageBehaviourReporter();
65     const std::string GetOperateInfo(int32_t infoCode);
66     std::string GetOperateAction(int32_t infoCode);
67     bool StartTimer(const TimerCallback &callback, uint32_t interval);
68     void StopTimer();
69 
70 private:
71     static const std::unordered_map<int32_t, std::string> operateInfo_;
72     static std::map<int32_t, int32_t> inputmethodBehaviour_;
73     std::mutex behaviourMutex_;
74 
75     std::shared_ptr<Utils::Timer> timer_ = nullptr;
76     int32_t userId_ = 0;
77     uint32_t timerId_ = 0;
78     std::mutex timerLock_;
79     static inline constexpr int32_t ONE_DAY_IN_HOURS = 24;
80     static inline constexpr int32_t ONE_HOUR_IN_SECONDS = 1 * 60 * 60; // 1 hour
81     static inline constexpr int32_t SECONDS_TO_MILLISECONDS = 1000;
82 };
83 } // namespace MiscServices
84 } // namespace OHOS
85 #endif // INPUTMETHOD_SYSEVENT_H