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