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 "DfxManager"
16
17 #include "dfx_manager.h"
18
19 #include "dfx_worker.h"
20 #include "ringtone_errno.h"
21 #include "ringtone_file_utils.h"
22 #include "ringtone_log.h"
23 #include "ringtone_rdbstore.h"
24
25 namespace OHOS {
26 namespace Media {
27 using namespace std;
28
29 shared_ptr<RingtoneUnistore> g_dfxUnistore = nullptr;
30
31 shared_ptr<DfxManager> DfxManager::dfxManagerInstance_{nullptr};
32 mutex DfxManager::instanceLock_;
33
GetInstance()34 shared_ptr<DfxManager> DfxManager::GetInstance()
35 {
36 if (dfxManagerInstance_ == nullptr) {
37 lock_guard<mutex> lockGuard(instanceLock_);
38 dfxManagerInstance_ = make_shared<DfxManager>();
39 }
40 return dfxManagerInstance_;
41 }
42
DfxManager()43 DfxManager::DfxManager() : isInitSuccess_(false)
44 {
45 }
46
~DfxManager()47 DfxManager::~DfxManager()
48 {
49 }
50
Init(const shared_ptr<OHOS::AbilityRuntime::Context> & context)51 int32_t DfxManager::Init(const shared_ptr<OHOS::AbilityRuntime::Context> &context)
52 {
53 RINGTONE_INFO_LOG("Init DfxManager");
54 if (context == nullptr) {
55 return E_DB_FAIL;
56 }
57 if (g_dfxUnistore == nullptr) {
58 g_dfxUnistore = RingtoneRdbStore::GetInstance(context);
59 if (g_dfxUnistore == nullptr) {
60 RINGTONE_ERR_LOG("RingtoneDataManager is not initialized");
61 return E_DB_FAIL;
62 }
63 }
64 dfxReporter_ = make_shared<DfxReporter>();
65 DfxWorker::GetInstance()->Init();
66 context_ = context;
67 isInitSuccess_ = true;
68 return E_OK;
69 }
70
RequestTonesCount(SourceType type)71 int64_t DfxManager::RequestTonesCount(SourceType type)
72 {
73 if (type > SOURCE_TYPE_CUSTOMISED || type < SOURCE_TYPE_PRESET) {
74 RINGTONE_ERR_LOG("source type err, type=%{public}d", type);
75 return 0;
76 }
77
78 Uri uri("");
79 RingtoneDataCommand cmd(uri, RINGTONE_TABLE, RingtoneOperationType::QUERY);
80 cmd.GetAbsRdbPredicates()->EqualTo(RINGTONE_COLUMN_SOURCE_TYPE, type);
81
82 auto resultSet = g_dfxUnistore->Query(cmd, { RINGTONE_COLUMN_TONE_ID, RINGTONE_COLUMN_SOURCE_TYPE });
83 if (resultSet == nullptr) {
84 RINGTONE_ERR_LOG("Failed to obtain file asset from database");
85 return 0;
86 }
87 int32_t rowCount = 0;
88 int32_t ret = resultSet->GetRowCount(rowCount);
89 if (ret != NativeRdb::E_OK) {
90 RINGTONE_ERR_LOG("failed to get row count");
91 rowCount = 0;
92 }
93 return rowCount;
94 }
95
HandleReportXml()96 int64_t DfxManager::HandleReportXml()
97 {
98 if (!isInitSuccess_) {
99 RINGTONE_WARN_LOG("DfxManager not init");
100 return RingtoneFileUtils::UTCTimeSeconds();
101 }
102 dfxReporter_->ReportDfxMessage();
103 return RingtoneFileUtils::UTCTimeSeconds();
104 }
105 } // namespace Media
106 } // namespace OHOS
107