1 /*
2 * Copyright (C) 2024-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 #include "external_files_count_statistic.h"
16
17 #include "media_log.h"
18 #include "backup_database_utils.h"
19 #include "backup_const.h"
20 #include "media_backup_report_data_type.h"
21
22 namespace OHOS::Media {
23 static const int32_t INIT_FAIL_TOTAL_COUNT = -1;
24 static const int32_t IMAGE_MEDIA_TYPE = 1;
25 static const int32_t VIDEO_MEDIA_TYPE = 3;
Load()26 std::vector<AlbumMediaStatisticInfo> ExternalFilesCountStatistic::Load()
27 {
28 if (this->externalRdb_ == nullptr) {
29 MEDIA_ERR_LOG("externalRdb_ is nullptr, Maybe init failed.");
30 return {};
31 }
32 return {this->GetMediaStatInfo(), this->GetAudioStatInfo(), this->GetGalleryNotSyncMediaStatInfo()};
33 }
34
GetMediaStatInfo()35 AlbumMediaStatisticInfo ExternalFilesCountStatistic::GetMediaStatInfo()
36 {
37 AlbumMediaStatisticInfo info;
38 info.sceneCode = this->sceneCode_;
39 info.taskId = this->taskId_;
40 info.albumName = "External_Media";
41 auto mediaTypeCountMap = this->QueryExternalImageAndVideoCount();
42 GetMediaStatInfoFromMap(mediaTypeCountMap, info);
43 return info;
44 }
45
GetAudioStatInfo()46 AlbumMediaStatisticInfo ExternalFilesCountStatistic::GetAudioStatInfo()
47 {
48 int32_t externalAudioCount = this->QueryExternalAudioCount();
49 AlbumMediaStatisticInfo info;
50 info.sceneCode = this->sceneCode_;
51 info.taskId = this->taskId_;
52 info.albumName = "External_Audio";
53 info.totalCount = externalAudioCount;
54 return info;
55 }
56
GetGalleryNotSyncMediaStatInfo()57 AlbumMediaStatisticInfo ExternalFilesCountStatistic::GetGalleryNotSyncMediaStatInfo()
58 {
59 AlbumMediaStatisticInfo info;
60 info.sceneCode = this->sceneCode_;
61 info.taskId = this->taskId_;
62 info.albumName = "Extermal_Restore_Gallery_Not_Sync_Media";
63 if (this->galleryRdb_ == nullptr) {
64 MEDIA_ERR_LOG("galleryRdb_ is nullptr, Maybe init failed.");
65 info.totalCount = INIT_FAIL_TOTAL_COUNT;
66 return info;
67 }
68 int32_t maxId = BackupDatabaseUtils::QueryInt(galleryRdb_, QUERY_MAX_ID_ALL, CUSTOM_MAX_ID);
69 auto mediaTypeCountMap = this->QueryGalleryNotSyncMedia(maxId);
70 GetMediaStatInfoFromMap(mediaTypeCountMap, info);
71 return info;
72 }
73
GetMediaStatInfoFromMap(const std::unordered_map<int32_t,int32_t> & mediaTypeCountMap,AlbumMediaStatisticInfo & info)74 void ExternalFilesCountStatistic::GetMediaStatInfoFromMap(
75 const std::unordered_map<int32_t, int32_t>& mediaTypeCountMap, AlbumMediaStatisticInfo& info)
76 {
77 for (auto iter = mediaTypeCountMap.begin(); iter != mediaTypeCountMap.end(); iter++) {
78 int32_t mediaType = iter->first;
79 int32_t count = iter->second;
80 info.totalCount += count;
81 switch (mediaType) {
82 case IMAGE_MEDIA_TYPE:
83 info.imageCount = count;
84 break;
85 case VIDEO_MEDIA_TYPE:
86 info.videoCount = count;
87 break;
88 default:
89 break;
90 }
91 }
92 }
93
QueryExternalAudioCount()94 int32_t ExternalFilesCountStatistic::QueryExternalAudioCount()
95 {
96 static string QUERY_EXTERNAL_AUDIO_COUNT = "SELECT count(1) as count FROM files WHERE media_type = 2 AND _size > 0 \
97 AND _data LIKE '/storage/emulated/0/Music%'";
98 return BackupDatabaseUtils::QueryInt(this->externalRdb_, QUERY_EXTERNAL_AUDIO_COUNT, CUSTOM_COUNT);
99 }
100
QueryExternalImageAndVideoCount()101 std::unordered_map<int32_t, int32_t> ExternalFilesCountStatistic::QueryExternalImageAndVideoCount()
102 {
103 static string QUERY_EXTERNAL_IMAGE_AND_VIDEO_COUNT = QUERY_MEDIA_TYPE_AND_COUNT_FROM_FILES +
104 IMAGE_AND_VIDEO_TYPE + GROUP_BY_MEIDA_TYPE;
105 return BackupDatabaseUtils::QueryMediaTypeCount(this->externalRdb_, QUERY_EXTERNAL_IMAGE_AND_VIDEO_COUNT);
106 }
107
QueryGalleryNotSyncMedia(const int32_t maxId)108 std::unordered_map<int32_t, int32_t> ExternalFilesCountStatistic::QueryGalleryNotSyncMedia(const int32_t maxId)
109 {
110 std::string queryNotSyncByCount = QUERY_MEDIA_TYPE_AND_COUNT_FROM_FILES + IS_PENDING + " AND " +
111 COMPARE_ID + std::to_string(maxId) + " AND " + QUERY_NOT_SYNC + GROUP_BY_MEIDA_TYPE;
112 return BackupDatabaseUtils::QueryMediaTypeCount(externalRdb_, queryNotSyncByCount);
113 }
114
115 } // namespace OHOS::Media