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