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 "startup_schedule.h"
17
18 #include "service_control.h"
19
20 #include "alarm_helper.h"
21 #include "constant.h"
22 #include "firmware_preferences_utils.h"
23 #include "startup_constant.h"
24 #include "time_utils.h"
25 #include "update_log.h"
26
27 namespace OHOS {
28 namespace UpdateEngine {
StartupSchedule()29 StartupSchedule::StartupSchedule() : AlarmManager(AlarmType::STARTUP_TIME_LOOPER, AlarmHelper::GetStartupAlarmTypeStr())
30 {
31 ENGINE_LOGD("StartupSchedule constructor");
32 }
33
~StartupSchedule()34 StartupSchedule::~StartupSchedule()
35 {
36 ENGINE_LOGD("StartupSchedule deConstructor");
37 }
38
RegisterLooper(const ScheduleLooper & looper)39 void StartupSchedule::RegisterLooper(const ScheduleLooper &looper)
40 {
41 ENGINE_LOGI("RegisterLooper");
42 RegisterRepeatingAlarm(BusinessAlarmType::STARTUP, [=]() { looper(); });
43 }
44
UnregisterLooper()45 void StartupSchedule::UnregisterLooper()
46 {
47 ENGINE_LOGI("UnregisterLooper");
48 UnregisterRepeatingAlarm(BusinessAlarmType::STARTUP);
49 }
50
Schedule(const ScheduleTask & task)51 bool StartupSchedule::Schedule(const ScheduleTask &task)
52 {
53 ENGINE_LOGI("Schedule next SA start time is %{public}s",
54 TimeUtils::GetPrintTimeStr(TimeUtils::GetTimestamp() + task.minDelayTime).c_str());
55 uint64_t scheduleTime = task.minDelayTime * Startup::ONE_SECOND_MILLISECONDS;
56
57 // 由于SaMgr暂不支持记录启动原因,因此临时将启动原因写入SP文件中
58 DelayedSingleton<FirmwarePreferencesUtil>::GetInstance()->SaveInt(Constant::PROCESS_RESTART_REASON,
59 CAST_INT(task.startupReason));
60
61 int32_t ret = StartServiceByTimer(Startup::UPDATER_SA_NAME.c_str(), scheduleTime);
62 ENGINE_LOGI("StartServiceByTimer finish, ret is %{public}d", ret);
63 return ret == 0;
64 }
65 } // namespace UpdateEngine
66 } // namespace OHOS