• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 particleAbility from '@ohos.ability.particleAbility'
17import rpc from '@ohos.rpc'
18import prompt from '@ohos.prompt'
19import Logger from '../util/Logger'
20import { BUNDLE_NAME, SERVICE_ABILITY_NAME, TAST_ABILITY_NAME } from '../model/DaHelperConst'
21
22const TAG: string = 'ParticleAbilityHelper'
23
24class ParticleAbilityHelper {
25  private connId: number = -1
26
27  // 启动指定的particleAbility,Stage模型ServiceExtContextController的startAbility
28  startAbility() {
29    particleAbility.startAbility({
30      want: {
31        bundleName: BUNDLE_NAME,
32        abilityName: TAST_ABILITY_NAME
33      }
34    }, () => {
35      Logger.info(TAG, 'startAbility success')
36      prompt.showToast({
37        message: 'particleAbility startAbility success'
38      })
39    })
40  }
41
42  // 终止particleAbility,Stage模型ServiceExtContextController的terminateSelf
43  terminateSelf() {
44    particleAbility.terminateSelf(() => {
45      Logger.info(TAG, 'terminateSelf success')
46      prompt.showToast({
47        message: 'particleAbility terminateSelf success'
48      })
49    })
50  }
51
52  // 获取dataAbilityHelper
53  acquireDataAbilityHelper() {
54    particleAbility.acquireDataAbilityHelper('')
55    Logger.info(TAG, 'terminateSelf success')
56    prompt.showToast({
57      message: 'particleAbility acquireDataAbilityHelper success'
58    })
59  }
60
61  onConnectCallback(element, remote) {
62    Logger.info(TAG, `ConnectAbility onConnect remote is proxy: ${remote instanceof rpc.RemoteProxy}`)
63    prompt.showToast({
64      message: 'particleAbility connectAbility success'
65    })
66  }
67
68  onDisconnectCallback(element) {
69    Logger.info(TAG, `ConnectAbility onDisconnect element.deviceId : ${element.deviceId}`)
70    prompt.showToast({
71      message: 'particleAbility disconnectAbility success'
72    })
73  }
74
75  onFailedCallback(code) {
76    Logger.info(TAG, `particleAbilityTest ConnectAbility onFailed errCode : ${code}`)
77    prompt.showToast({
78      message: `particleAbility Failed ${code}`
79    })
80  }
81
82  // 将当前ability连接到指定ServiceAbility,Stage模型ServiceExtContextController的connectAbility
83  connectAbility() {
84    this.connId = particleAbility.connectAbility({
85      bundleName: BUNDLE_NAME,
86      abilityName: SERVICE_ABILITY_NAME,
87    },
88      {
89        onConnect: this.onConnectCallback,
90        onDisconnect: this.onDisconnectCallback,
91        onFailed: this.onFailedCallback,
92      })
93  }
94
95  // 将功能与服务功能断开连接,Stage模型ServiceExtContextController的disconnectAbility
96  disconnectAbility() {
97    particleAbility.disconnectAbility(this.connId, () => {
98      Logger.info(TAG, 'disconnectAbility success')
99    })
100  }
101}
102
103export default new ParticleAbilityHelper()
104