1 /*
2 * Copyright (c) 2022 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 #include "bundle_active_account_helper.h"
18 #include "bundle_active_group_handler.h"
19
20 namespace OHOS {
21 namespace DeviceUsageStats {
22 #ifndef OS_ACCOUNT_PART_ENABLED
23 namespace {
24 constexpr int32_t DEFAULT_OS_ACCOUNT_ID = 0; // 0 is the default id when there is no os_account part
25 } // namespace
26 #endif // OS_ACCOUNT_PART_ENABLED
BundleActiveGroupHandlerObject(const BundleActiveGroupHandlerObject & orig)27 BundleActiveGroupHandlerObject::BundleActiveGroupHandlerObject(const BundleActiveGroupHandlerObject& orig)
28 {
29 bundleName_ = orig.bundleName_;
30 userId_ = orig.userId_;
31 }
32
BundleActiveGroupHandler(const std::shared_ptr<AppExecFwk::EventRunner> & runner,const bool debug)33 BundleActiveGroupHandler::BundleActiveGroupHandler
34 (const std::shared_ptr<AppExecFwk::EventRunner> &runner, const bool debug) : AppExecFwk::EventHandler(runner)
35 {
36 if (debug) {
37 checkIdleInterval_ = ONE_MINUTE;
38 } else {
39 checkIdleInterval_ = THREE_HOUR;
40 }
41 }
42
Init(const std::shared_ptr<BundleActiveGroupController> & bundleActiveController)43 void BundleActiveGroupHandler::Init(const std::shared_ptr<BundleActiveGroupController>& bundleActiveController)
44 {
45 BUNDLE_ACTIVE_LOGI("Init called");
46 if (bundleActiveController == nullptr) {
47 BUNDLE_ACTIVE_LOGE("Init failed bundleActiveController is null");
48 return;
49 }
50 bundleActiveGroupController_ = bundleActiveController;
51 }
52
ProcessEvent(const AppExecFwk::InnerEvent::Pointer & event)53 void BundleActiveGroupHandler::ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event)
54 {
55 if (event == nullptr) {
56 return;
57 }
58 switch (event->GetInnerEventId()) {
59 case MSG_CHECK_BUNDLE_STATE: {
60 auto ptrToHandlerobj = event->GetSharedObject<BundleActiveGroupHandlerObject>();
61 BundleActiveGroupHandlerObject tmpHandlerobj = *ptrToHandlerobj;
62 sptr<MiscServices::TimeServiceClient> timer = MiscServices::TimeServiceClient::GetInstance();
63 int64_t bootBasedTimeStamp = timer->GetBootTimeMs();
64 bundleActiveGroupController_->CheckAndUpdateGroup(
65 tmpHandlerobj.bundleName_, tmpHandlerobj.userId_, bootBasedTimeStamp);
66 bundleActiveGroupController_->RestoreToDatabase(tmpHandlerobj.userId_);
67 break;
68 }
69 case MSG_ONE_TIME_CHECK_BUNDLE_STATE: {
70 std::vector<int32_t> activatedOsAccountIds;
71 BundleActiveAccountHelper::GetActiveUserId(activatedOsAccountIds);
72 for (uint32_t i = 0; i < activatedOsAccountIds.size(); i++) {
73 bundleActiveGroupController_->CheckEachBundleState(activatedOsAccountIds[i]);
74 bundleActiveGroupController_->RestoreToDatabase(activatedOsAccountIds[i]);
75 }
76 RemoveEvent(MSG_ONE_TIME_CHECK_BUNDLE_STATE);
77 break;
78 }
79 case MSG_CHECK_IDLE_STATE: {
80 auto ptrToHandlerobj = event->GetSharedObject<BundleActiveGroupHandlerObject>();
81 BundleActiveGroupHandlerObject tmpHandlerobj = *ptrToHandlerobj;
82 if (bundleActiveGroupController_->CheckEachBundleState(tmpHandlerobj.userId_) &&
83 bundleActiveGroupController_->bundleGroupEnable_) {
84 BundleActiveGroupHandlerObject GroupHandlerObj;
85 GroupHandlerObj.userId_ = tmpHandlerobj.userId_;
86 std::shared_ptr<BundleActiveGroupHandlerObject> handlerobjToPtr =
87 std::make_shared<BundleActiveGroupHandlerObject>(GroupHandlerObj);
88 auto handlerEvent = AppExecFwk::InnerEvent::Get(BundleActiveGroupHandler::MSG_CHECK_IDLE_STATE,
89 handlerobjToPtr);
90 bundleActiveGroupController_->RestoreToDatabase(GroupHandlerObj.userId_);
91 SendEvent(handlerEvent, checkIdleInterval_);
92 }
93 break;
94 }
95 default: {
96 break;
97 }
98 }
99 }
100 } // namespace DeviceUsageStats
101 } // namespace OHOS
102
103