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 - Failed to write data into parcel. Possible reasons: 1. Invalid parameters; 196 * <br> 2. Failed to apply for memory. 197 * @throws { BusinessError } 9700003 - System service operation failed. 198 * @throws { BusinessError } 9700004 - Check on workInfo failed. 199 * @throws { BusinessError } 9700005 - Calling startWork failed. 200 * @syscap SystemCapability.ResourceSchedule.WorkScheduler 201 * @StageModelOnly 202 * @since 9 203 */ 204 function startWork(work: WorkInfo): void; 205 206 /** 207 * Stop a work. 208 * 209 * @param { WorkInfo } work - The info of work. 210 * @param { boolean } needCancel - True if need to be canceled after being stopped, otherwise false. 211 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 212 * <br> 2. Incorrect parameters types; 3. Parameter verification failed. 213 * @throws { BusinessError } 9700001 - Memory operation failed. 214 * @throws { BusinessError } 9700002 - Failed to write data into parcel. Possible reasons: 1. Invalid parameters; 215 * <br> 2. Failed to apply for memory. 216 * @throws { BusinessError } 9700003 - System service operation failed. 217 * @throws { BusinessError } 9700004 - Check on workInfo failed. 218 * @syscap SystemCapability.ResourceSchedule.WorkScheduler 219 * @StageModelOnly 220 * @since 9 221 */ 222 function stopWork(work: WorkInfo, needCancel?: boolean): void; 223 224 /** 225 * Obtains the work info of the wordId. 226 * 227 * @param { number } workId - The id of work. 228 * @param { AsyncCallback<WorkInfo> } callback - The callback of the function. 229 * @throws { BusinessError } 401 - Parameter error. Possible causes: Parameter verification failed. 230 * @throws { BusinessError } 9700001 - Memory operation failed. 231 * @throws { BusinessError } 9700002 - Failed to write data into parcel. Possible reasons: 1. Invalid parameters; 232 * <br> 2. Failed to apply for memory. 233 * @throws { BusinessError } 9700003 - System service operation failed. 234 * @throws { BusinessError } 9700004 - Check on workInfo failed. 235 * @syscap SystemCapability.ResourceSchedule.WorkScheduler 236 * @StageModelOnly 237 * @since 9 238 */ 239 function getWorkStatus(workId: number, callback: AsyncCallback<WorkInfo>): void; 240 241 /** 242 * Obtains the work info of the wordId. 243 * 244 * @param { number } workId - The id of work. 245 * @returns { Promise<WorkInfo> } The promise returned by the function. 246 * @throws { BusinessError } 401 - Parameter error. Possible causes: Parameter verification failed. 247 * @throws { BusinessError } 9700001 - Memory operation failed. 248 * @throws { BusinessError } 9700002 - Failed to write data into parcel. Possible reasons: 1. Invalid parameters; 249 * <br> 2. Failed to apply for memory. 250 * @throws { BusinessError } 9700003 - System service operation failed. 251 * @throws { BusinessError } 9700004 - Check on workInfo failed. 252 * @syscap SystemCapability.ResourceSchedule.WorkScheduler 253 * @StageModelOnly 254 * @since 9 255 */ 256 function getWorkStatus(workId: number): Promise<WorkInfo>; 257 258 /** 259 * Get all works of the calling application. 260 * 261 * @param { AsyncCallback<void> } callback - The callback of the function. 262 * @returns { Array<WorkInfo> } the work info list. 263 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 264 * <br> 2. Incorrect parameters types. 265 * @throws { BusinessError } 9700001 - Memory operation failed. 266 * @throws { BusinessError } 9700002 - Failed to write data into parcel. Possible reasons: 1. Invalid parameters; 267 * <br> 2. Failed to apply for memory. 268 * @throws { BusinessError } 9700003 - System service operation failed. 269 * @syscap SystemCapability.ResourceSchedule.WorkScheduler 270 * @StageModelOnly 271 * @since 9 272 * @deprecated since 10 273 */ 274 function obtainAllWorks(callback: AsyncCallback<void>): Array<WorkInfo>; 275 276 /** 277 * Get all works of the calling application. 278 * 279 * @param { AsyncCallback<Array<WorkInfo>> } callback - The callback of the function. 280 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 281 * <br> 2. Incorrect parameters types. 282 * @throws { BusinessError } 9700001 - Memory operation failed. 283 * @throws { BusinessError } 9700002 - Failed to write data into parcel. Possible reasons: 1. Invalid parameters; 284 * <br> 2. Failed to apply for memory. 285 * @throws { BusinessError } 9700003 - System service operation failed. 286 * @syscap SystemCapability.ResourceSchedule.WorkScheduler 287 * @StageModelOnly 288 * @since 10 289 */ 290 function obtainAllWorks(callback: AsyncCallback<Array<WorkInfo>>): void; 291 292 /** 293 * Get all works of the calling application. 294 * 295 * @returns { Promise<Array<WorkInfo>> } The work info list. 296 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 297 * <br> 2. Incorrect parameters types. 298 * @throws { BusinessError } 9700001 - Memory operation failed. 299 * @throws { BusinessError } 9700002 - Failed to write data into parcel. Possible reasons: 1. Invalid parameters; 300 * <br> 2. Failed to apply for memory. 301 * @throws { BusinessError } 9700003 - System service operation failed. 302 * @syscap SystemCapability.ResourceSchedule.WorkScheduler 303 * @StageModelOnly 304 * @since 9 305 */ 306 function obtainAllWorks(): Promise<Array<WorkInfo>>; 307 308 /** 309 * Stop all and clear all works of the calling application. 310 * 311 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 312 * <br> 2. Incorrect parameters types. 313 * @throws { BusinessError } 9700001 - Memory operation failed. 314 * @throws { BusinessError } 9700002 - Failed to write data into parcel. Possible reasons: 1. Invalid parameters; 315 * <br> 2. Failed to apply for memory. 316 * @throws { BusinessError } 9700003 - System service operation failed. 317 * @syscap SystemCapability.ResourceSchedule.WorkScheduler 318 * @StageModelOnly 319 * @since 9 320 */ 321 function stopAndClearWorks(): void; 322 323 /** 324 * Check whether last work running is timeout. The interface is for repeating work. 325 * 326 * @param { number } workId - The id of work. 327 * @param { AsyncCallback<void> } callback - The callback of the function. 328 * @returns { boolean } true if last work running is timeout, otherwise false. 329 * @throws { BusinessError } 401 - Parameter error. Possible causes: Parameter verification failed. 330 * @throws { BusinessError } 9700001 - Memory operation failed. 331 * @throws { BusinessError } 9700002 - Failed to write data into parcel. Possible reasons: 1. Invalid parameters; 332 * <br> 2. Failed to apply for memory. 333 * @throws { BusinessError } 9700003 - System service operation failed. 334 * @throws { BusinessError } 9700004 - Check on workInfo failed. 335 * @syscap SystemCapability.ResourceSchedule.WorkScheduler 336 * @StageModelOnly 337 * @since 9 338 * @deprecated since 10 339 */ 340 function isLastWorkTimeOut(workId: number, callback: AsyncCallback<void>): boolean; 341 342 /** 343 * Check whether last work running is timeout. The interface is for repeating work. 344 * 345 * @param { number } workId - The id of work. 346 * @param { AsyncCallback<boolean> } callback - The callback of the function. 347 * @throws { BusinessError } 401 - Parameter error. Possible causes: Parameter verification failed. 348 * @throws { BusinessError } 9700001 - Memory operation failed. 349 * @throws { BusinessError } 9700002 - Failed to write data into parcel. Possible reasons: 1. Invalid parameters; 350 * <br> 2. Failed to apply for memory. 351 * @throws { BusinessError } 9700003 - System service operation failed. 352 * @throws { BusinessError } 9700004 - Check on workInfo failed. 353 * @syscap SystemCapability.ResourceSchedule.WorkScheduler 354 * @StageModelOnly 355 * @since 10 356 */ 357 function isLastWorkTimeOut(workId: number, callback: AsyncCallback<boolean>): void; 358 359 /** 360 * Check whether last work running is timeout. The interface is for repeating work. 361 * 362 * @param { number } workId - The id of work. 363 * @returns { Promise<boolean> } True if last work running is timeout, otherwise false. 364 * @throws { BusinessError } 401 - Parameter error. Possible causes: Parameter verification failed. 365 * @throws { BusinessError } 9700001 - Memory operation failed. 366 * @throws { BusinessError } 9700002 - Failed to write data into parcel. Possible reasons: 1. Invalid parameters; 367 * <br> 2. Failed to apply for memory. 368 * @throws { BusinessError } 9700003 - System service operation failed. 369 * @throws { BusinessError } 9700004 - Check on workInfo failed. 370 * @syscap SystemCapability.ResourceSchedule.WorkScheduler 371 * @StageModelOnly 372 * @since 9 373 */ 374 function isLastWorkTimeOut(workId: number): Promise<boolean>; 375 376 /** 377 * Describes network type. 378 * 379 * @enum { number } 380 * @syscap SystemCapability.ResourceSchedule.WorkScheduler 381 * @StageModelOnly 382 * @since 9 383 * @name NetworkType 384 */ 385 export enum NetworkType { 386 /** 387 * Describes any network connection. 388 * 389 * @syscap SystemCapability.ResourceSchedule.WorkScheduler 390 * @StageModelOnly 391 * @since 9 392 */ 393 NETWORK_TYPE_ANY = 0, 394 /** 395 * Describes a mobile network connection. 396 * 397 * @syscap SystemCapability.ResourceSchedule.WorkScheduler 398 * @StageModelOnly 399 * @since 9 400 */ 401 NETWORK_TYPE_MOBILE, 402 /** 403 * Describes a wifi network connection. 404 * 405 * @syscap SystemCapability.ResourceSchedule.WorkScheduler 406 * @StageModelOnly 407 * @since 9 408 */ 409 NETWORK_TYPE_WIFI, 410 /** 411 * Describes a bluetooth network connection. 412 * 413 * @syscap SystemCapability.ResourceSchedule.WorkScheduler 414 * @StageModelOnly 415 * @since 9 416 */ 417 NETWORK_TYPE_BLUETOOTH, 418 /** 419 * Describes a wifi p2p network connection. 420 * 421 * @syscap SystemCapability.ResourceSchedule.WorkScheduler 422 * @StageModelOnly 423 * @since 9 424 */ 425 NETWORK_TYPE_WIFI_P2P, 426 /** 427 * Describes a wifi wire network connection. 428 * 429 * @syscap SystemCapability.ResourceSchedule.WorkScheduler 430 * @StageModelOnly 431 * @since 9 432 */ 433 NETWORK_TYPE_ETHERNET 434 } 435 436 /** 437 * Describes charging type. 438 * 439 * @enum { number } 440 * @syscap SystemCapability.ResourceSchedule.WorkScheduler 441 * @StageModelOnly 442 * @since 9 443 * @name ChargingType 444 */ 445 export enum ChargingType { 446 /** 447 * Describes any charger is connected. 448 * 449 * @syscap SystemCapability.ResourceSchedule.WorkScheduler 450 * @StageModelOnly 451 * @since 9 452 */ 453 CHARGING_PLUGGED_ANY = 0, 454 /** 455 * Describes ac charger is connected. 456 * 457 * @syscap SystemCapability.ResourceSchedule.WorkScheduler 458 * @StageModelOnly 459 * @since 9 460 */ 461 CHARGING_PLUGGED_AC, 462 /** 463 * Describes usb charger is connected. 464 * 465 * @syscap SystemCapability.ResourceSchedule.WorkScheduler 466 * @StageModelOnly 467 * @since 9 468 */ 469 CHARGING_PLUGGED_USB, 470 /** 471 * Describes wireless charger is connected. 472 * 473 * @syscap SystemCapability.ResourceSchedule.WorkScheduler 474 * @StageModelOnly 475 * @since 9 476 */ 477 CHARGING_PLUGGED_WIRELESS 478 } 479 480 /** 481 * Describes the battery status. 482 * 483 * @enum { number } 484 * @syscap SystemCapability.ResourceSchedule.WorkScheduler 485 * @StageModelOnly 486 * @since 9 487 * @name BatteryStatus 488 */ 489 export enum BatteryStatus { 490 /** 491 * Describes battery status is to low. 492 * 493 * @syscap SystemCapability.ResourceSchedule.WorkScheduler 494 * @StageModelOnly 495 * @since 9 496 */ 497 BATTERY_STATUS_LOW = 0, 498 /** 499 * Describes battery status is to ok. 500 * 501 * @syscap SystemCapability.ResourceSchedule.WorkScheduler 502 * @StageModelOnly 503 * @since 9 504 */ 505 BATTERY_STATUS_OKAY, 506 /** 507 * Describes battery status is to low or ok. 508 * 509 * @syscap SystemCapability.ResourceSchedule.WorkScheduler 510 * @StageModelOnly 511 * @since 9 512 */ 513 BATTERY_STATUS_LOW_OR_OKAY 514 } 515 516 /** 517 * Describes the storage request. 518 * 519 * @enum { number } 520 * @syscap SystemCapability.ResourceSchedule.WorkScheduler 521 * @StageModelOnly 522 * @since 9 523 * @name StorageRequest 524 */ 525 export enum StorageRequest { 526 /** 527 * Describes storage is to low. 528 * 529 * @syscap SystemCapability.ResourceSchedule.WorkScheduler 530 * @StageModelOnly 531 * @since 9 532 */ 533 STORAGE_LEVEL_LOW = 0, 534 /** 535 * Describes storage is to ok. 536 * 537 * @syscap SystemCapability.ResourceSchedule.WorkScheduler 538 * @StageModelOnly 539 * @since 9 540 */ 541 STORAGE_LEVEL_OKAY, 542 /** 543 * Describes storage is to low or ok. 544 * 545 * @syscap SystemCapability.ResourceSchedule.WorkScheduler 546 * @StageModelOnly 547 * @since 9 548 */ 549 STORAGE_LEVEL_LOW_OR_OKAY 550 } 551} 552export default workScheduler;