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