• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_user_history.h"
20 #include "bundle_active_group_handler.h"
21 #include "ibundle_active_service.h"
22 #include "bundle_active_group_controller.h"
23 #include "bundle_active_util.h"
24 #include "bundle_active_bundle_mgr_helper.h"
25 
26 namespace OHOS {
27 namespace DeviceUsageStats {
28 using namespace DeviceUsageStatsGroupConst;
29     const int32_t MAIN_APP_INDEX = 0;
30 
GetInstance()31 BundleActiveGroupController& BundleActiveGroupController::GetInstance()
32 {
33     static BundleActiveGroupController instance;
34     return instance;
35 }
BundleActiveGroupHandlerObject()36 BundleActiveGroupHandlerObject::BundleActiveGroupHandlerObject()
37 {
38     bundleName_ = "";
39     userId_ = -1;
40     uid_ = -1;
41 }
42 
43 
Init(const bool debug)44 void BundleActiveGroupController::Init(const bool debug)
45 {
46     std::lock_guard<ffrt::mutex> lock(initMutex_);
47     if (isInit_) {
48         return;
49     }
50     timeoutForDirectlyUse_ = debug ? THREE_MINUTE : ONE_HOUR;
51     timeoutForNotifySeen_ = debug ? ONE_MINUTE : TWELVE_HOUR;
52     timeoutForSystemInteraction_ = debug ? ONE_MINUTE : TEN_MINUTE;
53     screenTimeLevel_ = {0, 0, debug ? TWO_MINUTE : ONE_HOUR, debug ? FOUR_MINUTE : TWO_HOUR};
54     bootTimeLevel_ = {0, debug ? TWO_MINUTE : TWELVE_HOUR, debug ? FOUR_MINUTE : TWENTY_FOUR_HOUR,
55         debug ? SIXTEEN_MINUTE : FOURTY_EIGHT_HOUR};
56     eventIdMatchReason_ = {
57         {BundleActiveEvent::ABILITY_FOREGROUND, GROUP_EVENT_REASON_FOREGROUND},
58         {BundleActiveEvent::ABILITY_BACKGROUND, GROUP_EVENT_REASON_BACKGROUND},
59         {BundleActiveEvent::SYSTEM_INTERACTIVE, GROUP_EVENT_REASON_SYSTEM},
60         {BundleActiveEvent::USER_INTERACTIVE, GROUP_EVENT_REASON_USER_INTERACTION},
61         {BundleActiveEvent::NOTIFICATION_SEEN, GROUP_EVENT_REASON_NOTIFY_SEEN},
62         {BundleActiveEvent::LONG_TIME_TASK_STARTTED, GROUP_EVENT_REASON_LONG_TIME_TASK_STARTTED},
63     };
64     activeGroupHandler_ = std::make_shared<BundleActiveGroupHandler>(debug);
65     activeGroupHandler_->Init();
66     isInit_ = true;
67 }
68 
DeInit()69 void BundleActiveGroupController::DeInit()
70 {
71     std::lock_guard<ffrt::mutex> lock(initMutex_);
72     if (!isInit_) {
73         return;
74     }
75     isInit_ = false;
76     activeGroupHandler_->DeInit();
77 }
78 
79 
GetBundleGroupHandler()80 std::shared_ptr<BundleActiveGroupHandler> BundleActiveGroupController::GetBundleGroupHandler()
81 {
82     std::lock_guard<ffrt::mutex> lock(initMutex_);
83     if (!isInit_) {
84         return nullptr;
85     }
86     return activeGroupHandler_;
87 }
88 
RestoreDurationToDatabase()89 void BundleActiveGroupController::RestoreDurationToDatabase()
90 {
91     std::lock_guard<ffrt::mutex> lock(mutex_);
92     bundleUserHistory_->WriteDeviceDuration();
93 }
94 
RestoreToDatabase(const int32_t userId)95 void BundleActiveGroupController::RestoreToDatabase(const int32_t userId)
96 {
97     std::lock_guard<ffrt::mutex> lock(mutex_);
98     bundleUserHistory_->WriteBundleUsage(userId);
99 }
100 
OnUserRemoved(const int32_t userId)101 void BundleActiveGroupController::OnUserRemoved(const int32_t userId)
102 {
103     std::lock_guard<ffrt::mutex> lock(mutex_);
104     bundleUserHistory_->userHistory_.erase(userId);
105     if (activeGroupHandler_ != nullptr) {
106         activeGroupHandler_->RemoveEvent(BundleActiveGroupHandler::MSG_CHECK_IDLE_STATE);
107     }
108 }
109 
SetBundleGroupEnable(bool bundleGroupEnable)110 void BundleActiveGroupController::SetBundleGroupEnable(bool bundleGroupEnable)
111 {
112     bundleGroupEnable_ = bundleGroupEnable;
113 }
114 
GetBundleGroupEnable()115 bool BundleActiveGroupController::GetBundleGroupEnable()
116 {
117     return bundleGroupEnable_;
118 }
119 
OnUserSwitched(const int32_t userId,const int32_t currentUsedUser)120 void BundleActiveGroupController::OnUserSwitched(const int32_t userId, const int32_t currentUsedUser)
121 {
122     BUNDLE_ACTIVE_LOGI("last time check for user %{public}d", currentUsedUser);
123     CheckEachBundleState(currentUsedUser);
124     bundleUserHistory_->WriteBundleUsage(currentUsedUser);
125     std::lock_guard<ffrt::mutex> lock(mutex_);
126     if (activeGroupHandler_ != nullptr) {
127         activeGroupHandler_->RemoveEvent(BundleActiveGroupHandler::MSG_CHECK_IDLE_STATE);
128         activeGroupHandler_->RemoveEvent(BundleActiveGroupHandler::MSG_CHECK_DEFAULT_BUNDLE_STATE);
129         activeGroupHandler_->RemoveEvent(BundleActiveGroupHandler::MSG_CHECK_NOTIFICATION_SEEN_BUNDLE_STATE);
130         activeGroupHandler_->RemoveEvent(BundleActiveGroupHandler::MSG_CHECK_SYSTEM_INTERACTIVE_BUNDLE_STATE);
131     }
132     PeriodCheckBundleState(userId);
133 }
134 
OnScreenChanged(const bool & isScreenOn,const int64_t bootFromTimeStamp)135 void BundleActiveGroupController::OnScreenChanged(const bool& isScreenOn, const int64_t bootFromTimeStamp)
136 {
137     if (activeGroupHandler_ != nullptr) {
138         activeGroupHandler_->PostTask([isScreenOn, bootFromTimeStamp]() {
139             std::lock_guard<ffrt::mutex> lock(BundleActiveGroupController::GetInstance().mutex_);
140             BundleActiveGroupController::GetInstance().bundleUserHistory_->UpdateBootBasedAndScreenTime(isScreenOn,
141                 bootFromTimeStamp);
142         });
143     }
144 }
145 
CreateUserHistory(const int64_t bootFromTimeStamp,const std::shared_ptr<BundleActiveCore> & bundleActiveCore)146 void BundleActiveGroupController::CreateUserHistory(const int64_t bootFromTimeStamp,
147     const std::shared_ptr<BundleActiveCore>& bundleActiveCore)
148 {
149     if (bundleUserHistory_ == nullptr) {
150         BUNDLE_ACTIVE_LOGI("SetHandlerAndCreateUserHistory bundleUserHistory_ is null, "
151             "called constructor, bootstamp is %{public}lld", (long long)bootFromTimeStamp);
152         bundleUserHistory_ = std::make_shared<BundleActiveUserHistory>(bootFromTimeStamp, bundleActiveCore);
153     }
154     OnScreenChanged(IsScreenOn(), bootFromTimeStamp);
155 }
156 
OnBundleUninstalled(const int32_t userId,const std::string & bundleName,const int32_t uid,const int32_t appIndex)157 void BundleActiveGroupController::OnBundleUninstalled(const int32_t userId, const std::string& bundleName,
158     const int32_t uid, const int32_t appIndex)
159 {
160     std::lock_guard<ffrt::mutex> lock(mutex_);
161     BUNDLE_ACTIVE_LOGI("OnBundleUninstalled called, userId is %{public}d, bundlename is %{public}s",
162         userId, bundleName.c_str());
163     auto oneUserHistory = bundleUserHistory_->GetUserHistory(userId, false);
164     if (oneUserHistory == nullptr) {
165         return;
166     }
167     DeleteUsageGroupCache(oneUserHistory, bundleName, uid, appIndex);
168     bundleUserHistory_->OnBundleUninstalled(userId, bundleName, uid, appIndex);
169 }
170 
DeleteUsageGroupCache(const std::shared_ptr<std::map<std::string,std::shared_ptr<BundleActivePackageHistory>>> & userHostory,const std::string & bundleName,const int32_t uid,const int32_t appIndex)171 void BundleActiveGroupController::DeleteUsageGroupCache(
172     const std::shared_ptr<std::map<std::string, std::shared_ptr<BundleActivePackageHistory>>>& userHostory,
173     const std::string& bundleName, const int32_t uid, const int32_t appIndex)
174 {
175     if (appIndex != MAIN_APP_INDEX) {
176         std::string moduleKey = BundleActiveUtil::GetBundleUsageKey(bundleName, uid);
177         userHostory->erase(moduleKey);
178     }
179     for (auto it = userHostory->begin(); it != userHostory->end();) {
180         if (it->first.find(bundleName) != std::string::npos) {
181             it = userHostory->erase(it);
182         } else {
183             it++;
184         }
185     }
186 }
187 
PeriodCheckBundleState(const int32_t userId)188 void BundleActiveGroupController::PeriodCheckBundleState(const int32_t userId)
189 {
190     BUNDLE_ACTIVE_LOGI("PeriodCheckBundleState called");
191     if (activeGroupHandler_ != nullptr) {
192         BundleActiveGroupHandlerObject tmpGroupHandlerObj;
193         tmpGroupHandlerObj.userId_ = userId;
194         std::shared_ptr<BundleActiveGroupHandlerObject> handlerobjToPtr =
195             std::make_shared<BundleActiveGroupHandlerObject>(tmpGroupHandlerObj);
196         activeGroupHandler_->SendEvent(BundleActiveGroupHandler::MSG_CHECK_DEFAULT_BUNDLE_STATE,
197             handlerobjToPtr, FIVE_SECOND);
198     }
199 }
200 
CheckEachBundleState(const int32_t userId)201 bool BundleActiveGroupController::CheckEachBundleState(const int32_t userId)
202 {
203     BUNDLE_ACTIVE_LOGI("CheckEachBundleState called, userid is %{public}d", userId);
204     std::vector<BundleActiveApplication> allBundlesForUser;
205     BundleActiveBundleMgrHelper::GetInstance()->GetApplicationInfos(flag, userId, allBundlesForUser);
206     sptr<MiscServices::TimeServiceClient> timer = MiscServices::TimeServiceClient::GetInstance();
207     int64_t bootBasedTimeStamp = timer->GetBootTimeMs();
208     for (auto oneBundle : allBundlesForUser) {
209         CheckAndUpdateGroup(oneBundle.bundleName, userId, oneBundle.uid, bootBasedTimeStamp);
210     }
211     return true;
212 }
213 
CheckIdleStatsOneTime()214 void BundleActiveGroupController::CheckIdleStatsOneTime()
215 {
216     BundleActiveGroupHandlerObject tmpGroupHandlerObj;
217     std::shared_ptr<BundleActiveGroupHandlerObject> handlerobjToPtr =
218         std::make_shared<BundleActiveGroupHandlerObject>(tmpGroupHandlerObj);
219     if (activeGroupHandler_ != nullptr) {
220         activeGroupHandler_->SendEvent(BundleActiveGroupHandler::MSG_ONE_TIME_CHECK_BUNDLE_STATE,
221             handlerobjToPtr);
222     }
223 }
224 
GetNewGroup(const std::string & bundleName,const int32_t userId,const int64_t bootBasedTimeStamp,const int32_t uid)225 int32_t BundleActiveGroupController::GetNewGroup(const std::string& bundleName, const int32_t userId,
226     const int64_t bootBasedTimeStamp, const int32_t uid)
227 {
228     int32_t groupIndex = bundleUserHistory_->GetLevelIndex(bundleName, userId, bootBasedTimeStamp, screenTimeLevel_,
229         bootTimeLevel_, uid);
230     if (groupIndex < 0) {
231         return -1;
232     }
233     return LEVEL_GROUP[groupIndex];
234 }
235 
calculationTimeOut(const std::shared_ptr<BundleActivePackageHistory> & oneBundleHistory,const int64_t bootBasedTimeStamp)236 bool BundleActiveGroupController::calculationTimeOut(
237     const std::shared_ptr<BundleActivePackageHistory>& oneBundleHistory, const int64_t bootBasedTimeStamp)
238 {
239     if (oneBundleHistory == nullptr) {
240         return false;
241     }
242     int64_t lastGroupCalculatedTimeStamp = oneBundleHistory->lastGroupCalculatedTimeStamp_;
243     return lastGroupCalculatedTimeStamp > 0 && bundleUserHistory_->GetBootBasedTimeStamp(bootBasedTimeStamp)
244         - lastGroupCalculatedTimeStamp > timeoutCalculated_;
245 }
246 
ReportEvent(const BundleActiveEvent & event,const int64_t bootBasedTimeStamp,const int32_t userId)247 void BundleActiveGroupController::ReportEvent(const BundleActiveEvent& event, const int64_t bootBasedTimeStamp,
248     const int32_t userId)
249 {
250     if (bundleGroupEnable_ == false) {
251         return;
252     }
253     std::lock_guard<ffrt::mutex> lock(mutex_);
254     if (IsBundleInstalled(event.bundleName_, userId) == false) {
255         BUNDLE_ACTIVE_LOGE("Report an uninstalled package event, return!");
256         return;
257     }
258     int32_t eventId = event.eventId_;
259     auto item  = eventIdMatchReason_.find(eventId);
260     if (item != eventIdMatchReason_.end()) {
261         std::shared_ptr<BundleActivePackageHistory> bundleUsageHistory = bundleUserHistory_->GetUsageHistoryForBundle(
262             event.bundleName_, userId, bootBasedTimeStamp, true, event.uid_);
263         if (bundleUsageHistory == nullptr) {
264             return;
265         }
266         int64_t timeUntilNextCheck;
267         uint32_t eventReason = item->second;
268         int32_t checkBundleMsgEventId = BundleActiveGroupHandler::MSG_CHECK_DEFAULT_BUNDLE_STATE;
269         switch (eventId) {
270             case BundleActiveEvent::NOTIFICATION_SEEN:
271                 bundleUserHistory_->ReportUsage(bundleUsageHistory, event, ACTIVE_GROUP_DAILY,
272                     eventReason, 0, bootBasedTimeStamp + timeoutForNotifySeen_, userId);
273                 timeUntilNextCheck = timeoutForNotifySeen_;
274                 checkBundleMsgEventId = BundleActiveGroupHandler::MSG_CHECK_NOTIFICATION_SEEN_BUNDLE_STATE;
275                 break;
276             case BundleActiveEvent::SYSTEM_INTERACTIVE:
277                 bundleUserHistory_->ReportUsage(bundleUsageHistory, event, ACTIVE_GROUP_ALIVE,
278                     eventReason, 0, bootBasedTimeStamp + timeoutForSystemInteraction_, userId);
279                 timeUntilNextCheck = timeoutForSystemInteraction_;
280                 checkBundleMsgEventId = BundleActiveGroupHandler::MSG_CHECK_SYSTEM_INTERACTIVE_BUNDLE_STATE;
281                 break;
282             default:
283                 bundleUserHistory_->ReportUsage(bundleUsageHistory, event, ACTIVE_GROUP_ALIVE,
284                     eventReason, bootBasedTimeStamp, bootBasedTimeStamp + timeoutForDirectlyUse_, userId);
285                 timeUntilNextCheck = timeoutForDirectlyUse_;
286                 break;
287         }
288         SendCheckBundleMsg(event, userId, timeUntilNextCheck, checkBundleMsgEventId);
289     }
290 }
291 
SendCheckBundleMsg(const BundleActiveEvent & event,const int32_t & userId,const int64_t & timeUntilNextCheck,const int64_t & checkBundleMsgEventId)292 void BundleActiveGroupController::SendCheckBundleMsg(const BundleActiveEvent& event, const int32_t& userId,
293     const int64_t& timeUntilNextCheck, const int64_t& checkBundleMsgEventId)
294 {
295     BundleActiveGroupHandlerObject tmpGroupHandlerObj;
296     tmpGroupHandlerObj.userId_ = userId;
297     tmpGroupHandlerObj.bundleName_ = event.bundleName_;
298     tmpGroupHandlerObj.uid_ = event.uid_;
299     std::shared_ptr<BundleActiveGroupHandlerObject> handlerobjToPtr =
300         std::make_shared<BundleActiveGroupHandlerObject>(tmpGroupHandlerObj);
301     if (activeGroupHandler_ != nullptr) {
302         activeGroupHandler_->SendCheckBundleMsg(checkBundleMsgEventId, handlerobjToPtr, timeUntilNextCheck);
303     }
304 }
305 
CheckAndUpdateGroup(const std::string & bundleName,const int32_t userId,const int32_t uid,const int64_t bootBasedTimeStamp)306 void BundleActiveGroupController::CheckAndUpdateGroup(const std::string& bundleName, const int32_t userId,
307     const int32_t uid, const int64_t bootBasedTimeStamp)
308 {
309     std::lock_guard<ffrt::mutex> lock(mutex_);
310     auto oneBundleHistory = bundleUserHistory_->GetUsageHistoryForBundle(bundleName, userId,
311         bootBasedTimeStamp, true, uid);
312     if (oneBundleHistory == nullptr) {
313         return;
314     }
315     uint32_t groupReason = oneBundleHistory->reasonInGroup_;
316     uint32_t oldGroupControlReason = groupReason & GROUP_CONTROL_REASON_MASK;
317     if (oldGroupControlReason == GROUP_CONTROL_REASON_FORCED) {
318         BUNDLE_ACTIVE_LOGI("%{public}s is forced set, return", bundleName.c_str());
319         return;
320     }
321     int32_t oldGroup = oneBundleHistory->currentGroup_;
322     int32_t newGroup = std::max(oldGroup, ACTIVE_GROUP_ALIVE);
323     if (oldGroupControlReason == GROUP_CONTROL_REASON_DEFAULT ||
324         oldGroupControlReason == GROUP_CONTROL_REASON_USAGE ||
325         oldGroupControlReason == GROUP_CONTROL_REASON_TIMEOUT) {
326         newGroup = GetNewGroup(bundleName, userId, bootBasedTimeStamp, uid);
327         if (newGroup < 0) {
328             return;
329         }
330         groupReason = GROUP_CONTROL_REASON_TIMEOUT;
331     }
332     int64_t bootBasedTimeStampAdjusted = bundleUserHistory_->GetBootBasedTimeStamp(bootBasedTimeStamp);
333     bool notTimeout = false;
334     if (newGroup >= ACTIVE_GROUP_ALIVE && oneBundleHistory->bundleAliveTimeoutTimeStamp_ >
335         bootBasedTimeStampAdjusted) {
336         newGroup = ACTIVE_GROUP_ALIVE;
337         groupReason = GROUP_CONTROL_REASON_USAGE | GROUP_EVENT_REASON_ALIVE_NOT_TIMEOUT;
338         notTimeout = true;
339     } else if (newGroup >= ACTIVE_GROUP_DAILY && oneBundleHistory->bundleDailyTimeoutTimeStamp_ >
340         bootBasedTimeStampAdjusted) {
341         newGroup = ACTIVE_GROUP_DAILY;
342         groupReason = GROUP_CONTROL_REASON_USAGE | GROUP_EVENT_REASON_ALIVE_TIMEOUT;
343         notTimeout = true;
344     }
345     if (oldGroup < newGroup || notTimeout) {
346         BUNDLE_ACTIVE_LOGD("CheckAndUpdateGroup called SetAppGroup");
347         bundleUserHistory_->SetAppGroup(bundleName, userId, bootBasedTimeStamp, newGroup,
348             groupReason, false, uid);
349     }
350 }
351 
SetAppGroup(const std::string & bundleName,const int32_t userId,int32_t newGroup,uint32_t reason,const int64_t bootBasedTimeStamp,const bool isFlush)352 ErrCode BundleActiveGroupController::SetAppGroup(const std::string& bundleName, const int32_t userId,
353     int32_t newGroup, uint32_t reason, const int64_t bootBasedTimeStamp, const bool isFlush)
354 {
355     std::lock_guard<ffrt::mutex> lock(mutex_);
356     if (!IsBundleInstalled(bundleName, userId)) {
357         return ERR_NO_APP_GROUP_INFO_IN_DATABASE;
358     }
359     auto iter = bundleUserHistory_->userHistory_.find(userId);
360     if (iter == bundleUserHistory_->userHistory_.end()) {
361         return ERR_NO_APP_GROUP_INFO_IN_DATABASE;
362     }
363     auto packageHistoryMap = iter->second;
364     int32_t result = 0;
365     int32_t tempResult = 0;
366     for (auto packageHistoryIter : *packageHistoryMap) {
367         if (packageHistoryIter.first.find(bundleName) == std::string::npos || packageHistoryIter.second == nullptr) {
368             continue;
369         }
370         auto oneBundleHistory = bundleUserHistory_->GetUsageHistoryForBundle(bundleName,
371             userId, bootBasedTimeStamp, true, packageHistoryIter.second->uid_);
372         if (!oneBundleHistory) {
373             continue;
374         }
375         tempResult = bundleUserHistory_->SetAppGroup(bundleName, userId,
376             packageHistoryIter.second->uid_, bootBasedTimeStamp, newGroup, reason, isFlush);
377         if (tempResult != ERR_OK) {
378             result = tempResult;
379         }
380     }
381     return result;
382 }
383 
IsBundleIdle(const std::string & bundleName,const int32_t userId)384 int32_t BundleActiveGroupController::IsBundleIdle(const std::string& bundleName, const int32_t userId)
385 {
386     std::lock_guard<ffrt::mutex> lock(mutex_);
387     sptr<MiscServices::TimeServiceClient> timer = MiscServices::TimeServiceClient::GetInstance();
388     if (IsBundleInstalled(bundleName, userId) == false) {
389         return -1;
390     }
391     int64_t bootBasedTimeStamp = timer->GetBootTimeMs();
392     auto iter = bundleUserHistory_->userHistory_.find(userId);
393     if (iter == bundleUserHistory_->userHistory_.end()) {
394         return -1;
395     }
396     auto packageHistoryMap = iter->second;
397     int32_t IsBundleIdle = 1;
398     for (auto packageHistoryIter : *packageHistoryMap) {
399         if (packageHistoryIter.first.find(bundleName) == std::string::npos || packageHistoryIter.second == nullptr) {
400             continue;
401         }
402         auto oneBundleHistory = bundleUserHistory_->GetUsageHistoryForBundle(bundleName,
403             userId, bootBasedTimeStamp, false, packageHistoryIter.second->uid_);
404         if (!oneBundleHistory) {
405             continue;
406         }
407         if (oneBundleHistory->currentGroup_ <= ACTIVE_GROUP_RARE) {
408             IsBundleIdle = 0;
409         }
410     }
411     return IsBundleIdle;
412 }
413 
QueryAppGroup(int32_t & appGroup,const std::string & bundleName,const int32_t userId)414 ErrCode BundleActiveGroupController::QueryAppGroup(int32_t& appGroup,
415     const std::string& bundleName, const int32_t userId)
416 {
417     std::lock_guard<ffrt::mutex> lock(mutex_);
418     if (bundleName.empty()) {
419         BUNDLE_ACTIVE_LOGE("bundleName can not get by userId");
420         return ERR_NO_APP_GROUP_INFO_IN_DATABASE;
421     }
422     sptr<MiscServices::TimeServiceClient> timer = MiscServices::TimeServiceClient::GetInstance();
423     if (!IsBundleInstalled(bundleName, userId)) {
424         BUNDLE_ACTIVE_LOGI("QueryAppGroup is not bundleInstalled");
425         return ERR_APPLICATION_IS_NOT_INSTALLED;
426     }
427     int64_t bootBasedTimeStamp = timer->GetBootTimeMs();
428     auto iter = bundleUserHistory_->userHistory_.find(userId);
429     if (iter == bundleUserHistory_->userHistory_.end()) {
430         return ERR_NO_APP_GROUP_INFO_IN_DATABASE;
431     }
432     appGroup = ACTIVE_GROUP_NEVER;
433     auto packageHistoryMap = iter->second;
434     for (auto packageHistoryIter : *packageHistoryMap) {
435         if (packageHistoryIter.first.find(bundleName) == std::string::npos || packageHistoryIter.second == nullptr) {
436             continue;
437         }
438         auto oneBundleHistory = bundleUserHistory_->GetUsageHistoryForBundle(bundleName,
439             userId, bootBasedTimeStamp, false, packageHistoryIter.second->uid_);
440         if (!oneBundleHistory) {
441             continue;
442         }
443         BUNDLE_ACTIVE_LOGI("QueryAppGroup group is %{public}d", oneBundleHistory->currentGroup_);
444         appGroup = std::min(oneBundleHistory->currentGroup_, appGroup);
445     }
446     return ERR_OK;
447 }
448 
IsBundleInstalled(const std::string & bundleName,const int32_t userId)449 bool BundleActiveGroupController::IsBundleInstalled(const std::string& bundleName, const int32_t userId)
450 {
451     ApplicationInfo bundleInfo;
452     bool getInfoIsSuccess = BundleActiveBundleMgrHelper::GetInstance()->GetApplicationInfo(
453         bundleName, ApplicationFlag::GET_BASIC_APPLICATION_INFO, userId, bundleInfo);
454     if (getInfoIsSuccess == false) {
455         BUNDLE_ACTIVE_LOGE("IsBundleInstalled bundle is not installed!");
456         return false;
457     }
458     return true;
459 }
460 
ShutDown(const int64_t bootBasedTimeStamp,const int32_t userId)461 void BundleActiveGroupController::ShutDown(const int64_t bootBasedTimeStamp, const int32_t userId)
462 {
463     BUNDLE_ACTIVE_LOGI("ShutDown called");
464     CheckEachBundleState(userId);
465     bundleUserHistory_->UpdateBootBasedAndScreenTime(false, bootBasedTimeStamp, true);
466 }
467 
IsScreenOn()468 bool BundleActiveGroupController::IsScreenOn()
469 {
470     bool result = true;
471 #ifdef DEVICE_USAGES_STATISTICS_POWERMANGER_ENABLE
472     result = PowerMgrClient::GetInstance().IsScreenOn();
473 #endif
474     BUNDLE_ACTIVE_LOGI("IsScreenOn() is %{public}d", result);
475     return result;
476 }
IsUsedOverOneWeek(const std::string & bundleName,const int32_t userId)477 bool BundleActiveGroupController::IsUsedOverOneWeek(const std::string& bundleName, const int32_t userId)
478 {
479     if (bundleUserHistory_ == nullptr) {
480         return false;
481     }
482     int64_t firstUseTime = bundleUserHistory_->GetFirstUseTime(bundleName, userId);
483     if (firstUseTime == MAX_END_TIME) {
484         return false;
485     }
486     int64_t curTime = BundleActiveUtil::GetSystemTimeMs();
487     if (curTime < 0 || curTime - firstUseTime > ONE_WEEK_TIME) {
488         return false;
489     }
490     return true;
491 }
492 }  // namespace DeviceUsageStats
493 }  // namespace OHOS
494 
495