1 /*
2  * Copyright (c) 2021-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 "sync_coordinator.h"
17 
18 #include <string>
19 
20 #include "device_profile_log.h"
21 
22 namespace OHOS {
23 namespace DeviceProfile {
24 namespace {
25 const std::string TAG = "SyncCoordinator";
26 }
27 
28 IMPLEMENT_SINGLE_INSTANCE(SyncCoordinator);
29 
Init()30 bool SyncCoordinator::Init()
31 {
32     auto runner = AppExecFwk::EventRunner::Create("syncHandler");
33     syncHandler_ = std::make_shared<AppExecFwk::EventHandler>(runner);
34     if (syncHandler_ == nullptr) {
35         return false;
36     }
37     return true;
38 }
39 
AcquireSync()40 bool SyncCoordinator::AcquireSync()
41 {
42     if (isOnSync_) {
43         HILOGI("acquire from %{public}s", isOnlineTrigger_ ? "online" : "manual");
44         return false;
45     }
46     isOnSync_ = true;
47     return true;
48 }
49 
ReleaseSync()50 void SyncCoordinator::ReleaseSync()
51 {
52     isOnSync_ = false;
53 }
54 
IsOnSync()55 bool SyncCoordinator::IsOnSync()
56 {
57     HILOGI("isOnSync_ %{public}s", std::to_string(isOnSync_).c_str());
58     return isOnSync_;
59 }
60 
SetSyncTrigger(bool isOnlineTrigger)61 void SyncCoordinator::SetSyncTrigger(bool isOnlineTrigger)
62 {
63     isOnlineTrigger_ = isOnlineTrigger;
64 }
65 
IsOnlineSync()66 bool SyncCoordinator::IsOnlineSync()
67 {
68     return isOnlineTrigger_;
69 }
70 
DispatchSyncTask(const SyncTask & syncTask,int64_t delayTime)71 bool SyncCoordinator::DispatchSyncTask(const SyncTask& syncTask, int64_t delayTime)
72 {
73     if (!syncHandler_->PostTask(syncTask, delayTime)) {
74         HILOGE("post task failed");
75         isOnSync_ = false;
76         return false;
77     }
78     return true;
79 }
80 } // namespace DeviceProfile
81 } // namespace OHOS
82