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 #define MLOG_TAG "MultiStagesCaptureManager"
17
18 #include "multistages_capture_manager.h"
19
20
21 #include "media_log.h"
22 #include "medialibrary_rdbstore.h"
23 #include "result_set_utils.h"
24 #include "medialibrary_tracer.h"
25
26 using namespace std;
27
28 namespace OHOS {
29 namespace Media {
30
MultiStagesCaptureManager()31 MultiStagesCaptureManager::MultiStagesCaptureManager() {}
32
~MultiStagesCaptureManager()33 MultiStagesCaptureManager::~MultiStagesCaptureManager() {}
34
RemovePhotos(const NativeRdb::AbsRdbPredicates & predicates,bool isRestorable)35 void MultiStagesCaptureManager::RemovePhotos(const NativeRdb::AbsRdbPredicates &predicates,
36 bool isRestorable)
37 {
38 MEDIA_INFO_LOG("Remove photos enter, isRestorable is: %{public}d", isRestorable);
39 if (predicates.GetTableName() != PhotoColumn::PHOTOS_TABLE) {
40 MEDIA_INFO_LOG("Invalid table name: %{public}s", predicates.GetTableName().c_str());
41 return;
42 }
43
44 NativeRdb::AbsRdbPredicates predicatesNew(predicates.GetTableName());
45 string where = predicates.GetWhereClause() + " AND (" + PhotoColumn::PHOTO_QUALITY + "=" +
46 to_string(static_cast<int32_t>(MultiStagesPhotoQuality::LOW)) + " OR " +
47 PhotoColumn::STAGE_VIDEO_TASK_STATUS + " = " +
48 to_string(static_cast<int32_t>(StageVideoTaskStatus::STAGE_TASK_DELIVERED)) + ")";
49
50 predicatesNew.SetWhereClause(where);
51 predicatesNew.SetWhereArgs(predicates.GetWhereArgs());
52 vector<string> columns { MediaColumn::MEDIA_ID, MEDIA_DATA_DB_PHOTO_ID, MEDIA_DATA_DB_PHOTO_QUALITY,
53 MEDIA_DATA_DB_MEDIA_TYPE, MEDIA_DATA_DB_STAGE_VIDEO_TASK_STATUS };
54 auto resultSet = MediaLibraryRdbStore::QueryWithFilter(predicatesNew, columns);
55 if (resultSet == nullptr || resultSet->GoToFirstRow() != NativeRdb::E_OK) {
56 MEDIA_INFO_LOG("Result set is empty");
57 return;
58 }
59
60 do {
61 string photoId = GetStringVal(MEDIA_DATA_DB_PHOTO_ID, resultSet);
62 int32_t stageVideoTaskStatus = GetInt32Val(MEDIA_DATA_DB_STAGE_VIDEO_TASK_STATUS, resultSet);
63 // Moving photo remove video task
64 if (stageVideoTaskStatus == static_cast<int32_t>(StageVideoTaskStatus::STAGE_TASK_DELIVERED)) {
65 MultiStagesVideoCaptureManager::GetInstance().RemoveVideo(photoId, isRestorable);
66 continue;
67 }
68
69 int32_t photoQuality = GetInt32Val(MEDIA_DATA_DB_PHOTO_QUALITY, resultSet);
70 if (photoId.empty() || photoQuality == static_cast<int32_t>(MultiStagesPhotoQuality::FULL)) {
71 MEDIA_DEBUG_LOG("photoId is empty or task status invalid ");
72 continue;
73 }
74
75 int32_t mediaType = GetInt32Val(MEDIA_DATA_DB_MEDIA_TYPE, resultSet);
76 switch (mediaType) {
77 case MediaType::MEDIA_TYPE_IMAGE:
78 MultiStagesPhotoCaptureManager::GetInstance().RemoveImage(photoId, isRestorable);
79 break;
80 case MediaType::MEDIA_TYPE_VIDEO:
81 MultiStagesVideoCaptureManager::GetInstance().RemoveVideo(photoId, isRestorable);
82 break;
83 default:
84 break;
85 }
86 } while (!resultSet->GoToNextRow());
87 }
88
RemovePhotosWithResultSet(const shared_ptr<NativeRdb::ResultSet> & resultSet,bool isRestorable)89 void MultiStagesCaptureManager::RemovePhotosWithResultSet(const shared_ptr<NativeRdb::ResultSet> &resultSet,
90 bool isRestorable)
91 {
92 if (resultSet == nullptr) {
93 MEDIA_ERR_LOG("Result set is empty");
94 return;
95 }
96 string photoId = GetStringVal(MEDIA_DATA_DB_PHOTO_ID, resultSet);
97 int32_t stageVideoTaskStatus = GetInt32Val(MEDIA_DATA_DB_STAGE_VIDEO_TASK_STATUS, resultSet);
98 // Moving photo remove video task
99 if (stageVideoTaskStatus == static_cast<int32_t>(StageVideoTaskStatus::STAGE_TASK_DELIVERED)) {
100 string mediaFilePath = GetStringVal(MediaColumn::MEDIA_FILE_PATH, resultSet);
101 int32_t photoSubType = GetInt32Val(PhotoColumn::PHOTO_SUBTYPE, resultSet);
102 MultiStagesVideoCaptureManager::GetInstance().RemoveVideo(photoId, mediaFilePath, photoSubType,
103 isRestorable);
104 return;
105 }
106 int32_t photoQuality = GetInt32Val(MEDIA_DATA_DB_PHOTO_QUALITY, resultSet);
107 if (photoId.empty() || photoQuality == static_cast<int32_t>(MultiStagesPhotoQuality::FULL)) {
108 MEDIA_DEBUG_LOG("photoId is empty or task status invalid ");
109 return;
110 }
111 int32_t mediaType = GetInt32Val(MEDIA_DATA_DB_MEDIA_TYPE, resultSet);
112 switch (mediaType) {
113 case MediaType::MEDIA_TYPE_IMAGE:
114 MultiStagesPhotoCaptureManager::GetInstance().RemoveImage(photoId, isRestorable);
115 break;
116 case MediaType::MEDIA_TYPE_VIDEO:
117 {
118 string mediaFilePath = GetStringVal(MediaColumn::MEDIA_FILE_PATH, resultSet);
119 int32_t photoSubType = GetInt32Val(PhotoColumn::PHOTO_SUBTYPE, resultSet);
120 MultiStagesVideoCaptureManager::GetInstance()
121 .RemoveVideo(photoId, mediaFilePath, photoSubType, isRestorable);
122 }
123 break;
124 default:
125 break;
126 }
127 }
128
RestorePhotos(const NativeRdb::AbsRdbPredicates & predicates)129 void MultiStagesCaptureManager::RestorePhotos(const NativeRdb::AbsRdbPredicates &predicates)
130 {
131 MEDIA_INFO_LOG("Restore photos enter");
132 if (predicates.GetTableName() != PhotoColumn::PHOTOS_TABLE) {
133 MEDIA_INFO_LOG("Invalid table name: %{public}s", predicates.GetTableName().c_str());
134 return;
135 }
136 MediaLibraryTracer tracer;
137 tracer.Start("MultiStagesCaptureManager::RestorePhotos");
138 NativeRdb::AbsRdbPredicates predicatesNew(predicates.GetTableName());
139 string where = predicates.GetWhereClause() + " AND " + PhotoColumn::PHOTO_QUALITY + "=" +
140 to_string(static_cast<int32_t>(MultiStagesPhotoQuality::LOW));
141 predicatesNew.SetWhereClause(where);
142 predicatesNew.SetWhereArgs(predicates.GetWhereArgs());
143 vector<string> columns { MediaColumn::MEDIA_ID, MEDIA_DATA_DB_PHOTO_ID, MEDIA_DATA_DB_PHOTO_QUALITY,
144 MEDIA_DATA_DB_MEDIA_TYPE };
145 auto resultSet = MediaLibraryRdbStore::QueryWithFilter(predicatesNew, columns);
146 bool cond = (resultSet == nullptr || resultSet->GoToFirstRow() != NativeRdb::E_OK);
147 CHECK_AND_RETURN_INFO_LOG(!cond, "Result set is empty");
148
149 do {
150 string photoId = GetStringVal(MEDIA_DATA_DB_PHOTO_ID, resultSet);
151 int32_t photoQuality = GetInt32Val(MEDIA_DATA_DB_PHOTO_QUALITY, resultSet);
152 if (photoId.empty() || photoQuality == static_cast<int32_t>(MultiStagesPhotoQuality::FULL)) {
153 MEDIA_DEBUG_LOG("photoId is empty or full quality ");
154 continue;
155 }
156
157 int32_t mediaType = GetInt32Val(MEDIA_DATA_DB_MEDIA_TYPE, resultSet);
158 switch (mediaType) {
159 case MediaType::MEDIA_TYPE_IMAGE:
160 MultiStagesPhotoCaptureManager::GetInstance().RestoreImage(photoId);
161 break;
162 case MediaType::MEDIA_TYPE_VIDEO:
163 MultiStagesVideoCaptureManager::GetInstance().RestoreVideo(photoId);
164 break;
165 default:
166 break;
167 }
168 } while (!resultSet->GoToNextRow());
169 }
170
QuerySubType(const string & photoId)171 int32_t MultiStagesCaptureManager::QuerySubType(const string &photoId)
172 {
173 NativeRdb::AbsRdbPredicates predicatesNew(PhotoColumn::PHOTOS_TABLE);
174 string where = PhotoColumn::PHOTO_ID + " = ? ";
175 vector<string> whereArgs { photoId };
176 predicatesNew.SetWhereClause(where);
177 predicatesNew.SetWhereArgs(whereArgs);
178 vector<string> columns { PhotoColumn::PHOTO_SUBTYPE };
179 auto resultSet = MediaLibraryRdbStore::QueryWithFilter(predicatesNew, columns);
180 if (resultSet == nullptr || resultSet->GoToFirstRow() != NativeRdb::E_OK) {
181 MEDIA_ERR_LOG("Result set is empty, photoId: %{public}s", photoId.c_str());
182 return static_cast<int32_t>(PhotoSubType::CAMERA);
183 }
184
185 return GetInt32Val(PhotoColumn::PHOTO_SUBTYPE, resultSet);
186 }
187 } // Media
188 } // OHOS