1
0

DistributedPage.ets 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import deviceManager from '@ohos.distributedDeviceManager';
  2. import common from '@ohos.app.ability.common';
  3. import { KvStoreModel } from '../model/KvStoreModel';
  4. import { BUNDLE_NAME, RemoteDeviceModel } from '../model/RemoteDeviceModel';
  5. import Want from '@ohos.app.ability.Want';
  6. import { DeviceDialog } from '../common/DeviceDialog';
  7. import router from '@ohos.router';
  8. import { class1 } from './Storage';
  9. const TAG: string = 'testTag'
  10. const DATA_CHANGE: string = 'dataChange'
  11. const DATA_CHANGE1: number = 0
  12. const EXIT: string = 'exit'
  13. @Entry
  14. @Component
  15. struct DistributedPage{
  16. // @StorageLink('deviceList') deviceList: Array<deviceManager.DeviceBasicInfo> = AppStorage.get('deviceList')!;
  17. //private selectedIndex: number | undefined = 0;
  18. // private onSelectedIndexChange: (selectedIndex: number | undefined) => void = () => {
  19. //}
  20. @State deviceDialogWidth: number = 0
  21. @State @Watch('dataChange') click: number = 0;
  22. @State click1: number = 0;
  23. @State @Watch('dataChange') message: string = 'Hello World';
  24. @State inputMsg: string = '';
  25. @State isDistributed: boolean = false
  26. @StorageLink('deviceList') deviceList: Array<deviceManager.DeviceBasicInfo> = []
  27. @State selectedIndex: number | undefined = -1
  28. private remoteDeviceModel: RemoteDeviceModel = new RemoteDeviceModel()
  29. private kvStoreModel: KvStoreModel = new KvStoreModel()
  30. private dialogController: CustomDialogController | null = null
  31. onSelectedIndexChange = async (index: number | undefined) => {
  32. console.info(TAG, `selectedIndexChange`)
  33. this.selectedIndex = index
  34. this.selectDevice()
  35. }
  36. async aboutToAppear() {
  37. this.showDeviceDialog()
  38. }
  39. // aboutToAppear(){
  40. // this.showDeviceDialog()
  41. // }
  42. aboutToDisappear() {
  43. this.kvStoreModel.deleteKvStore()
  44. }
  45. startAbilityCallBack = (key: string) => {
  46. if (DATA_CHANGE === key) {
  47. this.kvStoreModel.put(DATA_CHANGE, this.message)
  48. }
  49. if (EXIT === key) {
  50. this.kvStoreModel.put(DATA_CHANGE, EXIT)
  51. }
  52. }
  53. selectDevice() {
  54. this.isDistributed = true
  55. if (this.selectedIndex !== undefined && (this.remoteDeviceModel === null || this.remoteDeviceModel.discoverList.length <= 0)) {
  56. console.info(TAG, `continue unauthed device: ${JSON.stringify(this.deviceList)}`)
  57. this.startAbility(this.deviceList[this.selectedIndex].networkId as string)
  58. this.clearSelectState()
  59. return
  60. }
  61. if (this.selectedIndex !== undefined) {
  62. this.remoteDeviceModel.authenticateDevice(this.deviceList[this.selectedIndex], () => {
  63. if (this.remoteDeviceModel !== null && this.remoteDeviceModel.deviceList !== null && this.selectedIndex !== undefined) {
  64. for (let i = 0; i < this.remoteDeviceModel.deviceList!.length; i++) {
  65. if (this.remoteDeviceModel.deviceList![i].deviceName === this.deviceList[this.selectedIndex].deviceName) {
  66. this.startAbility(this.remoteDeviceModel.deviceList![i].networkId);
  67. }
  68. }
  69. }
  70. })
  71. }
  72. this.clearSelectState()
  73. }
  74. clearSelectState() {
  75. this.deviceList = []
  76. if (this.dialogController !== null) {
  77. this.dialogController.close()
  78. }
  79. console.info(TAG, `cancelDialog`)
  80. if (this.remoteDeviceModel === undefined) {
  81. return
  82. }
  83. this.remoteDeviceModel.unregisterDeviceListCallback()
  84. }
  85. async startAbility(deviceId: string | undefined) {
  86. console.debug(TAG, `startAbility deviceId: ${deviceId}`)
  87. let context = getContext(this) as common.UIAbilityContext
  88. let choicePages:class1={}
  89. // console.log("获取当前页面"+choicePages.choicePages?.toString())
  90. let want: Want = {
  91. bundleName: BUNDLE_NAME,
  92. abilityName: 'EntryAbility',
  93. // abilityName: choicePages.choicePages===2?'':'firstAbility',
  94. deviceId: deviceId,
  95. parameters: {
  96. isRemote: 'isRemote'
  97. }
  98. }
  99. context.startAbility(want).then((data) => {
  100. console.info(TAG, `start ability finished: ${JSON.stringify(data)}`)
  101. this.startAbilityCallBack(DATA_CHANGE)
  102. })
  103. }
  104. dataChange() {
  105. console.info(TAG, `dataChange, message = ${this.message},`)
  106. this.kvStoreModel.put(DATA_CHANGE1.toString(), this.click.toString())
  107. this.kvStoreModel.put(DATA_CHANGE, this.message,)
  108. }
  109. showDeviceDialog() {
  110. this.deviceList = []
  111. // 注册监听回调,发现设备或查找到已认证设备会弹窗显示
  112. this.remoteDeviceModel.registerDeviceListCallback(() => {
  113. this.deviceList = []
  114. let deviceTempList = this.remoteDeviceModel.discoverList.length > 0 ? this.remoteDeviceModel.discoverList : this.remoteDeviceModel.deviceList;
  115. if (deviceTempList !== null) {
  116. for (let i = 0; i < deviceTempList!.length; i++) {
  117. console.debug(TAG, `device ${i}/${deviceTempList!.length} deviceId= ${deviceTempList![i].deviceId},
  118. deviceName= ${deviceTempList![i].deviceName}, deviceType= ${deviceTempList![i].deviceType}`);
  119. if (deviceTempList !== null) {
  120. this.deviceList.push({
  121. deviceId: deviceTempList![i].deviceId,
  122. deviceName: deviceTempList![i].deviceName,
  123. deviceType: deviceTempList![i].deviceType,
  124. networkId: deviceTempList![i].networkId,
  125. })
  126. AppStorage.set('deviceList', this.deviceList)
  127. }
  128. }
  129. }
  130. })
  131. if (this.dialogController === null) {
  132. this.dialogController = new CustomDialogController({
  133. builder: DeviceDialog({
  134. selectedIndex: this.selectedIndex,
  135. onSelectedIndexChange: this.onSelectedIndexChange
  136. }),
  137. cancel: () => {
  138. this.clearSelectState()
  139. },
  140. autoCancel: true,
  141. alignment: DialogAlignment.Center,
  142. customStyle: false
  143. })
  144. }
  145. if (this.dialogController !== null) {
  146. this.dialogController.open()
  147. }
  148. }
  149. build(){
  150. Column(){
  151. }.width('100%')
  152. .backgroundColor('#ccfdfcfc')
  153. .height('100%')
  154. .justifyContent(FlexAlign.Center)
  155. .padding({ left: 18, right: 32 })
  156. .margin({ bottom: 15 })
  157. .onAreaChange((oldArea: Area, newArea: Area) => {
  158. this.deviceDialogWidth = (newArea.width > newArea.height ? newArea.height : newArea.width) as number * 0.1 //percentage
  159. })
  160. .onClick(()=>{
  161. router.pushUrl({
  162. url:'pages/Index'
  163. })
  164. })
  165. }
  166. }