1 /*
2 * Copyright (c) 2022 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 "adapter/ohos/entrance/data_ability_helper_standard.h"
17
18 #if !defined(PREVIEW)
19 #include <dlfcn.h>
20 #endif
21
22 #include "data_ability_helper.h"
23
24 #include "base/utils/string_utils.h"
25 #include "utils.h"
26
27 namespace OHOS::Ace {
28 const std::string MEDIA_SERVER_HEAD = "datashare:///media";
29 const std::string IMAGE_PATH_DATA = "data";
30 const std::string MOVINGPHOTO_IMAGE_COVERPOSITION = "cover_positon";
31 const std::string IMAGE_URI = "uri";
32
33 template<typename ResultSet>
GetStringVal(const std::string & field,const ResultSet & result)34 static inline std::string GetStringVal(const std::string &field, const ResultSet &result)
35 {
36 return std::get<std::string>(ResultSetUtils::GetValFromColumn(field, result, ResultSetDataType::TYPE_STRING));
37 }
38 template<typename ResultSet>
GetInt32Val(const std::string & field,const ResultSet & result)39 static inline int32_t GetInt32Val(const std::string &field, const ResultSet &result)
40 {
41 return std::get<int32_t>(ResultSetUtils::GetValFromColumn(field, result, ResultSetDataType::TYPE_INT32));
42 }
43 template<typename ResultSet>
GetInt64Val(const std::string & field,const ResultSet & result)44 static inline int64_t GetInt64Val(const std::string &field, const ResultSet &result)
45 {
46 return std::get<int64_t>(ResultSetUtils::GetValFromColumn(field, result, ResultSetDataType::TYPE_INT64));
47 }
48 template<typename ResultSet>
GetDoubleVal(const std::string & field,const ResultSet & result)49 static inline double GetDoubleVal(const std::string &field, const ResultSet &result)
50 {
51 return std::get<double>(ResultSetUtils::GetValFromColumn(field, result, ResultSetDataType::TYPE_DOUBLE));
52 }
53
DataAbilityHelperStandard(const std::shared_ptr<OHOS::AppExecFwk::Context> & context,const std::shared_ptr<OHOS::AbilityRuntime::Context> & runtimeContext,bool useStageModel)54 DataAbilityHelperStandard::DataAbilityHelperStandard(const std::shared_ptr<OHOS::AppExecFwk::Context>& context,
55 const std::shared_ptr<OHOS::AbilityRuntime::Context>& runtimeContext, bool useStageModel)
56 : useStageModel_(useStageModel)
57 {
58 if (useStageModel) {
59 runtimeContext_ = runtimeContext;
60 #ifdef MEDIA_LIBRARY_EXISTS
61 mgr_.InitMediaLibraryManager(runtimeContext->GetToken());
62 #endif
63 } else {
64 context_ = context;
65 #ifdef MEDIA_LIBRARY_EXISTS
66 mgr_.InitMediaLibraryManager(context->GetToken());
67 #endif
68 }
69 }
70
QueryThumbnailResFromDataAbility(const std::string & uri)71 void* DataAbilityHelperStandard::QueryThumbnailResFromDataAbility(const std::string& uri)
72 {
73 #ifdef PREVIEW
74 return nullptr;
75 #else
76 #ifdef MEDIA_LIBRARY_EXISTS
77 ACE_FUNCTION_TRACE();
78 Uri fileUri(uri);
79 return mgr_.GetThumbnail(fileUri).release();
80 #else
81 return nullptr;
82 #endif
83 #endif
84 }
85
GetMovingPhotoImageUri(const std::string & uri)86 std::string DataAbilityHelperStandard::GetMovingPhotoImageUri(const std::string& uri)
87 {
88 #ifdef MEDIA_LIBRARY_EXISTS
89 return mgr_.GetMovingPhotoImageUri(uri);
90 #else
91 return "";
92 #endif
93 }
94
GetMovingPhotoDateModified(const std::string & uri)95 int64_t DataAbilityHelperStandard::GetMovingPhotoDateModified(const std::string& uri)
96 {
97 #ifdef MEDIA_LIBRARY_EXISTS
98 return mgr_.GetMovingPhotoDateModified(uri);
99 #else
100 return -1;
101 #endif
102 }
103
GetMovingPhotoCoverPosition(const std::string & columnName,const std::string & value,std::vector<std::string> & columns)104 int64_t DataAbilityHelperStandard::GetMovingPhotoCoverPosition(const std::string& columnName,
105 const std::string& value,
106 std::vector<std::string>& columns)
107 {
108 #ifdef MEDIA_LIBRARY_EXISTS
109 auto resultSet = mgr_.GetResultSetFromDb(columnName, value, columns);
110 if (resultSet == nullptr || resultSet->GoToFirstRow() != 0) {
111 LOGE("Result is nullptr or GoToFirstRow failed.");
112 return -1;
113 }
114 int64_t coverPosition = GetInt64Val(MOVINGPHOTO_IMAGE_COVERPOSITION, resultSet);
115 return coverPosition;
116 #else
117 return -1;
118 #endif
119 }
120
GetMovingPhotoImagePath(const std::string & uri)121 std::string DataAbilityHelperStandard::GetMovingPhotoImagePath(const std::string& uri)
122 {
123 #ifdef MEDIA_LIBRARY_EXISTS
124 std::vector<std::string> columns = {IMAGE_PATH_DATA};
125 auto resultSet = mgr_.GetResultSetFromDb(IMAGE_URI, uri, columns);
126 if (resultSet == nullptr || resultSet->GoToFirstRow() != 0) {
127 LOGE("Result is nullptr or GoToFirstRow failed.");
128 return "";
129 }
130 std::string path = GetStringVal(IMAGE_PATH_DATA, resultSet);
131 LOGI("resultSet path : %{public}s.", path.c_str());
132 return path;
133 #else
134 return "";
135 #endif
136 }
137
OpenFile(const std::string & uriStr,const std::string & mode)138 int32_t DataAbilityHelperStandard::OpenFile(const std::string& uriStr, const std::string& mode)
139 {
140 // FA model always uses DataAbility
141 if (!useStageModel_ || StringUtils::StartWith(uriStr, "dataability://")) {
142 return OpenFileWithDataAbility(uriStr, mode);
143 }
144 if (StringUtils::StartWith(uriStr, "datashare://") || StringUtils::StartWith(uriStr, "file://")) {
145 return OpenFileWithDataShare(uriStr, mode);
146 }
147 LOGE("DataAbilityHelperStandard OpenFile uri is not support.");
148 return -1;
149 }
150
OpenFileWithDataAbility(const std::string & uriStr,const std::string & mode)151 int32_t DataAbilityHelperStandard::OpenFileWithDataAbility(const std::string& uriStr, const std::string& mode)
152 {
153 std::shared_ptr<OHOS::Uri> uri = std::make_shared<Uri>(uriStr);
154 if (!dataAbilityHelper_) {
155 if (useStageModel_) {
156 dataAbilityHelper_ = AppExecFwk::DataAbilityHelper::Creator(runtimeContext_.lock(), uri, false);
157 } else {
158 dataAbilityHelper_ = AppExecFwk::DataAbilityHelper::Creator(context_.lock(), uri);
159 }
160 }
161
162 CHECK_NULL_RETURN(dataAbilityHelper_, -1);
163 return dataAbilityHelper_->OpenFile(*uri, mode);
164 }
165
OpenFileWithDataShare(const std::string & uriStr,const std::string & mode)166 int32_t DataAbilityHelperStandard::OpenFileWithDataShare(const std::string& uriStr, const std::string& mode)
167 {
168 auto context = runtimeContext_.lock();
169 if (useStageModel_ && !dataShareHelper_ && context) {
170 dataShareHelper_ = DataShare::DataShareHelper::Creator(context->GetToken(), MEDIA_SERVER_HEAD);
171 }
172
173 CHECK_NULL_RETURN(dataShareHelper_, -1);
174 Uri uri = Uri(uriStr);
175 return dataShareHelper_->OpenFile(uri, mode);
176 }
177
ReadMovingPhotoVideo(const std::string & uri)178 int32_t DataAbilityHelperStandard::ReadMovingPhotoVideo(const std::string &uri)
179 {
180 #ifdef MEDIA_LIBRARY_EXISTS
181 return mgr_.ReadMovingPhotoVideo(uri);
182 #else
183 return -1;
184 #endif
185 }
186
187 } // namespace OHOS::Ace
188