1 /*
2  * Copyright (C) 2021-2023 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 #ifndef INTERFACES_INNERKITS_NATIVE_INCLUDE_MEDIA_LIBRARY_MANAGER_H_
17 #define INTERFACES_INNERKITS_NATIVE_INCLUDE_MEDIA_LIBRARY_MANAGER_H_
18 #define USERID "100"
19 
20 #include "datashare_helper.h"
21 #include "media_volume.h"
22 #include "pixel_map.h"
23 #include "unique_fd.h"
24 #include "media_photo_asset_proxy.h"
25 
26 namespace OHOS {
27 namespace Media {
28 using namespace std;
29 using namespace OHOS::DataShare;
30 #define EXPORT __attribute__ ((visibility ("default")))
31 struct UriParams;
32 /**
33  * @brief Interface for accessing all the File operation and AlbumAsset operation APIs
34  *
35  * @since 1.0
36  * @version 1.0
37  */
38 enum class PhotoPermissionType : int32_t {
39     TEMPORARY_READ_IMAGEVIDEO = 0,
40     PERSIST_READ_IMAGEVIDEO,
41     TEMPORARY_WRITE_IMAGEVIDEO,
42     TEMPORARY_READWRITE_IMAGEVIDEO
43 };
44 
45 enum class HideSensitiveType : int32_t {
46     ALL_DESENSITIZE = 0,
47     GEOGRAPHIC_LOCATION_DESENSITIZE,
48     SHOOTING_PARAM_DESENSITIZE,
49     NO_DESENSITIZE
50 };
51 
52 class MediaLibraryManager {
53 public:
54     EXPORT MediaLibraryManager() = default;
55     EXPORT virtual ~MediaLibraryManager() = default;
56 
57     /**
58      * @brief Returns the Media Library Manager Instance
59      *
60      * @return Returns the Media Library Manager Instance
61      * @since 1.0
62      * @version 1.0
63      */
64     EXPORT static MediaLibraryManager *GetMediaLibraryManager();
65 
66     /**
67      * @brief Initializes the environment for Media Library Manager
68      *
69      * @param context The Ability context required for calling Data Ability Helper APIs
70      * @since 1.0
71      * @version 1.0
72      */
73     EXPORT void InitMediaLibraryManager(const sptr<IRemoteObject> &token);
74 
75     /**
76      * @brief Initializes the environment for Media Library Manager
77      *
78      * @since 1.0
79      * @version 1.0
80      */
81     EXPORT void InitMediaLibraryManager();
82 
83     /**
84      * @brief Close an opened file
85      *
86      * @param uri source uri of a file which is to be closed
87      * @param fd file descriptor for the file which is to be closed
88      * @return close status. <0> for success and <-1> for fail
89      * @since 1.0
90      * @version 1.0
91      */
92     EXPORT int32_t CloseAsset(const string &uri, const int32_t fd);
93 
94     /**
95      * @brief create an photo or video asset
96      *
97      * @param displayName file displayName
98      * @return asset uri for success and <""> for fail
99      * @since 1.0
100      * @version 1.0
101      */
102     EXPORT string CreateAsset(const string &displayName);
103 
104     /**
105      * @brief open photo or video
106      *
107      * @param uri uri of the asset
108      * @param openMode openMode "rw", "w", "r"
109      * @return fileDescriptor for success and <-1> for fail
110      * @since 1.0
111      * @version 1.0
112      */
113     EXPORT int32_t OpenAsset(string &uri, const string openMode);
114 
115     /**
116      * @brief Obtain a mediaVolume object from MediaAssets can be obtained
117      *
118      * @param MediaVolume MediaVolume for outValue
119      * @return errorcode
120      * @since 1.0
121      * @version 1.0
122      */
123     EXPORT int32_t QueryTotalSize(MediaVolume &outMediaVolume);
124 
125     /**
126      * @brief Query new uri by old uri
127      *
128      * @param uris old uris
129      * @return map of old uris to new uris
130      * @since 1.0
131      * @version 1.0
132      */
133     EXPORT std::unordered_map<std::string, std::string> GetUrisByOldUris(std::vector<std::string> uris);
134 
135     /**
136      * @brief Make a query from database
137      *
138      * @param columnName a column name in datebase
139      * @param value a parameter for input which is a uri or path
140      * @param columns query conditions
141      * @return query result
142      * @since 1.0
143      * @version 1.0
144      */
145     EXPORT static std::shared_ptr<DataShareResultSet> GetResultSetFromDb(string columnName, const string &value,
146         vector<string> &columns);
147 
148     /**
149      * @brief get file path from uri
150      *
151      * @param fileUri a parameter for input  which is uri
152      * @param filePath a parameter for output  which is path
153      * @param userId  a parameter for user id
154      * @return errorcode
155      * @since 1.0
156      * @version 1.0
157      */
158     EXPORT int32_t GetFilePathFromUri(const Uri &fileUri, std::string &filePath, string userId = USERID);
159 
160     /**
161      * @brief Obtain a mediaVolume object from MediaAssets can be obtained
162      *
163      * @param fileUri a parameter for output  which is uri
164      * @param filePath a parameter for input  which is path
165      * @param userId  a parameter for user id
166      * @return errorcode
167      * @since 1.0
168      * @version 1.0
169      */
170     EXPORT int32_t GetUriFromFilePath(const std::string &filePath, Uri &fileUri, string &userId);
171 
172     EXPORT std::unique_ptr<PixelMap> GetThumbnail(const Uri &uri);
173 
174     /**
175      * @brief Obtain a batch of astc data
176      *
177      * @param uriBatch parameter for input, indicates the range of astc data that needs to be obtained
178      * @param astcBatch parameter for output
179      * @return if obtain success, return 0; Otherwise return error code.
180      */
181     EXPORT int32_t GetBatchAstcs(
182         const std::vector<std::string> &uriBatch, std::vector<std::vector<uint8_t>> &astcBatch);
183 
184     /**
185      * @brief Obtain pixelmap of astc
186      *
187      * @param uri a parameter for input which is uri
188      * @return if obtain success, return PixelMap; Otherwise return nullptr
189      */
190     EXPORT std::unique_ptr<PixelMap> GetAstc(const Uri &uri);
191 
192     /**
193      * @brief Open video of moving photo to read
194      *
195      * @param uri asset uri of the moving photo
196      * @return read fd for success and <-1> for fail
197      */
198     EXPORT int32_t ReadMovingPhotoVideo(const string &uri);
199 
200     /**
201      * @brief Open private moving photo to read
202      *
203      * @param uri asset uri of the moving photo
204      * @return read fd for success and <-1> for fail
205      */
206     EXPORT int32_t ReadPrivateMovingPhoto(const string &uri);
207 
208     /**
209      * @brief Get image uri of moving photo
210      *
211      * @param uri asset uri of the moving photo
212      * @return image uri
213      */
214     EXPORT std::string GetMovingPhotoImageUri(const string &uri);
215 
216     /**
217      * @brief Get date modified of moving photo
218      *
219      * @param uri asset uri of the moving photo
220      * @return if obtain success, return date_modified; Otherwise return -1
221      */
222     EXPORT int64_t GetMovingPhotoDateModified(const string &uri);
223 
224     /**
225      * @brief Create PhotoAssetProxy
226      *
227      * @param cameraShotType a parameter for input, indicates camera shot type
228      * @param callingUid a parameter for input, indicates calling uid
229      * @param userId a parameter for input, indicates user id
230      * @return if obtain success, return PhotoAssetProxy; Otherwise return nullptr
231      */
232     EXPORT std::shared_ptr<PhotoAssetProxy> CreatePhotoAssetProxy(CameraShotType cameraShotType, uint32_t callingUid,
233         int32_t userId);
234        /**
235      * @brief Check PhotoUri Permission
236      *
237      * @param tokenId a parameter for input, indicating the expected app's tokenId to check
238      * @param appid a parameter for input, indicating the expected appid to check
239      * @param urisSource a parameter for input, indicating the source of URIs expected to check
240      * @param result a parameter for output, indicating the check result (permission granted or not)
241      * @param flag a parameter for input, indicating the expected type of permission check
242      * @return If the check is successful, return 0; otherwise, return -1 for failure.
243      */
244     EXPORT int32_t CheckPhotoUriPermission(uint32_t tokenId, const string &appid,
245         const std::vector<string> &urisSource, std::vector<bool> &result, uint32_t flag);
246 
247     /**
248      * @brief Grant PhotoUri Permission
249      *
250      * @param appid a parameter for input, indicating the calling appid
251      * @param uris a parameter for input, indicating the uris expected to grant permission
252      * @param photoPermissionType a parameter for input, indicating the expected grant permission type for photos
253      * @param hideSensitiveType a parameter for input, indicating the expected grant hideSensitiveType
254      * @return If the grant is successful, return 0; otherwise, return -1 for failure.
255      */
256     EXPORT int32_t GrantPhotoUriPermission(const string &appid, const std::vector<string> &uris,
257         PhotoPermissionType photoPermissionType, HideSensitiveType hideSensitiveTpye);
258 
259     sptr<IRemoteObject> InitToken();
260     int32_t CheckResultSet(std::shared_ptr<DataShareResultSet> &resultSet);
261 
262 private:
263     static int OpenThumbnail(std::string &uriStr, const std::string &path, const Size &size, bool isAstc);
264     static unique_ptr<PixelMap> QueryThumbnail(UriParams& params);
265     static unique_ptr<PixelMap> DecodeThumbnail(UniqueFd& uniqueFd, const Size& size, DecodeDynamicRange dynamicRange);
266     static unique_ptr<PixelMap> GetPixelMapWithoutDecode(UniqueFd &uniqueFd, const Size& size);
267     static unique_ptr<PixelMap> DecodeAstc(UniqueFd &uniqueFd);
268 
269     static shared_ptr<DataShare::DataShareHelper> sDataShareHelper_;
270     static sptr<IRemoteObject> token_;
271 };
272 } // namespace Media
273 } // namespace OHOS
274 
275 #endif  // INTERFACES_INNERKITS_NATIVE_INCLUDE_MEDIA_LIBRARY_MANAGER_H_
276