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 
16 #include "media_analysis_callback_stub.h"
17 
18 #include "media_log.h"
19 #include "media_file_utils.h"
20 #include "medialibrary_errno.h"
21 #include "medialibrary_notify.h"
22 #include "photo_album_column.h"
23 
24 namespace OHOS {
25 namespace Media {
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)26 int MediaAnalysisCallbackStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
27     MessageOption &option)
28 {
29     int errCode = ERR_UNKNOWN_TRANSACTION;
30 
31     CHECK_AND_RETURN_RET(data.ReadInterfaceToken() == GetDescriptor(), errCode);
32     std::string albumId = data.ReadString();
33     switch (code) {
34         case static_cast<uint32_t>(MediaAnalysisCallbackInterfaceCode::PORTRAIT_COVER_SELECTION_COMPLETED_CALLBACK):
35             errCode = MediaAnalysisCallbackStub::PortraitCoverSelectionCompleted(albumId);
36             break;
37         default:
38             MEDIA_ERR_LOG("MediaAnalysisCallbackStub request code %{public}u not handled", code);
39             errCode = IPCObjectStub::OnRemoteRequest(code, data, reply, option);
40             break;
41     }
42 
43     return errCode;
44 }
45 
PortraitCoverSelectionCompleted(const std::string albumId)46 int32_t MediaAnalysisCallbackStub::PortraitCoverSelectionCompleted(const std::string albumId)
47 {
48     MEDIA_INFO_LOG("PortraitCoverSelectionCompleted callback start, albumId: %{public}s", albumId.c_str());
49 
50     if (albumId.empty()) {
51         MEDIA_ERR_LOG("PortraitCoverSelectionCompleted callback error, albumId is empty");
52         return ERR_INVALID_DATA;
53     }
54 
55     auto watch = MediaLibraryNotify::GetInstance();
56     if (watch == nullptr) {
57         MEDIA_ERR_LOG("PortraitCoverSelectionCompleted Can not get MediaLibraryNotify Instance");
58         return ERR_NULL_OBJECT;
59     }
60 
61     int32_t ret =
62         watch->Notify(MediaFileUtils::GetUriByExtrConditions(PhotoAlbumColumns::ANALYSIS_ALBUM_URI_PREFIX, albumId),
63         NotifyType::NOTIFY_UPDATE, std::stoi(albumId));
64     if (ret != E_OK) {
65         MEDIA_ERR_LOG("PortraitCoverSelectionCompleted Notify error: %{public}d", ret);
66         return ret;
67     }
68 
69     MEDIA_INFO_LOG("PortraitCoverSelectionCompleted callback end, albumId: %{public}s", albumId.c_str());
70     return ERR_NONE;
71 }
72 } // namespace Media
73 } // namespace OHOS