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