• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2023 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 common from '@ohos.app.ability.common';
18import Want from '@ohos.app.ability.Want';
19import promptAction from '@ohos.promptAction';
20import rpc from '@ohos.rpc';
21import hilog from '@ohos.hilog';
22
23const TAG: string = 'PageServiceAbility';
24const domain: number = 0xFF00;
25
26@Entry
27@Component
28struct PageServiceAbility {
29  async startServiceAbility(): Promise<void> {
30    try {
31      hilog.info(domain, TAG, 'Begin to start ability');
32      let want: Want = {
33        bundleName: 'com.samples.famodelabilitydevelop',
34        abilityName: 'com.samples.famodelabilitydevelop.ServiceAbility'
35      };
36      await featureAbility.startAbility({ want });
37      promptAction.showToast({
38        message: $r('app.string.start_service_success_toast')
39      });
40      hilog.info(domain, TAG, `Start ability succeed`);
41    } catch (error) {
42      hilog.error(domain, TAG, 'Start ability failed with ' + error);
43    }
44  }
45
46  build() {
47    Column() {
48      Row() {
49        Flex({ justifyContent: FlexAlign.Start, alignContent: FlexAlign.Center }) {
50          Text($r('app.string.serviceAbility_button'))
51            .fontSize(24)
52            .fontWeight(FontWeight.Bold)
53            .textAlign(TextAlign.Start)
54            .margin({ top: 12, bottom: 11, right: 24, left: 24 })
55        }
56      }
57      .width('100%')
58      .height(56)
59      .justifyContent(FlexAlign.Start)
60      .backgroundColor($r('app.color.backGrounding'))
61
62
63      List({ initialIndex: 0 }) {
64        ListItem() {
65          Row() {
66            Row() {
67              Text($r('app.string.create_service_button'))
68                .textAlign(TextAlign.Start)
69                .fontWeight(FontWeight.Medium)
70                .margin({ top: 13, bottom: 13, left: 0, right: 8 })
71                .fontSize(16)
72                .width(232)
73                .height(22)
74                .fontColor($r('app.color.text_color'))
75            }
76            .height(48)
77            .width('100%')
78            .borderRadius(24)
79            .margin({ top: 4, bottom: 4, left: 12, right: 84 })
80          }
81          .onClick(() => {
82            this.startServiceAbility();
83          })
84        }
85        .height(56)
86        .backgroundColor($r('app.color.start_window_background'))
87        .borderRadius(24)
88        .margin({ top: 8, right: 12, left: 12 })
89
90        ListItem() {
91          Row() {
92            Row() {
93              Text($r('app.string.connect_service_button'))
94                .textAlign(TextAlign.Start)
95                .fontWeight(FontWeight.Medium)
96                .margin({ top: 13, bottom: 13, left: 0, right: 8 })
97                .fontSize(16)
98                .width(232)
99                .height(22)
100                .fontColor($r('app.color.text_color'))
101            }
102            .height(48)
103            .width('100%')
104            .borderRadius(24)
105            .margin({ top: 4, bottom: 4, left: 12, right: 84 })
106          }
107          .onClick(() => {
108            let option: common.ConnectOptions = {
109              onConnect: (element, proxy) => {
110                hilog.info(domain, TAG, `onConnectLocalService onConnectDone element:` + JSON.stringify(element));
111                if (proxy === null) {
112                  promptAction.showToast({
113                    message: $r('app.string.connect_service_failed_toast')
114                  });
115                  return;
116                }
117                let data = rpc.MessageParcel.create();
118                let reply = rpc.MessageParcel.create();
119                let option = new rpc.MessageOption();
120                data.writeInterfaceToken('connect.test.token');
121                proxy.sendRequest(0, data, reply, option);
122                promptAction.showToast({
123                  message: $r('app.string.connect_service_success_toast')
124                });
125              },
126              onDisconnect: (element) => {
127                hilog.info(domain, TAG, `onConnectLocalService onDisconnectDone element:${element}`);
128                promptAction.showToast({
129                  message: $r('app.string.disconnect_service_success_toast')
130                });
131              },
132              onFailed: (code) => {
133                hilog.info(domain, TAG, `onConnectLocalService onFailed errCode:${code}`);
134                promptAction.showToast({
135                  message: $r('app.string.connect_service_failed_toast')
136                });
137              }
138            };
139
140            let request: Want = {
141              bundleName: 'com.samples.famodelabilitydevelop',
142              abilityName: 'com.samples.famodelabilitydevelop.ServiceAbility',
143            };
144            let connId = featureAbility.connectAbility(request, option);
145            hilog.info(domain, TAG, `onConnectLocalService onFailed errCode:${connId}`);
146          })
147        }
148        .height(56)
149        .backgroundColor($r('app.color.start_window_background'))
150        .borderRadius(24)
151        .margin({ top: 12, right: 12, left: 12 })
152
153        ListItem() {
154          Row() {
155            Row() {
156              Text($r('app.string.connect_wrong_service_button'))
157                .textAlign(TextAlign.Start)
158                .fontWeight(FontWeight.Medium)
159                .margin({ top: 13, bottom: 13, left: 0, right: 8 })
160                .fontSize(16)
161                .width(232)
162                .height(22)
163                .fontColor($r('app.color.text_color'))
164            }
165            .height(48)
166            .width('100%')
167            .borderRadius(24)
168            .margin({ top: 4, bottom: 4, left: 12, right: 84 })
169          }
170          .onClick(() => {
171            let option: common.ConnectOptions = {
172              onConnect: (element, proxy) => {
173                hilog.info(domain, TAG, `onConnectLocalService onConnectDone element:` + JSON.stringify(element));
174                if (proxy === null) {
175                  promptAction.showToast({
176                    message: $r('app.string.connect_service_failed_toast')
177                  });
178                  return;
179                }
180                let data = rpc.MessageParcel.create();
181                let reply = rpc.MessageParcel.create();
182                let option = new rpc.MessageOption();
183                data.writeInterfaceToken('connect.test.token');
184                proxy.sendRequest(0, data, reply, option);
185                promptAction.showToast({
186                  message: $r('app.string.connect_service_success_toast')
187                });
188              },
189              onDisconnect: (element) => {
190                hilog.info(domain, TAG, `onConnectLocalService onDisconnectDone element:${element}`)
191                promptAction.showToast({
192                  message: $r('app.string.disconnect_service_success_toast')
193                });
194              },
195              onFailed: (code) => {
196                hilog.info(domain, TAG, `onConnectLocalService onFailed errCode:${code}`);
197                promptAction.showToast({
198                  message: $r('app.string.connect_service_failed_toast')
199                });
200              }
201            };
202
203            let request: Want = {
204              bundleName: 'com.samples.famodelabilitydevelop',
205              abilityName: 'com.samples.famodelabilitydevelop.UnKnownServiceAbility',
206            };
207            let connId = featureAbility.connectAbility(request, option);
208            hilog.info(domain, TAG, `onConnectLocalService onFailed errCode:${connId}`);
209          })
210        }
211        .height(56)
212        .backgroundColor($r('app.color.start_window_background'))
213        .borderRadius(24)
214        .margin({ top: 12, right: 12, left: 12 })
215      }
216      .height('100%')
217      .backgroundColor($r('app.color.backGrounding'))
218    }
219    .width('100%')
220    .margin({ top: 8 })
221    .backgroundColor($r('app.color.backGrounding'))
222  }
223}