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 prompt from '@ohos.promptAction'; 17import Logger from '../util/Logger' 18import type common from '@ohos.app.ability.common' 19import abilityAccessCtrl from '@ohos.abilityAccessCtrl' 20 21const TAG: string = 'AbilityContextController' 22const accountId = 100 23const resultCode = 100 24const connectionNumber = 0 25const permissions = [''] 26 27let want = { 28 bundleName: 'ohos.samples.stagemodel', 29 abilityName: 'JumpAbility', 30 moduleName: 'entry' 31}; 32 33let serviceWant = { 34 deviceId: '', 35 bundleName: 'ohos.samples.stagemodel', 36 abilityName: 'ServiceExtAbility', 37}; 38 39export default class AbilityContextController { 40 private context: common.UIAbilityContext 41 42 constructor(context: common.UIAbilityContext) { 43 this.context = context 44 } 45 46 private regOnRelease(caller) { 47 try { 48 caller.onRelease((msg) => { 49 Logger.info(TAG, `caller onRelease is called ${msg}`) 50 }) 51 Logger.info(TAG, 'caller register OnRelease succeed') 52 } catch (error) { 53 Logger.error(TAG, `caller register OnRelease failed with ${error}`) 54 } 55 } 56 57 // 启动Ability,对应FA的StartServiceAbility 58 startAbility() { 59 this.context.startAbility(want, (error) => { 60 Logger.info(TAG, `error.code: ${JSON.stringify(error.code)}`) 61 }) 62 } 63 64 // 启动Ability并在结束的时候返回执行结果,对应FA的startAbilityForResult 65 startAbilityForResult() { 66 this.context.startAbilityForResult( 67 { 68 deviceId: '', bundleName: 'ohos.samples.stagemodel', abilityName: 'JumpAbility' 69 }, 70 (error, result) => { 71 Logger.info(TAG, `startAbilityForResult AsyncCallback is called, error.code: ${JSON.stringify(error)}`) 72 Logger.info(TAG, `startAbilityForResult AsyncCallback is called, result.resultCode: ${JSON.stringify(result.resultCode)}`) 73 } 74 ); 75 } 76 77 // 启动一个Ability并在该Ability帐号销毁时返回执行结果, 78 startAbilityForResultWithAccount() { 79 this.context.startAbilityWithAccount(want, accountId, (error, data) => { 80 Logger.info(TAG, `startAbilityWithAccount fail, error: ${JSON.stringify(error)}`) 81 Logger.info(TAG, `startAbilityWithAccount success, data: ${JSON.stringify(data)}`) 82 }) 83 } 84 85 // 启动一个新的ServiceExtensionAbility, 86 startServiceExtensionAbility() { 87 this.context.startServiceExtensionAbility(want) 88 .then(() => { 89 Logger.info(TAG, `startServiceExtensionAbility success`) 90 prompt.showToast({ 91 message: 'startServiceExtensionAbility success' 92 }) 93 }) 94 .catch((error) => { 95 Logger.error(TAG, `startServiceExtensionAbility fail, error: ${JSON.stringify(error)}`) 96 prompt.showToast({ 97 message: 'startServiceExtensionAbility' 98 }) 99 }) 100 } 101 102 // 启动一个新的ServiceExtensionAbility, 103 startServiceExtensionAbilityWithAccount() { 104 this.context.startServiceExtensionAbilityWithAccount(want, accountId) 105 .then(() => { 106 Logger.info(TAG, `startServiceExtensionAbilityWithAccount success`) 107 prompt.showToast({ 108 message: 'startServiceExtensionAbilityWithAccount success' 109 }) 110 }) 111 .catch((error) => { 112 Logger.error(TAG, `startServiceExtensionAbilityWithAccount fail, error: ${JSON.stringify(error)}`) 113 prompt.showToast({ 114 message: 'startServiceExtensionAbilityWithAccount' 115 }) 116 }) 117 } 118 119 // 停止同一应用程序内的服务, 120 stopServiceExtensionAbility() { 121 this.context.stopServiceExtensionAbility(want) 122 .then(() => { 123 Logger.info(TAG, `stopServiceExtensionAbility success`) 124 prompt.showToast({ 125 message: 'stopServiceExtensionAbility success' 126 }) 127 }) 128 .catch((error) => { 129 Logger.error(TAG, `stopServiceExtensionAbility fail, error: ${JSON.stringify(error)}`) 130 prompt.showToast({ 131 message: 'stopServiceExtensionAbility' 132 }) 133 }) 134 } 135 136 // 使用帐户停止同一应用程序内的服务, 137 stopServiceExtensionAbilityWithAccount() { 138 this.context.stopServiceExtensionAbilityWithAccount(want, accountId) 139 .then(() => { 140 Logger.info(TAG, `stopServiceExtensionAbilityWithAccount success`) 141 prompt.showToast({ 142 message: 'stopServiceExtensionAbilityWithAccount success' 143 }) 144 }) 145 .catch((error) => { 146 Logger.error(TAG, `stopServiceExtensionAbilityWithAccount fail, error: ${JSON.stringify(error)}`) 147 prompt.showToast({ 148 message: 'stopServiceExtensionAbilityWithAccount' 149 }) 150 }) 151 } 152 153 // 停止Ability自身,对应FA的terminateSelf 154 terminateSelf() { 155 this.context.terminateSelf((error) => { 156 Logger.info(TAG, `terminateSelf result: ${JSON.stringify(error)}`) 157 }) 158 } 159 160 // 停止Ability,配合startAbilityForResult使用,返回给接口调用方AbilityResult信息,对应FA的terminateSelfWithResult 161 terminateSelfWithResult() { 162 const abilityResult = { 163 want, 164 resultCode 165 } 166 this.context.terminateSelfWithResult(abilityResult, (error) => { 167 Logger.info(TAG, `terminateSelfWithResult is called: ${JSON.stringify(error.code)}`) 168 } 169 ) 170 } 171 172 // 使用AbilityInfo.AbilityType.SERVICE模板将当前Ability连接到一个Ability,对应FA的ConnectService 173 connectAbility() { 174 const options = { 175 onConnect(elementName, remote) { 176 Logger.info(TAG, `onConnect`) 177 prompt.showToast({ 178 message: 'onConnect' 179 }) 180 }, 181 onDisconnect(elementName) { 182 Logger.info(TAG, `onDisconnect`) 183 prompt.showToast({ 184 message: 'onDisconnect' 185 }) 186 }, 187 onFailed(code) { 188 Logger.info(TAG, `connectAbility: ${JSON.stringify(code)}`) 189 prompt.showToast({ 190 message: `connectAbility: ${JSON.stringify(code)}` 191 }) 192 } 193 } 194 const result = this.context.connectServiceExtensionAbility(serviceWant, options); 195 Logger.info(TAG, `connectAbilityResult: ${JSON.stringify(result)}`) 196 prompt.showToast({ 197 message: `connectAbilityResult: ${JSON.stringify(result)}` 198 }) 199 } 200 201 // 使用AbilityInfo.AbilityType.SERVICE模板和account将当前Ability连接到一个Ability。 202 connectAbilityWithAccount() { 203 const options = { 204 onConnect(elementName, remote) { 205 Logger.info(TAG, `onConnect`) 206 prompt.showToast({ 207 message: 'onConnect' 208 }) 209 }, 210 onDisconnect(elementName) { 211 Logger.info(TAG, `onDisconnect`) 212 prompt.showToast({ 213 message: 'onDisconnect' 214 }) 215 }, 216 onFailed(code) { 217 Logger.info(TAG, `connectAbilityWithAccount: ${JSON.stringify(code)}`) 218 prompt.showToast({ 219 message: `connectAbilityWithAccount: ${JSON.stringify(code)}` 220 }) 221 } 222 } 223 const result = this.context.connectServiceExtensionAbilityWithAccount(serviceWant, accountId, options); 224 Logger.info(TAG, `connectAbilityResult: ${JSON.stringify(result)}`) 225 prompt.showToast({ 226 message: `connectAbilityResult: ${JSON.stringify(result)}` 227 }) 228 } 229 230 // 断开连接,对应FA模型的disconnectService 231 disconnectAbility() { 232 this.context.disconnectServiceExtensionAbility(connectionNumber).then((data) => { 233 Logger.info(TAG, `disconnectAbility success, data: ${JSON.stringify(data)}`) 234 prompt.showToast({ 235 message: 'disconnectAbility success' 236 }) 237 }).catch((error) => { 238 Logger.error(TAG, `disconnectAbility fail, error: ${JSON.stringify(error)}`) 239 prompt.showToast({ 240 message: 'disconnectAbility' 241 }) 242 }) 243 } 244 245 // 根据account启动Ability 246 startAbilityWithAccount() { 247 let options = { 248 windowMode: 0 249 } 250 this.context.startAbilityWithAccount(want, accountId, options) 251 .then(() => { 252 Logger.info(TAG, `startAbilityWithAccount success`) 253 prompt.showToast({ 254 message: 'startAbilityWithAccount success' 255 }) 256 }) 257 .catch((error) => { 258 Logger.error(TAG, `startAbilityWithAccount fail, error: ${JSON.stringify(error)}`) 259 }) 260 } 261 262 // 拉起弹窗请求用户授权,对应FA模型的AppContext中的requestPermissionsFromUser 263 requestPermissionsFromUser() { 264 let atManager: abilityAccessCtrl.AtManager = abilityAccessCtrl.createAtManager() 265 try { 266 atManager.requestPermissionsFromUser(this.context, ['ohos.permission.ABILITY_BACKGROUND_COMMUNICATION']).then((data) => { 267 Logger.info(TAG, `data: ${JSON.stringify(data)}`) 268 prompt.showToast({ 269 message: 'requestPermissionsFromUser success' 270 }) 271 }).catch((err) => { 272 Logger.info(TAG, `err: ${JSON.stringify(err)}`) 273 }) 274 } catch (err) { 275 Logger.info(TAG, `catch err->${JSON.stringify(err)}`); 276 } 277 } 278 279 // 设置ability在任务中显示的名称, 280 setMissionLabel() { 281 this.context.setMissionLabel('test', (result) => { 282 Logger.info(TAG, `setMissionLabel: ${JSON.stringify(result)}`) 283 prompt.showToast({ 284 message: 'setMissionLabel success' 285 }) 286 }) 287 } 288 289 // 查询ability是否在terminating状态。 290 isTerminating() { 291 const isTerminating = this.context.isTerminating() 292 prompt.showToast({ 293 message: 'isTerminating success' 294 }) 295 Logger.info(TAG, `ability state: ${JSON.stringify(isTerminating)}`) 296 } 297} 298 299