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