• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "ptp_album_handles.h"
17 
18 #include "datashare_predicates.h"
19 #include "datashare_abs_result_set.h"
20 #include "media_column.h"
21 #include "media_log.h"
22 #include "medialibrary_errno.h"
23 #include "result_set_utils.h"
24 
25 namespace OHOS {
26 namespace Media {
27 using namespace std;
28 std::shared_ptr<PtpAlbumHandles> PtpAlbumHandles::instance_ = nullptr;
29 std::mutex PtpAlbumHandles::mutex_;
30 
GetInstance()31 std::shared_ptr<PtpAlbumHandles> PtpAlbumHandles::GetInstance()
32 {
33     if (instance_ == nullptr) {
34         std::lock_guard<std::mutex> lock(mutex_);
35         if (instance_ == nullptr) {
36             instance_ = std::make_shared<PtpAlbumHandles>();
37         }
38     }
39     return instance_;
40 }
41 
~PtpAlbumHandles()42 PtpAlbumHandles::~PtpAlbumHandles()
43 {
44     std::lock_guard<std::mutex> lock(mutex_);
45     dataHandles_.clear();
46 }
47 
AddHandle(int32_t value)48 void PtpAlbumHandles::AddHandle(int32_t value)
49 {
50     std::lock_guard<std::mutex> lock(mutex_);
51     auto iter = std::find(dataHandles_.begin(), dataHandles_.end(), value);
52     if (iter == dataHandles_.end()) {
53         dataHandles_.push_back(value);
54     }
55 }
56 
RemoveHandle(int32_t value)57 void PtpAlbumHandles::RemoveHandle(int32_t value)
58 {
59     std::lock_guard<std::mutex> lock(mutex_);
60     auto iter = std::find(dataHandles_.begin(), dataHandles_.end(), value);
61     if (iter != dataHandles_.end()) {
62         dataHandles_.erase(iter);
63     }
64 }
65 
AddAlbumHandles(const std::shared_ptr<DataShare::DataShareResultSet> & resultSet)66 void PtpAlbumHandles::AddAlbumHandles(const std::shared_ptr<DataShare::DataShareResultSet> &resultSet)
67 {
68     CHECK_AND_RETURN_LOG(resultSet != nullptr, "resultSet is nullptr");
69     std::lock_guard<std::mutex> lock(mutex_);
70     dataHandles_.clear();
71     while (resultSet->GoToNextRow() == NativeRdb::E_OK) {
72         int32_t id = GetInt32Val(MediaColumn::MEDIA_ID, resultSet);
73         dataHandles_.push_back(id);
74     }
75     resultSet->GoToFirstRow();
76 }
77 
FindHandle(int32_t value)78 bool PtpAlbumHandles::FindHandle(int32_t value)
79 {
80     std::lock_guard<std::mutex> lock(mutex_);
81     auto iter = std::find(dataHandles_.begin(), dataHandles_.end(), value);
82     return iter != dataHandles_.end();
83 }
84 
UpdateHandle(const std::set<int32_t> & albumIds,std::vector<int32_t> & removeIds)85 void PtpAlbumHandles::UpdateHandle(const std::set<int32_t> &albumIds, std::vector<int32_t> &removeIds)
86 {
87     std::lock_guard<std::mutex> lock(mutex_);
88     for (auto value : dataHandles_) {
89         if (albumIds.count(value) == 0) {
90             removeIds.push_back(value);
91         }
92     }
93 }
94 } // namespace Media
95 } // namespace OHOS
96