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 #define MLOG_TAG "CloudMediaAssetObserver"
17 
18 #include "cloud_media_asset_observer.h"
19 
20 #include <list>
21 
22 #include "cloud_media_asset_download_operation.h"
23 #include "cloud_sync_utils.h"
24 #include "common_event_utils.h"
25 #include "media_log.h"
26 #include "medialibrary_subscriber.h"
27 #include "uri.h"
28 
29 using namespace std;
30 using Uri = OHOS::Uri;
31 
32 namespace OHOS {
33 namespace Media {
34 static const std::string CLOUD_DATASHARE_URI = "datashareproxy://generic.cloudstorage/cloud_sp?";
35 static const std::string CLOUD_URI = CLOUD_DATASHARE_URI + "key=useMobileNetworkData";
36 
OnChange(const ChangeInfo & changeInfo)37 void CloudMediaAssetObserver::OnChange(const ChangeInfo &changeInfo)
38 {
39     if (operation_ == nullptr || operation_->GetTaskStatus() == CloudMediaAssetTaskStatus::IDLE) {
40         return;
41     }
42     if (CommonEventUtils::IsWifiConnected()) {
43         MEDIA_INFO_LOG("wifi is connection.");
44         return;
45     }
46     std::list<Uri> uris = changeInfo.uris_;
47     for (auto &uri : uris) {
48         if (uri.ToString() != CLOUD_URI || changeInfo.changeType_ != DataShareObserver::ChangeType::OTHER) {
49             MEDIA_INFO_LOG("Current uri is not suitable for task.");
50             return;
51         }
52 
53         bool isUnlimitedTrafficStatusOn = CloudSyncUtils::IsUnlimitedTrafficStatusOn();
54         if (operation_->GetTaskStatus() == CloudMediaAssetTaskStatus::DOWNLOADING) {
55             operation_->isUnlimitedTrafficStatusOn_ = isUnlimitedTrafficStatusOn;
56             if (isUnlimitedTrafficStatusOn) {
57                 return;
58             }
59             CloudMediaTaskPauseCause pauseCause = MedialibrarySubscriber::IsCellularNetConnected() ?
60                 CloudMediaTaskPauseCause::WIFI_UNAVAILABLE : CloudMediaTaskPauseCause::NETWORK_FLOW_LIMIT;
61             operation_->PauseDownloadTask(pauseCause);
62             MEDIA_INFO_LOG("Cloud media asset download paused, pauseCause: %{public}d.",
63                 static_cast<int32_t>(pauseCause));
64             return;
65         }
66 
67         if (operation_->GetTaskStatus() == CloudMediaAssetTaskStatus::PAUSED &&
68             MedialibrarySubscriber::IsCellularNetConnected() && isUnlimitedTrafficStatusOn) {
69             operation_->PassiveStatusRecoverTask(CloudMediaTaskRecoverCause::NETWORK_NORMAL);
70             MEDIA_INFO_LOG("Cloud media asset download recovered, recoverCause: %{public}d.",
71                 static_cast<int32_t>(CloudMediaTaskRecoverCause::NETWORK_NORMAL));
72             return;
73         }
74     }
75 }
76 } // namespace Media
77 } // namespace OHOS