• 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_CLOUD_MEDIA_ASSET_DOWNLOAD_OPERATION_H
17 #define OHOS_CLOUD_MEDIA_ASSET_DOWNLOAD_OPERATION_H
18 
19 #include <iostream>
20 #include <thread>
21 #include <mutex>
22 #include <condition_variable>
23 #include <queue>
24 #include <atomic>
25 #include <memory>
26 #include <chrono>
27 
28 #include "cloud_media_asset_callback.h"
29 #include "cloud_media_asset_types.h"
30 #include "cloud_media_asset_observer.h"
31 #include "cloud_sync_common.h"
32 #include "cloud_sync_manager.h"
33 #include "datashare_helper.h"
34 #include "medialibrary_command.h"
35 #include "medialibrary_rdbstore.h"
36 
37 namespace OHOS {
38 namespace Media {
39 #define EXPORT __attribute__ ((visibility ("default")))
40 using namespace FileManagement::CloudSync;
41 
42 class CloudDeathRecipient : public IRemoteObject::DeathRecipient {
43 public:
CloudDeathRecipient(std::shared_ptr<CloudMediaAssetDownloadOperation> operation)44     CloudDeathRecipient(std::shared_ptr<CloudMediaAssetDownloadOperation> operation) : operation_(operation) {}
~CloudDeathRecipient()45     ~CloudDeathRecipient() {}
46     void OnRemoteDied(const wptr<IRemoteObject> &object);
47 
48 private:
49     std::shared_ptr<CloudMediaAssetDownloadOperation> operation_ = nullptr;
50 };
51 
52 class CloudMediaAssetDownloadOperation {
53 public:
54     struct DownloadFileData {
55         std::vector<std::string> pathVec;
56         std::map<std::string, int64_t> fileDownloadMap;
57         std::vector<std::string> batchFileIdNeedDownload;
58         int64_t batchSizeNeedDownload = 0;
59         int64_t batchCountNeedDownload = 0;
60     };
61 
62     enum class Status : int32_t {
63         FORCE_DOWNLOADING,
64         GENTLE_DOWNLOADING,
65         PAUSE_FOR_TEMPERATURE_LIMIT,
66         PAUSE_FOR_ROM_LIMIT,
67         PAUSE_FOR_NETWORK_FLOW_LIMIT,
68         PAUSE_FOR_WIFI_UNAVAILABLE,
69         PAUSE_FOR_POWER_LIMIT,
70         PAUSE_FOR_BACKGROUND_TASK_UNAVAILABLE,
71         PAUSE_FOR_FREQUENT_USER_REQUESTS,
72         PAUSE_FOR_CLOUD_ERROR,
73         PAUSE_FOR_USER_PAUSE,
74         RECOVER_FOR_MANAUL_ACTIVE,
75         RECOVER_FOR_PASSIVE_STATUS,
76         IDLE,
77     };
78 
CloudMediaAssetDownloadOperation()79     CloudMediaAssetDownloadOperation() {}
~CloudMediaAssetDownloadOperation()80     ~CloudMediaAssetDownloadOperation() {}
81     CloudMediaAssetDownloadOperation(const CloudMediaAssetDownloadOperation &) = delete;
82     CloudMediaAssetDownloadOperation& operator=(const CloudMediaAssetDownloadOperation &) = delete;
83 
84     EXPORT static std::shared_ptr<CloudMediaAssetDownloadOperation> GetInstance();
85     EXPORT int32_t StartDownloadTask(int32_t cloudMediaDownloadType);
86     EXPORT int32_t PauseDownloadTask(const CloudMediaTaskPauseCause &pauseCause);
87     EXPORT int32_t CancelDownloadTask();
88     EXPORT int32_t ManualActiveRecoverTask(int32_t cloudMediaDownloadType);
89     EXPORT int32_t PassiveStatusRecoverTask(const CloudMediaTaskRecoverCause &recoverCause);
90     void CheckStorageAndRecoverDownloadTask();
91 
92     EXPORT void HandleSuccessCallback(const DownloadProgressObj &progress);
93     EXPORT void HandleFailedCallback(const DownloadProgressObj &progress);
94     EXPORT void HandleStoppedCallback(const DownloadProgressObj &progress);
95 
96     EXPORT CloudMediaDownloadType GetDownloadType();
97     EXPORT CloudMediaAssetTaskStatus GetTaskStatus();
98     EXPORT CloudMediaTaskPauseCause GetTaskPauseCause();
99     EXPORT std::string GetTaskInfo();
100     EXPORT int32_t InitDownloadTaskInfo();
101     void ResetDownloadTryTime();
102 
103 private:
104     void ClearData(DownloadFileData &data);
105     bool IsDataEmpty(const DownloadFileData &data);
106     int32_t DoRelativedRegister();
107     int32_t SetDeathRecipient();
108     bool IsProperFgTemperature();
109     void InitStartDownloadTaskStatus(const bool &isForeground);
110     void ResetParameter();
111     bool IsNetworkAvailable();
112 
113     void SetTaskStatus(Status status);
114     std::shared_ptr<NativeRdb::ResultSet> QueryDownloadFilesNeeded(const bool &isQueryInfo);
115     DownloadFileData ReadyDataForBatchDownload();
116     EXPORT int32_t DoForceTaskExecute();
117     EXPORT int32_t SubmitBatchDownload(DownloadFileData &data, const bool &isCache);
118     void StartFileCacheFailed(const int64_t batchNum, const int64_t batchSize);
119     void StartBatchDownload(const int64_t batchNum, const int64_t batchSize);
120     EXPORT int32_t DoRecoverExecute();
121     EXPORT int32_t PassiveStatusRecover();
122     int32_t SubmitBatchDownloadAgain();
123     void MoveDownloadFileToCache(const DownloadProgressObj &progress);
124     void MoveDownloadFileToNotFound(const DownloadProgressObj &progress);
125 
126 public:
127     static std::shared_ptr<CloudMediaAssetDownloadOperation> instance_;
128 
129     // Confirmation of the notification
130     bool isThumbnailUpdate_ = true;
131     bool isBgDownloadPermission_ = false;
132     bool isUnlimitedTrafficStatusOn_ = false;
133 
134 private:
135     std::reference_wrapper<CloudSyncManager> cloudSyncManager_ = CloudSyncManager::GetInstance();
136     CloudMediaAssetTaskStatus taskStatus_ = CloudMediaAssetTaskStatus::IDLE;
137     CloudMediaDownloadType downloadType_ = CloudMediaDownloadType::DOWNLOAD_GENTLE;
138     CloudMediaTaskPauseCause pauseCause_ = CloudMediaTaskPauseCause::NO_PAUSE;
139     std::shared_ptr<DataShare::DataShareHelper> cloudHelper_;
140     std::shared_ptr<CloudMediaAssetObserver> cloudMediaAssetObserver_;
141     std::shared_ptr<MediaCloudDownloadCallback> downloadCallback_;
142     OHOS::sptr<OHOS::IRemoteObject> cloudRemoteObject_;
143     static std::mutex mutex_;
144     static std::mutex callbackMutex_;
145 
146     DownloadFileData readyForDownload_;
147     DownloadFileData notFoundForDownload_;
148 
149     // data cache
150     DownloadFileData cacheForDownload_;
151 
152     // data downloading
153     bool isCache_ = false;
154     int64_t downloadId_ = -1;
155     DownloadFileData dataForDownload_;
156 
157     // common info
158     int64_t totalCount_ = 0;
159     int64_t totalSize_ = 0;
160     int64_t remainCount_ = 0;
161     int64_t remainSize_ = 0;
162 
163     uint32_t downloadTryTime_ = 0;
164 };
165 } // namespace Media
166 } // namespace OHOS
167 #endif // OHOS_CLOUD_MEDIA_ASSET_DOWNLOAD_OPERATION_H