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 
16 #include "volume/notification.h"
17 
18 #include "common_event_data.h"
19 #include "common_event_manager.h"
20 #include "common_event_support.h"
21 #include "int_wrapper.h"
22 #include "storage_service_log.h"
23 #include "string_wrapper.h"
24 #include "volume_core.h"
25 #include "want.h"
26 #include "want_params.h"
27 
28 namespace OHOS {
29 namespace StorageManager {
Notification()30 Notification::Notification() {}
~Notification()31 Notification::~Notification() {}
32 
NotifyVolumeChange(VolumeState notifyCode,std::shared_ptr<VolumeExternal> volume)33 void Notification::NotifyVolumeChange(VolumeState notifyCode, std::shared_ptr<VolumeExternal> volume)
34 {
35     AAFwk::Want want;
36     AAFwk::WantParams wantParams;
37     if (volume == nullptr) {
38         LOGE("Notification::NotifyVolumeChange volume is nullptr");
39         return;
40     }
41     wantParams.SetParam("id", AAFwk::String::Box(volume->GetId()));
42     wantParams.SetParam("diskId", AAFwk::String::Box(volume->GetDiskId()));
43     wantParams.SetParam("fsUuid", AAFwk::String::Box(volume->GetUuid()));
44     wantParams.SetParam("flags", AAFwk::Integer::Box(volume->GetFlags()));
45     switch (notifyCode) {
46         case VolumeState::REMOVED:
47             LOGI("notifycode: VOLUME_REMOVED");
48             want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_VOLUME_REMOVED);
49             break;
50         case VolumeState::UNMOUNTED:
51             LOGI("notifycode: VOLUME_UNMOUNTED");
52             wantParams.SetParam("volumeState", AAFwk::Integer::Box(UNMOUNTED));
53             want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_VOLUME_UNMOUNTED);
54             break;
55         case VolumeState::MOUNTED:
56             LOGI("notifycode: VOLUME_MOUNTED");
57             wantParams.SetParam("volumeState", AAFwk::Integer::Box(MOUNTED));
58             wantParams.SetParam("path", AAFwk::String::Box(volume->GetPath()));
59             wantParams.SetParam("fsType", AAFwk::Integer::Box(volume->GetFsType()));
60             want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_VOLUME_MOUNTED);
61             break;
62         case VolumeState::BAD_REMOVAL:
63             LOGI("notifycode: VOLUME_BAD_REMOVAL");
64             want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_VOLUME_BAD_REMOVAL);
65             break;
66         case VolumeState::EJECTING:
67             LOGI("notifycode: VOLUME_EJECT");
68             wantParams.SetParam("volumeState", AAFwk::Integer::Box(EJECTING));
69             want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_VOLUME_EJECT);
70             break;
71         default: {
72             break;
73         }
74     }
75     want.SetParams(wantParams);
76     EventFwk::CommonEventData commonData { want };
77     EventFwk::CommonEventManager::PublishCommonEvent(commonData);
78 }
79 }
80 } // namespace OHOS
81