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 #include "inputmethod_sysevent.h"
17
18 #include <unistd.h>
19
20 #include "common_timer_errors.h"
21 #include "hisysevent.h"
22
23 namespace OHOS {
24 namespace MiscServices {
25 namespace {
26 using HiSysEventNameSpace = OHOS::HiviewDFX::HiSysEvent;
27 } // namespace
28
29 const std::unordered_map<int32_t, std::string> InputMethodSysEvent::operateInfo_ = {
30 { static_cast<int32_t>(OperateIMEInfoCode::IME_SHOW_ATTACH), "Attach: attach, bind and show soft keyboard." },
31 { static_cast<int32_t>(OperateIMEInfoCode::IME_SHOW_ENEDITABLE), "ShowTextInput: enter editable state, show soft "
32 "keyboard." },
33 { static_cast<int32_t>(OperateIMEInfoCode::IME_SHOW_NORMAL), "ShowSoftKeyboard: show soft keyboard." },
34 { static_cast<int32_t>(OperateIMEInfoCode::IME_UNBIND), "Close: unbind." },
35 { static_cast<int32_t>(OperateIMEInfoCode::IME_HIDE_UNBIND), "Close: hide soft keyboard, and unbind." },
36 { static_cast<int32_t>(OperateIMEInfoCode::IME_HIDE_UNEDITABLE), "HideTextInput: hide soft keyboard, quit "
37 "editable state." },
38 { static_cast<int32_t>(OperateIMEInfoCode::IME_HIDE_NORMAL), "HideSoftKeyboard, hide soft keyboard." },
39 { static_cast<int32_t>(OperateIMEInfoCode::IME_HIDE_UNFOCUSED), "OnUnfocused: unfocused, hide soft keyboard." },
40 { static_cast<int32_t>(OperateIMEInfoCode::IME_HIDE_SELF), "HideKeyboardSelf: hide soft keyboard self." }
41 };
42
43 std::map<int32_t, int32_t> InputMethodSysEvent::inputmethodBehaviour_ = {
44 { static_cast<int32_t>(IMEBehaviour::START_IME), 0 }, { static_cast<int32_t>(IMEBehaviour::CHANGE_IME), 0 }
45 };
46
~InputMethodSysEvent()47 InputMethodSysEvent::~InputMethodSysEvent()
48 {
49 StopTimer();
50 }
51
GetInstance()52 InputMethodSysEvent &InputMethodSysEvent::GetInstance()
53 {
54 static InputMethodSysEvent instance;
55 return instance;
56 }
57
ServiceFaultReporter(const std::string & componentName,int32_t errCode)58 void InputMethodSysEvent::ServiceFaultReporter(const std::string &componentName, int32_t errCode)
59 {
60 IMSA_HILOGD("start.");
61 int32_t ret = HiSysEventWrite(HiSysEventNameSpace::Domain::INPUTMETHOD, "SERVICE_INIT_FAILED",
62 HiSysEventNameSpace::EventType::FAULT, "USER_ID", userId_, "COMPONENT_ID", componentName, "ERROR_CODE",
63 errCode);
64 if (ret != HiviewDFX::SUCCESS) {
65 IMSA_HILOGE("hisysevent ServiceFaultReporter failed! ret: %{public}d, errCode: %{public}d", ret, errCode);
66 }
67 }
68
InputmethodFaultReporter(int32_t errCode,const std::string & name,const std::string & info)69 void InputMethodSysEvent::InputmethodFaultReporter(int32_t errCode, const std::string &name, const std::string &info)
70 {
71 IMSA_HILOGD("start.");
72 int32_t ret = HiSysEventWrite(HiSysEventNameSpace::Domain::INPUTMETHOD, "UNAVAILABLE_INPUTMETHOD",
73 HiSysEventNameSpace::EventType::FAULT, "USER_ID", userId_, "APP_NAME", name, "ERROR_CODE", errCode, "INFO",
74 info);
75 if (ret != HiviewDFX::SUCCESS) {
76 IMSA_HILOGE("hisysevent InputmethodFaultReporter failed! ret: %{public}d,errCode %{public}d", ret, errCode);
77 }
78 }
79
ImeUsageBehaviourReporter()80 void InputMethodSysEvent::ImeUsageBehaviourReporter()
81 {
82 IMSA_HILOGD("start.");
83 int ret = HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::INPUTMETHOD, "IME_USAGE",
84 HiSysEventNameSpace::EventType::STATISTIC, "IME_START",
85 inputmethodBehaviour_[static_cast<int32_t>(IMEBehaviour::START_IME)], "IME_CHANGE",
86 inputmethodBehaviour_[static_cast<int32_t>(IMEBehaviour::CHANGE_IME)]);
87 if (ret != HiviewDFX::SUCCESS) {
88 IMSA_HILOGE("hisysevent BehaviourReporter failed! ret: %{public}d", ret);
89 }
90 {
91 std::lock_guard<std::mutex> lock(behaviourMutex_);
92 inputmethodBehaviour_[static_cast<int32_t>(IMEBehaviour::START_IME)] = 0;
93 inputmethodBehaviour_[static_cast<int32_t>(IMEBehaviour::CHANGE_IME)] = 0;
94 }
95 StartTimerForReport();
96 }
97
RecordEvent(IMEBehaviour behaviour)98 void InputMethodSysEvent::RecordEvent(IMEBehaviour behaviour)
99 {
100 IMSA_HILOGD("run in.");
101 std::lock_guard<std::mutex> lock(behaviourMutex_);
102 if (behaviour == IMEBehaviour::START_IME) {
103 ++inputmethodBehaviour_[static_cast<int32_t>(IMEBehaviour::START_IME)];
104 } else if (behaviour == IMEBehaviour::CHANGE_IME) {
105 ++inputmethodBehaviour_[static_cast<int32_t>(IMEBehaviour::CHANGE_IME)];
106 }
107 }
108
OperateSoftkeyboardBehaviour(OperateIMEInfoCode infoCode)109 void InputMethodSysEvent::OperateSoftkeyboardBehaviour(OperateIMEInfoCode infoCode)
110 {
111 IMSA_HILOGD("run in.");
112 int32_t ret = HiSysEventWrite(HiSysEventNameSpace::Domain::INPUTMETHOD, "OPERATE_SOFTKEYBOARD",
113 HiSysEventNameSpace::EventType::BEHAVIOR, "OPERATING", GetOperateAction(static_cast<int32_t>(infoCode)),
114 "OPERATE_INFO", GetOperateInfo(static_cast<int32_t>(infoCode)));
115 if (ret != HiviewDFX::SUCCESS) {
116 IMSA_HILOGE("Hisysevent: operate soft keyboard report failed! ret: %{public}d", ret);
117 }
118 }
119
ReportImeState(ImeState state,pid_t pid,const std::string & bundleName)120 void InputMethodSysEvent::ReportImeState(ImeState state, pid_t pid, const std::string &bundleName)
121 {
122 IMSA_HILOGD("run in.");
123 int32_t ret = HiSysEventWrite(HiSysEventNameSpace::Domain::INPUTMETHOD, "IME_STATE_CHANGED",
124 HiSysEventNameSpace::EventType::BEHAVIOR, "STATE", static_cast<int32_t>(state), "PID", pid, "BUNDLE_NAME",
125 bundleName);
126 if (ret != HiviewDFX::SUCCESS) {
127 IMSA_HILOGE("ime: %{public}s state: %{public}d report failed! ret: %{public}d", bundleName.c_str(),
128 static_cast<int32_t>(state), ret);
129 }
130 }
131
GetOperateInfo(int32_t infoCode)132 const std::string InputMethodSysEvent::GetOperateInfo(int32_t infoCode)
133 {
134 auto iter = operateInfo_.find(static_cast<int32_t>(infoCode));
135 if (iter != operateInfo_.end()) {
136 return iter->second;
137 }
138 return "unknow operating.";
139 }
140
GetOperateAction(int32_t infoCode)141 std::string InputMethodSysEvent::GetOperateAction(int32_t infoCode)
142 {
143 switch (infoCode) {
144 case static_cast<int32_t>(OperateIMEInfoCode::IME_SHOW_ATTACH):
145 case static_cast<int32_t>(OperateIMEInfoCode::IME_SHOW_ENEDITABLE):
146 case static_cast<int32_t>(OperateIMEInfoCode::IME_SHOW_NORMAL):
147 return "show";
148 case static_cast<int32_t>(OperateIMEInfoCode::IME_UNBIND):
149 return "unbind";
150 case static_cast<int32_t>(OperateIMEInfoCode::IME_HIDE_UNBIND):
151 return "hide and unbind";
152 case static_cast<int32_t>(OperateIMEInfoCode::IME_HIDE_UNEDITABLE):
153 case static_cast<int32_t>(OperateIMEInfoCode::IME_HIDE_NORMAL):
154 case static_cast<int32_t>(OperateIMEInfoCode::IME_HIDE_UNFOCUSED):
155 case static_cast<int32_t>(OperateIMEInfoCode::IME_HIDE_SELF):
156 return "hide";
157 default:
158 break;
159 }
160 return "unknow action.";
161 }
162
SetUserId(int32_t userId)163 void InputMethodSysEvent::SetUserId(int32_t userId)
164 {
165 userId_ = userId;
166 }
167
StopTimer()168 void InputMethodSysEvent::StopTimer()
169 {
170 IMSA_HILOGD("start.");
171 std::lock_guard<std::mutex> lock(timerLock_);
172 if (timer_ == nullptr) {
173 IMSA_HILOGE("timer_ is nullptr.");
174 return;
175 }
176 timer_->Unregister(timerId_);
177 timer_->Shutdown();
178 }
179
StartTimer(const TimerCallback & callback,uint32_t interval)180 bool InputMethodSysEvent::StartTimer(const TimerCallback &callback, uint32_t interval)
181 {
182 IMSA_HILOGD("start.");
183 if (timer_ == nullptr) {
184 timer_ = std::make_shared<Utils::Timer>("OS_imfTimer");
185 uint32_t ret = timer_->Setup();
186 if (ret != Utils::TIMER_ERR_OK) {
187 IMSA_HILOGE("create Timer error.");
188 return false;
189 }
190 timerId_ = timer_->Register(callback, interval, true);
191 } else {
192 IMSA_HILOGD("timer_ is not nullptr, Update timer.");
193 timer_->Unregister(timerId_);
194 timerId_ = timer_->Register(callback, interval, false);
195 }
196 return true;
197 }
198
StartTimerForReport()199 bool InputMethodSysEvent::StartTimerForReport()
200 {
201 IMSA_HILOGD("start.");
202 auto reportCallback = [this]() { ImeUsageBehaviourReporter(); };
203 std::lock_guard<std::mutex> lock(timerLock_);
204 return StartTimer(reportCallback, ONE_DAY_IN_HOURS * ONE_HOUR_IN_SECONDS * SECONDS_TO_MILLISECONDS);
205 }
206
ReportSystemShortCut(const std::string & shortcutName)207 void InputMethodSysEvent::ReportSystemShortCut(const std::string &shortcutName)
208 {
209 IMSA_HILOGD("run in.");
210 int32_t ret = HiSysEventWrite(HiSysEventNameSpace::Domain::INPUTMETHOD_UE, "SYSTEM_SHORTCUT",
211 HiSysEventNameSpace::EventType::BEHAVIOR, "PROCESS_NAME", "inputmethod_service", "SHORTCUT_NAME", shortcutName);
212 if (ret != HiviewDFX::SUCCESS) {
213 IMSA_HILOGE("system shortcut: %{public}s report failed! ret: %{public}d", shortcutName.c_str(), ret);
214 }
215 }
216 } // namespace MiscServices
217 } // namespace OHOS