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
ParseGeoInfo(const std::vector<std::string> geoInfo,const bool isForceQuery)98 bool MediaAnalysisHelper::ParseGeoInfo(const std::vector<std::string> geoInfo, const bool isForceQuery)
99 {
100 MessageParcel data;
101 MediaAnalysisProxy mediaAnalysisProxy(nullptr);
102
103 if (!data.WriteInterfaceToken(mediaAnalysisProxy.GetDescriptor())) {
104 MEDIA_ERR_LOG("Parse Geographic Information Write InterfaceToken failed");
105 return false;
106 }
107
108 if (!data.WriteStringVector(geoInfo)) {
109 MEDIA_ERR_LOG("Parse Geographic Information Write fileId, latitude, longitude failed");
110 return false;
111 }
112
113 int32_t code = IMediaAnalysisService::ActivateServiceType::PARSE_GEO_INFO;
114 if (isForceQuery) {
115 code = IMediaAnalysisService::ActivateServiceType::PARSE_GEO_INFO_LIST;
116 }
117 MessageParcel reply;
118 MessageOption option(MessageOption::TF_SYNC);
119 if (!mediaAnalysisProxy.SendTransactCmd(code, data, reply, option)) {
120 MEDIA_ERR_LOG("Actively Calling Analysis For Parse Geographic Information failed");
121 return false;
122 }
123
124 std::string addressDescription = reply.ReadString();
125 MEDIA_INFO_LOG("ParseGeoInfo success, fileId: %{public}s, addressDescription: %{private}s", geoInfo.front().c_str(),
126 addressDescription.c_str());
127 return true;
128 }
129
StartForegroundAnalysisServiceSync(int32_t code,const std::vector<std::string> & fileIds,int32_t taskId)130 void MediaAnalysisHelper::StartForegroundAnalysisServiceSync(int32_t code, const std::vector<std::string> &fileIds,
131 int32_t taskId)
132 {
133 MessageParcel data;
134 MediaAnalysisProxy mediaAnalysisProxy(nullptr);
135 if (!data.WriteInterfaceToken(mediaAnalysisProxy.GetDescriptor())) {
136 MEDIA_ERR_LOG("Write InterfaceToken failed");
137 return;
138 }
139
140 if (!data.WriteInt32(taskId)) {
141 MEDIA_ERR_LOG("Write taskid failed");
142 return;
143 }
144
145 if (!fileIds.empty()) {
146 if (!data.WriteStringVector(fileIds)) {
147 MEDIA_ERR_LOG("write fileIds failed");
148 return;
149 }
150 }
151
152 MessageParcel reply;
153 MessageOption option(MessageOption::TF_SYNC);
154 if (!mediaAnalysisProxy.SendTransactCmd(code, data, reply, option)) {
155 MEDIA_ERR_LOG("Actively Calling Analysis For Foreground failed");
156 return;
157 }
158 }
159 } // namespace Media
160 } // namespace OHOS