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 deviceManager from '@ohos.distributedHardware.deviceManager' 17import DeviceDialog from '../component/DeviceDialog' 18import Logger from '../data/Logger' 19import RemoteDeviceModel from '../data/RemoteDeviceModel' 20import { distributedDataModel } from '../data/DistributedDataModel' 21import { dmsConst } from '../data/DmsConst' 22 23const TAG: string = 'TitleBar' 24 25@Component 26export default struct TitleBar { 27 @State selectedIndex: number = 0 28 @Link devices: Array<deviceManager.DeviceInfo> 29 @Link dialogShow: boolean 30 @Link remoteDeviceModel: RemoteDeviceModel 31 private customDialogController: CustomDialogController = null 32 private isStart: boolean = false 33 34 build() { 35 Row() { 36 Image($r('app.media.back')) 37 .height(40) 38 .width(40) 39 .margin({ left: 10 }) 40 .onClick(() => { 41 Logger.info(TAG, `onClick app exit`) 42 distributedDataModel.distributedObject.setSessionId() 43 let context = getContext(this) as any 44 context.terminateSelf() 45 } 46 ) 47 Text($r('app.string.MainAbility_label')) 48 .width('68%') 49 .fontColor(Color.White) 50 .fontSize(25) 51 .margin({ left: 30 }) 52 53 if (!this.isStart) { 54 Image($r('app.media.ic_hop')) 55 .key('flowBtn') 56 .width(40) 57 .height(40) 58 .margin({ left: 15 }) 59 .objectFit(ImageFit.Fill) 60 .backgroundColor('#0C9FFC') 61 .onClick(() => { 62 this.showDialogInfo() 63 }) 64 } 65 } 66 .width('100%') 67 .height(60) 68 .backgroundColor('#0C9FFC') 69 .alignItems(VerticalAlign.Center) 70 } 71 72 clearSelectState() { 73 this.dialogShow = false 74 this.customDialogController.close() 75 } 76 77 selectDevice() { 78 Logger.info(TAG, `selectDevice start......`) 79 if (this.remoteDeviceModel === null || this.remoteDeviceModel.discovers.length <= 0) { 80 Logger.info(TAG, `continue unauthed device: ${JSON.stringify(this.devices)}`) 81 this.clearSelectState() 82 return 83 } 84 Logger.info(TAG, 'selectDevice, needAuth:') 85 this.remoteDeviceModel.authenticateDevice(this.devices[this.selectedIndex], () => { 86 Logger.info(TAG, 'selectDevice auth and online finished') 87 }) 88 Logger.info(TAG, 'selectDevice end......') 89 this.clearSelectState() 90 } 91 92 onSelectedIndexChange = (index: number) => { 93 Logger.info(TAG, 'selectedIndexChange') 94 this.selectedIndex = index 95 if (this.selectedIndex === dmsConst.ZERO) { 96 Logger.info(TAG, 'stop ability') 97 this.devices = [] 98 this.customDialogController.close() 99 this.dialogShow = false 100 return 101 } 102 this.selectDevice() 103 } 104 105 showDialogInfo() { 106 this.dialogShow = true 107 this.devices = [] 108 this.remoteDeviceModel.registerDeviceCallback(() => { 109 Logger.info(TAG, 'registerDeviceCallback, callback entered') 110 this.devices.push({ 111 deviceId: '0', 112 deviceName: '本机(结束协同)', 113 deviceType: dmsConst.ZERO, 114 networkId: '' 115 }) 116 Logger.info(TAG, `registerDeviceCallback discovers: ${JSON.stringify(this.remoteDeviceModel.discovers)}`) 117 Logger.info(TAG, `registerDeviceCallback devices: ${JSON.stringify(this.remoteDeviceModel.devices)}`) 118 this.remoteDeviceModel.devices.map((item) => { 119 this.devices.push(item) 120 }) 121 this.remoteDeviceModel.discovers.map((item) => { 122 this.devices.push(item) 123 }) 124 125 Logger.info(TAG, `registerDeviceCallback devices--: ${JSON.stringify(this.devices)}`) 126 }) 127 let alignments = DialogAlignment.Bottom 128 if (this.customDialogController !== null) { 129 this.customDialogController.close() 130 this.customDialogController = null 131 } 132 this.customDialogController = new CustomDialogController({ 133 builder: DeviceDialog({ 134 devices: this.devices, 135 selectedIndex: this.selectedIndex, 136 onSelectedIndexChange: this.onSelectedIndexChange 137 }), 138 autoCancel: true, 139 alignment: alignments 140 }) 141 this.customDialogController.open() 142 } 143} 144