1 /*
2 * Copyright (C) 2025 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
16 #define MLOG_TAG "RingtoneXCollie"
17
18 #include "ringtone_xcollie.h"
19
20 #include <unistd.h>
21
22 #include "ringtone_errno.h"
23 #include "ringtone_log.h"
24 #include "xcollie/xcollie.h"
25 #include "xcollie/xcollie_define.h"
26
27 namespace OHOS {
28 namespace Media {
29
RingtoneXCollie(const std::string & tag,std::function<void (void *)> func,uint32_t timeoutSeconds,void * arg,uint32_t flag)30 RingtoneXCollie::RingtoneXCollie(const std::string &tag, std::function<void(void *)> func,
31 uint32_t timeoutSeconds, void *arg, uint32_t flag)
32 {
33 tag_ = tag;
34 id_ = HiviewDFX::XCollie::GetInstance().SetTimer(tag, timeoutSeconds, func, arg, flag);
35 if (id_ == HiviewDFX::INVALID_ID) {
36 RINGTONE_ERR_LOG("Set XCollie Timer failed");
37 return;
38 }
39 RINGTONE_INFO_LOG("Set XCollie OK");
40 }
41
~RingtoneXCollie()42 RingtoneXCollie::~RingtoneXCollie()
43 {
44 CancelXCollieTimer();
45 }
46
CancelXCollieTimer()47 void RingtoneXCollie::CancelXCollieTimer()
48 {
49 if (!isCanceled_ && id_ != HiviewDFX::INVALID_ID) {
50 HiviewDFX::XCollie::GetInstance().CancelTimer(id_);
51 isCanceled_ = true;
52 RINGTONE_INFO_LOG("Cancel XCollie OK");
53 }
54 }
55
56 } // namespace Media
57 } // namespace OHOS