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 
16 #include "time_service_client.h"
17 
18 #include "bundle_active_log.h"
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 
24 namespace OHOS {
25 namespace DeviceUsageStats {
Init(const std::shared_ptr<BundleActiveReportHandler> & reportHandler)26 void BundleActiveAppStateObserver::Init(const std::shared_ptr<BundleActiveReportHandler>& reportHandler)
27 {
28     if (reportHandler != nullptr) {
29         BUNDLE_ACTIVE_LOGI("Init report handler is not null, init success");
30         reportHandler_ = reportHandler;
31     }
32 }
33 
OnAbilityStateChanged(const AbilityStateData & abilityStateData)34 void BundleActiveAppStateObserver::OnAbilityStateChanged(const AbilityStateData &abilityStateData)
35 {
36     if (!ValidateAbilityStateData(abilityStateData)) {
37         BUNDLE_ACTIVE_LOGE("validate ability state data failed!");
38         return;
39     }
40     if (abilityStateData.abilityType != 1) {
41         return;
42     }
43     int32_t userId = -1;
44     OHOS::ErrCode ret = BundleActiveAccountHelper::GetUserId(abilityStateData.uid, userId);
45     if (ret == ERR_OK && userId != -1) {
46         std::stringstream stream;
47         stream << abilityStateData.token.GetRefPtr();
48         BundleActiveReportHandlerObject tmpHandlerObject(userId, "");
49         BundleActiveEvent event(abilityStateData.bundleName, abilityStateData.abilityName,
50             abilityStateData.abilityName, abilityStateData.moduleName, abilityStateData.uid);
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("OnAblityStateChanged %{public}s", tmpHandlerObject.ToString().c_str());
74         if (reportHandler_ != nullptr) {
75             std::shared_ptr<BundleActiveReportHandlerObject> handlerobjToPtr =
76                 std::make_shared<BundleActiveReportHandlerObject>(tmpHandlerObject);
77             reportHandler_->SendEvent(BundleActiveReportHandler::MSG_REPORT_EVENT, handlerobjToPtr);
78         }
79     }
80     return;
81 }
82 }  // namespace DeviceUsageStats
83 }  // namespace OHOS
84 
85