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 #ifndef OHOS_PHOTO_PICTURE_DATA_OPERATIONS_H 17 #define OHOS_PHOTO_PICTURE_DATA_OPERATIONS_H 18 19 #include <cstdint> 20 #include <functional> 21 #include <memory> 22 #include <mutex> 23 #include <string> 24 #include <list> 25 #include <thread> 26 #include "picture.h" 27 #include "media_log.h" 28 #include "medialibrary_async_worker.h" 29 30 namespace OHOS { 31 namespace Media { 32 enum PictureType { 33 LOW_QUALITY_PICTURE = 0, 34 HIGH_QUALITY_PICTURE 35 }; 36 37 class PicturePair : public RefBase { 38 public: 39 std::shared_ptr<Media::Picture> picture_; 40 std::string photoId_ = "default"; 41 time_t expireTime_; 42 bool isCleanImmediately_ = true; 43 bool isEdited_ = false; PicturePair(std::shared_ptr<Media::Picture> picture,std::string photoId,time_t expireTime,bool isCleanImmediately,bool isEdited)44 explicit PicturePair(std::shared_ptr<Media::Picture> picture, std::string photoId, 45 time_t expireTime, bool isCleanImmediately, bool isEdited) 46 { 47 picture_ = std::move(picture); 48 photoId_ = std::move(photoId); 49 expireTime_ = expireTime; 50 isCleanImmediately_ = isCleanImmediately; 51 isEdited_ = isEdited; 52 } 53 PicturePair(const PicturePair & other)54 PicturePair(const PicturePair& other) 55 { 56 picture_ = std::move(other.picture_); 57 photoId_ = std::move(other.photoId_); 58 expireTime_ = other.expireTime_; 59 isCleanImmediately_ = other.isCleanImmediately_; 60 isEdited_ = other.isEdited_; 61 } 62 63 PicturePair& operator=(const PicturePair& other) 64 { 65 if (this != &other) { 66 picture_ = std::move(other.picture_); 67 photoId_ = std::move(other.photoId_); 68 expireTime_ = other.expireTime_; 69 isCleanImmediately_ = other.isCleanImmediately_; 70 isEdited_ = other.isEdited_; 71 } 72 return *this; 73 } 74 ~PicturePair()75 ~PicturePair() 76 { 77 if (picture_) { 78 picture_ = nullptr; 79 } 80 } 81 }; 82 83 class SavePictureData : public AsyncTaskData { 84 public: SavePictureData(sptr<PicturePair> picturePair)85 SavePictureData(sptr<PicturePair> picturePair) : picturePair_(picturePair){}; 86 ~SavePictureData() override = default; 87 88 sptr<PicturePair> picturePair_; 89 }; 90 class PictureDataOperations : public RefBase { 91 public: 92 explicit PictureDataOperations(); 93 ~PictureDataOperations(); 94 void CleanDateForPeriodical(); 95 void CleanPictureMapData(std::map<std::string, sptr<PicturePair>>& pictureMap, PictureType pictureType); 96 void InsertPictureData(const std::string& imageId, sptr<PicturePair>& picturePair, PictureType pictureType); 97 std::shared_ptr<Media::Picture> GetDataWithImageId(const std::string& imageId, 98 bool &isHighQualityPicture, bool isCleanImmediately = true); 99 std::shared_ptr<Media::Picture> GetDataWithImageIdAndPictureType(const std::string& imageId, 100 PictureType pictureType, bool isCleanImmediately = true); 101 void DeleteDataWithImageId(const std::string& imageId, PictureType pictureType); 102 bool IsExsitDataForPictureType(PictureType pictureType); 103 bool IsExsitDataForPictureType(const std::string& imageId, PictureType pictureType); 104 void SaveLowQualityPicture(const std::string& imageId = "default"); 105 void FinishAccessingPicture(const std::string& imageId, PictureType pictureType); 106 void SavePictureWithImageId(const std::string& imageId); 107 static void SavePictureExecutor(AsyncTaskData *data); 108 int32_t AddSavePictureTask(sptr<PicturePair>& picturePair); 109 int32_t GetPendingTaskSize(); 110 private: 111 bool SavePicture(const std::string& imageId, std::map<std::string, sptr<PicturePair>>& pictureMap, 112 bool isLowQualityPicture); 113 void CleanHighQualityPictureDataInternal(const std::string& imageId, sptr<PicturePair>& picturePair, 114 std::list<std::string>& pictureImageIdList); 115 116 const int MAX_PICTURE_CAPBILITY = 3; 117 int max_capibilty = MAX_PICTURE_CAPBILITY; 118 std::mutex pictureMapMutex_; 119 std::map<std::string, sptr<PicturePair>> lowQualityPictureMap_; 120 std::map<std::string, sptr<PicturePair>> highQualityPictureMap_; 121 std::list<std::string> highQualityPictureImageId; 122 std::list<std::string> pendingSavedPicturelist_; 123 static int32_t taskSize; 124 }; // class PictureDataOperation 125 } // namespace Media 126 } // namespace OHOS 127 #endif // OHOS_PHOTO_PICTURE_DATA_OPERATION_H