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 "DfxWorker"
16 
17 #include "dfx_worker.h"
18 
19 #include "dfx_const.h"
20 #include "dfx_manager.h"
21 #include "parameters.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 shared_ptr<DfxWorker> DfxWorker::dfxWorkerInstance_{nullptr};
30 
GetInstance()31 shared_ptr<DfxWorker> DfxWorker::GetInstance()
32 {
33     if (dfxWorkerInstance_ == nullptr) {
34         dfxWorkerInstance_ = make_shared<DfxWorker>();
35     }
36     return dfxWorkerInstance_;
37 }
38 
DfxWorker()39 DfxWorker::DfxWorker()
40 {
41     longTimeSec_ = stoi(system::GetParameter("persist.multimedia.ringtonelibrary.dfx.longtime", ONE_WEEK)) *
42         ONE_MINUTE * ONE_MINUTE;
43 }
44 
~DfxWorker()45 DfxWorker::~DfxWorker()
46 {
47 }
48 
Init()49 void DfxWorker::Init()
50 {
51     RINGTONE_INFO_LOG("init");
52     thread(bind(&DfxWorker::InitCycleThread, this)).detach();
53 }
54 
InitCycleThread()55 void DfxWorker::InitCycleThread()
56 {
57     RINGTONE_INFO_LOG("DFX start");
58     int32_t errCode;
59     shared_ptr<NativePreferences::Preferences> prefs =
60         NativePreferences::PreferencesHelper::GetPreferences(DFX_COMMON_XML, errCode);
61     if (!prefs) {
62         RINGTONE_ERR_LOG("get preferences error: %{public}d", errCode);
63         return;
64     }
65     lastReportTime_ = prefs->GetLong(LAST_REPORT_TIME, 0);
66     if (RingtoneFileUtils::UTCTimeSeconds() - lastReportTime_ > longTimeSec_) {
67         RINGTONE_INFO_LOG("Report Xml");
68         lastReportTime_ = DfxManager::GetInstance()->HandleReportXml();
69         prefs->PutLong(LAST_REPORT_TIME, lastReportTime_);
70         prefs->FlushSync();
71     }
72     RINGTONE_INFO_LOG("DFX end");
73 }
74 } // namespace Media
75 } // namespace OHOS
76