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