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 21import { ApplicationInfo } from '../bundleManager/ApplicationInfo'; 22import type { AsyncCallback } from '../@ohos.base'; 23import resmgr from '../@ohos.resourceManager'; 24import BaseContext from './BaseContext'; 25import EventHub from './EventHub'; 26import ApplicationContext from './ApplicationContext'; 27import contextConstant from '../@ohos.app.ability.contextConstant'; 28 29/** 30 * The base context of an ability or an application. It allows access to 31 * application-specific resources. 32 * 33 * @extends BaseContext 34 * @syscap SystemCapability.Ability.AbilityRuntime.Core 35 * @stagemodelonly 36 * @since 9 37 */ 38/** 39 * The base context of an ability or an application. It allows access to 40 * application-specific resources. 41 * 42 * @extends BaseContext 43 * @syscap SystemCapability.Ability.AbilityRuntime.Core 44 * @stagemodelonly 45 * @crossplatform 46 * @since 10 47 */ 48/** 49 * The base context of an ability or an application. It allows access to 50 * application-specific resources. 51 * 52 * @extends BaseContext 53 * @syscap SystemCapability.Ability.AbilityRuntime.Core 54 * @stagemodelonly 55 * @crossplatform 56 * @atomicservice 57 * @since 11 58 */ 59export default class Context extends BaseContext { 60 /** 61 * Indicates the capability of accessing application resources. 62 * 63 * @type { resmgr.ResourceManager } 64 * @syscap SystemCapability.Ability.AbilityRuntime.Core 65 * @stagemodelonly 66 * @since 9 67 */ 68 /** 69 * Indicates the capability of accessing application resources. 70 * 71 * @type { resmgr.ResourceManager } 72 * @syscap SystemCapability.Ability.AbilityRuntime.Core 73 * @stagemodelonly 74 * @crossplatform 75 * @since 10 76 */ 77 /** 78 * Indicates the capability of accessing application resources. 79 * 80 * @type { resmgr.ResourceManager } 81 * @syscap SystemCapability.Ability.AbilityRuntime.Core 82 * @stagemodelonly 83 * @crossplatform 84 * @atomicservice 85 * @since 11 86 */ 87 resourceManager: resmgr.ResourceManager; 88 89 /** 90 * Indicates configuration information about an application. 91 * 92 * @type { ApplicationInfo } 93 * @syscap SystemCapability.Ability.AbilityRuntime.Core 94 * @stagemodelonly 95 * @since 9 96 */ 97 /** 98 * Indicates configuration information about an application. 99 * 100 * @type { ApplicationInfo } 101 * @syscap SystemCapability.Ability.AbilityRuntime.Core 102 * @stagemodelonly 103 * @crossplatform 104 * @since 10 105 */ 106 /** 107 * Indicates configuration information about an application. 108 * 109 * @type { ApplicationInfo } 110 * @syscap SystemCapability.Ability.AbilityRuntime.Core 111 * @stagemodelonly 112 * @crossplatform 113 * @atomicservice 114 * @since 11 115 */ 116 applicationInfo: ApplicationInfo; 117 118 /** 119 * Indicates app cache dir. 120 * 121 * @type { string } 122 * @syscap SystemCapability.Ability.AbilityRuntime.Core 123 * @stagemodelonly 124 * @since 9 125 */ 126 /** 127 * Indicates app cache dir. 128 * 129 * @type { string } 130 * @syscap SystemCapability.Ability.AbilityRuntime.Core 131 * @stagemodelonly 132 * @crossplatform 133 * @since 10 134 */ 135 /** 136 * Indicates app cache dir. 137 * 138 * @type { string } 139 * @syscap SystemCapability.Ability.AbilityRuntime.Core 140 * @stagemodelonly 141 * @crossplatform 142 * @atomicservice 143 * @since 11 144 */ 145 cacheDir: string; 146 147 /** 148 * Indicates app temp dir. 149 * 150 * @type { string } 151 * @syscap SystemCapability.Ability.AbilityRuntime.Core 152 * @stagemodelonly 153 * @since 9 154 */ 155 /** 156 * Indicates app temp dir. 157 * 158 * @type { string } 159 * @syscap SystemCapability.Ability.AbilityRuntime.Core 160 * @stagemodelonly 161 * @crossplatform 162 * @since 10 163 */ 164 /** 165 * Indicates app temp dir. 166 * 167 * @type { string } 168 * @syscap SystemCapability.Ability.AbilityRuntime.Core 169 * @stagemodelonly 170 * @crossplatform 171 * @atomicservice 172 * @since 11 173 */ 174 tempDir: string; 175 176 /** 177 * Indicates app files dir. 178 * 179 * @type { string } 180 * @syscap SystemCapability.Ability.AbilityRuntime.Core 181 * @stagemodelonly 182 * @since 9 183 */ 184 /** 185 * Indicates app files dir. 186 * 187 * @type { string } 188 * @syscap SystemCapability.Ability.AbilityRuntime.Core 189 * @stagemodelonly 190 * @crossplatform 191 * @since 10 192 */ 193 /** 194 * Indicates app files dir. 195 * 196 * @type { string } 197 * @syscap SystemCapability.Ability.AbilityRuntime.Core 198 * @stagemodelonly 199 * @crossplatform 200 * @atomicservice 201 * @since 11 202 */ 203 filesDir: string; 204 205 /** 206 * Indicates app database dir. 207 * 208 * @type { string } 209 * @syscap SystemCapability.Ability.AbilityRuntime.Core 210 * @stagemodelonly 211 * @since 9 212 */ 213 /** 214 * Indicates app database dir. 215 * 216 * @type { string } 217 * @syscap SystemCapability.Ability.AbilityRuntime.Core 218 * @stagemodelonly 219 * @crossplatform 220 * @since 10 221 */ 222 /** 223 * Indicates app database dir. 224 * 225 * @type { string } 226 * @syscap SystemCapability.Ability.AbilityRuntime.Core 227 * @stagemodelonly 228 * @crossplatform 229 * @atomicservice 230 * @since 11 231 */ 232 databaseDir: string; 233 234 /** 235 * Indicates app preferences dir. 236 * 237 * @type { string } 238 * @syscap SystemCapability.Ability.AbilityRuntime.Core 239 * @stagemodelonly 240 * @since 9 241 */ 242 /** 243 * Indicates app preferences dir. 244 * 245 * @type { string } 246 * @syscap SystemCapability.Ability.AbilityRuntime.Core 247 * @stagemodelonly 248 * @crossplatform 249 * @since 10 250 */ 251 /** 252 * Indicates app preferences dir. 253 * 254 * @type { string } 255 * @syscap SystemCapability.Ability.AbilityRuntime.Core 256 * @stagemodelonly 257 * @crossplatform 258 * @atomicservice 259 * @since 11 260 */ 261 preferencesDir: string; 262 263 /** 264 * Indicates app bundle code dir. 265 * 266 * @type { string } 267 * @syscap SystemCapability.Ability.AbilityRuntime.Core 268 * @stagemodelonly 269 * @since 9 270 */ 271 /** 272 * Indicates app bundle code dir. 273 * 274 * @type { string } 275 * @syscap SystemCapability.Ability.AbilityRuntime.Core 276 * @stagemodelonly 277 * @crossplatform 278 * @since 10 279 */ 280 /** 281 * Indicates app bundle code dir. 282 * 283 * @type { string } 284 * @syscap SystemCapability.Ability.AbilityRuntime.Core 285 * @stagemodelonly 286 * @crossplatform 287 * @atomicservice 288 * @since 11 289 */ 290 bundleCodeDir: string; 291 292 /** 293 * Indicates app distributed files dir. 294 * 295 * @type { string } 296 * @syscap SystemCapability.Ability.AbilityRuntime.Core 297 * @stagemodelonly 298 * @since 9 299 */ 300 /** 301 * Indicates app distributed files dir. 302 * 303 * @type { string } 304 * @syscap SystemCapability.Ability.AbilityRuntime.Core 305 * @stagemodelonly 306 * @atomicservice 307 * @since 11 308 */ 309 distributedFilesDir: string; 310 311 /** 312 * Indicates app bundle resource dir. 313 * 314 * @type { string } 315 * @syscap SystemCapability.Ability.AbilityRuntime.Core 316 * @stagemodelonly 317 * @crossplatform 318 * @atomicservice 319 * @since 11 320 */ 321 resourceDir: string; 322 323 /** 324 * Indicates app cloud storage files dir. 325 * 326 * @type { string } 327 * @syscap SystemCapability.Ability.AbilityRuntime.Core 328 * @stagemodelonly 329 * @atomicservice 330 * @since 12 331 */ 332 cloudFileDir: string; 333 334 /** 335 * Indicates event hub. 336 * 337 * @type { EventHub } 338 * @syscap SystemCapability.Ability.AbilityRuntime.Core 339 * @stagemodelonly 340 * @since 9 341 */ 342 /** 343 * Indicates event hub. 344 * 345 * @type { EventHub } 346 * @syscap SystemCapability.Ability.AbilityRuntime.Core 347 * @stagemodelonly 348 * @atomicservice 349 * @since 11 350 */ 351 /** 352 * Indicates event hub. 353 * 354 * @type { EventHub } 355 * @syscap SystemCapability.Ability.AbilityRuntime.Core 356 * @stagemodelonly 357 * @crossplatform 358 * @atomicservice 359 * @since 12 360 */ 361 eventHub: EventHub; 362 363 /** 364 * Indicates file area. 365 * 366 * @type { contextConstant.AreaMode } 367 * @syscap SystemCapability.Ability.AbilityRuntime.Core 368 * @stagemodelonly 369 * @since 9 370 */ 371 /** 372 * Indicates file area. 373 * 374 * @type { contextConstant.AreaMode } 375 * @syscap SystemCapability.Ability.AbilityRuntime.Core 376 * @stagemodelonly 377 * @atomicservice 378 * @since 11 379 */ 380 area: contextConstant.AreaMode; 381 382 /** 383 * Indicates process name. 384 * 385 * @type { string } 386 * @syscap SystemCapability.Ability.AbilityRuntime.Core 387 * @stagemodelonly 388 * @crossplatform 389 * @atomicservice 390 * @since 18 391 */ 392 processName: string; 393 394 /** 395 * Create a bundle context 396 * 397 * @permission ohos.permission.GET_BUNDLE_INFO_PRIVILEGED 398 * @param { string } bundleName - Indicates the bundle name. 399 * @returns { Context } Returns the application context. 400 * @throws { BusinessError } 201 - Permission denied. 401 * @throws { BusinessError } 202 - Permission denied, non-system app called system api. 402 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 403 * @syscap SystemCapability.Ability.AbilityRuntime.Core 404 * @systemapi 405 * @stagemodelonly 406 * @since 9 407 * @deprecated since 12 408 * @useinstead ohos.app.ability.application/application#createBundleContext 409 */ 410 createBundleContext(bundleName: string): Context; 411 412 /** 413 * Create a module context 414 * 415 * @param { string } moduleName - Indicates the module name. 416 * @returns { Context } Returns the application context. 417 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 418 * @syscap SystemCapability.Ability.AbilityRuntime.Core 419 * @stagemodelonly 420 * @since 9 421 */ 422 /** 423 * Create a module context 424 * 425 * @param { string } moduleName - Indicates the module name. 426 * @returns { Context } Returns the application context. 427 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 428 * @syscap SystemCapability.Ability.AbilityRuntime.Core 429 * @stagemodelonly 430 * @crossplatform 431 * @since 10 432 */ 433 /** 434 * Create a module context 435 * 436 * @param { string } moduleName - Indicates the module name. 437 * @returns { Context } Returns the application context. 438 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 439 * @syscap SystemCapability.Ability.AbilityRuntime.Core 440 * @stagemodelonly 441 * @crossplatform 442 * @atomicservice 443 * @since 11 444 * @deprecated since 12 445 * @useinstead ohos.app.ability.application/application#createModuleContext 446 */ 447 createModuleContext(moduleName: string): Context; 448 449 /** 450 * Create a module context 451 * 452 * @param { string } bundleName - Indicates the bundle name. 453 * @param { string } moduleName - Indicates the module name. 454 * @returns { Context } Returns the application context. 455 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 456 * @syscap SystemCapability.Ability.AbilityRuntime.Core 457 * @systemapi 458 * @stagemodelonly 459 * @since 9 460 * @deprecated since 12 461 * @useinstead ohos.app.ability.application/application#createModuleContext 462 */ 463 createModuleContext(bundleName: string, moduleName: string): Context; 464 465 /** 466 * Create a system HSP module resource manager. 467 * 468 * @param { string } bundleName - Indicates the bundle name. 469 * @param { string } moduleName - Indicates the module name. 470 * @returns { resmgr.ResourceManager } Returns the system HSP module resource manager. 471 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 472 * @throws { BusinessError } 16400001 - If the input bundleName is not system hsp. 473 * @syscap SystemCapability.Ability.AbilityRuntime.Core 474 * @systemapi 475 * @stagemodelonly 476 * @since 12 477 */ 478 createSystemHspModuleResourceManager(bundleName: string, moduleName: string): resmgr.ResourceManager; 479 480 /** 481 * Get application context 482 * 483 * @returns { ApplicationContext } Returns the application context. 484 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 485 * @syscap SystemCapability.Ability.AbilityRuntime.Core 486 * @stagemodelonly 487 * @since 9 488 */ 489 /** 490 * Get application context 491 * 492 * @returns { ApplicationContext } Returns the application context. 493 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 494 * @syscap SystemCapability.Ability.AbilityRuntime.Core 495 * @stagemodelonly 496 * @crossplatform 497 * @since 10 498 */ 499 /** 500 * Get application context 501 * 502 * @returns { ApplicationContext } Returns the application context. 503 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 504 * @syscap SystemCapability.Ability.AbilityRuntime.Core 505 * @stagemodelonly 506 * @crossplatform 507 * @atomicservice 508 * @since 11 509 */ 510 getApplicationContext(): ApplicationContext; 511 512 /** 513 * Get group dir by the groupId. 514 * 515 * @param { string } dataGroupID - Indicates the groupId. 516 * @param { AsyncCallback<string> } callback - The callback of getGroupDir. 517 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 518 * @throws { BusinessError } 16000011 - The context does not exist. 519 * @syscap SystemCapability.Ability.AbilityRuntime.Core 520 * @stagemodelonly 521 * @since 10 522 */ 523 /** 524 * Get group dir by the groupId. 525 * 526 * @param { string } dataGroupID - Indicates the groupId. 527 * @param { AsyncCallback<string> } callback - The callback of getGroupDir. 528 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 529 * @throws { BusinessError } 16000011 - The context does not exist. 530 * @syscap SystemCapability.Ability.AbilityRuntime.Core 531 * @stagemodelonly 532 * @atomicservice 533 * @since 11 534 */ 535 getGroupDir(dataGroupID: string, callback: AsyncCallback<string>): void; 536 537 /** 538 * Get group dir by the groupId. 539 * 540 * @param { string } dataGroupID - Indicates the groupId. 541 * @returns { Promise<string> } The promise returned by the function. 542 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 543 * @throws { BusinessError } 16000011 - The context does not exist. 544 * @syscap SystemCapability.Ability.AbilityRuntime.Core 545 * @stagemodelonly 546 * @since 10 547 */ 548 /** 549 * Get group dir by the groupId. 550 * 551 * @param { string } dataGroupID - Indicates the groupId. 552 * @returns { Promise<string> } The promise returned by the function. 553 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 554 * @throws { BusinessError } 16000011 - The context does not exist. 555 * @syscap SystemCapability.Ability.AbilityRuntime.Core 556 * @stagemodelonly 557 * @atomicservice 558 * @since 11 559 */ 560 getGroupDir(dataGroupID: string): Promise<string>; 561 562 /** 563 * Create a module resource manager. 564 * 565 * @permission ohos.permission.GET_BUNDLE_INFO_PRIVILEGED 566 * @param { string } bundleName - Indicates the bundle name. 567 * @param { string } moduleName - Indicates the module name. 568 * @returns { resmgr.ResourceManager } Returns the module resource manager. 569 * @throws { BusinessError } 201 - Permission denied. 570 * @throws { BusinessError } 202 - Permission denied, non-system app called system api. 571 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 572 * @syscap SystemCapability.Ability.AbilityRuntime.Core 573 * @systemapi 574 * @stagemodelonly 575 * @since 11 576 */ 577 createModuleResourceManager(bundleName: string, moduleName: string): resmgr.ResourceManager; 578 579 /** 580 * Create a area mode context.This context uses the same resourceManager as the original context. 581 * 582 * @param { contextConstant.AreaMode } areaMode - Indicates the area mode. 583 * @returns { Context } Returns the context with the specified area mode. 584 * @syscap SystemCapability.Ability.AbilityRuntime.Core 585 * @stagemodelonly 586 * @atomicservice 587 * @since 18 588 */ 589 createAreaModeContext(areaMode: contextConstant.AreaMode): Context; 590 591 /** 592 * Create a context by displayId. This Context updates the density and direction properties based on the displayId, 593 * while other property values remain the same as in the original Context. 594 * 595 * @param { number } displayId - Indicates the displayId. 596 * @returns { Context } Returns the context with the specified displayId. 597 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 598 * @syscap SystemCapability.Ability.AbilityRuntime.Core 599 * @stagemodelonly 600 * @atomicservice 601 * @since 15 602 */ 603 createDisplayContext(displayId: number): Context; 604} 605