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 { calc, isOperator } from '../../common/calculator.js' 17import app from '@system.app' 18import RemoteDeviceModel from '../../common/RemoteDeviceModel.js' 19import featureAbility from '@ohos.ability.featureAbility' 20import { KvStoreModel } from '../../common/kvstoreModel.js' 21import { logger } from '../../common/Logger' 22import window from '@ohos.window'; 23 24let pressedEqual = false 25let kvStoreModel = new KvStoreModel() 26let remoteDeviceModel = new RemoteDeviceModel() 27const TAG = 'Index' 28 29export default { 30 data: { 31 title: '', 32 expression: '', 33 result: '', 34 selectedIndex: 0, 35 isFA: false, 36 isPush: false, 37 isShow: false, 38 isDistributed: false, 39 deviceList: [], 40 }, 41 onInit() { 42 this.title = this.$t('strings.title') 43 this.grantPermission() 44 }, 45 onShow() { 46 this.$watch('expression', (value) => { 47 if (value !== '') { 48 logger.debug(TAG, `value ${value}`) 49 this.result = calc(value) 50 logger.debug(TAG, `result = ${this.result}`) 51 logger.info(TAG, `put key start`) 52 this.dataChange('expression', value) 53 } 54 }) 55 this.initKVManager() 56 featureAbility.getWant((error, want) => { 57 logger.debug(TAG, `featureAbility.getWant =${JSON.stringify(want.parameters)}`) 58 if (want.parameters.isFA === 'FA') { 59 this.isFA = true 60 this.isDistributed = true 61 } 62 }) 63 }, 64 grantPermission() { 65 logger.info(TAG, `grantPermission`) 66 let context = featureAbility.getContext() 67 context.requestPermissionsFromUser(['ohos.permission.DISTRIBUTED_DATASYNC'], 666, function (result) { 68 logger.debug(TAG, `grantPermission,requestPermissionsFromUser,result.requestCode=${result}`) 69 }) 70 }, 71 dataChange(key, value) { 72 logger.info(TAG, `dataChange isDistributed = ${this.isDistributed}`) 73 if (this.isDistributed && kvStoreModel != null) { 74 kvStoreModel.put(key, value) 75 } 76 }, 77 initKVManager() { 78 if (kvStoreModel !== null) { 79 kvStoreModel.setOnMessageReceivedListener('expression', (value) => { 80 logger.info(TAG, `data changed:${value}`) 81 if (value === 'exit') { 82 logger.info('Calc[CalcPage] app exit!') 83 app.terminate() 84 return 85 } 86 if (value === 'clear') { 87 logger.info(TAG, `data expression:clear`) 88 this.expression = '' 89 this.result = '' 90 return 91 } 92 if (value === 'equal') { 93 if (this.result !== '') { 94 logger.info(TAG, `data expression:equal`) 95 this.expression = this.result 96 this.result = '' 97 pressedEqual = true 98 } 99 return 100 } 101 this.expression = value 102 pressedEqual = false 103 logger.info(TAG, `data expression:${this.expression}`) 104 }) 105 } 106 }, 107 stopDataListener() { 108 logger.info(TAG, `stopDataListener`) 109 if (kvStoreModel === null || kvStoreModel === undefined) { 110 return 111 } 112 kvStoreModel.off() 113 }, 114 onDestroy() { 115 if (remoteDeviceModel === undefined) { 116 return 117 } 118 remoteDeviceModel.unregisterDeviceListCallback() 119 if (this.isDistributed && kvStoreModel != null) { 120 this.stopDataListener() 121 this.isDistributed = false 122 } 123 kvStoreModel = null 124 remoteDeviceModel = undefined 125 }, 126 showDialog() { 127 logger.info(TAG, `showDialog start`) 128 this.isShow = true 129 setTimeout(() => { 130 this.deviceList = [] 131 if (remoteDeviceModel === undefined) { 132 remoteDeviceModel = new RemoteDeviceModel() 133 } 134 logger.debug(TAG, `showdialog = ${typeof (this.$element('showDialog'))}`) 135 logger.debug(TAG, `registerDeviceListCallback on remote device updated, count=${remoteDeviceModel.deviceList.length}`) 136 remoteDeviceModel.registerDeviceListCallback(() => { 137 let list = [] 138 list.push({ 139 deviceId: '0', 140 deviceName: 'Local device', 141 deviceType: 0, 142 networkId: '', 143 checked: this.selectedIndex === 0 144 }) 145 let tempList = remoteDeviceModel.discoverList.length > 0 ? remoteDeviceModel.discoverList : remoteDeviceModel.deviceList 146 logger.info(TAG, `callback this.discoverList=${JSON.stringify(remoteDeviceModel.discoverList)}`) 147 logger.info(TAG, `callback this.deviceList=${JSON.stringify(remoteDeviceModel.deviceList)}`) 148 for (let i = 0; i < tempList.length; i++) { 149 logger.debug(`device ${i}/${tempList.length} deviceId=${tempList[i].deviceId} deviceName=${tempList[i].deviceName} deviceType=${tempList[i].deviceType}`) 150 list.push({ 151 deviceId: tempList[i].deviceId, 152 deviceName: tempList[i].deviceName, 153 deviceType: tempList[i].deviceType, 154 networkId: tempList[i].networkId, 155 checked: this.selectedIndex === (i + 1) 156 }) 157 } 158 this.deviceList = list 159 this.$element('showDialog').close() 160 this.$element('showDialog').show() 161 }) 162 }, 200) 163 }, 164 cancelDialog() { 165 this.$element('showDialog').close() 166 if (remoteDeviceModel === undefined) { 167 return 168 } 169 remoteDeviceModel.unregisterDeviceListCallback() 170 }, 171 172 selectDevice(item) { 173 let index = this.deviceList.indexOf(item) 174 logger.info(TAG, `select index:${index}`) 175 logger.info(TAG, `select selectedIndex:${this.selectedIndex}`) 176 if (index === this.selectedIndex) { 177 logger.info(TAG, `index === this.selectedIndex`) 178 return 179 } 180 this.selectedIndex = index 181 if (index === 0) { 182 logger.info(TAG, `stop ability`) 183 this.dataChange('expression', 'exit') 184 this.isDistributed = false 185 this.stopDataListener() 186 this.clearSelectState() 187 return 188 } 189 logger.info(TAG, `start ability ......`) 190 this.isDistributed = true 191 if (remoteDeviceModel === undefined || remoteDeviceModel.discoverList.length <= 0) { 192 logger.info(TAG, `continue device:${JSON.stringify(this.deviceList)}`) 193 this.startAbility(this.deviceList[index].networkId) 194 this.clearSelectState() 195 return 196 } 197 logger.info(TAG, `start ability1, needAuth`) 198 remoteDeviceModel.authenticateDevice(this.deviceList[index], () => { 199 logger.info(TAG, ` auth and online finished`) 200 this.startAbility(this.deviceList[index].networkId) 201 }) 202 this.clearSelectState() 203 logger.info(TAG, ` start ability end....`) 204 }, 205 clearSelectState() { 206 this.deviceList = [] 207 this.$element('showDialog').close() 208 }, 209 async startAbility(deviceId) { 210 logger.debug(TAG, ` startAbility deviceId: ${deviceId}`) 211 await featureAbility.startAbility({ 212 want: { 213 bundleName: 'ohos.samples.distributedcalc', 214 abilityName: 'ohos.samples.distributedcalc.MainAbility', 215 deviceId: deviceId, 216 parameters: { 217 isFA: 'FA' 218 } 219 } 220 }) 221 logger.info(TAG, ` start ability finished`) 222 this.dataChange('expression', this.expression) 223 logger.info(TAG, ` startAbility end`) 224 }, 225 handleClear() { 226 this.expression = '' 227 this.result = '' 228 logger.info(TAG, ` handleClear`) 229 this.dataChange('expression', 'clear') 230 }, 231 handleInput(value) { 232 logger.info(TAG, ` handle input value:${value}`) 233 this.isPush = false 234 if (isOperator(value)) { 235 this.isPressedEqual() 236 if (!this.expression && (value === '*' || value === '/')) { 237 return 238 } 239 this.expression += value 240 } else { 241 if (pressedEqual) { 242 pressedEqual = false 243 } 244 this.expression += value 245 } 246 }, 247 isPressedEqual() { 248 if (pressedEqual) { 249 pressedEqual = false 250 } else { 251 const size = this.expression.length 252 if (size) { 253 const last = this.expression.charAt(size - 1) 254 if (isOperator(last)) { 255 this.expression = this.expression.substring(0, this.expression.length - 1) 256 } 257 } 258 } 259 }, 260 handleBackspace() { 261 if (pressedEqual) { 262 this.expression = '' 263 this.result = '' 264 pressedEqual = false 265 logger.info(TAG, `handleBackspace1`) 266 this.dataChange('expression', 'clear') 267 } else { 268 this.isPush = false 269 this.expression = this.expression.substring(0, this.expression.length - 1) 270 if (!this.expression.length) { 271 this.result = '' 272 logger.info(TAG, ` handleBackspace2`) 273 this.dataChange('expression', 'clear') 274 } 275 } 276 }, 277 handleEqual() { 278 if (this.result !== '') { 279 this.isPush = true 280 this.expression = this.result 281 this.result = '' 282 pressedEqual = true 283 logger.info(TAG, ` handleEqual`) 284 this.dataChange('expression', 'equal') 285 } 286 }, 287 handleExist() { 288 logger.info(TAG, `handleExist`) 289 app.terminate() 290 } 291}