1 /*
2 * Copyright (c) 2023 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 #include <memory>
17 #include "cycle_task.h"
18 #include "dfs_error.h"
19 #include "drive_kit.h"
20 #include "data_sync_manager.h"
21 #include "gallery_data_syncer.h"
22 #include "cycle_task_runner.h"
23 #include "tasks/optimize_storage_task.h"
24 #include "sync_rule/cloud_status.h"
25 #include "utils_log.h"
26
27
28 namespace OHOS {
29 namespace FileManagement {
30 namespace CloudSync {
OptimizeStorageTask(std::shared_ptr<DataSyncManager> dataSyncManager)31 OptimizeStorageTask::OptimizeStorageTask(std::shared_ptr<DataSyncManager> dataSyncManager)
32 : CycleTask("optimize_storage_task", "com.ohos.photos", CycleTaskRunner::ONE_DAY, dataSyncManager) {}
33
RunTask(int32_t userId)34 int32_t OptimizeStorageTask::RunTask(int32_t userId)
35 {
36 auto driveKit = DriveKit::DriveKitNative::GetInstance(userId);
37 auto dataSyncManager_ = GetDataSyncManager();
38 if (driveKit == nullptr) {
39 LOGE("get drive kit instance fail");
40 return E_CLOUD_SDK;
41 }
42
43 std::map<std::string, std::string> param;
44 auto err = driveKit->GetAppConfigParams(bundleName_, param);
45 if (err.HasError() || param.empty()) {
46 LOGE("GetAppConfigParams failed, server err:%{public}d and dk err:%{public}d", err.serverErrorCode,
47 err.dkErrorCode);
48 return E_CLOUD_SDK;
49 }
50
51 int32_t agingDays = std::stoi(param["validDays"]);
52 int32_t agingPolicy = std::stoi(param["dataAgingPolicy"]);
53 if (agingPolicy == 0) {
54 return dataSyncManager_->OptimizeStorage(bundleName_, userId, agingDays);
55 }
56
57 return E_OK;
58 }
59
60
61 } // namespace CloudSync
62 } // namespace FileManagement
63 } // namespace OHOS