1/* 2 * Copyright (c) 2021 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 16import { AsyncCallback } from './@ohos.base'; 17import { WantAgent } from './@ohos.app.ability.wantAgent'; 18 19/** 20 * Provides js api for systemTimer 21 * 22 * @namespace systemTimer 23 * @syscap SystemCapability.MiscServices.Time 24 * @systemapi Hide this for inner system use. 25 * @since 7 26 */ 27declare namespace systemTimer { 28 /** 29 * Indicates the timing policy the timer use, which can be REALTIME or UTC. 30 * 31 * @constant 32 * @syscap SystemCapability.MiscServices.Time 33 * @systemapi Hide this for inner system use. 34 * @since 7 35 */ 36 const TIMER_TYPE_REALTIME: number; 37 38 /** 39 * Describes whether a timer will wake the device up. 40 * 41 * @constant 42 * @syscap SystemCapability.MiscServices.Time 43 * @systemapi Hide this for inner system use. 44 * @since 7 45 */ 46 const TIMER_TYPE_WAKEUP: number; 47 48 /** 49 * Describes whether a timer will be delivered precisely at a scheduled time. 50 * 51 * @constant 52 * @syscap SystemCapability.MiscServices.Time 53 * @systemapi Hide this for inner system use. 54 * @since 7 55 */ 56 const TIMER_TYPE_EXACT: number; 57 58 /** 59 * Indicates whether the timer waking up the system is supported in low-power mode. 60 * 61 * @constant 62 * @syscap SystemCapability.MiscServices.Time 63 * @systemapi Hide this for inner system use. 64 * @since 7 65 */ 66 const TIMER_TYPE_IDLE: number; 67 68 /** 69 * Creates a timer. 70 * 71 * @param { TimerOptions } options - The timer options. 72 * @param { AsyncCallback<number> } callback - {number} is the timer ID. 73 * @throws { BusinessError } 202 - permission denied. 74 * @throws { BusinessError } 401 - if the parameter type is incorrect. 75 * @syscap SystemCapability.MiscServices.Time 76 * @systemapi Hide this for inner system use. 77 * @since 7 78 */ 79 function createTimer(options: TimerOptions, callback: AsyncCallback<number>): void; 80 81 /** 82 * Creates a timer. 83 * 84 * @param { TimerOptions } options - The timer options. 85 * @returns { Promise<number> } the timer ID. 86 * @throws { BusinessError } 202 - permission denied. 87 * @throws { BusinessError } 401 - if the parameter type is incorrect. 88 * @syscap SystemCapability.MiscServices.Time 89 * @systemapi Hide this for inner system use. 90 * @since 7 91 */ 92 function createTimer(options: TimerOptions): Promise<number>; 93 94 /** 95 * Starts a timer. 96 * 97 * @param { number } timer - The timer ID. 98 * @param { number } triggerTime - Indicates the time at which the timer is triggered for the first time, in milliseconds. 99 * The time will be automatically set to 5000 milliseconds after the current time if the passed 100 * value is smaller than the current time plus 5000 milliseconds. 101 * @param { AsyncCallback<void> } callback - The callback function. 102 * @throws { BusinessError } 202 - permission denied. 103 * @throws { BusinessError } 401 - if the parameter type is incorrect. 104 * @syscap SystemCapability.MiscServices.Time 105 * @systemapi Hide this for inner system use. 106 * @since 7 107 */ 108 function startTimer(timer: number, triggerTime: number, callback: AsyncCallback<void>): void; 109 110 /** 111 * Starts a timer. 112 * 113 * @param { number } timer - The timer ID. 114 * @param { number } triggerTime - Indicates the time at which the timer is triggered for the first time, in milliseconds. 115 * The time will be automatically set to 5000 milliseconds after the current time if the passed 116 * value is smaller than the current time plus 5000 milliseconds. 117 * @returns { Promise<void> } return a promise object. 118 * @throws { BusinessError } 202 - permission denied. 119 * @throws { BusinessError } 401 - if the parameter type is incorrect. 120 * @syscap SystemCapability.MiscServices.Time 121 * @systemapi Hide this for inner system use. 122 * @since 7 123 */ 124 function startTimer(timer: number, triggerTime: number): Promise<void>; 125 126 /** 127 * Stops a timer. 128 * 129 * @param { number } timer - The timer ID. 130 * @param { AsyncCallback<void> } callback - The callback function. 131 * @throws { BusinessError } 202 - permission denied. 132 * @throws { BusinessError } 401 - if the parameter type is incorrect. 133 * @syscap SystemCapability.MiscServices.Time 134 * @systemapi Hide this for inner system use. 135 * @since 7 136 */ 137 function stopTimer(timer: number, callback: AsyncCallback<void>): void; 138 139 /** 140 * Stops a timer. 141 * 142 * @param { number } timer - The timer ID. 143 * @returns { Promise<void> } return a promise object. 144 * @throws { BusinessError } 202 - permission denied. 145 * @throws { BusinessError } 401 - if the parameter type is incorrect. 146 * @syscap SystemCapability.MiscServices.Time 147 * @systemapi Hide this for inner system use. 148 * @since 7 149 */ 150 function stopTimer(timer: number): Promise<void>; 151 152 /** 153 * Destroy a timer. 154 * 155 * @param { number } timer - The timer ID. 156 * @param { AsyncCallback<void> } callback - The callback function. 157 * @throws { BusinessError } 202 - permission denied. 158 * @throws { BusinessError } 401 - if the parameter type is incorrect. 159 * @syscap SystemCapability.MiscServices.Time 160 * @systemapi Hide this for inner system use. 161 * @since 7 162 */ 163 function destroyTimer(timer: number, callback: AsyncCallback<void>): void; 164 165 /** 166 * Destroy a timer. 167 * 168 * @param { number } timer - The timer ID. 169 * @returns { Promise<void> } return a promise object. 170 * @throws { BusinessError } 202 - permission denied. 171 * @throws { BusinessError } 401 - if the parameter type is incorrect. 172 * @syscap SystemCapability.MiscServices.Time 173 * @systemapi Hide this for inner system use. 174 * @since 7 175 */ 176 function destroyTimer(timer: number): Promise<void>; 177 178 /** 179 * When the repeat is false,the interval is not needed, choose one of wantAgent and callback. 180 * When the repeat is true,the interval is required, the wantAgent is required, and the callback can be left blank. 181 * 182 * @interface TimerOptions 183 * @syscap SystemCapability.MiscServices.Time 184 * @systemapi Hide this for inner system use. 185 * @since 7 186 */ 187 interface TimerOptions { 188 /** 189 * The timer type. 190 * 191 * @syscap SystemCapability.MiscServices.Time 192 * @systemapi Hide this for inner system use. 193 * @since 7 194 */ 195 type: number; 196 197 /** 198 * Indicates a repeating timer 199 * 200 * @syscap SystemCapability.MiscServices.Time 201 * @systemapi Hide this for inner system use. 202 * @since 7 203 */ 204 repeat: boolean; 205 206 /** 207 * Indicates the interval between two consecutive triggers, in milliseconds. 208 * The interval will be set to 5000 milliseconds automatically if the passed value is smaller than 5000. 209 * 210 * @syscap SystemCapability.MiscServices.Time 211 * @systemapi Hide this for inner system use. 212 * @since 7 213 */ 214 interval?: number; 215 216 /** 217 * Indicates the intent to send when the timer goes off. 218 * 219 * @syscap SystemCapability.MiscServices.Time 220 * @systemapi Hide this for inner system use. 221 * @since 7 222 */ 223 wantAgent?: WantAgent; 224 225 /** 226 * Called back when the timer goes off. 227 * 228 * @syscap SystemCapability.MiscServices.Time 229 * @systemapi Hide this for inner system use. 230 * @since 7 231 */ 232 callback?: () => void; 233 } 234} 235 236export default systemTimer;