• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 #include "alarm_manager.h"
17 
18 #include "update_define.h"
19 #include "update_log.h"
20 #include "time_utils.h"
21 
22 namespace OHOS {
23 namespace UpdateEngine {
AlarmManager(AlarmType alarmType,const std::string & logTag)24 AlarmManager::AlarmManager(AlarmType alarmType, const std::string &logTag)
25 {
26     ENGINE_LOGI("AlarmManager::AlarmManager alarmType %{public}d", CAST_INT(alarmType));
27     alarmType_ = alarmType;
28     logTag_ = logTag;
29     timeLooperInterval_ = AlarmHelper::GetLooperInterval(alarmType);
30     StartTimeLooper();
31 }
32 
~AlarmManager()33 AlarmManager::~AlarmManager()
34 {
35     ENGINE_LOGI("AlarmManager::~AlarmManager");
36     DelayedSingleton<TimerManager>::GetInstance()->UnregisterRepeatingAlarm(alarmType_);
37 }
38 
StartTimeLooper()39 void AlarmManager::StartTimeLooper()
40 {
41     DelayedSingleton<TimerManager>::GetInstance()->RegisterRepeatingAlarm(alarmType_, timeLooperInterval_, [=]() {
42         int64_t currentTime = TimeUtils::GetTimestamp();
43         ENGINE_LOGD("%{public}s timeLooper currentTime %{public}s",
44             logTag_.c_str(), TimeUtils::GetPrintTimeStr(currentTime).c_str());
45         for (auto &[alarmType, callback] : callbackMap_) {
46             callback();
47         }
48     });
49 }
50 
RegisterRepeatingAlarm(BusinessAlarmType alarmType,const AlarmCallback & callback)51 void AlarmManager::RegisterRepeatingAlarm(BusinessAlarmType alarmType, const AlarmCallback &callback)
52 {
53     ENGINE_LOGI("%{public}s RegisterRepeatingAlarm alarmType %{public}d", logTag_.c_str(), CAST_INT(alarmType));
54     callbackMap_[alarmType] = callback;
55 }
56 
UnregisterRepeatingAlarm(BusinessAlarmType alarmType)57 void AlarmManager::UnregisterRepeatingAlarm(BusinessAlarmType alarmType)
58 {
59     ENGINE_LOGI("%{public}s UnregisterRepeatingAlarm alarmType %{public}d", logTag_.c_str(), CAST_INT(alarmType));
60     callbackMap_.erase(alarmType);
61 }
62 } // namespace UpdateEngine
63 } // namespace OHOS
64