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 #include "bundle_active_report_controller.h"
24
25 namespace OHOS {
26 namespace DeviceUsageStats {
27
OnAbilityStateChanged(const AbilityStateData & abilityStateData)28 void BundleActiveAppStateObserver::OnAbilityStateChanged(const AbilityStateData &abilityStateData)
29 {
30 if (!ValidateAbilityStateData(abilityStateData)) {
31 BUNDLE_ACTIVE_LOGE("validate ability state data failed!");
32 return;
33 }
34 if (abilityStateData.abilityType != 1) {
35 return;
36 }
37 int32_t userId = -1;
38 OHOS::ErrCode ret = BundleActiveAccountHelper::GetUserId(abilityStateData.uid, userId);
39 if (ret == ERR_OK && userId != -1) {
40 std::stringstream stream;
41 stream << abilityStateData.token.GetRefPtr();
42 BundleActiveReportHandlerObject tmpHandlerObject(userId, "");
43 BundleActiveEvent event(abilityStateData.bundleName, abilityStateData.abilityName,
44 abilityStateData.abilityName, abilityStateData.moduleName, abilityStateData.uid);
45 tmpHandlerObject.event_ = event;
46 sptr<MiscServices::TimeServiceClient> timer = MiscServices::TimeServiceClient::GetInstance();
47 tmpHandlerObject.event_.timeStamp_ = timer->GetBootTimeMs();
48 switch (abilityStateData.abilityState) {
49 case static_cast<int32_t>(AppExecFwk::AbilityState::ABILITY_STATE_FOREGROUND):
50 tmpHandlerObject.event_.eventId_ = BundleActiveEvent::ABILITY_FOREGROUND;
51 break;
52 case static_cast<int32_t>(AppExecFwk::AbilityState::ABILITY_STATE_BACKGROUND):
53 tmpHandlerObject.event_.eventId_ = BundleActiveEvent::ABILITY_BACKGROUND;
54 break;
55 case static_cast<int32_t>(AppExecFwk::AbilityState::ABILITY_STATE_TERMINATED):
56 tmpHandlerObject.event_.eventId_ = BundleActiveEvent::ABILITY_STOP;
57 break;
58 default:
59 return;
60 }
61 BUNDLE_ACTIVE_LOGI("OnAblityStateChanged %{public}s", tmpHandlerObject.ToString().c_str());
62 auto reportHandler = BundleActiveReportController::GetInstance().GetBundleReportHandler();
63 if (reportHandler != nullptr) {
64 std::shared_ptr<BundleActiveReportHandlerObject> handlerobjToPtr =
65 std::make_shared<BundleActiveReportHandlerObject>(tmpHandlerObject);
66 reportHandler->SendEvent(BundleActiveReportHandler::MSG_REPORT_EVENT, handlerobjToPtr);
67 }
68 }
69 return;
70 }
71 } // namespace DeviceUsageStats
72 } // namespace OHOS
73
74