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 "DfxReporter"
16 
17 #include "dfx_reporter.h"
18 
19 #include "dfx_const.h"
20 #include "dfx_manager.h"
21 #include "hisysevent.h"
22 #include "preferences_helper.h"
23 #include "ringtone_file_utils.h"
24 #include "ringtone_log.h"
25 
26 namespace OHOS {
27 namespace Media {
28 using namespace std;
29 static constexpr char RINGTONE_LIBRARY[] = "RINGTONE_LIBRARY";
30 
DfxReporter()31 DfxReporter::DfxReporter()
32 {
33 }
34 
~DfxReporter()35 DfxReporter::~DfxReporter()
36 {
37 }
38 
ReportDfxMessage()39 void DfxReporter::ReportDfxMessage()
40 {
41     int32_t errCode;
42     shared_ptr<NativePreferences::Preferences> prefs =
43         NativePreferences::PreferencesHelper::GetPreferences(DFX_COMMON_XML, errCode);
44     if (!prefs) {
45         RINGTONE_ERR_LOG("get preferences error: %{public}d", errCode);
46         return;
47     }
48     std::string date = RingtoneFileUtils::StrCreateTimeByMilliseconds(DATE_FORMAT,
49         RingtoneFileUtils::UTCTimeMilliSeconds());
50     int64_t presetNum = DfxManager::GetInstance()->RequestTonesCount(SourceType::SOURCE_TYPE_PRESET);
51     int64_t customNum = DfxManager::GetInstance()->RequestTonesCount(SourceType::SOURCE_TYPE_CUSTOMISED);
52     RINGTONE_INFO_LOG("presetNum:%{public}" PRId64 ", customNum:%{public}" PRId64, presetNum, customNum);
53 
54     int ret = HiSysEventWrite(
55         RINGTONE_LIBRARY,
56         "RINGTONELIB_DFX_MESSAGE",
57         HiviewDFX::HiSysEvent::EventType::STATISTIC,
58         "DATE", date,
59         "PRESET_NUM", presetNum,
60         "CUSTOM_NUM", customNum);
61     if (ret != 0) {
62         RINGTONE_ERR_LOG("ReportDfxMessage error:%{public}d", ret);
63     }
64     prefs->FlushSync();
65 }
66 } // namespace Media
67 } // namespace OHOS
68