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