1 /*
2 * Copyright (c) 2022-2024 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 #include "app_usage_event.h"
16
17 #include "hiview_logger.h"
18 #include "usage_event_common.h"
19
20 namespace OHOS {
21 namespace HiviewDFX {
22 DEFINE_LOG_TAG("AppUsageEvent");
23 using namespace AppUsageEventSpace;
24
AppUsageEvent(const std::string & name,HiSysEvent::EventType type)25 AppUsageEvent::AppUsageEvent(const std::string &name, HiSysEvent::EventType type)
26 : LoggerEvent(name, type)
27 {
28 this->paramMap_ = {
29 {KEY_OF_PACKAGE, DEFAULT_STRING}, {KEY_OF_VERSION, DEFAULT_STRING},
30 {KEY_OF_USAGE, DEFAULT_UINT64}, {KEY_OF_DATE, DEFAULT_STRING},
31 {KEY_OF_START_NUM, DEFAULT_UINT32}
32 };
33 }
34
Report()35 void AppUsageEvent::Report()
36 {
37 ReportDFX();
38 ReportDFXUE();
39 }
40
ReportDFX()41 void AppUsageEvent::ReportDFX()
42 {
43 auto ret = HiSysEventWrite(HiSysEvent::Domain::HIVIEWDFX, this->eventName_, this->eventType_,
44 KEY_OF_PACKAGE, this->paramMap_[KEY_OF_PACKAGE].GetString(),
45 KEY_OF_VERSION, this->paramMap_[KEY_OF_VERSION].GetString(),
46 KEY_OF_USAGE, this->paramMap_[KEY_OF_USAGE].GetUint64(),
47 KEY_OF_DATE, this->paramMap_[KEY_OF_DATE].GetString(),
48 KEY_OF_START_NUM, this->paramMap_[KEY_OF_START_NUM].GetUint32());
49 if (ret != 0) {
50 HIVIEW_LOGW("failed to report app usage event, ret=%{public}d", ret);
51 }
52 }
53
ReportDFXUE()54 void AppUsageEvent::ReportDFXUE()
55 {
56 auto ret = HiSysEventWrite(DomainSpace::HIVIEWDFX_UE_DOMAIN, this->eventName_, this->eventType_,
57 KEY_OF_PACKAGE, this->paramMap_[KEY_OF_PACKAGE].GetString(),
58 KEY_OF_VERSION, this->paramMap_[KEY_OF_VERSION].GetString(),
59 KEY_OF_USAGE, this->paramMap_[KEY_OF_USAGE].GetUint64(),
60 KEY_OF_DATE, this->paramMap_[KEY_OF_DATE].GetString(),
61 KEY_OF_START_NUM, this->paramMap_[KEY_OF_START_NUM].GetUint32());
62 if (ret != 0) {
63 HIVIEW_LOGW("failed to report app usage event, ret=%{public}d", ret);
64 }
65 }
66 } // namespace HiviewDFX
67 } // namespace OHOS
68