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 #include "time_service_client.h"
17
18 #include "bundle_active_app_state_observer.h"
19 #include "bundle_active_report_handler.h"
20 #include "bundle_active_event.h"
21 #include "bundle_active_account_helper.h"
22
23 namespace OHOS {
24 namespace DeviceUsageStats {
Init(const std::shared_ptr<BundleActiveReportHandler> & reportHandler)25 void BundleActiveAppStateObserver::Init(const std::shared_ptr<BundleActiveReportHandler>& reportHandler)
26 {
27 if (reportHandler != nullptr) {
28 BUNDLE_ACTIVE_LOGI("Init report handler is not null, init success");
29 reportHandler_ = reportHandler;
30 }
31 }
32
OnAbilityStateChanged(const AbilityStateData & abilityStateData)33 void BundleActiveAppStateObserver::OnAbilityStateChanged(const AbilityStateData &abilityStateData)
34 {
35 if (!ValidateAbilityStateData(abilityStateData)) {
36 BUNDLE_ACTIVE_LOGE("validate ability state data failed!");
37 return;
38 }
39 if (abilityStateData.abilityType != 1) {
40 return;
41 }
42 int32_t userId = -1;
43 OHOS::ErrCode ret = BundleActiveAccountHelper::GetUserId(abilityStateData.uid, userId);
44 if (ret == ERR_OK && userId != -1) {
45 std::stringstream stream;
46 stream << abilityStateData.token.GetRefPtr();
47 std::string abilityId = stream.str();
48 BundleActiveReportHandlerObject tmpHandlerObject(userId, "");
49 BundleActiveEvent event(abilityStateData.bundleName, abilityStateData.abilityName,
50 abilityStateData.abilityName, abilityStateData.moduleName);
51 tmpHandlerObject.event_ = event;
52 sptr<MiscServices::TimeServiceClient> timer = MiscServices::TimeServiceClient::GetInstance();
53 tmpHandlerObject.event_.timeStamp_ = timer->GetBootTimeMs();
54 switch (abilityStateData.abilityState) {
55 case static_cast<int32_t>(AppExecFwk::AbilityState::ABILITY_STATE_FOREGROUND):
56 if (!abilityStateData.isFocused) {
57 return;
58 }
59 tmpHandlerObject.event_.eventId_ = BundleActiveEvent::ABILITY_FOREGROUND;
60 break;
61 case static_cast<int32_t>(AppExecFwk::AbilityState::ABILITY_STATE_BACKGROUND):
62 if (abilityStateData.isFocused) {
63 return;
64 }
65 tmpHandlerObject.event_.eventId_ = BundleActiveEvent::ABILITY_BACKGROUND;
66 break;
67 case static_cast<int32_t>(AppExecFwk::AbilityState::ABILITY_STATE_TERMINATED):
68 tmpHandlerObject.event_.eventId_ = BundleActiveEvent::ABILITY_STOP;
69 break;
70 default:
71 return;
72 }
73 BUNDLE_ACTIVE_LOGI("OnAbilityStateChangeduser id is %{public}d, bundle name is %{public}s, "
74 "ability name is %{public}s, ability id is %{public}s, event id is %{public}d",
75 tmpHandlerObject.userId_, tmpHandlerObject.event_.bundleName_.c_str(),
76 tmpHandlerObject.event_.abilityName_.c_str(), abilityId.c_str(), tmpHandlerObject.event_.eventId_);
77 if (reportHandler_ != nullptr) {
78 std::shared_ptr<BundleActiveReportHandlerObject> handlerobjToPtr =
79 std::make_shared<BundleActiveReportHandlerObject>(tmpHandlerObject);
80 auto getEvent = AppExecFwk::InnerEvent::Get(BundleActiveReportHandler::MSG_REPORT_EVENT, handlerobjToPtr);
81 reportHandler_->SendEvent(getEvent);
82 }
83 }
84 return;
85 }
86 } // namespace DeviceUsageStats
87 } // namespace OHOS
88
89