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_usb_manager.h"
20 #include "print_http_server_manager.h"
21 #include "print_log.h"
22 #include "print_service_ability.h"
23 
24 namespace OHOS {
25 namespace Print {
PrintEventSubscriber(const EventFwk::CommonEventSubscribeInfo & subscribeInfo)26 PrintEventSubscriber::PrintEventSubscriber(
27     const EventFwk::CommonEventSubscribeInfo &subscribeInfo) : EventFwk::CommonEventSubscriber(subscribeInfo)
28 {}
29 
~PrintEventSubscriber()30 PrintEventSubscriber::~PrintEventSubscriber()
31 {}
32 
OnReceiveEvent(const EventFwk::CommonEventData & data)33 void PrintEventSubscriber::OnReceiveEvent(const EventFwk::CommonEventData &data)
34 {
35     std::string action = data.GetWant().GetAction();
36 #ifdef IPPOVERUSB_ENABLE
37     if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USB_DEVICE_ATTACHED) {
38         std::string devStr = data.GetData();
39         DelayedSingleton<PrintUsbManager>::GetInstance()->DealUsbDevStatusChange(devStr, true);
40     } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USB_DEVICE_DETACHED) {
41         std::string devStr = data.GetData();
42         DelayedSingleton<PrintHttpServerManager>::GetInstance()->DealUsbDevDetach(devStr);
43         DelayedSingleton<PrintUsbManager>::GetInstance()->DealUsbDevStatusChange(devStr, false);
44     }
45 #endif // IPPOVERUSB_ENABLE
46 
47     if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED) {
48         int32_t userId = data.GetCode();
49         PRINT_HILOGI("user switched, current userId: %{public}d", userId);
50         PrintServiceAbility::GetInstance()->NotifyCurrentUserChanged(userId);
51     } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED) {
52         int32_t userId = data.GetCode();
53         PRINT_HILOGI("user removed, removed userId: %{public}d", userId);
54         PrintServiceAbility::GetInstance()->CancelUserPrintJobs(userId);
55     }
56 }
57 
58 }  // namespace Print
59 }  // namespace OHOS