1 /*
2 * Copyright (c) 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 #ifdef BGTASKMGR_ENABLE
17 #include "time_service_client.h"
18
19 #include "bundle_active_app_state_observer.h"
20 #include "bundle_active_report_handler.h"
21 #include "bundle_active_event.h"
22 #include "bundle_active_account_helper.h"
23 #include "bundle_active_continuous_task_observer.h"
24
25 namespace OHOS {
26 namespace DeviceUsageStats {
Init(const std::shared_ptr<BundleActiveReportHandler> & reportHandler)27 void BundleActiveContinuousTaskObserver::Init(const std::shared_ptr<BundleActiveReportHandler>& reportHandler)
28 {
29 if (reportHandler != nullptr) {
30 BUNDLE_ACTIVE_LOGI("report handler is not null, init success");
31 reportHandler_ = reportHandler;
32 }
33 }
34
OnContinuousTaskStart(const std::shared_ptr<OHOS::BackgroundTaskMgr::ContinuousTaskCallbackInfo> & continuousTaskCallbackInfo)35 void BundleActiveContinuousTaskObserver::OnContinuousTaskStart(
36 const std::shared_ptr<OHOS::BackgroundTaskMgr::ContinuousTaskCallbackInfo>& continuousTaskCallbackInfo)
37 {
38 ReportContinuousTaskEvent(continuousTaskCallbackInfo, true);
39 }
40
OnContinuousTaskStop(const std::shared_ptr<OHOS::BackgroundTaskMgr::ContinuousTaskCallbackInfo> & continuousTaskCallbackInfo)41 void BundleActiveContinuousTaskObserver::OnContinuousTaskStop(
42 const std::shared_ptr<OHOS::BackgroundTaskMgr::ContinuousTaskCallbackInfo> &continuousTaskCallbackInfo)
43 {
44 ReportContinuousTaskEvent(continuousTaskCallbackInfo, false);
45 }
46
OnRemoteDied(const wptr<IRemoteObject> & object)47 void BundleActiveContinuousTaskObserver::OnRemoteDied(const wptr<IRemoteObject> &object)
48 {
49 isRemoteDied_.store(true);
50 }
51
GetBundleMgr()52 bool BundleActiveContinuousTaskObserver::GetBundleMgr()
53 {
54 if (!bundleMgr_) {
55 sptr<ISystemAbilityManager> systemAbilityManager =
56 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
57 if (!systemAbilityManager) {
58 BUNDLE_ACTIVE_LOGE("Failed to get system ability mgr.");
59 return false;
60 }
61 sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
62 if (!remoteObject) {
63 BUNDLE_ACTIVE_LOGE("Failed to get bundle manager service.");
64 return false;
65 }
66 bundleMgr_ = iface_cast<IBundleMgr>(remoteObject);
67 if (!bundleMgr_) {
68 BUNDLE_ACTIVE_LOGE("Failed to get system bundle manager services ability, bundleMgr_");
69 return false;
70 }
71 auto object = bundleMgr_->AsObject();
72 if (!object) {
73 BUNDLE_ACTIVE_LOGE("Failed to get system bundle manager services ability, bundleMgr_->AsObject()");
74 return false;
75 }
76 }
77 return true;
78 }
79
ReportContinuousTaskEvent(const std::shared_ptr<OHOS::BackgroundTaskMgr::ContinuousTaskCallbackInfo> & continuousTaskCallbackInfo,const bool isStart)80 void BundleActiveContinuousTaskObserver::ReportContinuousTaskEvent(
81 const std::shared_ptr<OHOS::BackgroundTaskMgr::ContinuousTaskCallbackInfo>& continuousTaskCallbackInfo,
82 const bool isStart)
83 {
84 int32_t uid = continuousTaskCallbackInfo->GetCreatorUid();
85 pid_t pid = continuousTaskCallbackInfo->GetCreatorPid();
86 std::string continuousTaskAbilityName_ = continuousTaskCallbackInfo->GetAbilityName();
87 int32_t userId = -1;
88 std::string bundleName = "";
89 if (GetBundleMgr()) {
90 bundleMgr_->GetBundleNameForUid(uid, bundleName);
91 } else {
92 BUNDLE_ACTIVE_LOGE("Get bundle mgr failed!");
93 return;
94 }
95 OHOS::ErrCode ret = BundleActiveAccountHelper::GetUserId(uid, userId);
96 if (ret == ERR_OK && userId != -1 && !bundleName.empty()) {
97 BundleActiveReportHandlerObject tmpHandlerObject(userId, "");
98 BundleActiveEvent event(bundleName, continuousTaskAbilityName_);
99 sptr<MiscServices::TimeServiceClient> timer = MiscServices::TimeServiceClient::GetInstance();
100 tmpHandlerObject.event_ = event;
101 tmpHandlerObject.event_.timeStamp_ = timer->GetBootTimeMs();
102 if (isStart) {
103 tmpHandlerObject.event_.eventId_ = BundleActiveEvent::LONG_TIME_TASK_STARTTED;
104 } else {
105 tmpHandlerObject.event_.eventId_ = BundleActiveEvent::LONG_TIME_TASK_ENDED;
106 }
107 BUNDLE_ACTIVE_LOGI("OnContinuousTaskStart id is %{public}d, bundle name is %{public}s, "
108 "ability name is %{public}s, event id is %{public}d,"
109 "uid is %{public}d, pid is %{public}d",
110 tmpHandlerObject.userId_, tmpHandlerObject.event_.bundleName_.c_str(),
111 tmpHandlerObject.event_.continuousTaskAbilityName_.c_str(), tmpHandlerObject.event_.eventId_,
112 uid, pid);
113 if (reportHandler_ != nullptr) {
114 BUNDLE_ACTIVE_LOGI("BundleActiveAppStateObserver::OnAbilityStateChanged handler not null, SEND");
115 std::shared_ptr<BundleActiveReportHandlerObject> handlerobjToPtr =
116 std::make_shared<BundleActiveReportHandlerObject>(tmpHandlerObject);
117 auto getEvent = AppExecFwk::InnerEvent::Get(BundleActiveReportHandler::MSG_REPORT_EVENT, handlerobjToPtr);
118 reportHandler_->SendEvent(getEvent);
119 }
120 }
121 }
122 } // namespace DeviceUsageStats
123 } // namespace OHOS
124
125 #endif