1/* 2 * Copyright (c) 2022 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 bundle from '@ohos.bundle' 17import featureAbility from '@ohos.ability.featureAbility' 18import prompt from '@ohos.prompt' 19import Logger from '../util/Logger' 20import { BUNDLE_NAME } from '../model/DaHelperConst' 21 22const TAG: string = 'AppContext' 23const context = featureAbility.getContext() 24 25class AppContext { 26 27 // 获取应用程序的本地根目录 28 getOrCreateLocalDir() { 29 context.getOrCreateLocalDir((err, data) => { 30 Logger.info(TAG, `getOrCreateLocalDir err = ${JSON.stringify(err)}, data = ${data}`) 31 prompt.showToast({ 32 message: `getWant success ${data}` 33 }) 34 }) 35 } 36 37 // 验证系统中运行的特定pid和uid是否允许指定的权限,Stage模型中abilityAccessCtrl.verifyAccessToken 38 verifyPermission() { 39 bundle.getBundleInfo(BUNDLE_NAME, 1, (err, dataInfo) => { // 1代表要查询的应用包标志 40 context.verifyPermission('ohos.permission.WRITE_MEDIA', { uid: dataInfo.uid }, (error, data) => { 41 Logger.info(TAG, `verifyPermission err = ${JSON.stringify(error)}, data = ${data}`) 42 prompt.showToast({ 43 message: `verifyPermission success ${data}` 44 }) 45 }) 46 }) 47 } 48 49 // 获取权限,对应stage模型AbilityContextController中的requestPermissionsFromUser 50 requestPermissionsFromUser() { 51 let requestCode = 666 // 请求码 52 context.requestPermissionsFromUser([''], requestCode, (err, data) => { 53 Logger.info(TAG, `requestPermissionsFromUser err = ${JSON.stringify(err)}, requestCode = ${data.requestCode}`) 54 prompt.showToast({ 55 message: `requestPermissionsFromUser success ${JSON.stringify(data)}` 56 }) 57 }) 58 } 59 60 // 获取有关当前应用程序的信息,Stage模型中BundleController中的getApplicationInfo 61 getApplicationInfo() { 62 context.getApplicationInfo((err, data) => { 63 Logger.info(TAG, `getApplicationInfo err = ${JSON.stringify(err)}, data = ${JSON.stringify(data)}`) 64 prompt.showToast({ 65 message: `getApplicationInfo success ${JSON.stringify(data)}` 66 }) 67 }) 68 } 69 70 // 获取当前ability的捆绑包名称,Stage模型的abilityinfo.bundleName 71 getBundleName() { 72 context.getBundleName((err, data) => { 73 Logger.info(TAG, `getBundleName err = ${JSON.stringify(err)}, data = ${JSON.stringify(data)}`) 74 prompt.showToast({ 75 message: `getBundleName success ${JSON.stringify(data)}` 76 }) 77 }) 78 } 79 80 // 获取此能力的当前显示方向,Stage模型中Configuration.direction 81 getDisplayOrientation() { 82 context.getDisplayOrientation((err, data) => { 83 Logger.info(TAG, `getDisplayOrientation err = ${JSON.stringify(err)}, data = ${JSON.stringify(data)}`) 84 prompt.showToast({ 85 message: `getDisplayOrientation success ${JSON.stringify(data)}` 86 }) 87 }) 88 } 89 90 // 设置当前能力的显示方向,Stage模型中window的setPreferredOrientation 91 setDisplayOrientation() { 92 context.setDisplayOrientation(bundle.DisplayOrientation.LANDSCAPE, (err, data) => { 93 Logger.info(TAG, `setDisplayOrientation err = ${JSON.stringify(err)}, data = ${JSON.stringify(data)}`) 94 prompt.showToast({ 95 message: `setDisplayOrientation success ${JSON.stringify(data)}` 96 }) 97 }) 98 } 99 100 // 设置每当显示锁屏时是否在锁屏顶部显示此功能,Stage模型MainAbility中的windowStage.setShowOnLockScreen 101 setShowOnLockScreen() { 102 context.setShowOnLockScreen(true, (err) => { 103 Logger.info(TAG, `setShowOnLockScreen err = ${JSON.stringify(err)}}`) 104 prompt.showToast({ 105 message: `setShowOnLockScreen success` 106 }) 107 }) 108 } 109 110 // 设置恢复此功能时是否唤醒屏幕,Stage模型WindowController中的setWakeUpScreen 111 setWakeUpScreen() { 112 context.setWakeUpScreen(true, (err) => { 113 Logger.info(TAG, `setWakeUpScreen err = ${JSON.stringify(err)}}`) 114 prompt.showToast({ 115 message: `setWakeUpScreen success` 116 }) 117 }) 118 } 119 120 // 获取有关当前进程的信息,包括进程ID和名称,Stage模型ApplicationInfo 121 getProcessInfo() { 122 context.getProcessInfo((err, data) => { 123 Logger.info(TAG, `getProcessInfo err = ${JSON.stringify(err)}, data = ${JSON.stringify(data)}`) 124 prompt.showToast({ 125 message: `getProcessInfo success ${JSON.stringify(data)}` 126 }) 127 }) 128 } 129 130 // 获取当前ability的ohos.bundle.ElementName对象,Stage模型abilityinfo 131 getElementName() { 132 context.getElementName((err, data) => { 133 Logger.info(TAG, `getElementName err = ${JSON.stringify(err)}, data = ${JSON.stringify(data)}`) 134 prompt.showToast({ 135 message: `getElementName success ${JSON.stringify(data)}` 136 }) 137 }) 138 } 139 140 // 获取当前进程的名称,Stage模型中BundleController中的process 141 getProcessName() { 142 context.getProcessName((err, data) => { 143 Logger.info(TAG, `getProcessName err = ${JSON.stringify(err)}, data = ${data}`) 144 prompt.showToast({ 145 message: `getProcessName success ${data}` 146 }) 147 }) 148 } 149 150 // 获取调用ability的包名称,Stage模型want.parameters 151 getCallingBundle() { 152 context.getCallingBundle((err, data) => { 153 Logger.info(TAG, `getCallingBundle err = ${JSON.stringify(err)}, data = ${data}`) 154 prompt.showToast({ 155 message: `getCallingBundle success ${data}` 156 }) 157 }) 158 } 159 160 // 获取该应用程序的内部存储目录,对应stage模型appcontext的cacheDir属性 161 getCacheDir() { 162 context.getCacheDir((err, data) => { 163 Logger.info(TAG, `getCacheDir err = ${JSON.stringify(err)}, data = ${data}`) 164 prompt.showToast({ 165 message: `getCacheDir success ${data}` 166 }) 167 }) 168 } 169 170 // 获取内部存储器上此应用程序的文件目录,对应stage模型appcontext的filesDir属性 171 getFilesDir() { 172 context.getFilesDir((err, data) => { 173 Logger.info(TAG, `getFilesDir err = ${JSON.stringify(err)}, data = ${data}`) 174 prompt.showToast({ 175 message: `getFilesDir success ${data}` 176 }) 177 }) 178 } 179 180 // 获取Ability或应用的分布式文件路径,对应stage模型appcontext的distributedFilesDir属性 181 getOrCreateDistributedDir() { 182 context.getOrCreateDistributedDir((err, data) => { 183 Logger.info(TAG, `getOrCreateDistributedDir err = ${JSON.stringify(err)}, data = ${data}`) 184 prompt.showToast({ 185 message: `getOrCreateDistributedDir success ${data}` 186 }) 187 }) 188 } 189 190 // 获取此应用的类型,对应stage模型BundleController的entityType 191 getAppType() { 192 context.getAppType((err, data) => { 193 Logger.info(TAG, `getAppType err = ${JSON.stringify(err)}, data = ${data}`) 194 prompt.showToast({ 195 message: `getAppType success ${data}` 196 }) 197 }) 198 } 199 200 // 获取应用的ModuleInfo对象,对应stage模型appcontext的currentHapModuleInfo属性 201 getHapModuleInfo() { 202 context.getHapModuleInfo((err, data) => { 203 Logger.info(TAG, `getHapModuleInfo err = ${JSON.stringify(err)}, data = ${JSON.stringify(data)}`) 204 prompt.showToast({ 205 message: `getHapModuleInfo success ${JSON.stringify(data)}` 206 }) 207 }) 208 } 209 210 // 获取应用的版本信息,Stage模型bundle.getDispatcherVersion 211 getAppVersionInfo() { 212 context.getAppVersionInfo((err, data) => { 213 Logger.info(TAG, `getAppVersionInfo err = ${JSON.stringify(err)}, data = ${JSON.stringify(data)}`) 214 prompt.showToast({ 215 message: `getAppVersionInfo success ${JSON.stringify(data)}` 216 }) 217 }) 218 } 219 220 // 查询当前归属Ability详细信息,对应stage模型appcontext的abilityInfo属性 221 getAbilityInfo() { 222 context.getAbilityInfo((err, data) => { 223 Logger.info(TAG, `getAbilityInfo err = ${JSON.stringify(err)}, data = ${JSON.stringify(data)}`) 224 prompt.showToast({ 225 message: `getAbilityInfo success ${data}` 226 }) 227 }) 228 } 229 230 // 获取应用上下文信息,对应stage模型appcontext的getApplicationContext 231 getApplicationContext() { 232 let appLicationContext = context.getApplicationContext() 233 Logger.info(TAG, `getApplicationContext ${JSON.stringify(appLicationContext)}`) 234 prompt.showToast({ 235 message: `getApplicationContext success` 236 }) 237 } 238 239 // 检查此能力的配置是否正在更改,Stage模型不支持 240 isUpdatingConfigurations() { 241 context.isUpdatingConfigurations((err, data) => { 242 Logger.info(TAG, `isUpdatingConfigurations err = ${JSON.stringify(err)}, data = ${data}`) 243 prompt.showToast({ 244 message: `isUpdatingConfigurations success ${data}` 245 }) 246 }) 247 } 248 249 // 通知系统绘制此页面功能所需的时间,Stage模型不支持 250 printDrawnCompleted() { 251 context.printDrawnCompleted((err, data) => { 252 Logger.info(TAG, `printDrawnCompleted err = ${JSON.stringify(err)}, data = ${data}`) 253 prompt.showToast({ 254 message: `printDrawnCompleted success ${data}` 255 }) 256 }) 257 } 258} 259 260export default new AppContext()