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 #ifndef FRAMEWORKS_SERVICES_MEDIA_MTP_INCLUDE_MTP_MEDIALIBRARY_MANAGER_H_
16 #define FRAMEWORKS_SERVICES_MEDIA_MTP_INCLUDE_MTP_MEDIALIBRARY_MANAGER_H_
17 
18 #include <filesystem>
19 #include <sys/stat.h>
20 #include "avmetadatahelper.h"
21 #include "datashare_helper.h"
22 #include "file_asset.h"
23 #include "mtp_operation_context.h"
24 #include "object_info.h"
25 #include "property.h"
26 
27 namespace OHOS {
28 namespace Media {
29 namespace sf = std::filesystem;
30 class MtpMedialibraryManager {
31 public:
32     MtpMedialibraryManager();
33     ~MtpMedialibraryManager();
34     static std::shared_ptr<MtpMedialibraryManager> GetInstance();
35     void Init(const sptr<IRemoteObject> &token, const std::shared_ptr<MtpOperationContext> &context);
36     void Clear();
37     int32_t GetHandles(int32_t parentId, std::vector<int> &outHandles, MediaType mediaType = MEDIA_TYPE_DEFAULT);
38     int32_t GetHandles(const std::shared_ptr<MtpOperationContext> &context, std::shared_ptr<UInt32List> &outHandles);
39     int32_t GetObjectInfo(const std::shared_ptr<MtpOperationContext> &context,
40         std::shared_ptr<ObjectInfo> &outObjectInfo);
41     int32_t GetFd(const std::shared_ptr<MtpOperationContext> &context, int32_t &outFd, const std::string &mode);
42     int32_t GetThumb(const std::shared_ptr<MtpOperationContext> &context, std::shared_ptr<UInt8List> &outThumb);
43     int32_t SendObjectInfo(const std::shared_ptr<MtpOperationContext> &context,
44         uint32_t &outStorageID, uint32_t &outParent, uint32_t &outHandle);
45     int32_t GetPathById(const int32_t id, std::string &outPath);
46     int32_t GetIdByPath(const std::string &path, uint32_t &outId);
47     int32_t MoveObject(const std::shared_ptr<MtpOperationContext> &context);
48     int32_t CopyObject(const std::shared_ptr<MtpOperationContext> &context, uint32_t &outObjectHandle);
49     int32_t DeleteObject(const std::shared_ptr<MtpOperationContext> &context);
50     int32_t SetObjectPropValue(const std::shared_ptr<MtpOperationContext> &context);
51     int32_t CloseFd(const std::shared_ptr<MtpOperationContext> &context, int32_t fd);
52     int32_t CloseFdForGet(const std::shared_ptr<MtpOperationContext> &context, int32_t fd);
53     int32_t GetObjectPropList(const std::shared_ptr<MtpOperationContext> &context,
54         std::shared_ptr<std::vector<Property>> &outProps);
55     int32_t GetObjectPropValue(const std::shared_ptr<MtpOperationContext> &context,
56         uint64_t &outIntVal, uint128_t &outLongVal, std::string &outStrVal);
57     void CondCloseFd(const bool condition, const int fd);
58     int32_t GetPictureThumb(const std::shared_ptr<MtpOperationContext> &context,
59         std::shared_ptr<UInt8List> &outThumb);
60     int32_t GetVideoThumb(const std::shared_ptr<MtpOperationContext> &context,
61         std::shared_ptr<UInt8List> &outThumb);
62     void DeleteCanceledObject(uint32_t id);
63     int32_t GetFdByOpenFile(const std::shared_ptr<MtpOperationContext> &context, int32_t &outFd);
64 private:
65     int32_t SetObjectInfo(const std::unique_ptr<FileAsset> &fileAsset, std::shared_ptr<ObjectInfo> &outObjectInfo);
66     int32_t SetObject(const std::shared_ptr<DataShare::DataShareResultSet> &resultSet,
67         const std::shared_ptr<MtpOperationContext> &context, std::shared_ptr<ObjectInfo> &outObjectInfo);
68     bool CompressImage(std::unique_ptr<PixelMap> &pixelMap, std::vector<uint8_t> &data);
69     int32_t GetAssetById(const int32_t id, std::shared_ptr<FileAsset> &outFileAsset);
70     int32_t GetAssetByPath(const std::string &path, std::shared_ptr<FileAsset> &outFileAsset);
71     int32_t GetAssetByPredicates(const DataShare::DataSharePredicates &predicates,
72         std::shared_ptr<FileAsset> &outFileAsset);
73     std::shared_ptr<DataShare::DataShareResultSet> GetAlbumInfo(const std::shared_ptr<MtpOperationContext> &context,
74         bool isHandle);
75     std::shared_ptr<DataShare::DataShareResultSet> GetPhotosInfo(const std::shared_ptr<MtpOperationContext> &context,
76         bool isHandle);
77     int32_t GetAlbumCloud();
78     int32_t GetAlbumCloudDisplay(std::vector<std::string> &ownerAlbumIds);
79     int32_t HaveMovingPhotesHandle(const std::shared_ptr<DataShare::DataShareResultSet> resultSet,
80         std::shared_ptr<UInt32List> &outHandles, const uint32_t parent);
81     uint32_t GetSizeFromOfft(const off_t &size);
82     std::vector<std::string> GetBurstKeyFromPhotosInfo();
83     std::shared_ptr<DataShare::DataShareResultSet> GetOwnerAlbumIdList();
84     std::string GetThumbUri(const int32_t &id, const std::string &thumbSizeValue, const std::string &dataPath);
85 private:
86     static std::mutex mutex_;
87     static std::shared_ptr<MtpMedialibraryManager> instance_;
88     static std::shared_ptr<DataShare::DataShareHelper> dataShareHelper_;
89     static sptr<IRemoteObject> getThumbToken_;
90 };
91 } // namespace Media
92 } // namespace OHOS
93 #endif  // FRAMEWORKS_SERVICES_MEDIA_MTP_INCLUDE_MTP_MEDIALIBRARY_MANAGER_H_
94