• 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     bool cond = (resultSet == nullptr || resultSet->GoToFirstRow() != NativeRdb::E_OK);
44     CHECK_AND_RETURN_LOG(!cond, "Failed to get max file_id!");
45     resultSet->GetInt(0, maxFileId);
46     resultSet->Close();
47     return;
48 }
49 
AddPermissionForCloudEnhancement()50 void CloudEnhancementChecker::AddPermissionForCloudEnhancement()
51 {
52     MEDIA_INFO_LOG("start to add permission for cloud enhancement photo!");
53     int32_t errCode = E_OK;
54     shared_ptr<NativePreferences::Preferences> prefs =
55         NativePreferences::PreferencesHelper::GetPreferences(TASK_PROGRESS_XML, errCode);
56     CHECK_AND_RETURN_LOG(prefs, "get preferences error: %{public}d", errCode);
57     int32_t curFileId = prefs->GetInt(PERMISSION_ADDED_FILE_ID, 0);
58     MEDIA_INFO_LOG("start file id: %{public}d", curFileId);
59     vector<string> columns = { MediaColumn::MEDIA_ID, PhotoColumn::PHOTO_ASSOCIATE_FILE_ID };
60     RdbPredicates predicates(PhotoColumn::PHOTOS_TABLE);
61     predicates.GreaterThan(MediaColumn::MEDIA_ID, curFileId);
62     predicates.And();
63     predicates.EqualTo(PhotoColumn::PHOTO_STRONG_ASSOCIATION,
64         to_string(static_cast<int32_t>(StrongAssociationType::CLOUD_ENHANCEMENT)));
65     auto resultSet = MediaLibraryRdbStore::StepQueryWithoutCheck(predicates, columns);
66     errCode = (resultSet == nullptr) ? E_ERR : E_OK;
67     CHECK_AND_EXECUTE(resultSet == nullptr, errCode = (resultSet->GoToFirstRow() != E_OK) ? E_ERR : E_OK);
68     if (errCode == E_ERR) {
69         int32_t maxFileId = 0;
70         GetMaxFileId(maxFileId);
71         prefs->PutInt(PERMISSION_ADDED_FILE_ID, maxFileId);
72         prefs->FlushSync();
73         MEDIA_INFO_LOG("no cloud enhancement photo need to add permission, current max id:%{public}d",
74             maxFileId);
75         return;
76     }
77 
78     do {
79         int32_t sourceId = GetInt32Val(PhotoColumn::PHOTO_ASSOCIATE_FILE_ID, resultSet);
80         int32_t targetId = GetInt32Val(MediaColumn::MEDIA_ID, resultSet);
81         EnhancementDatabaseOperations::InsertCloudEnhancementPerm(sourceId, targetId);
82         prefs->PutInt(PERMISSION_ADDED_FILE_ID, targetId);
83         prefs->FlushSync();
84     } while (resultSet->GoToNextRow() == E_OK);
85     MEDIA_INFO_LOG("end add permission for cloud enhancement photo!");
86 }
87 }  // namespace Media
88 }  // namespace OHOS
89