• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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 "CloudEnhancementChecker"
17 
18 #include "cloud_enhancement_checker.h"
19 
20 #include "media_column.h"
21 #include "preferences.h"
22 #include "preferences_helper.h"
23 #include "result_set_utils.h"
24 #include "media_log.h"
25 #include "medialibrary_rdbstore.h"
26 #include "enhancement_database_operations.h"
27 #include "medialibrary_unistore_manager.h"
28 
29 using namespace std;
30 using namespace OHOS::NativeRdb;
31 
32 namespace OHOS {
33 namespace Media {
34 static const std::string TASK_PROGRESS_XML = "/data/storage/el2/base/preferences/task_progress.xml";
35 static const std::string PERMISSION_ADDED_FILE_ID = "permission_added_file_id";
36 
GetMaxFileId(int32_t & maxFileId)37 static void GetMaxFileId(int32_t& maxFileId)
38 {
39     auto rdbStore = MediaLibraryUnistoreManager::GetInstance().GetRdbStore();
40     CHECK_AND_RETURN_LOG(rdbStore != nullptr, "get rdb store failed");
41     string queryMaxSql = "SELECT Max(file_id) FROM " + PhotoColumn::PHOTOS_TABLE;
42     auto resultSet = rdbStore->QuerySql(queryMaxSql);
43     if (resultSet == nullptr || resultSet->GoToFirstRow() != NativeRdb::E_OK) {
44         MEDIA_ERR_LOG("Failed to get max file_id!");
45         return;
46     }
47     resultSet->GetInt(0, maxFileId);
48     resultSet->Close();
49     return;
50 }
51 
AddPermissionForCloudEnhancement()52 void CloudEnhancementChecker::AddPermissionForCloudEnhancement()
53 {
54     MEDIA_INFO_LOG("start to add permission for cloud enhancement photo!");
55     int32_t errCode = E_OK;
56     shared_ptr<NativePreferences::Preferences> prefs =
57         NativePreferences::PreferencesHelper::GetPreferences(TASK_PROGRESS_XML, errCode);
58     CHECK_AND_RETURN_LOG(prefs, "get preferences error: %{public}d", errCode);
59     int32_t curFileId = prefs->GetInt(PERMISSION_ADDED_FILE_ID, 0);
60     MEDIA_INFO_LOG("start file id: %{public}d", curFileId);
61     vector<string> columns = { MediaColumn::MEDIA_ID, PhotoColumn::PHOTO_ASSOCIATE_FILE_ID };
62     RdbPredicates predicates(PhotoColumn::PHOTOS_TABLE);
63     predicates.GreaterThan(MediaColumn::MEDIA_ID, curFileId);
64     predicates.And();
65     predicates.EqualTo(PhotoColumn::PHOTO_STRONG_ASSOCIATION,
66         to_string(static_cast<int32_t>(StrongAssociationType::CLOUD_ENHANCEMENT)));
67     auto resultSet = MediaLibraryRdbStore::StepQueryWithoutCheck(predicates, columns);
68     errCode = (resultSet == nullptr) ? E_ERR : E_OK;
69     if (resultSet != nullptr) {
70         errCode = (resultSet->GoToFirstRow() != E_OK) ? E_ERR : E_OK;
71     }
72     if (errCode == E_ERR) {
73         int32_t maxFileId = 0;
74         GetMaxFileId(maxFileId);
75         prefs->PutInt(PERMISSION_ADDED_FILE_ID, maxFileId);
76         prefs->FlushSync();
77         MEDIA_INFO_LOG("no cloud enhancement photo need to add permission, current max id:%{public}d",
78             maxFileId);
79         return;
80     }
81 
82     do {
83         int32_t sourceId = GetInt32Val(PhotoColumn::PHOTO_ASSOCIATE_FILE_ID, resultSet);
84         int32_t targetId = GetInt32Val(MediaColumn::MEDIA_ID, resultSet);
85         EnhancementDatabaseOperations::InsertCloudEnhancementPerm(sourceId, targetId);
86         prefs->PutInt(PERMISSION_ADDED_FILE_ID, targetId);
87         prefs->FlushSync();
88     } while (resultSet->GoToNextRow() == E_OK);
89     MEDIA_INFO_LOG("end add permission for cloud enhancement photo!");
90 }
91 }  // namespace Media
92 }  // namespace OHOS
93