1/* 2 * Copyright (c) 2021-2024 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 AbilityKit 19 */ 20 21/*** if arkts 1.1 */ 22import type { AsyncCallback } from '../@ohos.base'; 23import EventHub from './EventHub'; 24/*** endif */ 25import { ApplicationInfo } from '../bundleManager/ApplicationInfo'; 26import ApplicationContext from './ApplicationContext'; 27import BaseContext from './BaseContext'; 28import resmgr from '../@ohos.resourceManager'; 29import contextConstant from '../@ohos.app.ability.contextConstant'; 30 31/** 32 * The base context of an ability or an application. It allows access to 33 * application-specific resources. 34 * 35 * @extends BaseContext 36 * @syscap SystemCapability.Ability.AbilityRuntime.Core 37 * @stagemodelonly 38 * @since 9 39 */ 40/** 41 * The base context of an ability or an application. It allows access to 42 * application-specific resources. 43 * 44 * @extends BaseContext 45 * @syscap SystemCapability.Ability.AbilityRuntime.Core 46 * @stagemodelonly 47 * @crossplatform 48 * @since 10 49 */ 50/** 51 * The Context module, inherited frome {@link BaseContext}, provides context for abilities or applications, including 52 * access to application-specific resources. 53 * 54 * @extends BaseContext 55 * @syscap SystemCapability.Ability.AbilityRuntime.Core 56 * @stagemodelonly 57 * @crossplatform 58 * @atomicservice 59 * @since arkts {'1.1':'11', '1.2':'20'} 60 * @arkts 1.1&1.2 61 */ 62declare class Context extends BaseContext { 63 /** 64 * Indicates the capability of accessing application resources. 65 * 66 * @type { resmgr.ResourceManager } 67 * @syscap SystemCapability.Ability.AbilityRuntime.Core 68 * @stagemodelonly 69 * @since 9 70 */ 71 /** 72 * Indicates the capability of accessing application resources. 73 * 74 * @type { resmgr.ResourceManager } 75 * @syscap SystemCapability.Ability.AbilityRuntime.Core 76 * @stagemodelonly 77 * @crossplatform 78 * @since 10 79 */ 80 /** 81 * Object for resource management. 82 * 83 * @type { resmgr.ResourceManager } 84 * @syscap SystemCapability.Ability.AbilityRuntime.Core 85 * @stagemodelonly 86 * @crossplatform 87 * @atomicservice 88 * @since arkts {'1.1':'11', '1.2':'20'} 89 * @arkts 1.1&1.2 90 */ 91 resourceManager: resmgr.ResourceManager; 92 93 /** 94 * Indicates configuration information about an application. 95 * 96 * @type { ApplicationInfo } 97 * @syscap SystemCapability.Ability.AbilityRuntime.Core 98 * @stagemodelonly 99 * @since 9 100 */ 101 /** 102 * Indicates configuration information about an application. 103 * 104 * @type { ApplicationInfo } 105 * @syscap SystemCapability.Ability.AbilityRuntime.Core 106 * @stagemodelonly 107 * @crossplatform 108 * @since 10 109 */ 110 /** 111 * Application information. 112 * 113 * @type { ApplicationInfo } 114 * @syscap SystemCapability.Ability.AbilityRuntime.Core 115 * @stagemodelonly 116 * @crossplatform 117 * @atomicservice 118 * @since arkts {'1.1':'11', '1.2':'20'} 119 * @arkts 1.1&1.2 120 */ 121 applicationInfo: ApplicationInfo; 122 123 /** 124 * Indicates app cache dir. 125 * 126 * @type { string } 127 * @syscap SystemCapability.Ability.AbilityRuntime.Core 128 * @stagemodelonly 129 * @since 9 130 */ 131 /** 132 * Indicates app cache dir. 133 * 134 * @type { string } 135 * @syscap SystemCapability.Ability.AbilityRuntime.Core 136 * @stagemodelonly 137 * @crossplatform 138 * @since 10 139 */ 140 /** 141 * Cache directory. 142 * 143 * @type { string } 144 * @syscap SystemCapability.Ability.AbilityRuntime.Core 145 * @stagemodelonly 146 * @crossplatform 147 * @atomicservice 148 * @since arkts {'1.1':'11', '1.2':'20'} 149 * @arkts 1.1&1.2 150 */ 151 cacheDir: string; 152 153 /** 154 * Indicates app temp dir. 155 * 156 * @type { string } 157 * @syscap SystemCapability.Ability.AbilityRuntime.Core 158 * @stagemodelonly 159 * @since 9 160 */ 161 /** 162 * Indicates app temp dir. 163 * 164 * @type { string } 165 * @syscap SystemCapability.Ability.AbilityRuntime.Core 166 * @stagemodelonly 167 * @crossplatform 168 * @since 10 169 */ 170 /** 171 * Temporary directory. 172 * 173 * @type { string } 174 * @syscap SystemCapability.Ability.AbilityRuntime.Core 175 * @stagemodelonly 176 * @crossplatform 177 * @atomicservice 178 * @since arkts {'1.1':'11', '1.2':'20'} 179 * @arkts 1.1&1.2 180 */ 181 tempDir: string; 182 183 /** 184 * Indicates app files dir. 185 * 186 * @type { string } 187 * @syscap SystemCapability.Ability.AbilityRuntime.Core 188 * @stagemodelonly 189 * @since 9 190 */ 191 /** 192 * Indicates app files dir. 193 * 194 * @type { string } 195 * @syscap SystemCapability.Ability.AbilityRuntime.Core 196 * @stagemodelonly 197 * @crossplatform 198 * @since 10 199 */ 200 /** 201 * File directory. 202 * 203 * @type { string } 204 * @syscap SystemCapability.Ability.AbilityRuntime.Core 205 * @stagemodelonly 206 * @crossplatform 207 * @atomicservice 208 * @since arkts {'1.1':'11', '1.2':'20'} 209 * @arkts 1.1&1.2 210 */ 211 filesDir: string; 212 213 /** 214 * Indicates app database dir. 215 * 216 * @type { string } 217 * @syscap SystemCapability.Ability.AbilityRuntime.Core 218 * @stagemodelonly 219 * @since 9 220 */ 221 /** 222 * Indicates app database dir. 223 * 224 * @type { string } 225 * @syscap SystemCapability.Ability.AbilityRuntime.Core 226 * @stagemodelonly 227 * @crossplatform 228 * @since 10 229 */ 230 /** 231 * Database directory. 232 * 233 * @type { string } 234 * @syscap SystemCapability.Ability.AbilityRuntime.Core 235 * @stagemodelonly 236 * @crossplatform 237 * @atomicservice 238 * @since arkts {'1.1':'11', '1.2':'20'} 239 * @arkts 1.1&1.2 240 */ 241 databaseDir: string; 242 243 /** 244 * Indicates app preferences dir. 245 * 246 * @type { string } 247 * @syscap SystemCapability.Ability.AbilityRuntime.Core 248 * @stagemodelonly 249 * @since 9 250 */ 251 /** 252 * Indicates app preferences dir. 253 * 254 * @type { string } 255 * @syscap SystemCapability.Ability.AbilityRuntime.Core 256 * @stagemodelonly 257 * @crossplatform 258 * @since 10 259 */ 260 /** 261 * Preferences directory. 262 * 263 * @type { string } 264 * @syscap SystemCapability.Ability.AbilityRuntime.Core 265 * @stagemodelonly 266 * @crossplatform 267 * @atomicservice 268 * @since arkts {'1.1':'11', '1.2':'20'} 269 * @arkts 1.1&1.2 270 */ 271 preferencesDir: string; 272 273 /** 274 * Indicates app bundle code dir. 275 * 276 * @type { string } 277 * @syscap SystemCapability.Ability.AbilityRuntime.Core 278 * @stagemodelonly 279 * @since 9 280 */ 281 /** 282 * Indicates app bundle code dir. 283 * 284 * @type { string } 285 * @syscap SystemCapability.Ability.AbilityRuntime.Core 286 * @stagemodelonly 287 * @crossplatform 288 * @since 10 289 */ 290 /** 291 * Bundle code directory. 292 * 293 * <p>**NOTE**: 294 * <br>Do not access resource files using concatenated paths. Use @ohos.resourceManager instead. 295 * </p> 296 * 297 * @type { string } 298 * @syscap SystemCapability.Ability.AbilityRuntime.Core 299 * @stagemodelonly 300 * @crossplatform 301 * @atomicservice 302 * @since 11 303 */ 304 bundleCodeDir: string; 305 306 /** 307 * Indicates app distributed files dir. 308 * 309 * @type { string } 310 * @syscap SystemCapability.Ability.AbilityRuntime.Core 311 * @stagemodelonly 312 * @since 9 313 */ 314 /** 315 * Distributed file directory. 316 * 317 * @type { string } 318 * @syscap SystemCapability.Ability.AbilityRuntime.Core 319 * @stagemodelonly 320 * @atomicservice 321 * @since 11 322 */ 323 distributedFilesDir: string; 324 325 /** 326 * Resource directory. 327 * 328 * <p>**NOTE**: 329 * <br>You are required to manually create the resfile directory in <code><module-name>\resource</code>. The resfile 330 * directory can be accessed only in read-only mode. 331 * </p> 332 * 333 * @type { string } 334 * @syscap SystemCapability.Ability.AbilityRuntime.Core 335 * @stagemodelonly 336 * @crossplatform 337 * @atomicservice 338 * @since 11 339 */ 340 resourceDir: string; 341 342 /** 343 * Cloud file directory. 344 * 345 * @type { string } 346 * @syscap SystemCapability.Ability.AbilityRuntime.Core 347 * @stagemodelonly 348 * @atomicservice 349 * @since 12 350 */ 351 cloudFileDir: string; 352 353 /** 354 * Indicates event hub. 355 * 356 * @type { EventHub } 357 * @syscap SystemCapability.Ability.AbilityRuntime.Core 358 * @stagemodelonly 359 * @since 9 360 */ 361 /** 362 * Indicates event hub. 363 * 364 * @type { EventHub } 365 * @syscap SystemCapability.Ability.AbilityRuntime.Core 366 * @stagemodelonly 367 * @atomicservice 368 * @since 11 369 */ 370 /** 371 * Event hub that implements event subscription, unsubscription, and triggering. 372 * 373 * @type { EventHub } 374 * @syscap SystemCapability.Ability.AbilityRuntime.Core 375 * @stagemodelonly 376 * @crossplatform 377 * @atomicservice 378 * @since 12 379 */ 380 eventHub: EventHub; 381 382 /** 383 * Indicates file area. 384 * 385 * @type { contextConstant.AreaMode } 386 * @syscap SystemCapability.Ability.AbilityRuntime.Core 387 * @stagemodelonly 388 * @since 9 389 */ 390 /** 391 * Encryption level of the directory. 392 * 393 * @type { contextConstant.AreaMode } 394 * @syscap SystemCapability.Ability.AbilityRuntime.Core 395 * @stagemodelonly 396 * @atomicservice 397 * @since 11 398 */ 399 area: contextConstant.AreaMode; 400 401 /** 402 * Get encryption level of the directory. 403 * 404 * @return { contextConstant.AreaMode } 405 * @syscap SystemCapability.Ability.AbilityRuntime.Core 406 * @stagemodelonly 407 * @atomicservice 408 * @since 20 409 * @arkts 1.2 410 */ 411 get area(): contextConstant.AreaMode; 412 413 /** 414 * Set encryption level of the directory. 415 * 416 * @param { contextConstant.AreaMode } 417 * @syscap SystemCapability.Ability.AbilityRuntime.Core 418 * @stagemodelonly 419 * @atomicservice 420 * @since 20 421 * @arkts 1.2 422 */ 423 set area(mode: contextConstant.AreaMode); 424 425 /** 426 * Process name of the current application. 427 * 428 * @type { string } 429 * @syscap SystemCapability.Ability.AbilityRuntime.Core 430 * @stagemodelonly 431 * @crossplatform 432 * @atomicservice 433 * @since 18 434 */ 435 processName: string; 436 437 /** 438 * Creates the context based on the bundle name. 439 * 440 * <p>**NOTE**: 441 * <br>If there are multiple modules in the stage model, resource ID conflicts may occur. You are advised to 442 * use application.createModuleContext instead. 443 * <br>This API is deprecated since API version 12. You are advised to use application.createBundleContext instead. 444 * </p> 445 * 446 * @permission ohos.permission.GET_BUNDLE_INFO_PRIVILEGED 447 * @param { string } bundleName - Bundle name. 448 * @returns { Context } Context created. 449 * @throws { BusinessError } 201 - Permission denied. 450 * @throws { BusinessError } 202 - Permission denied, non-system app called system api. 451 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 452 * 2.Incorrect parameter types. 453 * @syscap SystemCapability.Ability.AbilityRuntime.Core 454 * @systemapi 455 * @stagemodelonly 456 * @since 9 457 * @deprecated since 12 458 * @useinstead ohos.app.ability.application/application#createBundleContext 459 */ 460 createBundleContext(bundleName: string): Context; 461 462 /** 463 * Create a module context 464 * 465 * @param { string } moduleName - Indicates the module name. 466 * @returns { Context } Returns the application context. 467 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 468 * @syscap SystemCapability.Ability.AbilityRuntime.Core 469 * @stagemodelonly 470 * @since 9 471 */ 472 /** 473 * Create a module context 474 * 475 * @param { string } moduleName - Indicates the module name. 476 * @returns { Context } Returns the application context. 477 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 478 * @syscap SystemCapability.Ability.AbilityRuntime.Core 479 * @stagemodelonly 480 * @crossplatform 481 * @since 10 482 */ 483 /** 484 * Creates the context based on the module name. 485 * 486 * <p>**NOTE**: 487 * <br>This API is deprecated since API version 12. You are advised to use application.createModuleContext instead. 488 * </p> 489 * 490 * @param { string } moduleName - Module name. 491 * @returns { Context } Context created. 492 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 493 * 2.Incorrect parameter types. 494 * @syscap SystemCapability.Ability.AbilityRuntime.Core 495 * @stagemodelonly 496 * @crossplatform 497 * @atomicservice 498 * @since 11 499 * @deprecated since 12 500 * @useinstead ohos.app.ability.application/application#createModuleContext 501 */ 502 createModuleContext(moduleName: string): Context; 503 504 /** 505 * Creates the context based on the bundle name and module name. 506 * 507 * <p>**NOTE**: 508 * <br>This API is deprecated since API version 12. You are advised to use application.createModuleContext instead. 509 * </p> 510 * 511 * @param { string } bundleName - Bundle name. 512 * @param { string } moduleName - Module name. 513 * @returns { Context } Context created. 514 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 515 * 2.Incorrect parameter types. 516 * @syscap SystemCapability.Ability.AbilityRuntime.Core 517 * @systemapi 518 * @stagemodelonly 519 * @since 9 520 * @deprecated since 12 521 * @useinstead ohos.app.ability.application/application#createModuleContext 522 */ 523 createModuleContext(bundleName: string, moduleName: string): Context; 524 525 /** 526 * This API allows OEM manufacturers to set up their own ResourceManager in the pre-installed system HSP. 527 * 528 * @param { string } bundleName - Bundle name. 529 * @param { string } moduleName - Module name. 530 * @returns { resmgr.ResourceManager } Returns the system HSP module resource manager. 531 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 532 * 2.Incorrect parameter types. 533 * @throws { BusinessError } 16400001 - The input bundleName is not a system HSP. 534 * @syscap SystemCapability.Ability.AbilityRuntime.Core 535 * @systemapi 536 * @stagemodelonly 537 * @since 12 538 */ 539 createSystemHspModuleResourceManager(bundleName: string, moduleName: string): resmgr.ResourceManager; 540 541 /** 542 * Get application context 543 * 544 * @returns { ApplicationContext } Returns the application context. 545 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 546 * @syscap SystemCapability.Ability.AbilityRuntime.Core 547 * @stagemodelonly 548 * @since 9 549 */ 550 /** 551 * Get application context 552 * 553 * @returns { ApplicationContext } Returns the application context. 554 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 555 * @syscap SystemCapability.Ability.AbilityRuntime.Core 556 * @stagemodelonly 557 * @crossplatform 558 * @since 10 559 */ 560 /** 561 * Obtains the context of this application. 562 * 563 * @returns { ApplicationContext } Application context obtained. 564 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 565 * @syscap SystemCapability.Ability.AbilityRuntime.Core 566 * @stagemodelonly 567 * @crossplatform 568 * @atomicservice 569 * @since arkts {'1.1':'11', '1.2':'20'} 570 * @arkts 1.1&1.2 571 */ 572 getApplicationContext(): ApplicationContext; 573 574 /** 575 * Get group dir by the groupId. 576 * 577 * @param { string } dataGroupID - Indicates the groupId. 578 * @param { AsyncCallback<string> } callback - The callback of getGroupDir. 579 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 580 * @throws { BusinessError } 16000011 - The context does not exist. 581 * @syscap SystemCapability.Ability.AbilityRuntime.Core 582 * @stagemodelonly 583 * @since 10 584 */ 585 /** 586 * Obtains the shared directory based on a group ID. 587 * This API uses an asynchronous callback to return the result. 588 * 589 * @param { string } dataGroupID - Group ID, which is assigned by the system when an atomic service project 590 * is created. 591 * @param { AsyncCallback<string> } callback - Group ID, which is assigned by the system when an atomic service 592 * project is created. 593 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 594 * 2.Incorrect parameter types. 595 * @throws { BusinessError } 16000011 - The context does not exist. 596 * @syscap SystemCapability.Ability.AbilityRuntime.Core 597 * @stagemodelonly 598 * @atomicservice 599 * @since 11 600 */ 601 getGroupDir(dataGroupID: string, callback: AsyncCallback<string>): void; 602 603 /** 604 * Get group dir by the groupId. 605 * 606 * @param { string } dataGroupID - Indicates the groupId. 607 * @returns { Promise<string> } The promise returned by the function. 608 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 609 * @throws { BusinessError } 16000011 - The context does not exist. 610 * @syscap SystemCapability.Ability.AbilityRuntime.Core 611 * @stagemodelonly 612 * @since 10 613 */ 614 /** 615 * Obtains the shared directory based on a group ID. 616 * This API uses a promise to return the result. 617 * 618 * @param { string } dataGroupID - Group ID, which is assigned by the system when an atomic service project 619 * is created. 620 * @returns { Promise<string> } Promise used to return the result. If no shared directory exists, null is returned. 621 * Only the encryption level EL2 is supported. 622 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 623 * @throws { BusinessError } 16000011 - The context does not exist. 624 * @syscap SystemCapability.Ability.AbilityRuntime.Core 625 * @stagemodelonly 626 * @atomicservice 627 * @since 11 628 */ 629 getGroupDir(dataGroupID: string): Promise<string>; 630 631 /** 632 * Creates a resource management object for a module. 633 * 634 * @permission ohos.permission.GET_BUNDLE_INFO_PRIVILEGED 635 * @param { string } bundleName - Bundle name. 636 * @param { string } moduleName - Module name. 637 * @returns { resmgr.ResourceManager } Object for resource management. 638 * @throws { BusinessError } 201 - Permission denied. 639 * @throws { BusinessError } 202 - Permission denied, non-system app called system api. 640 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 641 * 2.Incorrect parameter types. 642 * @syscap SystemCapability.Ability.AbilityRuntime.Core 643 * @systemapi 644 * @stagemodelonly 645 * @since arkts {'1.1':'11', '1.2':'20'} 646 * @arkts 1.1&1.2 647 */ 648 createModuleResourceManager(bundleName: string, moduleName: string): resmgr.ResourceManager; 649 650 /** 651 * Creates the context for this application based on a data encryption level. 652 * This is required when an application needs to store different types of information in different directories. 653 * The application can obtain the corresponding directory. 654 * 655 * @param { contextConstant.AreaMode } areaMode - Data encryption level. 656 * @returns { Context } Context created based on the data encryption level. 657 * @syscap SystemCapability.Ability.AbilityRuntime.Core 658 * @stagemodelonly 659 * @atomicservice 660 * @since 18 661 */ 662 createAreaModeContext(areaMode: contextConstant.AreaMode): Context; 663 664 /** 665 * Creates the context based on the specified display ID, so as to obtain and use other application contexts with 666 * screen information (including ScreenDensity and Direction). 667 * 668 * @param { number } displayId - Display ID. 669 * @returns { Context } Context with the specified screen information. 670 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 671 * 2.Incorrect parameter types. 672 * @syscap SystemCapability.Ability.AbilityRuntime.Core 673 * @stagemodelonly 674 * @atomicservice 675 * @since 15 676 */ 677 createDisplayContext(displayId: number): Context; 678} 679 680export default Context;