• 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 #define MLOG_TAG "Cloud"
17 
18 #include "cloud_sync_helper.h"
19 
20 #include "medialibrary_errno.h"
21 #include "media_log.h"
22 
23 namespace OHOS {
24 namespace Media {
25 using namespace std;
26 using namespace FileManagement::CloudSync;
27 
28 shared_ptr<CloudSyncHelper> CloudSyncHelper::instance_ = nullptr;
29 mutex CloudSyncHelper::instanceMutex_;
30 
GetInstance()31 shared_ptr<CloudSyncHelper> CloudSyncHelper::GetInstance()
32 {
33     if (instance_ == nullptr) {
34         lock_guard<mutex> guard(instanceMutex_);
35         if (instance_ != nullptr) {
36             return instance_;
37         }
38         instance_ = shared_ptr<CloudSyncHelper>(new (nothrow)CloudSyncHelper());
39     }
40 
41     return instance_;
42 }
43 
CloudSyncHelper()44 CloudSyncHelper::CloudSyncHelper() : timer_("CloudSync")
45 {
46     timer_.Setup();
47 }
48 
~CloudSyncHelper()49 CloudSyncHelper::~CloudSyncHelper()
50 {
51     timer_.Unregister(timerId_);
52     timer_.Shutdown();
53 }
54 
StartSync()55 void CloudSyncHelper::StartSync()
56 {
57     lock_guard<mutex> lock(syncMutex_);
58     if (isPending_) {
59         /* cancel the previous timer */
60         timer_.Unregister(timerId_);
61     } else {
62         isPending_ = true;
63     }
64     timerId_ = timer_.Register(bind(&CloudSyncHelper::OnTimerCallback, this),
65         SYNC_INTERVAL, true);
66 }
67 
OnTimerCallback()68 void CloudSyncHelper::OnTimerCallback()
69 {
70     unique_lock<mutex> lock(syncMutex_);
71     isPending_ = false;
72     lock.unlock();
73 
74     MEDIA_INFO_LOG("cloud sync manager start sync");
75     auto callback = make_shared<MediaCloudSyncCallback>();
76     int32_t ret = CloudSyncManager::GetInstance().StartSync(false, callback);
77     if (ret != 0) {
78         MEDIA_ERR_LOG("cloud sync manager start sync err %{public}d", ret);
79     }
80 }
81 
OnSyncStateChanged(SyncType type,SyncPromptState state)82 void MediaCloudSyncCallback::OnSyncStateChanged(SyncType type, SyncPromptState state)
83 {
84     MEDIA_INFO_LOG("sync type %{public}d, state %{public}d", type, state);
85 }
86 } // namespace Media
87 } // namespace OHOS
88