• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "medialibrary_all_album_refresh_processor.h"
17 
18 #include "albums_refresh_manager.h"
19 #include "cloud_sync_helper.h"
20 #include "ffrt.h"
21 #include "ffrt_inner.h"
22 #include "medialibrary_album_fusion_utils.h"
23 #include "medialibrary_album_refresh.h"
24 
25 namespace OHOS {
26 namespace Media {
27 using namespace std;
28 namespace {
29 const int64_t REFRESH_ALL_ALBUMS_INTERVAL = 86400000000;  // 24 hours
30 }
31 
32 shared_ptr<MediaLibraryAllAlbumRefreshProcessor> MediaLibraryAllAlbumRefreshProcessor::instance_ = nullptr;
33 mutex MediaLibraryAllAlbumRefreshProcessor::instanceMutex_;
34 
GetInstance()35 shared_ptr<MediaLibraryAllAlbumRefreshProcessor> MediaLibraryAllAlbumRefreshProcessor::GetInstance()
36 {
37     if (instance_ == nullptr) {
38         lock_guard<mutex> guard(instanceMutex_);
39         if (instance_ != nullptr) {
40             return instance_;
41         }
42         auto *mediaLibraryAllAlbumRefreshProcessor = new (nothrow)MediaLibraryAllAlbumRefreshProcessor();
43         if (mediaLibraryAllAlbumRefreshProcessor == nullptr) {
44             MEDIA_ERR_LOG("Failed to new MediaLibraryAllAlbumRefreshProcessor");
45         }
46         instance_ = shared_ptr<MediaLibraryAllAlbumRefreshProcessor>(mediaLibraryAllAlbumRefreshProcessor);
47     }
48     return instance_;
49 }
50 
MediaLibraryAllAlbumRefreshProcessor()51 MediaLibraryAllAlbumRefreshProcessor::MediaLibraryAllAlbumRefreshProcessor()
52 {
53 }
54 
GetNowTimeUs()55 int64_t MediaLibraryAllAlbumRefreshProcessor::GetNowTimeUs()
56 {
57     struct timespec t;
58     constexpr int64_t SEC_TO_USEC = 1e6;
59     constexpr int64_t NSEC_TO_USEC = 1e3;
60     clock_gettime(CLOCK_REALTIME, &t);
61     return t.tv_sec * SEC_TO_USEC + t.tv_nsec / NSEC_TO_USEC;
62 }
63 
OnCurrentStatusChanged(bool currentStatus)64 void MediaLibraryAllAlbumRefreshProcessor::OnCurrentStatusChanged(bool currentStatus)
65 {
66     std::lock_guard<std::mutex> lock(refreshAllAlbumsLock_);
67     currentStatus_ = currentStatus;
68     MEDIA_INFO_LOG("OnCurrentStatusChanged! %{public}d", currentStatus_);
69     if (currentStatus_) {
70         PostRefreshAllAlbumsTask();
71     }
72 }
73 
OnCloudSyncStateChanged(bool isCloudSyncing)74 void MediaLibraryAllAlbumRefreshProcessor::OnCloudSyncStateChanged(bool isCloudSyncing)
75 {
76     std::lock_guard<std::mutex> lock(refreshAllAlbumsLock_);
77     isCloudSyncing_ = isCloudSyncing;
78     MEDIA_INFO_LOG("OnCloudSyncStateChanged! %{public}d", isCloudSyncing_);
79     if (!isCloudSyncing_) {
80         PostRefreshAllAlbumsTask();
81     }
82 }
83 
CheckRefreshConditionLocked()84 bool MediaLibraryAllAlbumRefreshProcessor::CheckRefreshConditionLocked()
85 {
86     return currentStatus_ && !isCloudSyncing_ &&
87         (lastRefreshAllAlbumsTime_ == 0 || GetNowTimeUs() - lastRefreshAllAlbumsTime_ > REFRESH_ALL_ALBUMS_INTERVAL);
88 }
89 
TryRefreshAllAlbums()90 void MediaLibraryAllAlbumRefreshProcessor::TryRefreshAllAlbums()
91 {
92     {
93         std::lock_guard<std::mutex> lock(refreshAllAlbumsLock_);
94         if (!CheckRefreshConditionLocked()) {
95             return;
96         }
97         MEDIA_INFO_LOG("RefreshAllAlbums! now: %{public}" PRId64 ", last : %{public}" PRId64,
98             GetNowTimeUs(), lastRefreshAllAlbumsTime_);
99         lastRefreshAllAlbumsTime_ = GetNowTimeUs();
100     }
101     MediaLibraryAlbumFusionUtils::RefreshAllAlbums();
102     ffrt::submit([this]() { TryRefreshAllAlbums(); },
103         ffrt::task_attr().delay(REFRESH_ALL_ALBUMS_INTERVAL));
104 }
105 
PostRefreshAllAlbumsTask()106 void MediaLibraryAllAlbumRefreshProcessor::PostRefreshAllAlbumsTask()
107 {
108     ffrt::submit([this]() { TryRefreshAllAlbums(); });
109 }
110 } // namespace Media
111 } // namespace OHOS
112