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},
45 {static_cast<int32_t>(IMEBehaviour::CHANGE_IME), 0}
46 };
47
~InputMethodSysEvent()48 InputMethodSysEvent::~InputMethodSysEvent()
49 {
50 StopTimer();
51 }
52
GetInstance()53 InputMethodSysEvent &InputMethodSysEvent::GetInstance()
54 {
55 static InputMethodSysEvent instance;
56 return instance;
57 }
58
ServiceFaultReporter(const std::string & componentName,int32_t errCode)59 void InputMethodSysEvent::ServiceFaultReporter(const std::string &componentName, int32_t errCode)
60 {
61 IMSA_HILOGD("run in.");
62 int32_t ret = HiSysEventWrite(HiSysEventNameSpace::Domain::INPUTMETHOD, "SERVICE_INIT_FAILED",
63 HiSysEventNameSpace::EventType::FAULT, "USER_ID", userId_, "COMPONENT_ID", componentName, "ERROR_CODE",
64 errCode);
65 if (ret != HiviewDFX::SUCCESS) {
66 IMSA_HILOGE("hisysevent ServiceFaultReporter failed! ret %{public}d,errCode %{public}d", ret, errCode);
67 }
68 }
69
InputmethodFaultReporter(int32_t errCode,const std::string & name,const std::string & info)70 void InputMethodSysEvent::InputmethodFaultReporter(int32_t errCode, const std::string &name, const std::string &info)
71 {
72 IMSA_HILOGD("run in.");
73 int32_t ret = HiSysEventWrite(HiSysEventNameSpace::Domain::INPUTMETHOD, "INPUTMETHOD_UNAVAILABLE",
74 HiSysEventNameSpace::EventType::FAULT, "USER_ID", userId_, "APP_NAME", name, "ERROR_CODE", errCode, "INFO",
75 info);
76 if (ret != HiviewDFX::SUCCESS) {
77 IMSA_HILOGE("hisysevent InputmethodFaultReporter failed! ret %{public}d,errCode %{public}d", ret, errCode);
78 }
79 }
80
ImeUsageBehaviourReporter()81 void InputMethodSysEvent::ImeUsageBehaviourReporter()
82 {
83 IMSA_HILOGD("run in.");
84 int ret = HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::INPUTMETHOD, "IME_USAGE",
85 HiSysEventNameSpace::EventType::STATISTIC, "IME_START",
86 inputmethodBehaviour_[static_cast<int32_t>(IMEBehaviour::START_IME)], "IME_CHANGE",
87 inputmethodBehaviour_[static_cast<int32_t>(IMEBehaviour::CHANGE_IME)]);
88 if (ret != HiviewDFX::SUCCESS) {
89 IMSA_HILOGE("hisysevent BehaviourReporter failed! ret %{public}d", ret);
90 }
91 {
92 std::lock_guard<std::mutex> lock(behaviourMutex_);
93 inputmethodBehaviour_[static_cast<int32_t>(IMEBehaviour::START_IME)] = 0;
94 inputmethodBehaviour_[static_cast<int32_t>(IMEBehaviour::CHANGE_IME)] = 0;
95 }
96 StartTimerForReport();
97 }
98
RecordEvent(IMEBehaviour behaviour)99 void InputMethodSysEvent::RecordEvent(IMEBehaviour behaviour)
100 {
101 IMSA_HILOGD("run in.");
102 std::lock_guard<std::mutex> lock(behaviourMutex_);
103 if (behaviour == IMEBehaviour::START_IME) {
104 ++inputmethodBehaviour_[static_cast<int32_t>(IMEBehaviour::START_IME)];
105 } else if (behaviour == IMEBehaviour::CHANGE_IME) {
106 ++inputmethodBehaviour_[static_cast<int32_t>(IMEBehaviour::CHANGE_IME)];
107 }
108 }
109
OperateSoftkeyboardBehaviour(OperateIMEInfoCode infoCode)110 void InputMethodSysEvent::OperateSoftkeyboardBehaviour(OperateIMEInfoCode infoCode)
111 {
112 IMSA_HILOGD("run in.");
113 int32_t ret = HiSysEventWrite(HiSysEventNameSpace::Domain::INPUTMETHOD, "OPERATE_SOFTKEYBOARD",
114 HiSysEventNameSpace::EventType::BEHAVIOR, "OPERATING", GetOperateAction(static_cast<int32_t>(infoCode)),
115 "OPERATE_INFO", GetOperateInfo(static_cast<int32_t>(infoCode)));
116 if (ret != HiviewDFX::SUCCESS) {
117 IMSA_HILOGE("Hisysevent: operate soft keyboard report failed! ret %{public}d", ret);
118 }
119 }
120
GetOperateInfo(int32_t infoCode)121 const std::string InputMethodSysEvent::GetOperateInfo(int32_t infoCode)
122 {
123 auto iter = operateInfo_.find(static_cast<int32_t>(infoCode));
124 if (iter != operateInfo_.end()) {
125 return iter->second;
126 }
127 return "unknow operating.";
128 }
129
GetOperateAction(int32_t infoCode)130 std::string InputMethodSysEvent::GetOperateAction(int32_t infoCode)
131 {
132 switch (infoCode) {
133 case static_cast<int32_t>(OperateIMEInfoCode::IME_SHOW_ATTACH):
134 case static_cast<int32_t>(OperateIMEInfoCode::IME_SHOW_ENEDITABLE):
135 case static_cast<int32_t>(OperateIMEInfoCode::IME_SHOW_NORMAL):
136 return "show";
137 case static_cast<int32_t>(OperateIMEInfoCode::IME_UNBIND):
138 return "unbind";
139 case static_cast<int32_t>(OperateIMEInfoCode::IME_HIDE_UNBIND):
140 return "hide and unbind";
141 case static_cast<int32_t>(OperateIMEInfoCode::IME_HIDE_UNEDITABLE):
142 case static_cast<int32_t>(OperateIMEInfoCode::IME_HIDE_NORMAL):
143 case static_cast<int32_t>(OperateIMEInfoCode::IME_HIDE_UNFOCUSED):
144 case static_cast<int32_t>(OperateIMEInfoCode::IME_HIDE_SELF):
145 return "hide";
146 default:
147 break;
148 }
149 return "unknow action.";
150 }
151
SetUserId(int32_t userId)152 void InputMethodSysEvent::SetUserId(int32_t userId)
153 {
154 userId_ = userId;
155 }
156
StopTimer()157 void InputMethodSysEvent::StopTimer()
158 {
159 IMSA_HILOGD("run in");
160 std::lock_guard<std::mutex> lock(timerLock_);
161 if (timer_ == nullptr) {
162 IMSA_HILOGE("timer_ is nullptr.");
163 return;
164 }
165 timer_->Unregister(timerId_);
166 timer_->Shutdown();
167 }
168
StartTimer(const TimerCallback & callback,uint32_t interval)169 bool InputMethodSysEvent::StartTimer(const TimerCallback &callback, uint32_t interval)
170 {
171 IMSA_HILOGD("run in");
172 if (timer_ == nullptr) {
173 timer_ = std::make_shared<Utils::Timer>("imfTimer");
174 uint32_t ret = timer_->Setup();
175 if (ret != Utils::TIMER_ERR_OK) {
176 IMSA_HILOGE("Create Timer error");
177 return false;
178 }
179 timerId_ = timer_->Register(callback, interval, true);
180 } else {
181 IMSA_HILOGD("timer_ is not nullptr, Update timer.");
182 timer_->Unregister(timerId_);
183 timerId_ = timer_->Register(callback, interval, false);
184 }
185 return true;
186 }
187
StartTimerForReport()188 bool InputMethodSysEvent::StartTimerForReport()
189 {
190 IMSA_HILOGD("run in");
191 auto reportCallback = [this]() { ImeUsageBehaviourReporter(); };
192 std::lock_guard<std::mutex> lock(timerLock_);
193 return StartTimer(reportCallback, ONE_DAY_IN_HOURS * ONE_HOUR_IN_SECONDS * SECONDS_TO_MILLISECONDS);
194 }
195 } // namespace MiscServices
196 } // namespace OHOS