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 "cloud_status.h"
17
18 #include "cloud_file_kit.h"
19 #include "dfs_error.h"
20 #include "utils_log.h"
21
22 namespace OHOS::FileManagement::CloudSync {
GetCurrentCloudInfo(const std::string & bundleName,const int32_t userId)23 int32_t CloudStatus::GetCurrentCloudInfo(const std::string &bundleName, const int32_t userId)
24 {
25 auto instance = CloudFile::CloudFileKit::GetInstance();
26 if (instance == nullptr) {
27 LOGE("get cloud file helper instance failed");
28 return E_NULLPTR;
29 }
30
31 auto ret = instance->GetCloudUserInfo(userId, userInfo_);
32 if (ret != E_OK) {
33 return ret;
34 }
35
36 bool switchStatus = false;
37 ret = instance->GetAppSwitchStatus(bundleName, userId, switchStatus);
38 if (ret != E_OK) {
39 return ret;
40 }
41 /* insert key-value */
42 appSwitches_.insert(std::make_pair(bundleName, switchStatus));
43 userId_ = userId;
44 return E_OK;
45 }
46
GetCurrentRemainSpace(const int32_t userId)47 uint64_t CloudStatus::GetCurrentRemainSpace(const int32_t userId)
48 {
49 auto instance = CloudFile::CloudFileKit::GetInstance();
50 return instance->GetRemainSpace(userId);
51 }
52
IsCloudStatusOkay(const std::string & bundleName,const int32_t userId)53 bool CloudStatus::IsCloudStatusOkay(const std::string &bundleName, const int32_t userId)
54 {
55 std::lock_guard<std::mutex> lock(mutex_);
56 /* User switching */
57 if (userId_ != userId) {
58 appSwitches_.erase(bundleName);
59 }
60
61 /* Obtain cloud information only during first sync */
62 auto iter = appSwitches_.find(bundleName);
63 if (iter == appSwitches_.end()) {
64 LOGI("appSwitches unknown, bundleName:%{private}s, userId:%{public}d", bundleName.c_str(), userId);
65 auto ret = GetCurrentCloudInfo(bundleName, userId);
66 if (ret) {
67 return false;
68 }
69 }
70
71 LOGI("bundleName:%{private}s, cloudSatus:%{public}d, switcheStatus:%{public}d", bundleName.c_str(),
72 userInfo_.enableCloud, appSwitches_[bundleName]);
73 return appSwitches_[bundleName];
74 }
75
ChangeAppSwitch(const std::string & bundleName,const int32_t userId,bool appSwitchStatus)76 int32_t CloudStatus::ChangeAppSwitch(const std::string &bundleName, const int32_t userId, bool appSwitchStatus)
77 {
78 std::lock_guard<std::mutex> lock(mutex_);
79 if (appSwitchStatus == true) {
80 auto iter = appSwitches_.find(bundleName);
81 if (iter != appSwitches_.end()) {
82 LOGI("change app swtich, originStatus:%{public}d, currentStatus:%{public}d", appSwitches_[bundleName],
83 appSwitchStatus);
84 appSwitches_[bundleName] = appSwitchStatus;
85 }
86 } else {
87 /* Actively obtaining cloud information when next sync */
88 appSwitches_.erase(bundleName);
89 }
90
91 return E_OK;
92 }
93
IsAccountIdChanged(const std::string & accountId)94 bool CloudStatus::IsAccountIdChanged(const std::string &accountId)
95 {
96 std::lock_guard<std::mutex> lock(mutex_);
97 if ((userInfo_.accountId != "") && (userInfo_.accountId != accountId)) {
98 /* accountId Changed, clear init flag */
99 appSwitches_.clear();
100 return true;
101 }
102 return false;
103 }
104 } // namespace OHOS::FileManagement::CloudSync