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 featureAbility from '@ohos.ability.featureAbility' 17import prompt from '@ohos.prompt' 18import rpc from '@ohos.rpc' 19import Logger from '../util/Logger' 20import { BASE_URI, BUNDLE_NAME, SERVICE_ABILITY_NAME, TAST_ABILITY_NAME } from '../model/DaHelperConst' 21 22const TAG: string = 'FeatureAbilityHelper' 23const localDeviceId: string = '' 24let rpcRemote: rpc.IRemoteObject = null 25let connection: number = -1 26 27class FeatureAbilityHelper { 28 29 // 获取从Ability发送的Want,Stage模型在MainAbility的onCreate生命周期函数中获取 30 async getWant() { 31 let want = await featureAbility.getWant() 32 Logger.info(TAG, `want = ${JSON.stringify(want)}`) 33 prompt.showToast({ 34 message: 'getWant success' 35 }) 36 } 37 38 // 启动一个ability,并在该ability被销毁时返回执行结果,对应stage模型startAbilityForResult 39 startAbilityForResult() { 40 featureAbility.startAbilityForResult({ 41 want: { 42 bundleName: BUNDLE_NAME, 43 abilityName: TAST_ABILITY_NAME 44 } 45 }, (data) => { 46 Logger.info(TAG, `${JSON.stringify(data)}`) 47 prompt.showToast({ 48 message: 'startAbilityForResult success' 49 }) 50 }) 51 } 52 53 // 获取dataAbilityHelper, 对应stage模型中DataShareHelper.ts中createDataShareHelper 54 acquireDataAbilityHelper() { 55 let DAHelper = featureAbility.acquireDataAbilityHelper(BASE_URI) 56 Logger.info(TAG, 'get acquireDataAbilityHelper success') 57 prompt.showToast({ 58 message: 'get acquireDataAbilityHelper success' 59 }) 60 return DAHelper 61 } 62 63 // 返回给调用者的结果代码和数据并破坏此Page Ability,对应stage模型terminateSelfWithResult 64 terminateSelfWithResult() { 65 featureAbility.terminateSelfWithResult({ 66 resultCode: 1, // 错误返回码 67 want: 68 { 69 type: 'MIMETYPE', 70 deviceId: '', 71 bundleName: BUNDLE_NAME, 72 abilityName: TAST_ABILITY_NAME, 73 uri: '', 74 parameters: { 75 mykey0: 2222, 76 mykey1: [1, 2, 3], 77 mykey2: '[1, 2, 3]' 78 } 79 } 80 }, 81 () => { 82 Logger.info(TAG, 'terminateSelfWithResult success') 83 prompt.showToast({ 84 message: 'terminateSelfWithResult success' 85 }) 86 }) 87 } 88 89 // 检查Ability的主窗口是否具有窗口焦点,Stage模型不支持 90 async hasWindowFocus() { 91 let isNo = await featureAbility.hasWindowFocus() 92 Logger.info(TAG, `hasWindowFocus = ${isNo}`) 93 prompt.showToast({ 94 message: isNo ? 'Ability currently has a window focus' : 'Ability currently has no window focus' 95 }) 96 } 97 98 // 销毁ability,对应stage模型terminateSelf 99 terminateSelf() { 100 featureAbility.terminateSelf(() => { 101 Logger.info(TAG, 'terminateSelf success') 102 prompt.showToast({ 103 message: 'terminateSelf success' 104 }) 105 }) 106 } 107 108 // 获取当前Ability对应的窗口, 109 getWindow() { 110 featureAbility.getWindow((err, data) => { 111 Logger.info(TAG, `getWindow = ${JSON.stringify(data)}`) 112 prompt.showToast({ 113 message: 'getWindow success' 114 }) 115 }) 116 } 117 118 // 连接service回调,对应stage模型connectCallback 119 connectCallback(element, remote) { 120 Logger.info(TAG, `ConnectLocalService ConnectDone element: ${element}, remote: ${remote}`) 121 rpcRemote = remote 122 if (rpcRemote === null) { 123 return 'ConnectLocalService not connected yet' 124 } 125 prompt.showToast({ 126 message: 'Connect service success' 127 }) 128 } 129 130 // 断开service回调,对应stage模型disconnectCallback 131 disconnectCallback(element) { 132 Logger.info(TAG, `ConnectLocalService DisconnectDone element: ${element}`) 133 } 134 135 // 连接service失败的回调,对应stage模型failedCallback 136 failedCallback(code) { 137 Logger.info(TAG, `ConnectLocalService Failed errCode: ${code}`) 138 prompt.showToast({ 139 message: `ConnectLocalService Failed: ${code}` 140 }) 141 } 142 143 // 启动service,对应Stage模型中startServiceAbility 144 async startServiceAbility(parameter) { 145 Logger.info(TAG, 'startAbility start') 146 let startNum = await featureAbility.startAbility(parameter) 147 Logger.info(TAG, `startAbility end ${startNum}`) 148 } 149 150 // 连接service,对应stage模型connectService 151 connectService() { 152 Logger.info(TAG, `CconnectService begin`) 153 154 // 对应stage模型Options 155 let ConnectOptions = { 156 onConnect: this.connectCallback, 157 onDisconnect: this.disconnectCallback, 158 onFailed: this.failedCallback 159 } 160 connection = featureAbility.connectAbility( 161 { 162 deviceId: localDeviceId, 163 bundleName: BUNDLE_NAME, 164 abilityName: SERVICE_ABILITY_NAME 165 }, 166 ConnectOptions 167 ) 168 } 169 170 // 断开service连接,对应stage模型disconnectService 171 disconnectService() { 172 Logger.info(TAG, `DisconnectService begin`) 173 rpcRemote = null 174 if (connection === -1) { 175 prompt.showToast({ 176 message: 'DisconnectService not connected yet' 177 }) 178 return 179 } 180 featureAbility.disconnectAbility(connection) 181 connection = -1 182 prompt.showToast({ 183 message: 'DisconnectService disconnect done' 184 }) 185 } 186} 187 188export default new FeatureAbilityHelper()