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