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