• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "print_event_subscriber.h"
17 #include "common_event_manager.h"
18 #include "common_event_support.h"
19 #include "print_log.h"
20 #include "print_service_ability.h"
21 
22 namespace OHOS {
23 namespace Print {
PrintEventSubscriber(const EventFwk::CommonEventSubscribeInfo & subscribeInfo)24 PrintEventSubscriber::PrintEventSubscriber(
25     const EventFwk::CommonEventSubscribeInfo &subscribeInfo) : EventFwk::CommonEventSubscriber(subscribeInfo)
26 {}
27 
~PrintEventSubscriber()28 PrintEventSubscriber::~PrintEventSubscriber()
29 {}
30 
OnReceiveEvent(const EventFwk::CommonEventData & data)31 void PrintEventSubscriber::OnReceiveEvent(const EventFwk::CommonEventData &data)
32 {
33     std::string action = data.GetWant().GetAction();
34     if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED) {
35 #ifdef ENTERPRISE_ENABLE
36         PrintServiceAbility::GetInstance()->RefreshPrinterStatusOnSwitchUser();
37 #endif // ENTERPRISE_ENABLE
38 
39         int32_t userId = data.GetCode();
40         PRINT_HILOGI("user switched, current userId: %{public}d", userId);
41         constexpr int32_t WAIT_TIME = 3;
42         std::this_thread::sleep_for(std::chrono::seconds(WAIT_TIME));
43         PrintServiceAbility::GetInstance()->NotifyCurrentUserChanged(userId);
44     } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED) {
45         int32_t userId = data.GetCode();
46         PRINT_HILOGI("user removed, removed userId: %{public}d", userId);
47         PrintServiceAbility::GetInstance()->CancelUserPrintJobs(userId);
48     } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_ENTER_HIBERNATE ||
49         action == EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF ||
50         action == EventFwk::CommonEventSupport::COMMON_EVENT_ENTER_FORCE_SLEEP) {
51         PRINT_HILOGI("enter low-power mode, stop discovery");
52         PrintServiceAbility::GetInstance()->DelayEnterLowPowerMode();
53     } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_EXIT_HIBERNATE ||
54         action == EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON ||
55         action == EventFwk::CommonEventSupport::COMMON_EVENT_EXIT_FORCE_SLEEP) {
56         PRINT_HILOGI("exit low-power mode, restart discovery");
57         PrintServiceAbility::GetInstance()->ExitLowPowerMode();
58     }
59 }
60 
61 }  // namespace Print
62 }  // namespace OHOS