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 {
25 // LCOV_EXCL_START
StartMediaAnalysisServiceSync(int32_t code,const std::vector<std::string> & fileIds)26 void MediaAnalysisHelper::StartMediaAnalysisServiceSync(int32_t code, const std::vector<std::string> &fileIds)
27 {
28 MessageOption option(MessageOption::TF_SYNC);
29 StartMediaAnalysisServiceInternal(code, option, fileIds);
30 }
31
StartMediaAnalysisServiceAsync(int32_t code,const std::vector<std::string> & uris)32 void MediaAnalysisHelper::StartMediaAnalysisServiceAsync(int32_t code, const std::vector<std::string> &uris)
33 {
34 std::vector<std::string> fileIds;
35 if (!uris.empty()) {
36 for (auto uri: uris) {
37 fileIds.push_back(MediaFileUri(uri).GetFileId());
38 }
39 }
40 MessageOption option(MessageOption::TF_ASYNC);
41 std::thread(&StartMediaAnalysisServiceInternal, code, option, fileIds).detach();
42 }
43
AsyncStartMediaAnalysisService(int32_t code,const std::vector<std::string> & albumIds)44 void MediaAnalysisHelper::AsyncStartMediaAnalysisService(int32_t code, const std::vector<std::string> &albumIds)
45 {
46 MessageOption option(MessageOption::TF_ASYNC);
47 std::thread(&StartMediaAnalysisServiceInternal, code, option, albumIds).detach();
48 }
49
StartMediaAnalysisServiceInternal(int32_t code,MessageOption option,std::vector<std::string> fileIds)50 void MediaAnalysisHelper::StartMediaAnalysisServiceInternal(int32_t code, MessageOption option,
51 std::vector<std::string> fileIds)
52 {
53 MessageParcel data;
54 MessageParcel reply;
55 MediaAnalysisProxy mediaAnalysisProxy(nullptr);
56 data.WriteInterfaceToken(mediaAnalysisProxy.GetDescriptor());
57 if (!fileIds.empty()) {
58 data.WriteStringVector(fileIds);
59 }
60 if (!mediaAnalysisProxy.SendTransactCmd(code, data, reply, option)) {
61 MEDIA_ERR_LOG("Start Analysis Service faile: %{public}d", code);
62 }
63 }
64
StartPortraitCoverSelectionAsync(const std::string albumId)65 void MediaAnalysisHelper::StartPortraitCoverSelectionAsync(const std::string albumId)
66 {
67 if (albumId.empty()) {
68 MEDIA_ERR_LOG("StartPortraitCoverSelectionAsync albumId is empty");
69 return;
70 }
71
72 std::thread(&AnalysePortraitCover, albumId).detach();
73 }
74
AnalysePortraitCover(const std::string albumId)75 void MediaAnalysisHelper::AnalysePortraitCover(const std::string albumId)
76 {
77 int32_t code = IMediaAnalysisService::ActivateServiceType::PORTRAIT_COVER_SELECTION;
78 MessageParcel data;
79 MessageParcel reply;
80 MessageOption option(MessageOption::TF_ASYNC);
81 MediaAnalysisProxy mediaAnalysisProxy(nullptr);
82 data.WriteInterfaceToken(mediaAnalysisProxy.GetDescriptor());
83
84 if (!data.WriteString(albumId)) {
85 MEDIA_ERR_LOG("Portrait Cover Selection Write albumId failed");
86 return;
87 }
88
89 if (!data.WriteRemoteObject(new MediaAnalysisCallbackStub())) {
90 MEDIA_ERR_LOG("Portrait Cover Selection Write MediaAnalysisCallbackStub failed");
91 return;
92 }
93
94 if (!mediaAnalysisProxy.SendTransactCmd(code, data, reply, option)) {
95 MEDIA_ERR_LOG("Actively Calling Analysis For Portrait Cover Selection failed");
96 }
97 }
98
ParseGeoInfo(const std::vector<std::string> geoInfo,const bool isForceQuery)99 bool MediaAnalysisHelper::ParseGeoInfo(const std::vector<std::string> geoInfo, const bool isForceQuery)
100 {
101 MessageParcel data;
102 MediaAnalysisProxy mediaAnalysisProxy(nullptr);
103
104 if (!data.WriteInterfaceToken(mediaAnalysisProxy.GetDescriptor())) {
105 MEDIA_ERR_LOG("Parse Geographic Information Write InterfaceToken failed");
106 return false;
107 }
108
109 if (!data.WriteStringVector(geoInfo)) {
110 MEDIA_ERR_LOG("Parse Geographic Information Write fileId, latitude, longitude failed");
111 return false;
112 }
113
114 int32_t code = IMediaAnalysisService::ActivateServiceType::PARSE_GEO_INFO;
115 if (isForceQuery) {
116 code = IMediaAnalysisService::ActivateServiceType::PARSE_GEO_INFO_LIST;
117 }
118 MessageParcel reply;
119 MessageOption option(MessageOption::TF_SYNC);
120 if (!mediaAnalysisProxy.SendTransactCmd(code, data, reply, option)) {
121 MEDIA_ERR_LOG("Actively Calling Analysis For Parse Geographic Information failed");
122 return false;
123 }
124
125 std::string addressDescription = reply.ReadString();
126 MEDIA_INFO_LOG("ParseGeoInfo success, fileId: %{private}s, addressDescription: %{private}s",
127 geoInfo.front().c_str(), addressDescription.c_str());
128 return true;
129 }
130
StartForegroundAnalysisServiceSync(int32_t code,const std::vector<std::string> & fileIds,int32_t taskId)131 void MediaAnalysisHelper::StartForegroundAnalysisServiceSync(int32_t code, const std::vector<std::string> &fileIds,
132 int32_t taskId)
133 {
134 MessageParcel data;
135 MediaAnalysisProxy mediaAnalysisProxy(nullptr);
136 if (!data.WriteInterfaceToken(mediaAnalysisProxy.GetDescriptor())) {
137 MEDIA_ERR_LOG("Write InterfaceToken failed");
138 return;
139 }
140
141 if (!data.WriteInt32(taskId)) {
142 MEDIA_ERR_LOG("Write taskid failed");
143 return;
144 }
145
146 if (!fileIds.empty()) {
147 if (!data.WriteStringVector(fileIds)) {
148 MEDIA_ERR_LOG("write fileIds failed");
149 return;
150 }
151 }
152
153 MessageParcel reply;
154 MessageOption option(MessageOption::TF_SYNC);
155 if (!mediaAnalysisProxy.SendTransactCmd(code, data, reply, option)) {
156 MEDIA_ERR_LOG("Actively Calling Analysis For Foreground failed");
157 return;
158 }
159 }
160 // LCOV_EXCL_STOP
161 } // namespace Media
162 } // namespace OHOS