1 /*
2  * Copyright (C) 2024 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 #define MLOG_TAG "MtpSubscriber"
16 
17 #include "mtp_subscriber.h"
18 #include "common_event_manager.h"
19 #include "common_event_support.h"
20 #include "media_log.h"
21 #include "usb_srv_support.h"
22 #include "mtp_service.h"
23 #include "mtp_manager.h"
24 
25 namespace OHOS {
26 namespace Media {
27 
MtpSubscriber(const EventFwk::CommonEventSubscribeInfo & subscriberInfo)28 MtpSubscriber::MtpSubscriber(const EventFwk::CommonEventSubscribeInfo &subscriberInfo)
29     : EventFwk::CommonEventSubscriber(subscriberInfo)
30 {
31 }
32 
Subscribe(void)33 bool MtpSubscriber::Subscribe(void)
34 {
35     EventFwk::MatchingSkills matchingSkills;
36     matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USB_STATE);
37     EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
38     std::shared_ptr<MtpSubscriber> subscriber = std::make_shared<MtpSubscriber>(subscribeInfo);
39     return EventFwk::CommonEventManager::SubscribeCommonEvent(subscriber);
40 }
41 
OnReceiveEvent(const EventFwk::CommonEventData & eventData)42 void MtpSubscriber::OnReceiveEvent(const EventFwk::CommonEventData &eventData)
43 {
44     MEDIA_INFO_LOG("MtpSubscriber OnReceiveEvent");
45     const AAFwk::Want &want = eventData.GetWant();
46     std::string action = want.GetAction();
47     if (action != EventFwk::CommonEventSupport::COMMON_EVENT_USB_STATE) {
48         MEDIA_INFO_LOG("MtpSubscriber OnReceiveEvent action = %{public}s", action.c_str());
49     }
50     for (std::string k : want.GetParams().KeySet()) {
51         MEDIA_INFO_LOG("MtpSubscriber OnReceiveEvent key = %{public}s", k.c_str());
52     }
53     bool isConnected = want.GetBoolParam(std::string {USB::UsbSrvSupport::CONNECTED}, false);
54     MEDIA_INFO_LOG("MtpSubscriber OnReceiveEvent eventData.GetCode = %{public}d isConnected= %{public}d",
55         eventData.GetCode(), isConnected);
56     if (!isConnected) {
57         MEDIA_INFO_LOG("MtpSubscriber OnReceiveEvent USB disconnected");
58         MtpManager::GetInstance().StopMtpService();
59         return;
60     }
61     MtpManager::GetInstance().StopMtpService();
62     bool isMtp = want.GetBoolParam(std::string {USB::UsbSrvSupport::FUNCTION_NAME_MTP}, false);
63     if (isMtp) {
64         MEDIA_INFO_LOG("MtpSubscriber OnReceiveEvent USB MTP connected");
65         MtpManager::GetInstance().StartMtpService(MtpManager::MtpMode::MTP_MODE);
66         return;
67     }
68     bool isPtp = want.GetBoolParam(std::string {USB::UsbSrvSupport::FUNCTION_NAME_PTP}, false);
69     if (isPtp) {
70         MEDIA_INFO_LOG("MtpSubscriber OnReceiveEvent USB PTP connected");
71         MtpManager::GetInstance().StartMtpService(MtpManager::MtpMode::PTP_MODE);
72         return;
73     }
74     MEDIA_INFO_LOG("MtpSubscriber OnReceiveEvent USB NOT MTP/PTP, Only HDC");
75     return;
76 }
77 } // namespace Media
78 } // namespace OHOS
79