13e5483f6Sopenharmony_ci/*
23e5483f6Sopenharmony_ci * Copyright (C) 2024 Huawei Device Co., Ltd.
33e5483f6Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
43e5483f6Sopenharmony_ci * you may not use this file except in compliance with the License.
53e5483f6Sopenharmony_ci * You may obtain a copy of the License at
63e5483f6Sopenharmony_ci *
73e5483f6Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
83e5483f6Sopenharmony_ci *
93e5483f6Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
103e5483f6Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
113e5483f6Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
123e5483f6Sopenharmony_ci * See the License for the specific language governing permissions and
133e5483f6Sopenharmony_ci * limitations under the License.
143e5483f6Sopenharmony_ci */
153e5483f6Sopenharmony_ci#define MLOG_TAG "DfxManager"
163e5483f6Sopenharmony_ci
173e5483f6Sopenharmony_ci#include "dfx_manager.h"
183e5483f6Sopenharmony_ci
193e5483f6Sopenharmony_ci#include "dfx_worker.h"
203e5483f6Sopenharmony_ci#include "ringtone_errno.h"
213e5483f6Sopenharmony_ci#include "ringtone_file_utils.h"
223e5483f6Sopenharmony_ci#include "ringtone_log.h"
233e5483f6Sopenharmony_ci#include "ringtone_rdbstore.h"
243e5483f6Sopenharmony_ci
253e5483f6Sopenharmony_cinamespace OHOS {
263e5483f6Sopenharmony_cinamespace Media {
273e5483f6Sopenharmony_ciusing namespace std;
283e5483f6Sopenharmony_ci
293e5483f6Sopenharmony_cishared_ptr<RingtoneUnistore> g_dfxUnistore = nullptr;
303e5483f6Sopenharmony_ci
313e5483f6Sopenharmony_cishared_ptr<DfxManager> DfxManager::dfxManagerInstance_{nullptr};
323e5483f6Sopenharmony_cimutex DfxManager::instanceLock_;
333e5483f6Sopenharmony_ci
343e5483f6Sopenharmony_cishared_ptr<DfxManager> DfxManager::GetInstance()
353e5483f6Sopenharmony_ci{
363e5483f6Sopenharmony_ci    if (dfxManagerInstance_ == nullptr) {
373e5483f6Sopenharmony_ci        lock_guard<mutex> lockGuard(instanceLock_);
383e5483f6Sopenharmony_ci        dfxManagerInstance_ = make_shared<DfxManager>();
393e5483f6Sopenharmony_ci    }
403e5483f6Sopenharmony_ci    return dfxManagerInstance_;
413e5483f6Sopenharmony_ci}
423e5483f6Sopenharmony_ci
433e5483f6Sopenharmony_ciDfxManager::DfxManager() : isInitSuccess_(false)
443e5483f6Sopenharmony_ci{
453e5483f6Sopenharmony_ci}
463e5483f6Sopenharmony_ci
473e5483f6Sopenharmony_ciDfxManager::~DfxManager()
483e5483f6Sopenharmony_ci{
493e5483f6Sopenharmony_ci}
503e5483f6Sopenharmony_ci
513e5483f6Sopenharmony_ciint32_t DfxManager::Init(const shared_ptr<OHOS::AbilityRuntime::Context> &context)
523e5483f6Sopenharmony_ci{
533e5483f6Sopenharmony_ci    RINGTONE_INFO_LOG("Init DfxManager");
543e5483f6Sopenharmony_ci    if (context == nullptr) {
553e5483f6Sopenharmony_ci        return E_DB_FAIL;
563e5483f6Sopenharmony_ci    }
573e5483f6Sopenharmony_ci    if (g_dfxUnistore == nullptr) {
583e5483f6Sopenharmony_ci        g_dfxUnistore = RingtoneRdbStore::GetInstance(context);
593e5483f6Sopenharmony_ci        if (g_dfxUnistore == nullptr) {
603e5483f6Sopenharmony_ci            RINGTONE_ERR_LOG("RingtoneDataManager is not initialized");
613e5483f6Sopenharmony_ci            return E_DB_FAIL;
623e5483f6Sopenharmony_ci        }
633e5483f6Sopenharmony_ci    }
643e5483f6Sopenharmony_ci    dfxReporter_ = make_shared<DfxReporter>();
653e5483f6Sopenharmony_ci    DfxWorker::GetInstance()->Init();
663e5483f6Sopenharmony_ci    context_ = context;
673e5483f6Sopenharmony_ci    isInitSuccess_ = true;
683e5483f6Sopenharmony_ci    return E_OK;
693e5483f6Sopenharmony_ci}
703e5483f6Sopenharmony_ci
713e5483f6Sopenharmony_ciint64_t DfxManager::RequestTonesCount(SourceType type)
723e5483f6Sopenharmony_ci{
733e5483f6Sopenharmony_ci    if (type > SOURCE_TYPE_CUSTOMISED || type < SOURCE_TYPE_PRESET) {
743e5483f6Sopenharmony_ci        RINGTONE_ERR_LOG("source type err, type=%{public}d", type);
753e5483f6Sopenharmony_ci        return 0;
763e5483f6Sopenharmony_ci    }
773e5483f6Sopenharmony_ci
783e5483f6Sopenharmony_ci    Uri uri("");
793e5483f6Sopenharmony_ci    RingtoneDataCommand cmd(uri, RINGTONE_TABLE, RingtoneOperationType::QUERY);
803e5483f6Sopenharmony_ci    cmd.GetAbsRdbPredicates()->EqualTo(RINGTONE_COLUMN_SOURCE_TYPE, type);
813e5483f6Sopenharmony_ci
823e5483f6Sopenharmony_ci    auto resultSet = g_dfxUnistore->Query(cmd, { RINGTONE_COLUMN_TONE_ID, RINGTONE_COLUMN_SOURCE_TYPE });
833e5483f6Sopenharmony_ci    if (resultSet == nullptr) {
843e5483f6Sopenharmony_ci        RINGTONE_ERR_LOG("Failed to obtain file asset from database");
853e5483f6Sopenharmony_ci        return 0;
863e5483f6Sopenharmony_ci    }
873e5483f6Sopenharmony_ci    int32_t rowCount = 0;
883e5483f6Sopenharmony_ci    int32_t ret = resultSet->GetRowCount(rowCount);
893e5483f6Sopenharmony_ci    if (ret != NativeRdb::E_OK) {
903e5483f6Sopenharmony_ci        RINGTONE_ERR_LOG("failed to get row count");
913e5483f6Sopenharmony_ci        rowCount = 0;
923e5483f6Sopenharmony_ci    }
933e5483f6Sopenharmony_ci    return rowCount;
943e5483f6Sopenharmony_ci}
953e5483f6Sopenharmony_ci
963e5483f6Sopenharmony_ciint64_t DfxManager::HandleReportXml()
973e5483f6Sopenharmony_ci{
983e5483f6Sopenharmony_ci    if (!isInitSuccess_) {
993e5483f6Sopenharmony_ci        RINGTONE_WARN_LOG("DfxManager not init");
1003e5483f6Sopenharmony_ci        return RingtoneFileUtils::UTCTimeSeconds();
1013e5483f6Sopenharmony_ci    }
1023e5483f6Sopenharmony_ci    dfxReporter_->ReportDfxMessage();
1033e5483f6Sopenharmony_ci    return RingtoneFileUtils::UTCTimeSeconds();
1043e5483f6Sopenharmony_ci}
1053e5483f6Sopenharmony_ci} // namespace Media
1063e5483f6Sopenharmony_ci} // namespace OHOS
107