1 /*
2 * Copyright (C) 2021 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 #define MLOG_TAG "MediaLibraryManager"
16
17 #include "media_library_manager.h"
18 #include "datashare_predicates.h"
19 #include "datashare_abs_result_set.h"
20 #include "media_log.h"
21 #include "medialibrary_errno.h"
22 #include "result_set_utils.h"
23
24 using namespace std;
25 using namespace OHOS::NativeRdb;
26
27 namespace OHOS {
28 namespace Media {
29 shared_ptr<DataShare::DataShareHelper> MediaLibraryManager::sDataShareHelper_ = nullptr;
30
GetMediaLibraryManager()31 MediaLibraryManager *MediaLibraryManager::GetMediaLibraryManager()
32 {
33 static MediaLibraryManager mediaLibMgr;
34 return &mediaLibMgr;
35 }
36
InitMediaLibraryManager(const sptr<IRemoteObject> & token)37 void MediaLibraryManager::InitMediaLibraryManager(const sptr<IRemoteObject> &token)
38 {
39 if (sDataShareHelper_ == nullptr) {
40 sDataShareHelper_ = DataShare::DataShareHelper::Creator(token, MEDIALIBRARY_DATA_URI);
41 }
42 }
43
CloseAsset(const string & uri,const int32_t fd)44 int32_t MediaLibraryManager::CloseAsset(const string &uri, const int32_t fd)
45 {
46 int32_t retVal = E_FAIL;
47 DataShareValuesBucket valuesBucket;
48 valuesBucket.Put(MEDIA_DATA_DB_URI, uri);
49
50 if (sDataShareHelper_ != nullptr) {
51 string abilityUri = MEDIALIBRARY_DATA_URI;
52 Uri closeAssetUri(abilityUri + "/" + MEDIA_FILEOPRN + "/" + MEDIA_FILEOPRN_CLOSEASSET);
53
54 if (close(fd) == E_SUCCESS) {
55 retVal = sDataShareHelper_->Insert(closeAssetUri, valuesBucket);
56 }
57
58 if (retVal == E_FAIL) {
59 MEDIA_ERR_LOG("Failed to close the file");
60 }
61 }
62
63 return retVal;
64 }
65
QueryTotalSize(MediaVolume & outMediaVolume)66 int32_t MediaLibraryManager::QueryTotalSize(MediaVolume &outMediaVolume)
67 {
68 if (sDataShareHelper_ == nullptr) {
69 MEDIA_ERR_LOG("sDataShareHelper_ is null");
70 return E_FAIL;
71 }
72 vector<string> columns;
73 Uri uri(MEDIALIBRARY_DATA_URI + "/" + MEDIA_QUERYOPRN + "/" + MEDIA_QUERYOPRN_QUERYVOLUME);
74 DataSharePredicates predicates;
75 auto queryResultSet = sDataShareHelper_->Query(uri, predicates, columns);
76 if (queryResultSet == nullptr) {
77 MEDIA_ERR_LOG("queryResultSet is null!");
78 return E_FAIL;
79 }
80 auto count = 0;
81 auto ret = queryResultSet->GetRowCount(count);
82 if (ret != NativeRdb::E_OK) {
83 MEDIA_ERR_LOG("get rdbstore failed");
84 return E_HAS_DB_ERROR;
85 }
86 MEDIA_INFO_LOG("count = %{public}d", (int)count);
87 if (count >= 0) {
88 while (queryResultSet->GoToNextRow() == NativeRdb::E_OK) {
89 int mediatype = get<int32_t>(ResultSetUtils::GetValFromColumn(MEDIA_DATA_DB_MEDIA_TYPE,
90 queryResultSet, TYPE_INT32));
91 int64_t size = get<int64_t>(ResultSetUtils::GetValFromColumn(MEDIA_DATA_DB_SIZE,
92 queryResultSet, TYPE_INT64));
93 outMediaVolume.SetSize(mediatype, size);
94 }
95 }
96 MEDIA_INFO_LOG("Size:Files:%{public}" PRId64 " Videos:%{public}" PRId64 " Images:%{public} " PRId64
97 " Audio:%{public}" PRId64,
98 outMediaVolume.GetFilesSize(), outMediaVolume.GetVideosSize(),
99 outMediaVolume.GetImagesSize(), outMediaVolume.GetAudiosSize());
100 return E_SUCCESS;
101 }
102 } // namespace Media
103 } // namespace OHOS
104