• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 #include "ext_storage_subscriber.h"
16 
17 #include <cinttypes>
18 
19 #include "appexecfwk_errors.h"
20 #include "bundle_info.h"
21 #include "common_event_manager.h"
22 #include "common_event_support.h"
23 #include "log.h"
24 #include "string_wrapper.h"
25 #include "int_wrapper.h"
26 #include "want.h"
27 
28 using namespace OHOS::AAFwk;
29 namespace OHOS {
30 namespace FileManagerService {
31 /**
32  * @brief Receiver Constructor.
33  * @param subscriberInfo Subscriber info.
34  */
ExtStorageSubscriber(const EventFwk::CommonEventSubscribeInfo & subscriberInfo)35 ExtStorageSubscriber::ExtStorageSubscriber(const EventFwk::CommonEventSubscribeInfo &subscriberInfo)
36     : EventFwk::CommonEventSubscriber(subscriberInfo)
37 {}
38 
39 std::shared_ptr<ExtStorageSubscriber> ExtStorageSubscriber_ = nullptr;
Subscriber(void)40 bool ExtStorageSubscriber::Subscriber(void)
41 {
42     if (ExtStorageSubscriber_ == nullptr) {
43         EventFwk::MatchingSkills matchingSkills;
44         matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_DISK_UNMOUNTED);
45         matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_DISK_MOUNTED);
46 
47         EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
48         ExtStorageSubscriber_ = std::make_shared<ExtStorageSubscriber>(subscribeInfo);
49         EventFwk::CommonEventManager::SubscribeCommonEvent(ExtStorageSubscriber_);
50     }
51     return true;
52 }
53 
54 /**
55  * @brief Receive common event.
56  * @param eventData Common event data.
57  */
OnReceiveEvent(const EventFwk::CommonEventData & eventData)58 void ExtStorageSubscriber::OnReceiveEvent(const EventFwk::CommonEventData &eventData)
59 {
60     const AAFwk::Want& want = eventData.GetWant();
61     std::string action = want.GetAction();
62     DEBUG_LOG("%{public}s, action:%{public}s.", __func__, action.c_str());
63 
64     const AAFwk::WantParams wantParams = want.GetParams();
65     std::string id = AAFwk::String::Unbox(AAFwk::IString::Query(wantParams.GetParam("id")));
66     std::string diskId = AAFwk::String::Unbox(AAFwk::IString::Query(wantParams.GetParam("diskId")));
67     DEBUG_LOG("%{public}s, id:%{public}s.", __func__, id.c_str());
68     DEBUG_LOG("%{public}s, diskId:%{public}s.", __func__, diskId.c_str());
69 
70     if (action == EventFwk::CommonEventSupport::COMMON_EVENT_DISK_MOUNTED) {
71         int32_t volumeState = AAFwk::Integer::Unbox(AAFwk::IInteger::Query(wantParams.GetParam("volumeState")));
72         std::string fsUuid = AAFwk::String::Unbox(AAFwk::IString::Query(wantParams.GetParam("fsUuid")));
73         std::string path = AAFwk::String::Unbox(AAFwk::IString::Query(wantParams.GetParam("path")));
74         DEBUG_LOG("%{public}s, volumeState:%{public}d.", __func__, volumeState);
75         DEBUG_LOG("%{public}s, id:%{public}s, fsUuid:%{public}s, path:%{public}s.",
76             __func__, id.c_str(), fsUuid.c_str(), path.c_str());
77 
78         ExtStorageStatus extStatus(id, diskId, fsUuid, path, VolumeState(volumeState));
79         mountStatus.insert(std::pair<std::string, ExtStorageStatus>(path, extStatus));
80     }
81 }
82 
CheckMountPoint(const std::string & path)83 bool ExtStorageSubscriber::CheckMountPoint(const std::string &path)
84 {
85     auto extStorageStatus = mountStatus.find(path);
86     if (extStorageStatus == mountStatus.end()) {
87         return false;
88     } else {
89         if (extStorageStatus->second.GetVolumeState() == VolumeState::MOUNTED) {
90             return true;
91         }
92         return false;
93     }
94 }
95 }  // namespace FileManagerService
96 }  // namespace OHOS