• 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 deviceManager from '@ohos.distributedDeviceManager';
16import Logger from '../utils/Logger'
17import { DeviceDialog } from '../common/DeviceDialog'
18import { RemoteDeviceModel, BUNDLE_NAME } from '../recorder/RemoteDeviceModel'
19import common from '@ohos.app.ability.common'
20import Want from '@ohos.app.ability.Want';
21import { router } from '@kit.ArkUI';
22
23const TAG: string = 'Sample_TitleBarComponent'
24const DATA_CHANGE: string = 'dataChange'
25const EXIT: string = 'exit'
26const DEVICE_DISCOVERY_RANGE: number = 1000
27
28@Component
29export struct TitleBarComponent {
30  @Prop isLand: boolean | null = null
31  @State selectedIndex: number | undefined = 0
32  @StorageLink('deviceList') deviceList: Array<deviceManager.DeviceBasicInfo> = []
33  private isShow: boolean = false
34  private startAbilityCallBack: (key: string) => void = () => {
35  }
36  private dialogController: CustomDialogController | null = null
37  private remoteDeviceModel: RemoteDeviceModel = new RemoteDeviceModel()
38  onSelectedIndexChange =  (index: number | undefined) => {
39    Logger.info(TAG, `selectedIndexChange`)
40    this.selectedIndex = index
41    this.dialogController?.close()
42    /*if (this.selectedIndex === 0) {
43      Logger.info(TAG, `stop ability`)
44      this.deviceList = []
45      if (this.dialogController !== null) {
46        this.dialogController.close()
47      }
48      return
49    }*/
50    this.selectDevice()
51  }
52
53  aboutToAppear() {
54    AppStorage.setOrCreate('deviceList', this.deviceList)
55  }
56
57  clearSelectState() {
58    this.deviceList = []
59    if (this.dialogController !== null) {
60      this.dialogController.close()
61    }
62    Logger.info(TAG, `cancelDialog`)
63    if (this.remoteDeviceModel === undefined) {
64      return
65    }
66    this.remoteDeviceModel.unregisterDeviceListCallback()
67  }
68
69  selectDevice() {
70    if (this.selectedIndex !== undefined && (this.remoteDeviceModel === null || this.remoteDeviceModel.discoverList.length <= 0)) {
71      Logger.info(TAG, `continue unauthed device: ${JSON.stringify(this.deviceList)}`)
72      this.clearSelectState()
73      return
74    }
75    Logger.info(TAG, `needAuth:`)
76    if (this.selectedIndex !== undefined) {
77      console.log(TAG, ",", this.deviceList.length, "," , this.deviceList[this.selectedIndex].deviceName);
78
79      this.remoteDeviceModel.authenticateDevice(this.deviceList[this.selectedIndex], () => {
80        Logger.info(TAG, `auth and online finished`);
81        if (this.remoteDeviceModel !== null && this.remoteDeviceModel.deviceList !== null && this.selectedIndex !== undefined) {
82          for (let i = 0; i < this.remoteDeviceModel.deviceList!.length; i++) {
83            if (this.remoteDeviceModel.deviceList![i].deviceName === this.deviceList[this.selectedIndex].deviceName) {
84            }
85          }
86        }
87      })
88    }
89    this.clearSelectState()
90  }
91
92  showDiainfo() {
93    this.deviceList = []
94    // 注册监听回调,发现设备或查找到已认证设备会弹窗显示
95    this.remoteDeviceModel.registerDeviceListCallback(() => {
96      this.deviceList = []
97      let context: common.UIAbilityContext | undefined = AppStorage.get('UIAbilityContext')
98      if (context !== undefined) {
99        this.deviceList.push({
100          deviceId: '0',
101          deviceName: context.resourceManager.getStringSync($r('app.string.localhost').id),
102          deviceType: '0',
103          networkId: ''
104        })
105      }
106      let deviceTempList = this.remoteDeviceModel.discoverList.length > 0 ? this.remoteDeviceModel.discoverList : this.remoteDeviceModel.deviceList;
107      if (deviceTempList !== null) {
108        for (let i = 0; i < deviceTempList!.length; i++) {
109          Logger.info(TAG, `found device ${i}/${deviceTempList!.length} deviceId= ${deviceTempList![i].deviceId}, deviceName= ${deviceTempList![i].deviceName}, deviceType= ${deviceTempList![i].deviceType}`);
110          if (deviceTempList !== null) {
111            this.deviceList.push({
112              deviceId: deviceTempList![i].deviceId,
113              deviceName: deviceTempList![i].deviceName,
114              deviceType: deviceTempList![i].deviceType,
115              networkId: deviceTempList![i].networkId,
116            })
117            AppStorage.set('deviceList', this.deviceList)
118          }
119        }
120      }
121    })
122    if (this.dialogController === null) {
123      this.dialogController = new CustomDialogController({
124        builder: DeviceDialog({
125          selectedIndex: this.selectedIndex,
126          onSelectedIndexChange: this.onSelectedIndexChange
127        }),
128        cancel: () => {
129          this.clearSelectState()
130        },
131        autoCancel: true,
132        alignment: this.isLand ? DialogAlignment.Center : DialogAlignment.Bottom,
133        customStyle: false
134      })
135    }
136    if (this.dialogController !== null) {
137      this.dialogController.open()
138    }
139  }
140
141  build() {
142    Row() {
143      Image($r('app.media.ic_back'))
144        .height('60%')
145        .margin({ left: '5%' })
146        .width('50px')
147        .objectFit(ImageFit.Contain)
148        .onClick(async () => {
149          //let context = getContext(this) as common.UIAbilityContext
150          //context.terminateSelf()
151          router.back();
152        })
153      Text("back")
154        .height('60%')
155        .fontSize('28px')
156        .margin({ left: 12 })
157      Blank().layoutWeight(1)
158      if (!this.isShow) {
159        Image($r("app.media.ic_hop_normal1"))
160          .id('selectDevice')
161          .margin({ right: 32 })
162          .width('9%')
163          .margin({ right: '12%' })
164          .objectFit(ImageFit.Contain)
165          .onClick(() => {
166            this.showDiainfo()
167            //router.back();
168          })
169      }
170    }
171    .width('100%')
172    .height(this.isLand ? '10%' : '6%')
173    .constraintSize({ minHeight: 50 })
174    .alignItems(VerticalAlign.Center)
175  }
176}