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 };
40 
41 enum class IMEBehaviour : int32_t {
42     START_IME = 0,
43     CHANGE_IME,
44 };
45 
46 enum class ImeState : int32_t { UNBIND = 0, BIND };
47 
48 class InputMethodSysEvent {
49 public:
50     static InputMethodSysEvent &GetInstance();
51     void ServiceFaultReporter(const std::string &componentName, int32_t errCode);
52     void InputmethodFaultReporter(int32_t errCode, const std::string &name, const std::string &info);
53     void RecordEvent(IMEBehaviour behaviour);
54     void OperateSoftkeyboardBehaviour(OperateIMEInfoCode infoCode);
55     void ReportImeState(ImeState state, pid_t pid, const std::string &bundleName);
56     bool StartTimerForReport();
57     void SetUserId(int32_t userId);
58 
59 private:
60     InputMethodSysEvent() = default;
61     ~InputMethodSysEvent();
62     using TimerCallback = std::function<void()>;
63     void ImeUsageBehaviourReporter();
64     const std::string GetOperateInfo(int32_t infoCode);
65     std::string GetOperateAction(int32_t infoCode);
66     bool StartTimer(const TimerCallback &callback, uint32_t interval);
67     void StopTimer();
68 
69 private:
70     static const std::unordered_map<int32_t, std::string> operateInfo_;
71     static std::map<int32_t, int32_t> inputmethodBehaviour_;
72     std::mutex behaviourMutex_;
73 
74     std::shared_ptr<Utils::Timer> timer_ = nullptr;
75     int32_t userId_ = 0;
76     uint32_t timerId_ = 0;
77     std::mutex timerLock_;
78     static inline constexpr int32_t ONE_DAY_IN_HOURS = 24;
79     static inline constexpr int32_t ONE_HOUR_IN_SECONDS = 1 * 60 * 60; // 1 hour
80     static inline constexpr int32_t SECONDS_TO_MILLISECONDS = 1000;
81 };
82 } // namespace MiscServices
83 } // namespace OHOS
84 #endif // INPUTMETHOD_SYSEVENT_H