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