• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2022 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 BackgroundTasksKit
19 */
20
21import { AsyncCallback } from './@ohos.base';
22
23/**
24 * Work scheduler interface.
25 *
26 * @namespace workScheduler
27 * @syscap SystemCapability.ResourceSchedule.WorkScheduler
28 * @StageModelOnly
29 * @since 9
30 */
31declare namespace workScheduler {
32  /**
33   * The info of work.
34   *
35   * @interface WorkInfo
36   * @syscap SystemCapability.ResourceSchedule.WorkScheduler
37   * @StageModelOnly
38   * @since 9
39   */
40  export interface WorkInfo {
41    /**
42     * The id of the current work.
43     *
44     * @type { number }
45     * @syscap SystemCapability.ResourceSchedule.WorkScheduler
46     * @StageModelOnly
47     * @since 9
48     */
49    workId: number;
50    /**
51     * The bundle name of the current work.
52     *
53     * @type { string }
54     * @syscap SystemCapability.ResourceSchedule.WorkScheduler
55     * @StageModelOnly
56     * @since 9
57     */
58    bundleName: string;
59    /**
60     * The ability name of the current work.
61     *
62     * @type { string }
63     * @syscap SystemCapability.ResourceSchedule.WorkScheduler
64     * @StageModelOnly
65     * @since 9
66     */
67    abilityName: string;
68    /**
69     * Whether the current work will be saved.
70     *
71     * @type { ?boolean }
72     * @syscap SystemCapability.ResourceSchedule.WorkScheduler
73     * @StageModelOnly
74     * @since 9
75     */
76    isPersisted?: boolean;
77    /**
78     * The network type of the current work.
79     *
80     * @type { ?NetworkType }
81     * @syscap SystemCapability.ResourceSchedule.WorkScheduler
82     * @StageModelOnly
83     * @since 9
84     */
85    networkType?: NetworkType;
86    /**
87     * Whether a charging state has been set for triggering the work.
88     *
89     * @type { ?boolean }
90     * @syscap SystemCapability.ResourceSchedule.WorkScheduler
91     * @StageModelOnly
92     * @since 9
93     */
94    isCharging?: boolean;
95    /**
96     * The charger type based on which the work is triggered.
97     *
98     * @type { ?ChargingType }
99     * @syscap SystemCapability.ResourceSchedule.WorkScheduler
100     * @StageModelOnly
101     * @since 9
102     */
103    chargerType?: ChargingType;
104    /**
105     * The battery level for triggering a work.
106     *
107     * @type { ?number }
108     * @syscap SystemCapability.ResourceSchedule.WorkScheduler
109     * @StageModelOnly
110     * @since 9
111     */
112    batteryLevel?: number;
113    /**
114     * The battery status for triggering a work.
115     *
116     * @type { ?BatteryStatus }
117     * @syscap SystemCapability.ResourceSchedule.WorkScheduler
118     * @StageModelOnly
119     * @since 9
120     */
121    batteryStatus?: BatteryStatus;
122    /**
123     * Whether a storage state has been set for triggering the work.
124     *
125     * @type { ?StorageRequest }
126     * @syscap SystemCapability.ResourceSchedule.WorkScheduler
127     * @StageModelOnly
128     * @since 9
129     */
130    storageRequest?: StorageRequest;
131    /**
132     * The interval at which the work is repeated.
133     *
134     * @type { ?number }
135     * @syscap SystemCapability.ResourceSchedule.WorkScheduler
136     * @StageModelOnly
137     * @since 9
138     */
139    repeatCycleTime?: number;
140    /**
141     * Whether the work has been set to repeat at the specified interval.
142     *
143     * @type { ?boolean }
144     * @syscap SystemCapability.ResourceSchedule.WorkScheduler
145     * @StageModelOnly
146     * @since 9
147     */
148    isRepeat?: boolean;
149    /**
150     * The repeat of the current work.
151     *
152     * @type { ?number }
153     * @syscap SystemCapability.ResourceSchedule.WorkScheduler
154     * @StageModelOnly
155     * @since 9
156     */
157    repeatCount?: number;
158    /**
159     * Whether the device deep idle state has been set for triggering the work.
160     *
161     * @type { ?boolean }
162     * @syscap SystemCapability.ResourceSchedule.WorkScheduler
163     * @StageModelOnly
164     * @since 9
165     */
166    isDeepIdle?: boolean;
167    /**
168     * The idle wait time based on which the work is triggered.
169     *
170     * @type { ?number }
171     * @syscap SystemCapability.ResourceSchedule.WorkScheduler
172     * @StageModelOnly
173     * @since 9
174     */
175    idleWaitTime?: number;
176    /**
177     * The parameters of the work. The value is only supported basic type(Number, String, Boolean).
178     *
179     * @type { ?Record<string, number | string | boolean> }
180     * @syscap SystemCapability.ResourceSchedule.WorkScheduler
181     * @StageModelOnly
182     * @since 9
183     */
184    parameters?: Record<string, number | string | boolean>;
185  }
186
187  /**
188   * Add a work to the queue. A work can be executed only when it meets the preset triggering condition
189   * <p> and complies with the rules of work scheduler manager. </p>
190   *
191   * @param { WorkInfo } work - The info of work.
192   * @throws { BusinessError } 401 - Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;
193   * <br> 2. Incorrect parameters types; 3. Parameter verification failed.
194   * @throws { BusinessError } 9700001 - Memory operation failed.
195   * @throws { BusinessError } 9700002 - Parcel operation failed.
196   * @throws { BusinessError } 9700003 - System service operation failed.
197   * @throws { BusinessError } 9700004 - Check on workInfo failed.
198   * @throws { BusinessError } 9700005 - Calling startWork failed.
199   * @syscap SystemCapability.ResourceSchedule.WorkScheduler
200   * @StageModelOnly
201   * @since 9
202   */
203  function startWork(work: WorkInfo): void;
204
205  /**
206   * Stop a work.
207   *
208   * @param { WorkInfo } work - The info of work.
209   * @param { boolean } needCancel - True if need to be canceled after being stopped, otherwise false.
210   * @throws { BusinessError } 401 - Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;
211   * <br> 2. Incorrect parameters types; 3. Parameter verification failed.
212   * @throws { BusinessError } 9700001 - Memory operation failed.
213   * @throws { BusinessError } 9700002 - Parcel operation failed.
214   * @throws { BusinessError } 9700003 - System service operation failed.
215   * @throws { BusinessError } 9700004 - Check on workInfo failed.
216   * @syscap SystemCapability.ResourceSchedule.WorkScheduler
217   * @StageModelOnly
218   * @since 9
219   */
220  function stopWork(work: WorkInfo, needCancel?: boolean): void;
221
222  /**
223   * Obtains the work info of the wordId.
224   *
225   * @param { number } workId - The id of work.
226   * @param { AsyncCallback<WorkInfo> } callback - The callback of the function.
227   * @throws { BusinessError } 401 - Parameter error. Possible causes: Parameter verification failed.
228   * @throws { BusinessError } 9700001 - Memory operation failed.
229   * @throws { BusinessError } 9700002 - Parcel operation failed.
230   * @throws { BusinessError } 9700003 - System service operation failed.
231   * @throws { BusinessError } 9700004 - Check on workInfo failed.
232   * @syscap SystemCapability.ResourceSchedule.WorkScheduler
233   * @StageModelOnly
234   * @since 9
235   */
236  function getWorkStatus(workId: number, callback: AsyncCallback<WorkInfo>): void;
237
238  /**
239   * Obtains the work info of the wordId.
240   *
241   * @param { number } workId - The id of work.
242   * @returns { Promise<WorkInfo> } The promise returned by the function.
243   * @throws { BusinessError } 401 - Parameter error. Possible causes: Parameter verification failed.
244   * @throws { BusinessError } 9700001 - Memory operation failed.
245   * @throws { BusinessError } 9700002 - Parcel operation failed.
246   * @throws { BusinessError } 9700003 - System service operation failed.
247   * @throws { BusinessError } 9700004 - Check on workInfo failed.
248   * @syscap SystemCapability.ResourceSchedule.WorkScheduler
249   * @StageModelOnly
250   * @since 9
251   */
252  function getWorkStatus(workId: number): Promise<WorkInfo>;
253
254  /**
255   * Get all works of the calling application.
256   *
257   * @param { AsyncCallback<void> } callback - The callback of the function.
258   * @returns { Array<WorkInfo> } the work info list.
259   * @throws { BusinessError } 401 - Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;
260   * <br> 2. Incorrect parameters types.
261   * @throws { BusinessError } 9700001 - Memory operation failed.
262   * @throws { BusinessError } 9700002 - Parcel operation failed.
263   * @throws { BusinessError } 9700003 - System service operation failed.
264   * @syscap SystemCapability.ResourceSchedule.WorkScheduler
265   * @StageModelOnly
266   * @since 9
267   * @deprecated since 10
268   */
269  function obtainAllWorks(callback: AsyncCallback<void>): Array<WorkInfo>;
270
271  /**
272   * Get all works of the calling application.
273   *
274   * @param { AsyncCallback<Array<WorkInfo>> } callback - The callback of the function.
275   * @throws { BusinessError } 401 - Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;
276   * <br> 2. Incorrect parameters types.
277   * @throws { BusinessError } 9700001 - Memory operation failed.
278   * @throws { BusinessError } 9700002 - Parcel operation failed.
279   * @throws { BusinessError } 9700003 - System service operation failed.
280   * @syscap SystemCapability.ResourceSchedule.WorkScheduler
281   * @StageModelOnly
282   * @since 10
283   */
284  function obtainAllWorks(callback: AsyncCallback<Array<WorkInfo>>): void;
285
286  /**
287   * Get all works of the calling application.
288   *
289   * @returns { Promise<Array<WorkInfo>> } The work info list.
290   * @throws { BusinessError } 401 - Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;
291   * <br> 2. Incorrect parameters types.
292   * @throws { BusinessError } 9700001 - Memory operation failed.
293   * @throws { BusinessError } 9700002 - Parcel operation failed.
294   * @throws { BusinessError } 9700003 - System service operation failed.
295   * @syscap SystemCapability.ResourceSchedule.WorkScheduler
296   * @StageModelOnly
297   * @since 9
298   */
299  function obtainAllWorks(): Promise<Array<WorkInfo>>;
300
301  /**
302   * Stop all and clear all works of the calling application.
303   *
304   * @throws { BusinessError } 401 - Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;
305   * <br> 2. Incorrect parameters types.
306   * @throws { BusinessError } 9700001 - Memory operation failed.
307   * @throws { BusinessError } 9700002 - Parcel operation failed.
308   * @throws { BusinessError } 9700003 - System service operation failed.
309   * @syscap SystemCapability.ResourceSchedule.WorkScheduler
310   * @StageModelOnly
311   * @since 9
312   */
313  function stopAndClearWorks(): void;
314
315  /**
316   * Check whether last work running is timeout. The interface is for repeating work.
317   *
318   * @param { number } workId - The id of work.
319   * @param { AsyncCallback<void> } callback - The callback of the function.
320   * @returns { boolean } true if last work running is timeout, otherwise false.
321   * @throws { BusinessError } 401 - Parameter error. Possible causes: Parameter verification failed.
322   * @throws { BusinessError } 9700001 - Memory operation failed.
323   * @throws { BusinessError } 9700002 - Parcel operation failed.
324   * @throws { BusinessError } 9700003 - System service operation failed.
325   * @throws { BusinessError } 9700004 - Check on workInfo failed.
326   * @syscap SystemCapability.ResourceSchedule.WorkScheduler
327   * @StageModelOnly
328   * @since 9
329   * @deprecated since 10
330   */
331  function isLastWorkTimeOut(workId: number, callback: AsyncCallback<void>): boolean;
332
333  /**
334   * Check whether last work running is timeout. The interface is for repeating work.
335   *
336   * @param { number } workId - The id of work.
337   * @param { AsyncCallback<boolean> } callback - The callback of the function.
338   * @throws { BusinessError } 401 - Parameter error. Possible causes: Parameter verification failed.
339   * @throws { BusinessError } 9700001 - Memory operation failed.
340   * @throws { BusinessError } 9700002 - Parcel operation failed.
341   * @throws { BusinessError } 9700003 - System service operation failed.
342   * @throws { BusinessError } 9700004 - Check on workInfo failed.
343   * @syscap SystemCapability.ResourceSchedule.WorkScheduler
344   * @StageModelOnly
345   * @since 10
346   */
347  function isLastWorkTimeOut(workId: number, callback: AsyncCallback<boolean>): void;
348
349  /**
350   * Check whether last work running is timeout. The interface is for repeating work.
351   *
352   * @param { number } workId - The id of work.
353   * @returns { Promise<boolean> } True if last work running is timeout, otherwise false.
354   * @throws { BusinessError } 401 - Parameter error. Possible causes: Parameter verification failed.
355   * @throws { BusinessError } 9700001 - Memory operation failed.
356   * @throws { BusinessError } 9700002 - Parcel operation failed.
357   * @throws { BusinessError } 9700003 - System service operation failed.
358   * @throws { BusinessError } 9700004 - Check on workInfo failed.
359   * @syscap SystemCapability.ResourceSchedule.WorkScheduler
360   * @StageModelOnly
361   * @since 9
362   */
363  function isLastWorkTimeOut(workId: number): Promise<boolean>;
364
365  /**
366   * Describes network type.
367   *
368   * @enum { number }
369   * @syscap SystemCapability.ResourceSchedule.WorkScheduler
370   * @StageModelOnly
371   * @since 9
372   * @name NetworkType
373  */
374  export enum NetworkType {
375    /**
376     * Describes any network connection.
377     *
378     * @syscap SystemCapability.ResourceSchedule.WorkScheduler
379     * @StageModelOnly
380     * @since 9
381     */
382    NETWORK_TYPE_ANY = 0,
383    /**
384     * Describes a mobile network connection.
385     *
386     * @syscap SystemCapability.ResourceSchedule.WorkScheduler
387     * @StageModelOnly
388     * @since 9
389     */
390    NETWORK_TYPE_MOBILE,
391    /**
392     * Describes a wifi network connection.
393     *
394     * @syscap SystemCapability.ResourceSchedule.WorkScheduler
395     * @StageModelOnly
396     * @since 9
397     */
398    NETWORK_TYPE_WIFI,
399    /**
400     * Describes a bluetooth network connection.
401     *
402     * @syscap SystemCapability.ResourceSchedule.WorkScheduler
403     * @StageModelOnly
404     * @since 9
405     */
406    NETWORK_TYPE_BLUETOOTH,
407    /**
408     * Describes a wifi p2p network connection.
409     *
410     * @syscap SystemCapability.ResourceSchedule.WorkScheduler
411     * @StageModelOnly
412     * @since 9
413     */
414    NETWORK_TYPE_WIFI_P2P,
415    /**
416     * Describes a wifi wire network connection.
417     *
418     * @syscap SystemCapability.ResourceSchedule.WorkScheduler
419     * @StageModelOnly
420     * @since 9
421     */
422    NETWORK_TYPE_ETHERNET
423  }
424
425  /**
426   * Describes charging type.
427   *
428   * @enum { number }
429   * @syscap SystemCapability.ResourceSchedule.WorkScheduler
430   * @StageModelOnly
431   * @since 9
432   * @name ChargingType
433  */
434  export enum ChargingType {
435    /**
436     * Describes any charger is connected.
437     *
438     * @syscap SystemCapability.ResourceSchedule.WorkScheduler
439     * @StageModelOnly
440     * @since 9
441     */
442    CHARGING_PLUGGED_ANY = 0,
443    /**
444     * Describes ac charger is connected.
445     *
446     * @syscap SystemCapability.ResourceSchedule.WorkScheduler
447     * @StageModelOnly
448     * @since 9
449     */
450    CHARGING_PLUGGED_AC,
451    /**
452     * Describes usb charger is connected.
453     *
454     * @syscap SystemCapability.ResourceSchedule.WorkScheduler
455     * @StageModelOnly
456     * @since 9
457     */
458    CHARGING_PLUGGED_USB,
459    /**
460     * Describes wireless charger is connected.
461     *
462     * @syscap SystemCapability.ResourceSchedule.WorkScheduler
463     * @StageModelOnly
464     * @since 9
465     */
466    CHARGING_PLUGGED_WIRELESS
467  }
468
469  /**
470   * Describes the battery status.
471   *
472   * @enum { number }
473   * @syscap SystemCapability.ResourceSchedule.WorkScheduler
474   * @StageModelOnly
475   * @since 9
476   * @name BatteryStatus
477  */
478  export enum BatteryStatus {
479    /**
480     * Describes battery status is to low.
481     *
482     * @syscap SystemCapability.ResourceSchedule.WorkScheduler
483     * @StageModelOnly
484     * @since 9
485     */
486    BATTERY_STATUS_LOW = 0,
487    /**
488     * Describes battery status is to ok.
489     *
490     * @syscap SystemCapability.ResourceSchedule.WorkScheduler
491     * @StageModelOnly
492     * @since 9
493     */
494    BATTERY_STATUS_OKAY,
495    /**
496     * Describes battery status is to low or ok.
497     *
498     * @syscap SystemCapability.ResourceSchedule.WorkScheduler
499     * @StageModelOnly
500     * @since 9
501     */
502    BATTERY_STATUS_LOW_OR_OKAY
503  }
504
505  /**
506   * Describes the storage request.
507   *
508   * @enum { number }
509   * @syscap SystemCapability.ResourceSchedule.WorkScheduler
510   * @StageModelOnly
511   * @since 9
512   * @name StorageRequest
513  */
514  export enum StorageRequest {
515    /**
516     * Describes storage is to low.
517     *
518     * @syscap SystemCapability.ResourceSchedule.WorkScheduler
519     * @StageModelOnly
520     * @since 9
521     */
522    STORAGE_LEVEL_LOW = 0,
523    /**
524     * Describes storage is to ok.
525     *
526     * @syscap SystemCapability.ResourceSchedule.WorkScheduler
527     * @StageModelOnly
528     * @since 9
529     */
530    STORAGE_LEVEL_OKAY,
531    /**
532     * Describes storage is to low or ok.
533     *
534     * @syscap SystemCapability.ResourceSchedule.WorkScheduler
535     * @StageModelOnly
536     * @since 9
537     */
538    STORAGE_LEVEL_LOW_OR_OKAY
539  }
540}
541export default workScheduler;