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 
25 using namespace std;
26 
27 namespace OHOS {
28 namespace Media {
29 
MultiStagesCaptureManager()30 MultiStagesCaptureManager::MultiStagesCaptureManager() {}
31 
~MultiStagesCaptureManager()32 MultiStagesCaptureManager::~MultiStagesCaptureManager() {}
33 
RemovePhotos(const NativeRdb::AbsRdbPredicates & predicates,bool isRestorable)34 void MultiStagesCaptureManager::RemovePhotos(const NativeRdb::AbsRdbPredicates &predicates,
35     bool isRestorable)
36 {
37     MEDIA_INFO_LOG("Remove photos enter, isRestorable is: %{public}d", isRestorable);
38     if (predicates.GetTableName() != PhotoColumn::PHOTOS_TABLE) {
39         MEDIA_INFO_LOG("Invalid table name: %{public}s", predicates.GetTableName().c_str());
40         return;
41     }
42 
43     NativeRdb::AbsRdbPredicates predicatesNew(predicates.GetTableName());
44     string where = predicates.GetWhereClause() + " AND " + PhotoColumn::PHOTO_QUALITY + "=" +
45         to_string(static_cast<int32_t>(MultiStagesPhotoQuality::LOW));
46 
47     predicatesNew.SetWhereClause(where);
48     predicatesNew.SetWhereArgs(predicates.GetWhereArgs());
49     vector<string> columns { MediaColumn::MEDIA_ID, MEDIA_DATA_DB_PHOTO_ID, MEDIA_DATA_DB_PHOTO_QUALITY,
50         MEDIA_DATA_DB_MEDIA_TYPE };
51     auto resultSet = MediaLibraryRdbStore::QueryWithFilter(predicatesNew, columns);
52     if (resultSet == nullptr || resultSet->GoToFirstRow() != NativeRdb::E_OK) {
53         MEDIA_INFO_LOG("Result set is empty");
54         return;
55     }
56 
57     do {
58         string photoId = GetStringVal(MEDIA_DATA_DB_PHOTO_ID, resultSet);
59         int32_t photoQuality = GetInt32Val(MEDIA_DATA_DB_PHOTO_QUALITY, resultSet);
60         if (photoId.empty() || photoQuality == static_cast<int32_t>(MultiStagesPhotoQuality::FULL)) {
61             MEDIA_DEBUG_LOG("photoId is empty or full quality ");
62             continue;
63         }
64 
65         int32_t mediaType = GetInt32Val(MEDIA_DATA_DB_MEDIA_TYPE, resultSet);
66         MEDIA_INFO_LOG("Media type is: %{public}d", mediaType);
67         switch (mediaType) {
68             case MediaType::MEDIA_TYPE_IMAGE:
69                 MultiStagesPhotoCaptureManager::GetInstance().RemoveImage(photoId, isRestorable);
70                 break;
71             case MediaType::MEDIA_TYPE_VIDEO:
72                 MultiStagesVideoCaptureManager::GetInstance().RemoveVideo(photoId, isRestorable);
73                 break;
74             default:
75                 break;
76         }
77     } while (!resultSet->GoToNextRow());
78 }
79 
RestorePhotos(const NativeRdb::AbsRdbPredicates & predicates)80 void MultiStagesCaptureManager::RestorePhotos(const NativeRdb::AbsRdbPredicates &predicates)
81 {
82     MEDIA_INFO_LOG("Restore photos enter");
83     if (predicates.GetTableName() != PhotoColumn::PHOTOS_TABLE) {
84         MEDIA_INFO_LOG("Invalid table name: %{public}s", predicates.GetTableName().c_str());
85         return;
86     }
87 
88     NativeRdb::AbsRdbPredicates predicatesNew(predicates.GetTableName());
89     string where = predicates.GetWhereClause() + " AND " + PhotoColumn::PHOTO_QUALITY + "=" +
90         to_string(static_cast<int32_t>(MultiStagesPhotoQuality::LOW));
91     predicatesNew.SetWhereClause(where);
92     predicatesNew.SetWhereArgs(predicates.GetWhereArgs());
93     vector<string> columns { MediaColumn::MEDIA_ID, MEDIA_DATA_DB_PHOTO_ID, MEDIA_DATA_DB_PHOTO_QUALITY,
94         MEDIA_DATA_DB_MEDIA_TYPE };
95     auto resultSet = MediaLibraryRdbStore::QueryWithFilter(predicatesNew, columns);
96     if (resultSet == nullptr || resultSet->GoToFirstRow() != NativeRdb::E_OK) {
97         MEDIA_INFO_LOG("Result set is empty");
98         return;
99     }
100 
101     do {
102         string photoId = GetStringVal(MEDIA_DATA_DB_PHOTO_ID, resultSet);
103         int32_t photoQuality = GetInt32Val(MEDIA_DATA_DB_PHOTO_QUALITY, resultSet);
104         if (photoId.empty() || photoQuality == static_cast<int32_t>(MultiStagesPhotoQuality::FULL)) {
105             MEDIA_DEBUG_LOG("photoId is empty or full quality ");
106             continue;
107         }
108 
109         int32_t mediaType = GetInt32Val(MEDIA_DATA_DB_MEDIA_TYPE, resultSet);
110         switch (mediaType) {
111             case MediaType::MEDIA_TYPE_IMAGE:
112                 MultiStagesPhotoCaptureManager::GetInstance().RestoreImage(photoId);
113                 break;
114             case MediaType::MEDIA_TYPE_VIDEO:
115                 MultiStagesVideoCaptureManager::GetInstance().RestoreVideo(photoId);
116                 break;
117             default:
118                 break;
119         }
120     } while (!resultSet->GoToNextRow());
121 }
122 } // Media
123 } // OHOS