• 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 "bundle_active_report_handler.h"
17 #include "bundle_active_event.h"
18 
19 namespace OHOS {
20 namespace DeviceUsageStats {
BundleActiveReportHandler(const std::shared_ptr<AppExecFwk::EventRunner> & runner)21 BundleActiveReportHandler::BundleActiveReportHandler
22     (const std::shared_ptr<AppExecFwk::EventRunner> &runner) : AppExecFwk::EventHandler(runner)
23 {
24 }
25 
Init(const std::shared_ptr<BundleActiveCore> & bundleActiveCore)26 void BundleActiveReportHandler::Init(const std::shared_ptr<BundleActiveCore>& bundleActiveCore)
27 {
28     bundleActiveCore_ = bundleActiveCore;
29 }
30 
ProcessEvent(const AppExecFwk::InnerEvent::Pointer & event)31 void BundleActiveReportHandler::ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event)
32 {
33     if (event == nullptr) {
34         BUNDLE_ACTIVE_LOGE("event is null, exit ProcessEvent");
35         return;
36     }
37     switch (event->GetInnerEventId()) {
38         case MSG_REPORT_EVENT: {
39             BUNDLE_ACTIVE_LOGI("MSG_REPORT_EVENT CALLED");
40             auto ptrToHandlerobj = event->GetSharedObject<BundleActiveReportHandlerObject>();
41             BundleActiveReportHandlerObject tmpHandlerobj = *ptrToHandlerobj;
42             bundleActiveCore_->ReportEvent(tmpHandlerobj.event_, tmpHandlerobj.userId_);
43             break;
44         }
45         case MSG_FLUSH_TO_DISK: {
46             BUNDLE_ACTIVE_LOGI("FLUSH TO DISK HANDLE");
47             auto ptrToHandlerobj = event->GetSharedObject<BundleActiveReportHandlerObject>();
48             BundleActiveReportHandlerObject tmpHandlerobj = *ptrToHandlerobj;
49             if (tmpHandlerobj.userId_ != bundleActiveCore_->currentUsedUser_) {
50                 BUNDLE_ACTIVE_LOGE("flush user is %{public}d, not last user %{public}d, return",
51                     tmpHandlerobj.userId_, bundleActiveCore_->currentUsedUser_);
52                 RemoveEvent(BundleActiveReportHandler::MSG_FLUSH_TO_DISK, tmpHandlerobj.userId_);
53                 return;
54             }
55             bundleActiveCore_->RestoreToDatabase(tmpHandlerobj.userId_);
56             break;
57         }
58         case MSG_REMOVE_USER: {
59             auto ptrToHandlerobj = event->GetSharedObject<BundleActiveReportHandlerObject>();
60             BundleActiveReportHandlerObject tmpHandlerobj = *ptrToHandlerobj;
61             bundleActiveCore_->OnUserRemoved(tmpHandlerobj.userId_);
62             break;
63         }
64         case MSG_BUNDLE_UNINSTALLED: {
65             BUNDLE_ACTIVE_LOGI("MSG_BUNDLE_UNINSTALLED CALLED");
66             auto ptrToHandlerobj = event->GetSharedObject<BundleActiveReportHandlerObject>();
67             BundleActiveReportHandlerObject tmpHandlerobj = *ptrToHandlerobj;
68             bundleActiveCore_->OnBundleUninstalled(tmpHandlerobj.userId_, tmpHandlerobj.bundleName_);
69             break;
70         }
71         case MSG_SWITCH_USER: {
72             BUNDLE_ACTIVE_LOGI("MSG_SWITCH_USER CALLED");
73             auto ptrToHandlerobj = event->GetSharedObject<BundleActiveReportHandlerObject>();
74             BundleActiveReportHandlerObject tmpHandlerobj = *ptrToHandlerobj;
75             bundleActiveCore_->OnUserSwitched(tmpHandlerobj.userId_);
76             break;
77         }
78         default: {
79             break;
80         }
81     }
82 }
83 }  // namespace DeviceUsageStats
84 }  // namespace OHOS
85 
86