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 #define MLOG_TAG "DfxAnalyzer"
16 
17 #include "dfx_analyzer.h"
18 
19 #include "dfx_utils.h"
20 #include "media_file_utils.h"
21 #include "media_log.h"
22 #include "preferences.h"
23 #include "preferences_helper.h"
24 
25 namespace OHOS {
26 namespace Media {
27 
28 using namespace std;
29 
DfxAnalyzer()30 DfxAnalyzer::DfxAnalyzer()
31 {
32 }
33 
~DfxAnalyzer()34 DfxAnalyzer::~DfxAnalyzer()
35 {
36 }
37 
FlushThumbnail(std::unordered_map<std::string,ThumbnailErrorInfo> & thumbnailErrorMap)38 void DfxAnalyzer::FlushThumbnail(std::unordered_map<std::string, ThumbnailErrorInfo> &thumbnailErrorMap)
39 {
40     if (thumbnailErrorMap.empty()) {
41         return;
42     }
43     int32_t errCode;
44     shared_ptr<NativePreferences::Preferences> prefs =
45         NativePreferences::PreferencesHelper::GetPreferences(THUMBNAIL_ERROR_XML, errCode);
46     if (!prefs) {
47         MEDIA_ERR_LOG("get preferences error: %{public}d", errCode);
48         return;
49     }
50     for (auto entry: thumbnailErrorMap) {
51         string key = entry.first + SPLIT_CHAR + to_string(entry.second.method) + SPLIT_CHAR +
52             to_string(entry.second.errCode);
53         if (!prefs->GetString(key).empty()) {
54             continue;
55         }
56         string value = to_string(entry.second.time);
57         prefs->PutString(key, value);
58     }
59     prefs->FlushSync();
60     MEDIA_INFO_LOG("flush %{public}zu itmes", thumbnailErrorMap.size());
61 }
62 
FlushCommonBehavior(std::unordered_map<string,CommonBehavior> & commonBehaviorMap)63 void DfxAnalyzer::FlushCommonBehavior(std::unordered_map<string, CommonBehavior> &commonBehaviorMap)
64 {
65     if (commonBehaviorMap.empty()) {
66         return;
67     }
68     int32_t errCode;
69     shared_ptr<NativePreferences::Preferences> prefs =
70         NativePreferences::PreferencesHelper::GetPreferences(COMMON_BEHAVIOR_XML, errCode);
71     if (!prefs) {
72         MEDIA_ERR_LOG("get preferences error: %{public}d", errCode);
73         return;
74     }
75     string behaviors;
76     for (auto entry: commonBehaviorMap) {
77         string bundleName = entry.first;
78         if (bundleName == "") {
79             continue;
80         }
81         int32_t times = entry.second.times;
82         int32_t oldValue = prefs->GetInt(bundleName, 0);
83         prefs->PutInt(bundleName, times + oldValue);
84         behaviors += "{" + bundleName + ": " + to_string(times) + "}";
85     }
86     prefs->FlushSync();
87     if (!behaviors.empty()) {
88         MEDIA_INFO_LOG("common behavior: %{public}s", behaviors.c_str());
89     }
90 }
91 
FlushDeleteBehavior(std::unordered_map<string,int32_t> & deleteBehaviorMap,int32_t type)92 void DfxAnalyzer::FlushDeleteBehavior(std::unordered_map<string, int32_t> &deleteBehaviorMap, int32_t type)
93 {
94     if (deleteBehaviorMap.empty()) {
95         return;
96     }
97     int32_t errCode;
98     shared_ptr<NativePreferences::Preferences> prefs =
99         NativePreferences::PreferencesHelper::GetPreferences(DELETE_BEHAVIOR_XML, errCode);
100     if (!prefs) {
101         MEDIA_ERR_LOG("get preferences error: %{public}d", errCode);
102         return;
103     }
104     for (auto entry: deleteBehaviorMap) {
105         string bundleName = entry.first;
106         int32_t times = entry.second;
107         int32_t oldValue = prefs->GetInt(bundleName, 0);
108         prefs->PutInt(bundleName + SPLIT_CHAR + to_string(type), times + oldValue);
109     }
110     prefs->FlushSync();
111 }
112 
FlushAdaptationToMovingPhoto(AdaptationToMovingPhotoInfo & newAdaptationInfo)113 void DfxAnalyzer::FlushAdaptationToMovingPhoto(AdaptationToMovingPhotoInfo& newAdaptationInfo)
114 {
115     if (newAdaptationInfo.unadaptedAppPackages.empty() && newAdaptationInfo.adaptedAppPackages.empty()) {
116         return;
117     }
118     int32_t errCode;
119     shared_ptr<NativePreferences::Preferences> prefs =
120         NativePreferences::PreferencesHelper::GetPreferences(ADAPTATION_TO_MOVING_PHOTO_XML, errCode);
121     if (!prefs) {
122         MEDIA_ERR_LOG("get preferences error: %{public}d", errCode);
123         return;
124     }
125     string unadaptedAppsStr = prefs->GetString(MOVING_PHOTO_KEY_UNADAPTED_PACKAGE);
126     string adaptedAppsStr = prefs->GetString(MOVING_PHOTO_KEY_ADAPTED_PACKAGE);
127     unordered_set<string> allUnadaptedApps = DfxUtils::SplitString(unadaptedAppsStr, ';');
128     unordered_set<string> allAdaptedApps = DfxUtils::SplitString(adaptedAppsStr, ';');
129     for (const auto& app : newAdaptationInfo.unadaptedAppPackages) {
130         allUnadaptedApps.insert(app);
131     }
132     for (const auto& app : newAdaptationInfo.adaptedAppPackages) {
133         allAdaptedApps.insert(app);
134     }
135 
136     prefs->PutInt(MOVING_PHOTO_KEY_UNADAPTED_NUM, allUnadaptedApps.size());
137     prefs->PutString(MOVING_PHOTO_KEY_UNADAPTED_PACKAGE, DfxUtils::JoinStrings(allUnadaptedApps, ';'));
138     prefs->PutInt(MOVING_PHOTO_KEY_ADAPTED_NUM, allAdaptedApps.size());
139     prefs->PutString(MOVING_PHOTO_KEY_ADAPTED_PACKAGE, DfxUtils::JoinStrings(allAdaptedApps, ';'));
140 
141     prefs->FlushSync();
142     MEDIA_INFO_LOG("flush adaptation to moving photo, unadapted num: %{private}zu, adapted num: %{private}zu",
143         allUnadaptedApps.size(), allAdaptedApps.size());
144 }
145 } // namespace Media
146 } // namespace OHOS