• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "background_cloud_file_download_callback.h"
21 #include "medialibrary_async_worker.h"
22 #include "metadata.h"
23 #include "rdb_predicates.h"
24 #include "timer.h"
25 #include "userfile_manager_types.h"
26 #include "values_bucket.h"
27 #include "media_file_uri.h"
28 
29 namespace OHOS {
30 namespace Media {
31 #define EXPORT __attribute__ ((visibility ("default")))
32 constexpr int32_t PROCESS_INTERVAL = 5 * 60 * 1000;  // 5 minute
33 constexpr int32_t DOWNLOAD_INTERVAL = 1 * 60 * 1000;  // 1 minute
34 constexpr int32_t DOWNLOAD_DURATION = 10 * 1000; // 10 seconds
35 constexpr int32_t DOWNLOAD_FAIL_MAX_TIMES = 5; // 5 times
36 
37 typedef struct {
38     bool isCloud;
39     bool isVideo;
40 } QueryOption;
41 
42 class BackgroundCloudFileProcessor {
43 public:
44     EXPORT static void StartTimer();
45     EXPORT static void StopTimer();
46     EXPORT static void SetDownloadLatestFinished(bool downloadLatestFinished);
47     EXPORT static bool GetDownloadLatestFinished();
48     EXPORT static void HandleSuccessCallback(const DownloadProgressObj &progress);
49     EXPORT static void HandleFailedCallback(const DownloadProgressObj &progress);
50     EXPORT static void HandleStoppedCallback(const DownloadProgressObj &progress);
51 
52 private:
53     typedef struct {
54         std::vector<std::string> uris;
55         MediaType mediaType;
56     } DownloadFiles;
57 
58     typedef struct {
59         int32_t fileId;
60         std::string path;
61         std::string displayName;
62         int64_t size;
63         int32_t width;
64         int32_t height;
65         std::string mimeType;
66         std::string mediaSuffix;
67         int32_t duration;
68         MediaType mediaType;
69         bool isCloud;
70         bool isVideo;
71     } AbnormalData;
72 
73     typedef struct {
74         std::vector<AbnormalData> abnormalData;
75     } UpdateData;
76 
77     enum DownloadStatus : int32_t {
78         INIT = 0,
79         SUCCESS,
80         NETWORK_UNAVAILABLE,
81         STORAGE_FULL,
82         STOPPED,
83         UNKNOWN,
84     };
85 
86     class DownloadCloudFilesData : public AsyncTaskData {
87     public:
DownloadCloudFilesData(DownloadFiles downloadFiles)88         DownloadCloudFilesData(DownloadFiles downloadFiles) : downloadFiles_(downloadFiles){};
89         ~DownloadCloudFilesData() override = default;
90 
91         DownloadFiles downloadFiles_;
92     };
93 
94     class UpdateAbnormalData : public AsyncTaskData {
95     public:
UpdateAbnormalData(UpdateData updateData)96         UpdateAbnormalData(UpdateData updateData) : updateData_(updateData){};
97         ~UpdateAbnormalData() override = default;
98 
99         UpdateData updateData_;
100     };
101 
102     class UpdateAbnormalDayMonthYearData : public AsyncTaskData {
103     public:
UpdateAbnormalDayMonthYearData(std::vector<std::string> fileIds)104         UpdateAbnormalDayMonthYearData(std::vector<std::string> fileIds) : fileIds_(fileIds){};
105         ~UpdateAbnormalDayMonthYearData() override = default;
106 
107         std::vector<std::string> fileIds_;
108     };
109 
110     static void DownloadCloudFiles();
111     static bool GetStorageFreeRatio(double &freeRatio);
112     static void SetLastDownloadMilliSecond(int64_t lastDownloadMilliSecond);
113     static int64_t GetLastDownloadMilliSecond();
114     static void ClearDownloadCnt();
115     static void UpdateDownloadCnt(std::string path, int64_t cnt);
116     static int64_t GetDownloadCnt(std::string path);
117     static std::shared_ptr<NativeRdb::ResultSet> QueryCloudFiles(double freeRatio);
118     static void CheckAndUpdateDownloadCnt(std::string path, int64_t cnt);
119     static void GetDownloadNum(int64_t &downloadNum);
120     static void DownloadLatestFinished();
121     static void ParseDownloadFiles(std::shared_ptr<NativeRdb::ResultSet> &resultSet, DownloadFiles &downloadFiles);
122     static void removeFinishedResult(const std::vector<std::string>& downloadingPaths);
123     static int32_t AddDownloadTask(const DownloadFiles &downloadFiles);
124     static void DownloadCloudFilesExecutor(AsyncTaskData *data);
125     static void StopDownloadFiles();
126     static void ProcessCloudData();
127     static void UpdateCloudData();
128     static void UpdateAbnormalDayMonthYearExecutor(AsyncTaskData *data);
129     static void UpdateAbnormalDayMonthYear();
130     static std::shared_ptr<NativeRdb::ResultSet> QueryUpdateData(bool isCloud, bool isVideo);
131     static void SetPredicates(NativeRdb::RdbPredicates &predicates, bool isCloud, bool isVideo);
132     static void ParseUpdateData(std::shared_ptr<NativeRdb::ResultSet> &resultSet, UpdateData &updateData,
133         bool isCloud, bool isVideo);
134     static int32_t AddUpdateDataTask(const UpdateData &updateData);
135     static void UpdateCloudDataExecutor(AsyncTaskData *data);
136     static void UpdateAbnormaldata(std::unique_ptr<Metadata> &metadata, const std::string &tableName);
137     static void GetSizeAndMimeType(std::unique_ptr<Metadata> &metadata);
138     static int32_t GetExtractMetadata(std::unique_ptr<Metadata> &metadata);
139     static void StopUpdateData();
140     static void UpdateCurrentOffset(bool isCloud, bool isVideo);
141 
142     static int32_t processInterval_;
143     static int32_t downloadInterval_;
144     static int32_t downloadDuration_;
145     static std::recursive_mutex mutex_;
146     static Utils::Timer timer_;
147     static uint32_t cloudDataTimerId_;
148     static uint32_t startTimerId_;
149     static uint32_t stopTimerId_;
150     static bool isUpdating_;
151     static int32_t cloudUpdateOffset_;
152     static int32_t localImageUpdateOffset_;
153     static int32_t localVideoUpdateOffset_;
154     static int32_t cloudRetryCount_;
155     static std::mutex downloadResultMutex_;
156     static std::unordered_map<std::string, DownloadStatus> downloadResult_;
157     static int64_t downloadId_;
158 };
159 } // namespace Media
160 } // namespace OHOS
161 #endif // OHOS_MEDIALIBRARY_BACKGROUND_CLOUD_FILE_PROCESSOR_H