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 ServiceModel from '../model/ServiceModel' 16 17const ERROR_CODE = -1; 18const SUCCESS_CODE = 1; 19 20@Entry 21@Component 22struct Index { 23 fontColor: string = '#000000' 24 private context:any = getContext(this) 25 private serviceModel = new ServiceModel(this.context) 26 @State message: Resource = $r("app.string.entry_desc") 27 @State localFir: number = 0 28 @State localSec: number = 0 29 @State remoteCallbackInfo: number = 0 30 @State connectState: Resource = $r("app.string.state_unconnected") 31 32 build() { 33 Row() { 34 Column() { 35 Text(this.message) 36 .fontSize(30) 37 .fontWeight(FontWeight.Bold) 38 .margin(20) 39 Row() { 40 Text($r("app.string.tips_current_state")) 41 .fontSize(25) 42 .fontWeight(FontWeight.Bold) 43 Text(`:`) 44 .fontSize(25) 45 .fontWeight(FontWeight.Bold) 46 Text(this.connectState) 47 .key('connectState') 48 .fontSize(25) 49 .fontWeight(FontWeight.Bold) 50 } 51 .margin({ bottom: 30 }) 52 53 Row() { 54 Text($r("app.string.tips_step")) 55 .fontSize(20) 56 .fontWeight(FontWeight.Bold) 57 Text(`1:`) 58 .fontSize(20) 59 .fontWeight(FontWeight.Bold) 60 Text($r("app.string.opt_start_ability")) 61 .fontSize(20) 62 .fontWeight(FontWeight.Bold) 63 } 64 65 Text($r("app.string.tips_start")) 66 .fontSize(15) 67 Button() { 68 Text($r("app.string.opt_start_ability")) 69 .fontSize(20) 70 .fontWeight(FontWeight.Bold) 71 } 72 .key('startService') 73 .type(ButtonType.Capsule) 74 .margin({ 75 top: 5, 76 bottom: 20 77 }) 78 .backgroundColor('#0D9FFB') 79 .width('60%') 80 .height('5%') 81 .onClick(() => { 82 let that = this; 83 this.serviceModel.startServiceExtAbility(function (code) { 84 if (code === SUCCESS_CODE) { 85 that.connectState = $r("app.string.state_start_ability"); 86 } else { 87 that.connectState = $r("app.string.state_start_ability_error"); 88 } 89 }) 90 }) 91 92 Row() { 93 Text($r("app.string.tips_step")) 94 .fontSize(20) 95 .fontWeight(FontWeight.Bold) 96 Text(`2:`) 97 .fontSize(20) 98 .fontWeight(FontWeight.Bold) 99 Text($r("app.string.tips_enter_two_number")) 100 .fontSize(20) 101 .fontWeight(FontWeight.Bold) 102 } 103 104 Text($r("app.string.tips_connect")) 105 .fontSize(15) 106 .margin({ bottom: 5 }) 107 TextInput({ placeholder: $r("app.string.tips_connect_first_value") }) 108 .key('inputFirstNum') 109 .width('50%') 110 .type(InputType.Number) 111 .margin({ bottom: 5 }) 112 .onChange((value) => { 113 this.localFir = Number(value); 114 }) 115 TextInput({ placeholder: $r("app.string.tips_connect_second_value") }) 116 .key('inputSecondNum') 117 .width('50%') 118 .type(InputType.Number) 119 .margin({ bottom: 20 }) 120 .onChange((value) => { 121 this.localSec = Number(value); 122 }) 123 Row() { 124 Text($r("app.string.tips_step")) 125 .fontSize(20) 126 .fontWeight(FontWeight.Bold) 127 Text(`3:`) 128 .fontSize(20) 129 .fontWeight(FontWeight.Bold) 130 Text($r("app.string.opt_connect_ability")) 131 .fontSize(20) 132 .fontWeight(FontWeight.Bold) 133 } 134 135 Button() { 136 Text($r("app.string.opt_connect_ability")) 137 .fontSize(20) 138 .fontWeight(FontWeight.Bold) 139 } 140 .key('connectService') 141 .type(ButtonType.Capsule) 142 .margin({ 143 top: 5, 144 bottom: 5 145 }) 146 .backgroundColor('#0D9FFB') 147 .width('60%') 148 .height('5%') 149 .onClick(() => { 150 let that = this; 151 this.serviceModel.connectServiceExtAbility(this.localFir, this.localSec, function (code, data) { 152 if (code === SUCCESS_CODE) { 153 that.connectState = $r("app.string.state_connect_ability"); 154 that.remoteCallbackInfo = data; 155 } else { 156 that.connectState = $r("app.string.state_connect_ability_error"); 157 that.remoteCallbackInfo = data; 158 } 159 }) 160 }) 161 162 Text($r("app.string.tips_connect_end")) 163 .fontSize(15) 164 .margin({ bottom: 5 }) 165 166 Row() { 167 Text($r("app.string.tips_return_result")) 168 .fontSize(20) 169 .fontWeight(FontWeight.Bold) 170 Text(`: ${this.remoteCallbackInfo}`) 171 .fontSize(20) 172 .fontWeight(FontWeight.Bold) 173 } 174 .margin({ bottom: 20 }) 175 176 Row() { 177 Text($r("app.string.tips_step")) 178 .fontSize(20) 179 .fontWeight(FontWeight.Bold) 180 Text(`4:`) 181 .fontSize(20) 182 .fontWeight(FontWeight.Bold) 183 Text($r("app.string.opt_disconnect_ability")) 184 .fontSize(20) 185 .fontWeight(FontWeight.Bold) 186 } 187 188 Button() { 189 Text($r("app.string.opt_disconnect_ability")) 190 .fontSize(20) 191 .fontWeight(FontWeight.Bold) 192 } 193 .key('disconnectService') 194 .type(ButtonType.Capsule) 195 .margin({ 196 top: 5 197 }) 198 .backgroundColor('#0D9FFB') 199 .width('60%') 200 .height('5%') 201 .onClick(() => { 202 let that = this; 203 this.serviceModel.disconnectServiceExtAbility(function (code) { 204 if (code === SUCCESS_CODE) { 205 that.connectState = $r("app.string.state_disconnect_ability"); 206 } else { 207 that.connectState = $r("app.string.state_disconnect_ability_error"); 208 } 209 }) 210 }) 211 } 212 .width('100%') 213 } 214 .height('100%') 215 } 216}