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 "net_conn_callback_observer.h"
17 #include "dfs_error.h"
18 #include "network_status.h"
19 #include "utils_log.h"
20 
21 using namespace OHOS::NetManagerStandard;
22 
23 namespace OHOS::FileManagement::CloudSync {
NetConnCallbackObserver(std::shared_ptr<CloudFile::DataSyncManager> dataSyncManager)24 NetConnCallbackObserver::NetConnCallbackObserver(std::shared_ptr<CloudFile::DataSyncManager> dataSyncManager)
25 {
26     dataSyncManager_ = dataSyncManager;
27 }
28 
NetAvailable(sptr<NetHandle> & netHandle)29 int32_t NetConnCallbackObserver::NetAvailable(sptr<NetHandle> &netHandle)
30 {
31     LOGI("network is available");
32     return E_OK;
33 }
34 
NetCapabilitiesChange(sptr<NetHandle> & netHandle,const sptr<NetAllCapabilities> & netAllCap)35 int32_t NetConnCallbackObserver::NetCapabilitiesChange(sptr<NetHandle> &netHandle,
36     const sptr<NetAllCapabilities> &netAllCap)
37 {
38     NetworkStatus::NetConnStatus oldStatus = NetworkStatus::GetNetConnStatus();
39     NetworkStatus::SetNetConnStatus(*netAllCap);
40     if (oldStatus != NetworkStatus::NO_NETWORK) {
41         return E_OK;
42     }
43     NetworkStatus::NetConnStatus newStatus = NetworkStatus::GetNetConnStatus();
44     if (newStatus == NetworkStatus::WIFI_CONNECT) {
45         LOGI("NetCapabilitiesChanged wifi connected");
46         dataSyncManager_->TriggerRecoverySync(triggerType_);
47         dataSyncManager_->DownloadThumb();
48         dataSyncManager_->CacheVideo();
49     } else if (newStatus == NetworkStatus::CELLULAR_CONNECT) {
50         LOGI("NetCapabilitiesChanged cellular connected");
51         dataSyncManager_->TriggerRecoverySync(triggerType_);
52         NetworkStatus::NetWorkChangeStopUploadTask();
53     } else {
54         LOGI("NetCapabilitiesChanged newStatus:%{public}d", newStatus);
55     }
56     return E_OK;
57 }
58 
NetConnectionPropertiesChange(sptr<NetHandle> & netHandle,const sptr<NetLinkInfo> & info)59 int32_t NetConnCallbackObserver::NetConnectionPropertiesChange(sptr<NetHandle> &netHandle,
60     const sptr<NetLinkInfo> &info)
61 {
62     return E_OK;
63 }
64 
NetLost(sptr<NetHandle> & netHandle)65 int32_t NetConnCallbackObserver::NetLost(sptr<NetHandle> &netHandle)
66 {
67     LOGI("NetConnCallbackObserver::NetLost");
68     NetworkStatus::SetNetConnStatus(NetworkStatus::NetConnStatus::NO_NETWORK);
69     return E_OK;
70 }
71 
NetUnavailable()72 int32_t NetConnCallbackObserver::NetUnavailable()
73 {
74     return E_OK;
75 }
76 
NetBlockStatusChange(sptr<NetHandle> & netHandle,bool blocked)77 int32_t NetConnCallbackObserver::NetBlockStatusChange(sptr<NetHandle> &netHandle, bool blocked)
78 {
79     return E_OK;
80 }
81 } // namespace OHOS::FileManagement::CloudSync