1 /* 2 * Copyright (c) 2021 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()30bool 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()40bool 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()50void SyncCoordinator::ReleaseSync() 51 { 52 isOnSync_ = false; 53 } 54 SetSyncTrigger(bool isOnlineTrigger)55void SyncCoordinator::SetSyncTrigger(bool isOnlineTrigger) 56 { 57 isOnlineTrigger_ = isOnlineTrigger; 58 } 59 IsOnlineSync()60bool SyncCoordinator::IsOnlineSync() 61 { 62 return isOnlineTrigger_; 63 } 64 DispatchSyncTask(const SyncTask & syncTask,int64_t delayTime)65bool SyncCoordinator::DispatchSyncTask(const SyncTask& syncTask, int64_t delayTime) 66 { 67 if (!syncHandler_->PostTask(syncTask, delayTime)) { 68 HILOGE("post task failed"); 69 isOnSync_ = false; 70 return false; 71 } 72 return true; 73 } 74 } // namespace DeviceProfile 75 } // namespace OHOS 76