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_helper.h"
17 
18 #include <thread>
19 
20 #include "media_analysis_callback_stub.h"
21 #include "media_file_uri.h"
22 
23 namespace OHOS {
24 namespace Media {
StartMediaAnalysisServiceSync(int32_t code,const std::vector<std::string> & fileIds)25 void MediaAnalysisHelper::StartMediaAnalysisServiceSync(int32_t code, const std::vector<std::string> &fileIds)
26 {
27     MessageOption option(MessageOption::TF_SYNC);
28     StartMediaAnalysisServiceInternal(code, option, fileIds);
29 }
30 
StartMediaAnalysisServiceAsync(int32_t code,const std::vector<std::string> & uris)31 void MediaAnalysisHelper::StartMediaAnalysisServiceAsync(int32_t code, const std::vector<std::string> &uris)
32 {
33     std::vector<std::string> fileIds;
34     if (!uris.empty()) {
35         for (auto uri: uris) {
36             fileIds.push_back(MediaFileUri(uri).GetFileId());
37         }
38     }
39     MessageOption option(MessageOption::TF_ASYNC);
40     std::thread(&StartMediaAnalysisServiceInternal, code, option, fileIds).detach();
41 }
42 
AsyncStartMediaAnalysisService(int32_t code,const std::vector<std::string> & albumIds)43 void MediaAnalysisHelper::AsyncStartMediaAnalysisService(int32_t code, const std::vector<std::string> &albumIds)
44 {
45     MessageOption option(MessageOption::TF_ASYNC);
46     std::thread(&StartMediaAnalysisServiceInternal, code, option, albumIds).detach();
47 }
48 
StartMediaAnalysisServiceInternal(int32_t code,MessageOption option,std::vector<std::string> fileIds)49 void MediaAnalysisHelper::StartMediaAnalysisServiceInternal(int32_t code, MessageOption option,
50     std::vector<std::string> fileIds)
51 {
52     MessageParcel data;
53     MessageParcel reply;
54     MediaAnalysisProxy mediaAnalysisProxy(nullptr);
55     data.WriteInterfaceToken(mediaAnalysisProxy.GetDescriptor());
56     if (!fileIds.empty()) {
57         data.WriteStringVector(fileIds);
58     }
59     if (!mediaAnalysisProxy.SendTransactCmd(code, data, reply, option)) {
60         MEDIA_ERR_LOG("Start Analysis Service faile: %{public}d", code);
61     }
62 }
63 
StartPortraitCoverSelectionAsync(const std::string albumId)64 void MediaAnalysisHelper::StartPortraitCoverSelectionAsync(const std::string albumId)
65 {
66     if (albumId.empty()) {
67         MEDIA_ERR_LOG("StartPortraitCoverSelectionAsync albumId is empty");
68         return;
69     }
70 
71     std::thread(&AnalysePortraitCover, albumId).detach();
72 }
73 
AnalysePortraitCover(const std::string albumId)74 void MediaAnalysisHelper::AnalysePortraitCover(const std::string albumId)
75 {
76     int32_t code = IMediaAnalysisService::ActivateServiceType::PORTRAIT_COVER_SELECTION;
77     MessageParcel data;
78     MessageParcel reply;
79     MessageOption option(MessageOption::TF_ASYNC);
80     MediaAnalysisProxy mediaAnalysisProxy(nullptr);
81     data.WriteInterfaceToken(mediaAnalysisProxy.GetDescriptor());
82 
83     if (!data.WriteString(albumId)) {
84         MEDIA_ERR_LOG("Portrait Cover Selection Write albumId failed");
85         return;
86     }
87 
88     if (!data.WriteRemoteObject(new MediaAnalysisCallbackStub())) {
89         MEDIA_ERR_LOG("Portrait Cover Selection Write MediaAnalysisCallbackStub failed");
90         return;
91     }
92 
93     if (!mediaAnalysisProxy.SendTransactCmd(code, data, reply, option)) {
94         MEDIA_ERR_LOG("Actively Calling Analysis For Portrait Cover Selection failed");
95     }
96 }
97 } // namespace Media
98 } // namespace OHOS