1 /*
2 * Copyright (C) 2024-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_album_refresh.h"
17
18 #include "cpu_utils.h"
19 #include "media_file_utils.h"
20 #include "medialibrary_notify.h"
21 #include "medialibrary_object_utils.h"
22 #include "medialibrary_rdb_utils.h"
23 #include "medialibrary_rdbstore.h"
24 #include "photo_album_column.h"
25
26 using namespace OHOS::NativeRdb;
27 using namespace OHOS::DataShare;
28 using namespace OHOS::RdbDataShareAdapter;
29
30 namespace OHOS::Media {
NotifyAnalysisAlbum(PhotoAlbumSubType subtype,int32_t albumId)31 static void NotifyAnalysisAlbum(PhotoAlbumSubType subtype, int32_t albumId)
32 {
33 const static set<PhotoAlbumSubType> NEED_FLUSH_ANALYSIS_ALBUM = {
34 PhotoAlbumSubType::SHOOTING_MODE,
35 };
36 if (NEED_FLUSH_ANALYSIS_ALBUM.find(subtype) != NEED_FLUSH_ANALYSIS_ALBUM.end()) {
37 auto watch = MediaLibraryNotify::GetInstance();
38 if (watch == nullptr) {
39 MEDIA_ERR_LOG("Can not get MediaLibraryNotify Instance");
40 return;
41 }
42 if (albumId > 0) {
43 watch->Notify(MediaFileUtils::GetUriByExtrConditions(PhotoAlbumColumns::ANALYSIS_ALBUM_URI_PREFIX,
44 std::to_string(albumId)), NotifyType::NOTIFY_ADD);
45 } else {
46 watch->Notify(PhotoAlbumColumns::ANALYSIS_ALBUM_URI_PREFIX, NotifyType::NOTIFY_ADD);
47 }
48 }
49 }
50
NotifySystemAlbumFunc(PhotoAlbumType albumtype,PhotoAlbumSubType subtype,int32_t albumId)51 static void NotifySystemAlbumFunc(PhotoAlbumType albumtype, PhotoAlbumSubType subtype, int32_t albumId)
52 {
53 if (albumtype == PhotoAlbumType::SMART) {
54 NotifyAnalysisAlbum(subtype, albumId);
55 return;
56 }
57 const static set<PhotoAlbumSubType> NEED_FLUSH_PHOTO_ALBUM = {
58 PhotoAlbumSubType::IMAGE,
59 PhotoAlbumSubType::VIDEO,
60 PhotoAlbumSubType::USER_GENERIC,
61 PhotoAlbumSubType::SOURCE_GENERIC,
62 };
63 if (NEED_FLUSH_PHOTO_ALBUM.find(subtype) != NEED_FLUSH_PHOTO_ALBUM.end()) {
64 auto watch = MediaLibraryNotify::GetInstance();
65 if (watch == nullptr) {
66 MEDIA_ERR_LOG("Can not get MediaLibraryNotify Instance");
67 return;
68 }
69 if (albumId > 0) {
70 watch->Notify(MediaFileUtils::GetUriByExtrConditions(PhotoAlbumColumns::ALBUM_URI_PREFIX,
71 std::to_string(albumId)), NotifyType::NOTIFY_ADD);
72 } else {
73 watch->Notify(PhotoAlbumColumns::ALBUM_URI_PREFIX, NotifyType::NOTIFY_ADD);
74 }
75 watch->Notify(PhotoColumn::PHOTO_URI_PREFIX, NotifyType::NOTIFY_UPDATE);
76 }
77 }
78
RefreshCallbackFunc()79 static void RefreshCallbackFunc()
80 {
81 auto watch = MediaLibraryNotify::GetInstance();
82 if (watch == nullptr) {
83 MEDIA_ERR_LOG("Can not get MediaLibraryNotify Instance");
84 return;
85 }
86 watch->Notify(PhotoAlbumColumns::ALBUM_URI_PREFIX, NotifyType::NOTIFY_ADD);
87 watch->Notify(PhotoAlbumColumns::ANALYSIS_ALBUM_URI_PREFIX, NotifyType::NOTIFY_ADD);
88 }
89
RefreshAlbumAsyncTask(AsyncTaskData * data)90 static void RefreshAlbumAsyncTask(AsyncTaskData *data)
91 {
92 auto rdbStore = MediaLibraryUnistoreManager::GetInstance().GetRdbStore();
93 if (rdbStore == nullptr) {
94 MEDIA_ERR_LOG("Medialibrary rdbStore is nullptr!");
95 return;
96 }
97
98 int32_t ret = MediaLibraryRdbUtils::RefreshAllAlbums(rdbStore,
99 NotifySystemAlbumFunc, RefreshCallbackFunc);
100 if (ret != E_OK) {
101 MEDIA_ERR_LOG("RefreshAllAlbums failed ret:%{public}d", ret);
102 }
103 }
104
RefreshAlbums(bool forceRefresh)105 void RefreshAlbums(bool forceRefresh)
106 {
107 if (MediaLibraryRdbUtils::IsNeedRefreshAlbum() && (forceRefresh || !MediaLibraryRdbUtils::IsInRefreshTask())) {
108 shared_ptr<MediaLibraryAsyncWorker> asyncWorker = MediaLibraryAsyncWorker::GetInstance();
109 if (asyncWorker == nullptr) {
110 MEDIA_ERR_LOG("Can not get asyncWorker");
111 return;
112 }
113
114 asyncWorker->ClearRefreshTaskQueue();
115 shared_ptr<MediaLibraryAsyncTask> notifyAsyncTask = make_shared<MediaLibraryAsyncTask>(
116 RefreshAlbumAsyncTask, nullptr, REFRESH_ALBUM);
117 if (notifyAsyncTask != nullptr) {
118 asyncWorker->AddTask(notifyAsyncTask, true);
119 } else {
120 MEDIA_ERR_LOG("Start UpdateAlbumsAndSendNotifyInTrash failed");
121 }
122 }
123 }
124 } // namespace OHOS::Media
125