• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2* Copyright (c) 2021 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
16import { AsyncCallback } from '../basic';
17import { ApplicationInfo } from '../bundle/applicationInfo';
18import { ProcessInfo } from './processInfo';
19import { ElementName } from '../bundle/elementName';
20import BaseContext from '../application/BaseContext';
21import { HapModuleInfo } from '../bundle/hapModuleInfo';
22import { AppVersionInfo } from './appVersionInfo';
23import { AbilityInfo } from '../bundle/abilityInfo';
24import bundle from '../@ohos.bundle';
25
26
27/**
28 * The context of an ability or an application.  It allows access to
29 * application-specific resources, request and verification permissions.
30 * Can only be obtained through the ability.
31 *
32 * @since 6
33 * @syscap SystemCapability.Ability.AbilityRuntime.Core
34 * @import import abilityManager from 'app/context'
35 * @permission N/A
36 * @FAModelOnly
37 */
38export interface Context extends BaseContext {
39
40    /**
41    * Get the local root dir of an app. If it is the first call, the dir
42    * will be created.
43    * @note If in the context of the ability, return the root dir of
44    * the ability; if in the context of the application, return the
45    * root dir of the application.
46    * @since 7
47    * @syscap SystemCapability.Ability.AbilityRuntime.Core
48    * @return the root dir
49    * @FAModelOnly
50    */
51    getOrCreateLocalDir(): Promise<string>;
52    getOrCreateLocalDir(callback: AsyncCallback<string>): void;
53    /**
54    * Verify whether the specified permission is allowed for a particular
55    * pid and uid running in the system.
56    * @param permission The name of the specified permission
57    * @param pid process id
58    * @param uid user id
59    * @note Pid and uid are optional. If you do not pass in pid and uid,
60    * it will check your own permission.
61    * @since 7
62    * @syscap SystemCapability.Ability.AbilityRuntime.Core
63    * @return asynchronous callback with {@code 0} if the PID
64    *         and UID have the permission; callback with {@code -1} otherwise.
65    * @FAModelOnly
66    */
67    verifyPermission(permission: string, options?: PermissionOptions): Promise<number>;
68    verifyPermission(permission: string, options: PermissionOptions, callback: AsyncCallback<number>): void;
69    verifyPermission(permission: string, callback: AsyncCallback<number>): void;
70
71    /**
72    * Requests certain permissions from the system.
73    * @param permissions Indicates the list of permissions to be requested. This parameter cannot be null.
74    * @param requestCode Indicates the request code to be passed to the PermissionRequestResult
75    * @since 7
76    * @syscap SystemCapability.Ability.AbilityRuntime.Core
77    * @FAModelOnly
78    */
79    requestPermissionsFromUser(permissions: Array<string>, requestCode: number, resultCallback: AsyncCallback<PermissionRequestResult>): void;
80    requestPermissionsFromUser(permissions: Array<string>, requestCode: number): Promise<PermissionRequestResult>;
81
82    /**
83    * Obtains information about the current application.
84    * @since 7
85    * @syscap SystemCapability.Ability.AbilityRuntime.Core
86    * @FAModelOnly
87    */
88    getApplicationInfo(callback: AsyncCallback<ApplicationInfo>): void
89    getApplicationInfo(): Promise<ApplicationInfo>;
90
91    /**
92    * Obtains the bundle name of the current ability.
93    * @since 7
94    * @syscap SystemCapability.Ability.AbilityRuntime.Core
95    * @FAModelOnly
96    */
97    getBundleName(callback: AsyncCallback<string>): void
98    getBundleName(): Promise<string>;
99
100    /**
101    * Obtains the current display orientation of this ability.
102    * @since 7
103    * @syscap SystemCapability.Ability.AbilityRuntime.Core
104    */
105    getDisplayOrientation(callback: AsyncCallback<bundle.DisplayOrientation>): void
106    getDisplayOrientation(): Promise<bundle.DisplayOrientation>;
107
108    /**
109    * Obtains the absolute path to the application-specific cache directory
110    * @since 6
111    * @syscap SystemCapability.Ability.AbilityRuntime.Core
112    * @deprecated since 7
113    */
114    getExternalCacheDir(callback: AsyncCallback<string>): void
115    getExternalCacheDir(): Promise<string>;
116
117    /**
118    * Sets the display orientation of the current ability.
119    * @param orientation Indicates the new orientation for the current ability.
120    * @since 7
121    * @syscap SystemCapability.Ability.AbilityRuntime.Core
122    */
123    setDisplayOrientation(orientation: bundle.DisplayOrientation, callback: AsyncCallback<void>): void
124    setDisplayOrientation(orientation: bundle.DisplayOrientation): Promise<void>;
125
126    /**
127    * Sets whether to show this ability on top of the lock screen whenever the lock screen is displayed, keeping the ability in the ACTIVE state.
128    * @param show Specifies whether to show this ability on top of the lock screen. The value true means to show it on the lock screen, and the value false means not.
129    * @since 7
130    * @syscap SystemCapability.Ability.AbilityRuntime.Core
131    */
132    setShowOnLockScreen(show: boolean, callback: AsyncCallback<void>): void
133    setShowOnLockScreen(show: boolean): Promise<void>;
134
135    /**
136    * Sets whether to wake up the screen when this ability is restored.
137    * @param wakeUp Specifies whether to wake up the screen. The value true means to wake it up, and the value false means not.
138    * @since 7
139    * @syscap SystemCapability.Ability.AbilityRuntime.Core
140    */
141    setWakeUpScreen(wakeUp: boolean, callback: AsyncCallback<void>): void
142    setWakeUpScreen(wakeUp: boolean): Promise<void>;
143
144    /**
145    * Obtains information about the current process, including the process ID and name.
146    * @since 7
147    * @syscap SystemCapability.Ability.AbilityRuntime.Core
148    * @FAModelOnly
149    */
150    getProcessInfo(callback: AsyncCallback<ProcessInfo>): void
151    getProcessInfo(): Promise<ProcessInfo>;
152
153    /**
154    * Obtains the ohos.bundle.ElementName object of the current ability. This method is available only to Page abilities.
155    * @since 7
156    * @syscap SystemCapability.Ability.AbilityRuntime.Core
157    * @FAModelOnly
158    */
159    getElementName(callback: AsyncCallback<ElementName>): void
160    getElementName(): Promise<ElementName>;
161
162    /**
163    * Obtains the name of the current process.
164    * @since 7
165    * @syscap SystemCapability.Ability.AbilityRuntime.Core
166    * @FAModelOnly
167    */
168    getProcessName(callback: AsyncCallback<string>): void
169    getProcessName(): Promise<string>;
170
171    /**
172    * Obtains the bundle name of the ability that called the current ability.
173    * @since 7
174    * @syscap SystemCapability.Ability.AbilityRuntime.Core
175    * @FAModelOnly
176    */
177    getCallingBundle(callback: AsyncCallback<string>): void
178    getCallingBundle(): Promise<string>;
179
180    /**
181    * Obtains the file directory of this application on the internal storage.
182    * @since 6
183    * @syscap SystemCapability.Ability.AbilityRuntime.Core
184    * @FAModelOnly
185    */
186    getFilesDir(callback: AsyncCallback<string>): void;
187    getFilesDir(): Promise<string>;
188
189    /**
190    * Obtains the cache directory of this application on the internal storage.
191    * @since 6
192    * @syscap SystemCapability.Ability.AbilityRuntime.Core
193    * @FAModelOnly
194    */
195    getCacheDir(callback: AsyncCallback<string>): void;
196    getCacheDir(): Promise<string>;
197
198    /**
199    * Obtains the distributed file path for storing ability or application data files.
200    * If the distributed file path does not exist, the system will create a path and return the created path.
201    * @since 7
202    * @syscap SystemCapability.Ability.AbilityRuntime.Core
203    * @FAModelOnly
204    */
205    getOrCreateDistributedDir(): Promise<string>;
206    getOrCreateDistributedDir(callback: AsyncCallback<string>): void;
207
208    /**
209    * Obtains the application type.
210    * @since 7
211    * @syscap SystemCapability.Ability.AbilityRuntime.Core
212    * @FAModelOnly
213    */
214    getAppType(callback: AsyncCallback<string>): void
215    getAppType(): Promise<string>;
216
217    /**
218    * Obtains the ModuleInfo object for this application.
219    * @since 7
220    * @syscap SystemCapability.Ability.AbilityRuntime.Core
221    * @FAModelOnly
222    */
223    getHapModuleInfo(callback: AsyncCallback<HapModuleInfo>): void
224    getHapModuleInfo(): Promise<HapModuleInfo>;
225
226    /**
227    * Obtains the application version information.
228    * @since 7
229    * @syscap SystemCapability.Ability.AbilityRuntime.Core
230    * @FAModelOnly
231    */
232    getAppVersionInfo(callback: AsyncCallback<AppVersionInfo>): void
233    getAppVersionInfo(): Promise<AppVersionInfo>;
234
235    /**
236    * Obtains the context of this application.
237    * @since 7
238    * @syscap SystemCapability.Ability.AbilityRuntime.Core
239    * @FAModelOnly
240    */
241    getApplicationContext(): Context;
242
243    /**
244    * Checks the detailed information of this ability.
245    * @since 7
246    * @syscap SystemCapability.Ability.AbilityRuntime.Core
247    * @FAModelOnly
248    */
249    getAbilityInfo(callback: AsyncCallback<AbilityInfo>): void
250    getAbilityInfo(): Promise<AbilityInfo>;
251
252    /**
253    * Checks whether the configuration of this ability is changing.
254    * @since 7
255    * @syscap SystemCapability.Ability.AbilityRuntime.Core
256    * @return true if the configuration of this ability is changing and false otherwise.
257    * @FAModelOnly
258    */
259    isUpdatingConfigurations(callback: AsyncCallback<boolean>): void;
260    isUpdatingConfigurations(): Promise<boolean>;
261
262    /**
263    * Informs the system of the time required for drawing this Page ability.
264    * @since 7
265    * @syscap SystemCapability.Ability.AbilityRuntime.Core
266    * @FAModelOnly
267    */
268    printDrawnCompleted(callback: AsyncCallback<void>): void;
269    printDrawnCompleted(): Promise<void>;
270}
271
272/**
273 * @name the result of requestPermissionsFromUser with asynchronous callback
274 * @since 7
275 * @syscap SystemCapability.Ability.AbilityRuntime.Core
276 * @permission N/A
277 * @FAModelOnly
278 */
279interface PermissionRequestResult {
280    /**
281    * @default The request code passed in by the user
282    * @since 7
283    * @syscap SystemCapability.Ability.AbilityRuntime.Core
284    * @FAModelOnly
285    */
286    requestCode: number;
287
288    /**
289    * @default The permissions passed in by the user
290    * @since 7
291    * @syscap SystemCapability.Ability.AbilityRuntime.Core
292    * @FAModelOnly
293    */
294    permissions: Array<string>;
295
296    /**
297    * @default The results for the corresponding request permissions
298    * @since 7
299    * @syscap SystemCapability.Ability.AbilityRuntime.Core
300    * @FAModelOnly
301    */
302    authResults: Array<number>;
303}
304
305/**
306 * @name PermissionOptions
307 * @since 7
308 * @syscap SystemCapability.Ability.AbilityRuntime.Core
309 * @permission N/A
310 * @FAModelOnly
311 */
312interface PermissionOptions {
313 /**
314  * @default The process id
315  * @since 7
316  * @syscap SystemCapability.Ability.AbilityRuntime.Core
317  * @FAModelOnly
318  */
319 pid?: number;
320
321 /**
322  * @default The user id
323  * @since 7
324  * @syscap SystemCapability.Ability.AbilityRuntime.Core
325  * @FAModelOnly
326  */
327 uid?: number;
328}
329