1 /*
2 * Copyright (c) 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
16 #include "app_event_handler.h"
17
18 #include <sstream>
19
20 #include "bundle_mgr_client.h"
21 #include "event_publish.h"
22 #include "logger.h"
23 #include "time_util.h"
24
25 namespace OHOS {
26 namespace HiviewDFX {
27 namespace {
28 DEFINE_LOG_TAG("HiView-AppEventHandler");
29
GetUidByBundleName(const std::string & bundleName)30 int32_t GetUidByBundleName(const std::string& bundleName)
31 {
32 AppExecFwk::BundleInfo info;
33 AppExecFwk::BundleMgrClient client;
34 if (!client.GetBundleInfo(bundleName, AppExecFwk::GET_BUNDLE_DEFAULT, info,
35 AppExecFwk::Constants::ALL_USERID)) {
36 HIVIEW_LOGE("Failed to query uid from bms, bundleName=%{public}s.", bundleName.c_str());
37 } else {
38 HIVIEW_LOGD("bundleName of uid=%{public}d, bundleName=%{public}s", info.uid, bundleName.c_str());
39 }
40 return info.uid;
41 }
42 }
43
PostEvent(const AppLaunchInfo & event)44 int AppEventHandler::PostEvent(const AppLaunchInfo& event)
45 {
46 if (event.bundleName.empty()) {
47 HIVIEW_LOGW("bundleName empty.");
48 return -1;
49 }
50 int32_t uid = GetUidByBundleName(event.bundleName);
51 std::stringstream jsonStr;
52 jsonStr << "{";
53 jsonStr << "\"" << "time" << "\":" << TimeUtil::GetMilliseconds() << ",";
54 jsonStr << "\"" << "bundle_version" << "\":" << "\"" << event.bundleVersion << "\",";
55 jsonStr << "\"" << "bundle_name" << "\":" << "\"" << event.bundleName << "\",";
56 jsonStr << "\"" << "process_name" << "\":" << "\"" << event.processName << "\",";
57 jsonStr << "\"" << "start_type" << "\":" << event.startType << ",";
58 jsonStr << "\"" << "icon_input_time" << "\":" << event.iconInputTime << ",";
59 jsonStr << "\"" << "animation_finish_time" << "\":" << event.animationFinishTime << ",";
60 jsonStr << "\"" << "extend_time" << "\":" << event.extendTime;
61 jsonStr << "}" << std::endl;
62 EventPublish::GetInstance().PushEvent(uid, "APP_LAUNCH", HiSysEvent::EventType::BEHAVIOR, jsonStr.str());
63 return 0;
64 }
65 } // namespace HiviewDFX
66 } // namespace OHOS