• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   * Create a bundle context
384   *
385   * @permission ohos.permission.GET_BUNDLE_INFO_PRIVILEGED
386   * @param { string } bundleName - Indicates the bundle name.
387   * @returns { Context } Returns the application context.
388   * @throws { BusinessError } 201 - Permission denied.
389   * @throws { BusinessError } 202 - Permission denied, non-system app called system api.
390   * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types.
391   * @syscap SystemCapability.Ability.AbilityRuntime.Core
392   * @systemapi
393   * @stagemodelonly
394   * @since 9
395   * @deprecated since 12
396   * @useinstead ohos.app.ability.application/application#createBundleContext
397   */
398  createBundleContext(bundleName: string): Context;
399
400  /**
401   * Create a module context
402   *
403   * @param { string } moduleName - Indicates the module name.
404   * @returns { Context } Returns the application context.
405   * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types.
406   * @syscap SystemCapability.Ability.AbilityRuntime.Core
407   * @stagemodelonly
408   * @since 9
409   */
410  /**
411   * Create a module context
412   *
413   * @param { string } moduleName - Indicates the module name.
414   * @returns { Context } Returns the application context.
415   * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types.
416   * @syscap SystemCapability.Ability.AbilityRuntime.Core
417   * @stagemodelonly
418   * @crossplatform
419   * @since 10
420   */
421  /**
422   * Create a module context
423   *
424   * @param { string } moduleName - Indicates the module name.
425   * @returns { Context } Returns the application context.
426   * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types.
427   * @syscap SystemCapability.Ability.AbilityRuntime.Core
428   * @stagemodelonly
429   * @crossplatform
430   * @atomicservice
431   * @since 11
432   * @deprecated since 12
433   * @useinstead ohos.app.ability.application/application#createModuleContext
434   */
435  createModuleContext(moduleName: string): Context;
436
437  /**
438   * Create a module context
439   *
440   * @param { string } bundleName - Indicates the bundle name.
441   * @param { string } moduleName - Indicates the module name.
442   * @returns { Context } Returns the application context.
443   * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types.
444   * @syscap SystemCapability.Ability.AbilityRuntime.Core
445   * @systemapi
446   * @stagemodelonly
447   * @since 9
448   * @deprecated since 12
449   * @useinstead ohos.app.ability.application/application#createModuleContext
450   */
451  createModuleContext(bundleName: string, moduleName: string): Context;
452
453  /**
454   * Create a system HSP module resource manager.
455   *
456   * @param { string } bundleName - Indicates the bundle name.
457   * @param { string } moduleName - Indicates the module name.
458   * @returns { resmgr.ResourceManager } Returns the system HSP module resource manager.
459   * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types.
460   * @throws { BusinessError } 16400001 - If the input bundleName is not system hsp.
461   * @syscap SystemCapability.Ability.AbilityRuntime.Core
462   * @systemapi
463   * @stagemodelonly
464   * @since 12
465   */
466  createSystemHspModuleResourceManager(bundleName: string, moduleName: string): resmgr.ResourceManager;
467
468  /**
469   * Get application context
470   *
471   * @returns { ApplicationContext } Returns the application context.
472   * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types.
473   * @syscap SystemCapability.Ability.AbilityRuntime.Core
474   * @stagemodelonly
475   * @since 9
476   */
477  /**
478   * Get application context
479   *
480   * @returns { ApplicationContext } Returns the application context.
481   * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types.
482   * @syscap SystemCapability.Ability.AbilityRuntime.Core
483   * @stagemodelonly
484   * @crossplatform
485   * @since 10
486   */
487  /**
488   * Get application context
489   *
490   * @returns { ApplicationContext } Returns the application context.
491   * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types.
492   * @syscap SystemCapability.Ability.AbilityRuntime.Core
493   * @stagemodelonly
494   * @crossplatform
495   * @atomicservice
496   * @since 11
497   */
498  getApplicationContext(): ApplicationContext;
499
500  /**
501   * Get group dir by the groupId.
502   *
503   * @param { string } dataGroupID - Indicates the groupId.
504   * @param { AsyncCallback<string> } callback - The callback of getGroupDir.
505   * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types.
506   * @throws { BusinessError } 16000011 - The context does not exist.
507   * @syscap SystemCapability.Ability.AbilityRuntime.Core
508   * @stagemodelonly
509   * @since 10
510   */
511  /**
512   * Get group dir by the groupId.
513   *
514   * @param { string } dataGroupID - Indicates the groupId.
515   * @param { AsyncCallback<string> } callback - The callback of getGroupDir.
516   * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types.
517   * @throws { BusinessError } 16000011 - The context does not exist.
518   * @syscap SystemCapability.Ability.AbilityRuntime.Core
519   * @stagemodelonly
520   * @atomicservice
521   * @since 11
522   */
523  getGroupDir(dataGroupID: string, callback: AsyncCallback<string>): void;
524
525  /**
526   * Get group dir by the groupId.
527   *
528   * @param { string } dataGroupID - Indicates the groupId.
529   * @returns { Promise<string> } The promise returned by the function.
530   * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types.
531   * @throws { BusinessError } 16000011 - The context does not exist.
532   * @syscap SystemCapability.Ability.AbilityRuntime.Core
533   * @stagemodelonly
534   * @since 10
535   */
536  /**
537   * Get group dir by the groupId.
538   *
539   * @param { string } dataGroupID - Indicates the groupId.
540   * @returns { Promise<string> } The promise returned by the function.
541   * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types.
542   * @throws { BusinessError } 16000011 - The context does not exist.
543   * @syscap SystemCapability.Ability.AbilityRuntime.Core
544   * @stagemodelonly
545   * @atomicservice
546   * @since 11
547   */
548  getGroupDir(dataGroupID: string): Promise<string>;
549
550  /**
551   * Create a module resource manager.
552   *
553   * @permission ohos.permission.GET_BUNDLE_INFO_PRIVILEGED
554   * @param { string } bundleName - Indicates the bundle name.
555   * @param { string } moduleName - Indicates the module name.
556   * @returns { resmgr.ResourceManager } Returns the module resource manager.
557   * @throws { BusinessError } 201 - Permission denied.
558   * @throws { BusinessError } 202 - Permission denied, non-system app called system api.
559   * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types.
560   * @syscap SystemCapability.Ability.AbilityRuntime.Core
561   * @systemapi
562   * @stagemodelonly
563   * @since 11
564   */
565  createModuleResourceManager(bundleName: string, moduleName: string): resmgr.ResourceManager;
566}
567