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_MEDIALIBRARY_BACKGROUND_CLOUD_FILE_PROCESSOR_H 17 #define OHOS_MEDIALIBRARY_BACKGROUND_CLOUD_FILE_PROCESSOR_H 18 19 #include "abs_shared_result_set.h" 20 #include "medialibrary_async_worker.h" 21 #include "metadata.h" 22 #include "rdb_predicates.h" 23 #include "timer.h" 24 #include "userfile_manager_types.h" 25 #include "values_bucket.h" 26 27 namespace OHOS { 28 namespace Media { 29 #define EXPORT __attribute__ ((visibility ("default"))) 30 constexpr int32_t PROCESS_INTERVAL = 5 * 60 * 1000; // 5 minute 31 constexpr int32_t DOWNLOAD_DURATION = 10 * 1000; // 10 seconds 32 33 typedef struct { 34 bool isCloud; 35 bool isVideo; 36 } QueryOption; 37 38 class BackgroundCloudFileProcessor { 39 public: 40 EXPORT static void StartTimer(); 41 EXPORT static void StopTimer(); 42 43 private: 44 typedef struct { 45 std::vector<std::string> paths; 46 MediaType mediaType; 47 } DownloadFiles; 48 49 typedef struct { 50 int32_t fileId; 51 std::string path; 52 int64_t size; 53 int32_t width; 54 int32_t height; 55 std::string mimeType; 56 int32_t duration; 57 MediaType mediaType; 58 bool isCloud; 59 bool isVideo; 60 } AbnormalData; 61 62 typedef struct { 63 std::vector<AbnormalData> abnormalData; 64 } UpdateData; 65 66 class DownloadCloudFilesData : public AsyncTaskData { 67 public: DownloadCloudFilesData(DownloadFiles downloadFiles)68 DownloadCloudFilesData(DownloadFiles downloadFiles) : downloadFiles_(downloadFiles){}; 69 ~DownloadCloudFilesData() override = default; 70 71 DownloadFiles downloadFiles_; 72 }; 73 74 class UpdateAbnormalData : public AsyncTaskData { 75 public: UpdateAbnormalData(UpdateData updateData)76 UpdateAbnormalData(UpdateData updateData) : updateData_(updateData){}; 77 ~UpdateAbnormalData() override = default; 78 79 UpdateData updateData_; 80 }; 81 82 class UpdateAbnormalDayMonthYearData : public AsyncTaskData { 83 public: UpdateAbnormalDayMonthYearData(std::vector<std::string> fileIds)84 UpdateAbnormalDayMonthYearData(std::vector<std::string> fileIds) : fileIds_(fileIds){}; 85 ~UpdateAbnormalDayMonthYearData() override = default; 86 87 std::vector<std::string> fileIds_; 88 }; 89 90 static void DownloadCloudFiles(); 91 static bool IsStorageInsufficient(); 92 static std::shared_ptr<NativeRdb::ResultSet> QueryCloudFiles(); 93 static void ParseDownloadFiles(std::shared_ptr<NativeRdb::ResultSet> &resultSet, DownloadFiles &downloadFiles); 94 static int32_t AddDownloadTask(const DownloadFiles &downloadFiles); 95 static void DownloadCloudFilesExecutor(AsyncTaskData *data); 96 static void StopDownloadFiles(); 97 static void ProcessCloudData(); 98 static void UpdateCloudData(); 99 static void UpdateAbnormalDayMonthYearExecutor(AsyncTaskData *data); 100 static void UpdateAbnormalDayMonthYear(); 101 static std::shared_ptr<NativeRdb::ResultSet> QueryUpdateData(bool isCloud, bool isVideo); 102 static void SetPredicates(NativeRdb::RdbPredicates &predicates, bool isCloud, bool isVideo); 103 static void ParseUpdateData(std::shared_ptr<NativeRdb::ResultSet> &resultSet, UpdateData &updateData, 104 bool isCloud, bool isVideo); 105 static int32_t AddUpdateDataTask(const UpdateData &updateData); 106 static void UpdateCloudDataExecutor(AsyncTaskData *data); 107 static void UpdateAbnormaldata(std::unique_ptr<Metadata> &metadata, const std::string &tableName); 108 static void GetSizeAndMimeType(std::unique_ptr<Metadata> &metadata); 109 static int32_t GetExtractMetadata(std::unique_ptr<Metadata> &metadata); 110 static void StopUpdateData(); 111 static void UpdateCurrentOffset(bool isCloud, bool isVideo); 112 113 static int32_t processInterval_; 114 static int32_t downloadDuration_; 115 static std::recursive_mutex mutex_; 116 static Utils::Timer timer_; 117 static uint32_t startTimerId_; 118 static uint32_t stopTimerId_; 119 static std::vector<std::string> curDownloadPaths_; 120 static bool isUpdating_; 121 static int32_t cloudUpdateOffset_; 122 static int32_t localImageUpdateOffset_; 123 static int32_t localVideoUpdateOffset_; 124 static int32_t cloudRetryCount_; 125 static bool isDownload_; 126 }; 127 } // namespace Media 128 } // namespace OHOS 129 #endif // OHOS_MEDIALIBRARY_BACKGROUND_CLOUD_FILE_PROCESSOR_H