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