• 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 */
15import rpc from '@ohos.rpc'
16import Logger from '../model/Logger'
17
18const BUNDLE_NAME = "ohos.samples.eTSServiceExtAbility";
19const SERVICE_EXT_ABILITY_NAME = "ServiceExtAbility"
20const REQUEST_CODE = 1;
21const ERROR_CODE = -1;
22const SUCCESS_CODE = 1;
23
24export default class ServiceModel {
25  connection = -1;
26  firstLocalValue = 0;
27  secondLocalValue = 0;
28  remoteCallback = null;
29  context = null;
30  options = null;
31
32  constructor(context) {
33    this.context = context
34    this.options = {
35      outObj: this,
36      onConnect: function (elementName, proxy) {
37        Logger.log(`onConnect success`);
38        if (proxy === null) {
39          Logger.error(`onConnect proxy is null`);
40          return;
41        }
42        let option = new rpc.MessageOption();
43        let data = new rpc.MessageParcel();
44        let reply = new rpc.MessageParcel();
45        data.writeInt(this.outObj.firstLocalValue);
46        data.writeInt(this.outObj.secondLocalValue);
47        proxy.sendRequest(REQUEST_CODE, data, reply, option).then((result) => {
48          Logger.log(`sendRequest: ${result}`);
49          let msg = reply.readInt();
50          Logger.log(`sendRequest:msg: ${msg}`);
51          this.outObj.remoteCallback(SUCCESS_CODE, msg);
52        }).catch((e) => {
53          Logger.error(`sendRequest error: ${e}`);
54          this.outObj.remoteCallback(ERROR_CODE, ERROR_CODE);
55        });
56
57      },
58      onDisconnect: function () {
59        Logger.log(`onDisconnect`);
60      },
61      onFailed: function () {
62        Logger.log(`onFailed`);
63      }
64    }
65  }
66
67  startServiceExtAbility(callback) {
68    Logger.log(`startServiceExtAbility`);
69    let want = {
70      bundleName: BUNDLE_NAME,
71      abilityName: SERVICE_EXT_ABILITY_NAME
72    };
73    this.context.startAbility(want).then((data) => {
74      Logger.log(`startAbility success: ${JSON.stringify(data)}`);
75      callback(SUCCESS_CODE);
76    }).catch((error) => {
77      Logger.error(`startAbility failed: ${JSON.stringify(error)}`);
78      callback(ERROR_CODE);
79    })
80  }
81
82  connectServiceExtAbility(fir, sec, callback) {
83    Logger.log(`connectServiceExtAbility`);
84    this.firstLocalValue = fir;
85    this.secondLocalValue = sec;
86    this.remoteCallback = callback;
87    let want = {
88      bundleName: BUNDLE_NAME,
89      abilityName: SERVICE_EXT_ABILITY_NAME
90    };
91    this.connection = this.context.connectAbility(want, this.options);
92    Logger.log(`connectServiceExtAbility result:${this.connection}`);
93  }
94
95  disconnectServiceExtAbility(callback) {
96    Logger.log(`disconnectServiceExtAbility`);
97    this.context.disconnectAbility(this.connection).then((data) => {
98      Logger.log(`disconnectAbility success:${JSON.stringify(data)}`);
99      callback(SUCCESS_CODE);
100    }).catch((error) => {
101      Logger.error(`disconnectAbility failed:${JSON.stringify(error)}`);
102      callback(ERROR_CODE);
103    })
104  }
105}