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