1 /*
2  * Copyright (C) 2022 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 "MtpService"
16 #include "mtp_service.h"
17 #include "media_log.h"
18 #include "mtp_global.h"
19 #include "mtp_media_library.h"
20 
21 using namespace std;
22 namespace OHOS {
23 namespace Media {
MtpService(void)24 MtpService::MtpService(void) : monitorPtr_(nullptr), isMonitorRun_(false)
25 {
26 }
27 
Init()28 void MtpService::Init()
29 {
30     if (monitorPtr_ == nullptr) {
31         monitorPtr_ = make_shared<MtpMonitor>();
32     }
33 }
34 
StartService()35 void MtpService::StartService()
36 {
37     MEDIA_INFO_LOG("MtpService::StartService");
38     {
39         std::unique_lock lock(mutex_);
40         Init();
41         CHECK_AND_RETURN_LOG(!isMonitorRun_, "MtpService::StartService -- monitor is already running, return");
42         CHECK_AND_RETURN_LOG(monitorPtr_ != nullptr, "MtpService::StartService monitorPtr_ is nullptr");
43         MtpGlobal::ResetBlockStatus();
44         monitorPtr_->Start();
45         isMonitorRun_ = true;
46     }
47 }
48 
StopService()49 void MtpService::StopService()
50 {
51     MEDIA_INFO_LOG("MtpService::StopService");
52     {
53         std::unique_lock lock(mutex_);
54         CHECK_AND_RETURN_LOG(isMonitorRun_, "MtpService::StopService -- monitor is not running, return");
55         CHECK_AND_RETURN_LOG(monitorPtr_ != nullptr, "MtpService::StopService monitorPtr_ is nullptr");
56         monitorPtr_->Stop();
57         isMonitorRun_ = false;
58         monitorPtr_.reset();
59         // after stop mtp service, clear the unordered_map memory of the MtpMediaLibrary
60         MtpMediaLibrary::GetInstance()->Clear();
61     }
62 }
63 } // namespace Media
64 } // namespace OHOS