1 /*
2 * Copyright (c) 2024-2025 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 "fold_app_usage_event_factory.h"
17
18 #include "app_mgr_interface.h"
19 #include "fold_app_usage_event.h"
20 #include "hiview_logger.h"
21 #include "if_system_ability_manager.h"
22 #include "iservice_registry.h"
23 #include "time_util.h"
24 #include "usage_event_common.h"
25
26 namespace OHOS {
27 namespace HiviewDFX {
28 DEFINE_LOG_TAG("FoldAppUsageFactory");
29 namespace {
30 constexpr int APP_MGR_SERVICE_ID = 501;
31 constexpr uint32_t DATA_KEEP_DAY = 3;
32 const std::string DATE_FORMAT = "%Y-%m-%d";
33 }
34
35 using namespace FoldAppUsageEventSpace;
36
FoldAppUsageEventFactory(const std::string & workPath)37 FoldAppUsageEventFactory::FoldAppUsageEventFactory(const std::string& workPath)
38 {
39 dbHelper_ = std::make_unique<FoldAppUsageDbHelper>(workPath);
40 }
Create()41 std::unique_ptr<LoggerEvent> FoldAppUsageEventFactory::Create()
42 {
43 return std::make_unique<FoldAppUsageEvent>(EVENT_NAME, HiSysEvent::STATISTIC);
44 }
45
Create(std::vector<std::unique_ptr<LoggerEvent>> & events)46 void FoldAppUsageEventFactory::Create(std::vector<std::unique_ptr<LoggerEvent>> &events)
47 {
48 today0Time_ = static_cast<uint64_t>(TimeUtil::Get0ClockStampMs());
49 uint64_t gapTime = TimeUtil::MILLISECS_PER_DAY;
50 startTime_ = today0Time_ > gapTime ? (today0Time_ - gapTime) : 0;
51 endTime_ = today0Time_ > 1 ? (today0Time_ - 1) : 0; // statistic endTime: 1 ms before 0 Time
52 clearDataTime_ = today0Time_ > gapTime * DATA_KEEP_DAY ?
53 (today0Time_ - gapTime * DATA_KEEP_DAY) : 0;
54 std::string dateStr = TimeUtil::TimestampFormatToDate(startTime_ / TimeUtil::SEC_TO_MILLISEC, DATE_FORMAT);
55 foldStatus_ = dbHelper_->QueryFinalScreenStatus(endTime_);
56 std::vector<FoldAppUsageInfo> foldAppUsageInfos;
57 GetAppUsageInfo(foldAppUsageInfos);
58 for (const auto &info : foldAppUsageInfos) {
59 std::unique_ptr<LoggerEvent> event = Create();
60 event->Update(KEY_OF_PACKAGE, info.package);
61 event->Update(KEY_OF_VERSION, info.version);
62 event->Update(KEY_OF_FOLD_VER_USAGE, static_cast<uint32_t>(info.foldVer));
63 event->Update(KEY_OF_FOLD_HOR_USAGE, static_cast<uint32_t>(info.foldHor));
64 event->Update(KEY_OF_EXPD_VER_USAGE, static_cast<uint32_t>(info.expdVer));
65 event->Update(KEY_OF_EXPD_HOR_USAGE, static_cast<uint32_t>(info.expdHor));
66 event->Update(KEY_OF_DATE, dateStr);
67 event->Update(KEY_OF_START_NUM, static_cast<uint32_t>(info.startNum));
68 event->Update(KEY_OF_USAGE, static_cast<uint32_t>(info.foldVer + info.foldHor + info.expdVer + info.expdHor));
69 events.emplace_back(std::move(event));
70 }
71 dbHelper_->DeleteEventsByTime(clearDataTime_);
72 }
73
GetAppUsageInfo(std::vector<FoldAppUsageInfo> & infos)74 void FoldAppUsageEventFactory::GetAppUsageInfo(std::vector<FoldAppUsageInfo> &infos)
75 {
76 std::unordered_map<std::string, FoldAppUsageInfo> statisticInfos;
77 dbHelper_->QueryStatisticEventsInPeriod(startTime_, endTime_, statisticInfos);
78 std::vector<std::string> appNames;
79 GetForegroundAppNames(appNames);
80 GetForegroundAppsAtEndTime(appNames);
81 std::unordered_map<std::string, FoldAppUsageInfo> forgroundInfos;
82 for (const auto &app : appNames) {
83 if (app == SCENEBOARD_BUNDLE_NAME) {
84 continue;
85 }
86 FoldAppUsageInfo usageInfo;
87 usageInfo.package = app;
88 dbHelper_->QueryForegroundAppsInfo(startTime_, endTime_, foldStatus_, usageInfo);
89 forgroundInfos[usageInfo.package + usageInfo.version] = usageInfo;
90 }
91 for (const auto &forgroundInfo : forgroundInfos) {
92 if (statisticInfos.count(forgroundInfo.first) == 0) {
93 statisticInfos[forgroundInfo.first] = forgroundInfo.second;
94 continue;
95 }
96 statisticInfos[forgroundInfo.first].foldVer += forgroundInfo.second.foldVer;
97 statisticInfos[forgroundInfo.first].foldHor += forgroundInfo.second.foldHor;
98 statisticInfos[forgroundInfo.first].expdVer += forgroundInfo.second.expdVer;
99 statisticInfos[forgroundInfo.first].expdHor += forgroundInfo.second.expdHor;
100 statisticInfos[forgroundInfo.first].startNum += forgroundInfo.second.startNum;
101 }
102 for (const auto &statisticInfo : statisticInfos) {
103 infos.emplace_back(statisticInfo.second);
104 }
105 }
106
GetForegroundAppNames(std::vector<std::string> & appNames)107 void FoldAppUsageEventFactory::GetForegroundAppNames(std::vector<std::string> &appNames)
108 {
109 sptr<ISystemAbilityManager> abilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
110 if (abilityMgr == nullptr) {
111 HIVIEW_LOGE("failed to get ISystemAbilityManager");
112 return;
113 }
114
115 sptr<IRemoteObject> remoteObject = abilityMgr->GetSystemAbility(APP_MGR_SERVICE_ID);
116 if (remoteObject == nullptr) {
117 HIVIEW_LOGE("failed to get app Manager service");
118 return;
119 }
120 sptr<AppExecFwk::IAppMgr> appMgrProxy = iface_cast<AppExecFwk::IAppMgr>(remoteObject);
121 if (appMgrProxy == nullptr || !appMgrProxy->AsObject()) {
122 HIVIEW_LOGE("failed to get app mgr proxy");
123 return;
124 }
125 std::vector<AppExecFwk::AppStateData> appList;
126 int ret = appMgrProxy->GetForegroundApplications(appList);
127 HIVIEW_LOGI("GetForegroundApplications ret: %{public}d", ret);
128 for (const auto &appData : appList) {
129 appNames.emplace_back(appData.bundleName);
130 }
131 }
132
GetForegroundAppsAtEndTime(std::vector<std::string> & appNames)133 void FoldAppUsageEventFactory::GetForegroundAppsAtEndTime(std::vector<std::string> &appNames)
134 {
135 std::vector<std::pair<int, std::string>> switchEvents = dbHelper_->QueryEventAfterEndTime(
136 endTime_, TimeUtil::GetMilliseconds());
137 for (const auto &event : switchEvents) {
138 auto iter = std::find(appNames.begin(), appNames.end(), event.second);
139 if (iter == appNames.end() && event.first == FoldEventId::EVENT_APP_EXIT) {
140 appNames.emplace_back(event.second);
141 }
142 if (iter != appNames.end() && event.first == FoldEventId::EVENT_APP_START) {
143 appNames.erase(iter);
144 }
145 }
146 }
147 } // namespace HiviewDFX
148 } // namespace OHOS
149