diff --git a/common/DistributeCalc/build.gradle b/common/DistributeCalc/build.gradle new file mode 100644 index 0000000000000000000000000000000000000000..3c7d48fcaa641d9156caad6f91773a5cf92c4685 --- /dev/null +++ b/common/DistributeCalc/build.gradle @@ -0,0 +1,37 @@ +// Top-level build file where you can add configuration options common to all sub-projects/modules. +apply plugin: 'com.huawei.ohos.app' + +ohos { + compileSdkVersion 6 + defaultConfig { + compatibleSdkVersion 6 + } +} + +buildscript { + repositories { + maven { + url 'https://repo.huaweicloud.com/repository/maven/' + } + maven { + url 'https://developer.huawei.com/repo/' + } + jcenter() + } + dependencies { + classpath 'com.huawei.ohos:hap:2.4.5.0' + classpath 'com.huawei.ohos:decctest:1.2.4.1' + } +} + +allprojects { + repositories { + maven { + url 'https://repo.huaweicloud.com/repository/maven/' + } + maven { + url 'https://developer.huawei.com/repo/' + } + jcenter() + } +} diff --git a/common/DistributeCalc/entry/build.gradle b/common/DistributeCalc/entry/build.gradle new file mode 100644 index 0000000000000000000000000000000000000000..5e47037754d9c59dbd3e393dfb2f6c58e2fc729a --- /dev/null +++ b/common/DistributeCalc/entry/build.gradle @@ -0,0 +1,22 @@ +apply plugin: 'com.huawei.ohos.hap' +apply plugin: 'com.huawei.ohos.decctest' +ohos { + compileSdkVersion 5 + defaultConfig { + compatibleSdkVersion 5 + } + buildTypes { + release { + proguardOpt { + proguardEnabled false + rulesFiles 'proguard-rules.pro' + } + } + } + +} + +dependencies {} +decc { + supportType = ['html', 'xml'] +} diff --git a/common/DistributeCalc/entry/src/main/config.json b/common/DistributeCalc/entry/src/main/config.json new file mode 100644 index 0000000000000000000000000000000000000000..a5509bf2330f31ba2830b827388216d499fc8c21 --- /dev/null +++ b/common/DistributeCalc/entry/src/main/config.json @@ -0,0 +1,57 @@ +{ + "app": { + "bundleName": "com.example.distributedcalc", + "vendor": "example", + "version": { + "code": 1000000, + "name": "1.0.0" + } + }, + "deviceConfig": {}, + "module": { + "package": "com.example.distributedcalc", + "name": ".MyApplication", + "deviceType": [ + "phone" + ], + "distro": { + "deliveryWithInstall": true, + "moduleName": "entry", + "moduleType": "entry", + "installationFree": false + }, + "abilities": [ + { + "skills": [ + { + "entities": [ + "entity.system.home" + ], + "actions": [ + "action.system.home" + ] + } + ], + "name": "com.example.distributedcalc.MainAbility", + "icon": "$media:icon", + "description": "$string:mainability_description", + "label": "$string:app_name", + "type": "page", + "launchType": "standard" + } + ], + "js": [ + { + "pages": [ + "pages/index/index", + "pages/calc/calc" + ], + "name": "default", + "window": { + "designWidth": 720, + "autoDesignWidth": false + } + } + ] + } +} \ No newline at end of file diff --git a/common/DistributeCalc/entry/src/main/js/default/app.js b/common/DistributeCalc/entry/src/main/js/default/app.js new file mode 100644 index 0000000000000000000000000000000000000000..7ff1e139a7dcbfe5d3ffe7bff66ed6ccaca6a97a --- /dev/null +++ b/common/DistributeCalc/entry/src/main/js/default/app.js @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export default { + onCreate() { + console.info('AceApplication onCreate'); + }, + onDestroy() { + console.info('AceApplication onDestroy'); + } +}; diff --git a/common/DistributeCalc/entry/src/main/js/default/common/RemoteDeviceModel.js b/common/DistributeCalc/entry/src/main/js/default/common/RemoteDeviceModel.js new file mode 100644 index 0000000000000000000000000000000000000000..752fddc6b4ebb04f378d1f40f32bdeef50495873 --- /dev/null +++ b/common/DistributeCalc/entry/src/main/js/default/common/RemoteDeviceModel.js @@ -0,0 +1,166 @@ +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import prompt from '@system.prompt'; +import deviceManager from '@ohos.distributedHardware.deviceManager'; + +var SUBSCRIBE_ID = 100; + +export default class RemoteDeviceModel { + deviceList = new Array(); + callback; + deviceManager_; + + constructor() { + } + + registerDeviceListCallback(callback) { + if (typeof (this.deviceManager_) == 'undefined') { + console.log("Calc[RemoteDeviceModel] deviceManager.createDeviceManager begin"); + let self = this; + deviceManager.createDeviceManager("com.example.distributedcalc", (error, value) => { + if (error) { + console.error("Calc[RemoteDeviceModel] createDeviceManager failed."); + return; + } + self.deviceManager_ = value; + self.registerDeviceListCallback_(callback); + console.log("Calc[RemoteDeviceModel] createDeviceManager callback returned, error=" + error + " value=" + value); + }); + console.log("Calc[RemoteDeviceModel] deviceManager.createDeviceManager end"); + } else { + this.registerDeviceListCallback_(callback); + } + } + + registerDeviceListCallback_(callback) { + console.info('Calc[RemoteDeviceModel] registerDeviceListCallback'); + this.callback = callback; + if (this.deviceManager_ == undefined) { + console.error('Calc[RemoteDeviceModel] deviceManager has not initialized'); + this.callback(); + return; + } + + console.info('Calc[RemoteDeviceModel] getTrustedDeviceListSync begin'); + var list = this.deviceManager_.getTrustedDeviceListSync(); + console.info('Calc[RemoteDeviceModel] getTrustedDeviceListSync end, deviceList=' + JSON.stringify(list)); + if (typeof (list) != 'undefined' && typeof (list.length) != 'undefined') { + this.deviceList = list; + } + this.callback(); + console.info('Calc[RemoteDeviceModel] callback finished'); + + let self = this; + this.deviceManager_.on('deviceStateChange', (data) => { + console.info('Calc[RemoteDeviceModel] deviceStateChange data=' + JSON.stringify(data)); + switch (data.action) { + case 0: + self.deviceList[self.deviceList.length] = data.device; + console.info('Calc[RemoteDeviceModel] online, updated device list=' + JSON.stringify(self.deviceList)); + self.callback(); + break; + case 2: + if (self.deviceList.length > 0) { + for (var i = 0; i < self.deviceList.length; i++) { + if (self.deviceList[i].deviceId == data.device.deviceId) { + self.deviceList[i] = data.device; + break; + } + } + } + console.info('Calc[RemoteDeviceModel] change, updated device list=' + JSON.stringify(self.deviceList)); + self.callback(); + break; + case 1: + if (self.deviceList.length > 0) { + var list = new Array(); + for (var i = 0; i < self.deviceList.length; i++) { + if (self.deviceList[i].deviceId != data.device.deviceId) { + list[i] = data.device; + } + } + self.deviceList = list; + } + console.info('Calc[RemoteDeviceModel] offline, updated device list=' + JSON.stringify(data.device)); + self.callback(); + break; + default: + break; + } + }); + this.deviceManager_.on('deviceFound', (data) => { + console.info('Calc[RemoteDeviceModel] deviceFound data=' + JSON.stringify(data)); + prompt.showToast({ + message: 'deviceFound device=' + JSON.stringify(data.device), + duration: 3000, + }); + console.info('Calc[RemoteDeviceModel] deviceFound self.deviceList=' + self.deviceList); + console.info('Calc[RemoteDeviceModel] deviceFound self.deviceList.length=' + self.deviceList.length); + console.info('Calc[RemoteDeviceModel] authenticateDevice ' + JSON.stringify(data.device)); + self.deviceManager_.authenticateDevice(data.device); + var list = self.deviceManager_.getTrustedDeviceListSync(); + console.info('Calc[RemoteDeviceModel] getTrustedDeviceListSync end, deviceList=' + JSON.stringify(list)); + if (typeof (list) != 'undefined' && typeof (list.length) != 'undefined') { + self.deviceList = list; + } + }); + this.deviceManager_.on('discoverFail', (data) => { + prompt.showToast({ + message: 'discoverFail reason=' + data.reason, + duration: 3000, + }); + console.info('Calc[RemoteDeviceModel] discoverFail data=' + JSON.stringify(data)); + }); + this.deviceManager_.on('authResult', (data) => { + prompt.showToast({ + message: 'authResult data=' + JSON.stringify(data), + duration: 3000, + }); + console.info('Calc[RemoteDeviceModel] authResult data=' + JSON.stringify(data)); + }); + this.deviceManager_.on('serviceDie', () => { + prompt.showToast({ + message: 'serviceDie', + duration: 3000, + }); + console.error('Calc[RemoteDeviceModel] serviceDie'); + }); + + SUBSCRIBE_ID = Math.floor(65536 * Math.random()); + var info = { + subscribeId: SUBSCRIBE_ID, + mode: 0xAA, + medium: 2, + freq: 2, + isSameAccount: false, + isWakeRemote: true, + capability: 0 + }; + console.info('Calc[RemoteDeviceModel] startDeviceDiscover ' + SUBSCRIBE_ID); + this.deviceManager_.startDeviceDiscover(info); + } + + unregisterDeviceListCallback() { + console.info('Calc[RemoteDeviceModel] stopDeviceDiscover ' + SUBSCRIBE_ID); + this.deviceManager_.stopDeviceDiscover(SUBSCRIBE_ID); + this.deviceManager_.off('deviceStateChange'); + this.deviceManager_.off('deviceFound'); + this.deviceManager_.off('discoverFail'); + this.deviceManager_.off('authResult'); + this.deviceManager_.off('serviceDie'); + this.deviceList = new Array(); + } +} \ No newline at end of file diff --git a/common/DistributeCalc/entry/src/main/js/default/common/calculator.js b/common/DistributeCalc/entry/src/main/js/default/common/calculator.js new file mode 100644 index 0000000000000000000000000000000000000000..7d5b85b6b8e0a8dd3c959a1e29d248e17f69b6a9 --- /dev/null +++ b/common/DistributeCalc/entry/src/main/js/default/common/calculator.js @@ -0,0 +1,145 @@ +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +const OperatorLevels = { + "+": 0, + "-": 0, + "*": 1, + "/": 1, +}; + +const OperatorHandlers = { + "+": (one, other) => one + other, + "-": (one, other) => one - other, + "*": (one, other) => one * other, + "/": (one, other) => one / other, +}; + +function calcSuffixExpression(expression) { + const numberStack = []; + + while (expression.length) { + const element = expression.shift(); + if (!isOperator(element)) { + numberStack.push(Number(element)); + } else { + const one = numberStack.pop(); + const other = numberStack.pop(); + const result = OperatorHandlers[element](other, one); + numberStack.push(result); + } + } + return numberStack[0]; +} + +function toSuffixExpression(expression) { + const operatorStack = []; + const suffixExpression = []; + let topOperator; + for (let idx = 0, size = expression.length; idx < size; idx++) { + const element = expression[idx]; + if (element === "(") { + operatorStack.push(element); + continue; + } + if (element === ")") { + if (operatorStack.length) { + let operator = operatorStack.pop(); + while (operator !== "(") { + suffixExpression.push(operator); + operator = operatorStack.pop(); + } + } + continue; + } + if (isOperator(element)) { + if (!operatorStack.length) { + operatorStack.push(element); + } else { + topOperator = operatorStack[operatorStack.length - 1]; + if (!isGrouping(topOperator) && !isPrioritized(element, topOperator)) { + suffixExpression.push(operatorStack.pop()); + } + operatorStack.push(element); + } + continue; + } + suffixExpression.push(element); + } + while (operatorStack.length) { + suffixExpression.push(operatorStack.pop()); + } + return suffixExpression; +} + +function parseInfixExpression(content) { + const size = content.length; + const lastIdx = size - 1; + let number = ""; + const expression = []; + for (let idx = 0; idx < size; idx++) { + const element = content[idx]; + if (isGrouping(element)) { + if (number !== "") { + expression.push(number); + number = ""; + } + expression.push(element); + } else if (isOperator(element)) { + if (isSymbol(element) && (idx === 0 || content[idx - 1] === "(")) { + number += element; + } else { + if (number !== "") { + expression.push(number); + number = ""; + } + + if (idx !== lastIdx) { + expression.push(element); + } + } + } else { + number += element; + } + + if (idx === lastIdx && number !== "") { + expression.push(number); + } + } + return expression; +} + +function isPrioritized(one, other) { + return OperatorLevels[one] > OperatorLevels[other]; +} + +export function isOperator(operator) { + return ( + operator === "+" || operator === "-" || operator === "*" || operator === "/" + ); +} + +function isSymbol(symbol) { + return symbol === "+" || symbol === "-"; +} + +function isGrouping(operator) { + return operator === "(" || operator === ")"; +} + +export function calc(content) { + const infixExpression = parseInfixExpression(content); + const suffixExpression = toSuffixExpression(infixExpression); + return calcSuffixExpression(suffixExpression); +} diff --git a/common/DistributeCalc/entry/src/main/js/default/common/ic_back.png b/common/DistributeCalc/entry/src/main/js/default/common/ic_back.png new file mode 100644 index 0000000000000000000000000000000000000000..4a269a11a6eca005d2eabb957992297446c858db Binary files /dev/null and b/common/DistributeCalc/entry/src/main/js/default/common/ic_back.png differ diff --git a/common/DistributeCalc/entry/src/main/js/default/common/ic_cal_delete.png b/common/DistributeCalc/entry/src/main/js/default/common/ic_cal_delete.png new file mode 100644 index 0000000000000000000000000000000000000000..9932ba4fbf7f37556ca1df1a69a243ac1f2adcab Binary files /dev/null and b/common/DistributeCalc/entry/src/main/js/default/common/ic_cal_delete.png differ diff --git a/common/DistributeCalc/entry/src/main/js/default/common/ic_cal_devide.png b/common/DistributeCalc/entry/src/main/js/default/common/ic_cal_devide.png new file mode 100644 index 0000000000000000000000000000000000000000..2bfc1ba7e3494166d13eb9c29ac4d2c009dbcf38 Binary files /dev/null and b/common/DistributeCalc/entry/src/main/js/default/common/ic_cal_devide.png differ diff --git a/common/DistributeCalc/entry/src/main/js/default/common/ic_cal_equal.png b/common/DistributeCalc/entry/src/main/js/default/common/ic_cal_equal.png new file mode 100644 index 0000000000000000000000000000000000000000..b212e9da2d02da969cbcbaf70fe9f649691e9e57 Binary files /dev/null and b/common/DistributeCalc/entry/src/main/js/default/common/ic_cal_equal.png differ diff --git a/common/DistributeCalc/entry/src/main/js/default/common/ic_cal_minus.png b/common/DistributeCalc/entry/src/main/js/default/common/ic_cal_minus.png new file mode 100644 index 0000000000000000000000000000000000000000..48c329a81b63b212e09dcee1d95605f72f5e9a6b Binary files /dev/null and b/common/DistributeCalc/entry/src/main/js/default/common/ic_cal_minus.png differ diff --git a/common/DistributeCalc/entry/src/main/js/default/common/ic_cal_multiply.png b/common/DistributeCalc/entry/src/main/js/default/common/ic_cal_multiply.png new file mode 100644 index 0000000000000000000000000000000000000000..e6e075df08511bfa3a95f3d358bb71d0ccb87019 Binary files /dev/null and b/common/DistributeCalc/entry/src/main/js/default/common/ic_cal_multiply.png differ diff --git a/common/DistributeCalc/entry/src/main/js/default/common/ic_cal_plus.png b/common/DistributeCalc/entry/src/main/js/default/common/ic_cal_plus.png new file mode 100644 index 0000000000000000000000000000000000000000..d43f0db5187673b2e5602fe7d914a30caf94ffcd Binary files /dev/null and b/common/DistributeCalc/entry/src/main/js/default/common/ic_cal_plus.png differ diff --git a/common/DistributeCalc/entry/src/main/js/default/common/ic_hop_normal.png b/common/DistributeCalc/entry/src/main/js/default/common/ic_hop_normal.png new file mode 100644 index 0000000000000000000000000000000000000000..435ec58bbde7bc034fe558dc890af0b653ae2e2f Binary files /dev/null and b/common/DistributeCalc/entry/src/main/js/default/common/ic_hop_normal.png differ diff --git a/common/DistributeCalc/entry/src/main/js/default/pages/calc/calc.css b/common/DistributeCalc/entry/src/main/js/default/pages/calc/calc.css new file mode 100644 index 0000000000000000000000000000000000000000..63df567ac19c06350d2dce206cc21ff988310c3d --- /dev/null +++ b/common/DistributeCalc/entry/src/main/js/default/pages/calc/calc.css @@ -0,0 +1,170 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +.calc-container { + height: 100%; + width: 100%; + flex-direction: column; +} + +.calc-icon { + width: 36px; + height: 36px; +} + +.calc-titlebar { + width: 100%; + height: 10%; +} + +.calc-back-btn { + left: 39px; + top: 10px; +} + +.calc-title { + left: 96px; + font-size: 30px; + top: 10px; +} + +.calc-expr, .calc-rsl { + padding: 0 3%; +} + +.calc-expr { + font-size: 72px; + color: #000000; + width: 100%; + height: 10%; +} + +.calc-rsl { + color: #999; + font-size: 38px; + width: 100%; + height: 8%; +} + +.input-area { + width: 100%; + height: 65%; + margin-top: 25%; +} + +.calc-text-btn, .calc-op-btn, .calc-equals-btn { + border-radius: 16px; + height: 106px; + width: 18%; + font-size: 38px; +} + +.calc-text-btn { + background-color: #f2f2f2; + color: #000000; +} + +.calc-text-btn:active { + background-color: rgb(72, 72, 72); +} + +.calc-op-btn { + background-color: #4D3F97E9; + color: #FF3F97E9; +} + +.calc-op-btn:active { + background-color: rgb(54, 76, 97); +} + +.calc-equals-btn { + background-color: #FF006CDE; + color: #fff; +} + +.calc-equals-btn:active { + background-color: rgb(38, 129, 226); +} + +.calc-btn-row-1 { + top: 112px; +} + +.calc-btn-row-2 { + top: 226px; +} + +.calc-btn-row-3 { + top: 340px; +} + +.calc-btn-row-4 { + top: 454px; +} + +.calc-btn-col-1 { + left: 3%; +} + +.calc-btn-col-2 { + left: 22%; +} + +.calc-btn-col-3 { + left: 41%; +} + +.calc-btn-col-4 { + left: 60%; +} + +.calc-btn-col-5 { + left: 79%; +} + +.calc-btn-row-span-2 { + width: 37%; +} + +.calc-btn-col-span-2 { + height: 220px; +} + +.calc-icon-row-1 { + top: 152px; +} + +.calc-icon-row-2 { + top: 266px; +} + +.calc-icon-row-3 { + top: 380px; +} + +.calc-icon-row-4 { + top: 484px; +} + +.calc-icon-col-1 { + left: 69%; + transform: translateX(-50%); +} + +.calc-icon-col-2 { + left: 88%; + transform: translateX(-50%); +} + + diff --git a/common/DistributeCalc/entry/src/main/js/default/pages/calc/calc.hml b/common/DistributeCalc/entry/src/main/js/default/pages/calc/calc.hml new file mode 100644 index 0000000000000000000000000000000000000000..f4aeed30226a8770634078e866ecb5d0acb04a32 --- /dev/null +++ b/common/DistributeCalc/entry/src/main/js/default/pages/calc/calc.hml @@ -0,0 +1,54 @@ + + +
+ + + + 计算器 + + + + {{expression}} + + + {{result}} + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/common/DistributeCalc/entry/src/main/js/default/pages/calc/calc.js b/common/DistributeCalc/entry/src/main/js/default/pages/calc/calc.js new file mode 100644 index 0000000000000000000000000000000000000000..afd161b3e8ffe4f2fea4abbc3ae6994d786a2dc8 --- /dev/null +++ b/common/DistributeCalc/entry/src/main/js/default/pages/calc/calc.js @@ -0,0 +1,190 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import {calc, isOperator} from '../../common/calculator.js'; +import app from '@system.app'; +import distributedDataKit from '@ohos.data.distributedData'; + +let pressedEqual = false; +let kvManager,kvStore; +const store_id = 'distributedCalc'; + +export default { + data: { + expression : '', + isPush:false, + result : '' + }, + onInit() { + this.$watch("expression", (value)=> { + if (value !== '') { + this.result = calc(value).toString(); + if(kvStore != null && !this.isPush) { + console.log('Calc[CalcPage] put key start'); + this.dataChange('expression',value); + } + } + }); + }, + dataChange(key, value){ + kvStore.put(key,value).then((data)=>{ + console.log('Calc[CalcPage] put key value successed! value:'+value); + }).catch((err)=>{ + console.log('Calc[CalcPage] put key value failed:'+err); + }); + }, + initKVManager(){ + let that = this; + console.log('Calc[CalcPage] get kv manager start'); + const config = + { + userInfo : { + userId : '0', + userType : 0 + }, + bundleName : 'com.example.distributedcalc' + }; + const promise = distributedDataKit.createKVManager(config); + promise.then((manager)=>{ + if(manager == null) { + console.log('Calc[CalcPage] get kv manager failed'); + return; + } + kvManager = manager; + console.log('Calc[CalcPage] get kv manager successed'); + const options = + { + createIfMissing : true, + encrypt : false, + backup : false, + autoSync : true, + kvStoreType : 1, + schema : '', + securityLevel : 3 + }; + kvManager.getKVStore(store_id,options).then((store)=>{ + if(store == null) { + console.log("Calc[CalcPage] get kv store failed"); + return; + } + kvStore = store; + console.log("Calc[CalcPage] get kv store successed"); + kvStore.on('dataChange', 1, function (data) { + console.info('Calc[CalcPage] dataChange, ' + JSON.stringify(data)); + console.info('Calc[CalcPage] dataChange, insert ' + data.insertEntries.length + " udpate " + data.updateEntries.length); + if(data) { + let arr = data.insertEntries.concat(data.updateEntries); + console.info('Calc[CalcPage] arr ' + JSON.stringify(arr)); + for(let i=0;i< arr.length;i++){ + let entry = arr[i]; + if(entry.key === 'expression') { + if(entry.value.value === 'exit') { + console.info('Calc[CalcPage] app exit! '); + app.terminate(); + return; + } + that.isPush = true; + if(entry.value.value == "clear") { + console.log("Calc[CalcPage] data expression:clear"); + that.expression = ''; + that.result = ''; + continue; + } + that.expression = entry.value.value; + console.log("Calc[CalcPage] data changed:"+entry.value.value); + } + } + } + }); + }).catch((err)=>{ + console.log("Calc[CalcPage] get kv store failed:"+err); + }); + }).catch((err)=>{ + console.log('Calc[CalcPage] get kv manager failed:'+err); + }); + console.log('Calc[CalcPage] get kv manager end'); + }, + stopDataListener(){ + console.log("Calc[CalcPage] stopDataListener"); + }, + onShow(){ + console.log("Calc[CalcPage] on show"); + this.initKVManager(); + }, + onDestroy(){ + this.stopDataListener(); + }, + handleClear() { + this.expression = ''; + this.result = ''; + if(kvStore != null) { + this.dataChange('expression','clear'); + } + }, + handleInput(value) { + if (isOperator(value)) { + if (pressedEqual) { + pressedEqual = false; + } else { + const size = this.expression.length; + if (size) { + const last = this.expression.charAt(size - 1); + if (isOperator(last)) { + this.expression = this.expression.slice(0, -1); + } + } + } + if (!this.expression && (value == '*' || value == '/')) { + return; + } + this.expression += value; + } else { + if (pressedEqual) { + this.expression = value; + pressedEqual = false; + } else { + this.expression += value; + } + } + this.isPush = false; + }, + handleBackspace() { + if(pressedEqual) { + this.expression = ''; + this.result = ''; + pressedEqual = false; + } else { + this.expression = this.expression.slice(0, -1); + if (!this.expression.length) { + this.result = ''; + } + } + }, + handleEqual() { + if (this.result !== '') { + this.expression = this.result; + this.result = ''; + pressedEqual = true; + } + }, + handleTerminate(e) { + if (e.direction === 'right') { + app.terminate(); + } + }, + handleExist() { + app.terminate(); + }, +} \ No newline at end of file diff --git a/common/DistributeCalc/entry/src/main/js/default/pages/index/index.css b/common/DistributeCalc/entry/src/main/js/default/pages/index/index.css new file mode 100644 index 0000000000000000000000000000000000000000..4397478a016ceb281360fb3240051140fca1a95e --- /dev/null +++ b/common/DistributeCalc/entry/src/main/js/default/pages/index/index.css @@ -0,0 +1,215 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +.calc-container { + height: 100%; + width: 100%; + flex-direction: column; +} + +.calc-icon { + width: 36px; + height: 36px; +} + +.calc-titlebar { + width: 100%; + height: 10%; +} + +.calc-back-btn { + left: 39px; + top: 10px; +} + +.calc-select-device-btn { + left:90%; + top: 10px; +} + +.calc-select-device-dialog { + width: 90%; + height: 33%; +} + +.calc-select-device-wrapper { + width: 95%; + margin-left: 5%; + height: 100%; + flex-direction: column; +} + +.calc-select-device-item { + width: 100%; + height: 50%; +} + +.calc-select-device-item-cancel-btn { + text-align: center; + color: #0000ff; +} + +.calc-select-device-item-title { + text-align: left; + font-size: 40px; +} + +.calc-select-device-item-left { + width: 90%; + height: 100%; + text-align: left; + font-size: 32px; +} + +.calc-select-device-item-right { + width: 10%; + height: 100%; +} + +.calc-title { + left: 96px; + font-size: 30px; + top: 10px; +} + +.calc-expr, .calc-rsl { + padding: 0 3%; +} + +.calc-expr { + font-size: 72px; + color: #000000; + width: 100%; + height: 10%; +} + +.calc-rsl { + color: #999; + font-size: 38px; + width: 100%; + height: 8%; +} + +.input-area { + width: 100%; + height: 65%; + margin-top: 25%; +} + +.calc-text-btn, .calc-op-btn, .calc-equals-btn { + border-radius: 16px; + height: 106px; + width: 18%; + font-size: 38px; +} + +.calc-text-btn { + background-color: #f2f2f2; + color: #000000; +} + +.calc-text-btn:active { + background-color: rgb(72, 72, 72); +} + +.calc-op-btn { + background-color: #4D3F97E9; + color: #FF3F97E9; +} + +.calc-op-btn:active { + background-color: rgb(54, 76, 97); +} + +.calc-equals-btn { + background-color: #FF006CDE; + color: #fff; +} + +.calc-equals-btn:active { + background-color: rgb(38, 129, 226); +} + +.calc-btn-row-1 { + top: 112px; +} + +.calc-btn-row-2 { + top: 226px; +} + +.calc-btn-row-3 { + top: 340px; +} + +.calc-btn-row-4 { + top: 454px; +} + +.calc-btn-col-1 { + left: 3%; +} + +.calc-btn-col-2 { + left: 22%; +} + +.calc-btn-col-3 { + left: 41%; +} + +.calc-btn-col-4 { + left: 60%; +} + +.calc-btn-col-5 { + left: 79%; +} + +.calc-btn-row-span-2 { + width: 37%; +} + +.calc-btn-col-span-2 { + height: 220px; +} + +.calc-icon-row-1 { + top: 152px; +} + +.calc-icon-row-2 { + top: 266px; +} + +.calc-icon-row-3 { + top: 380px; +} + +.calc-icon-row-4 { + top: 484px; +} + +.calc-icon-col-1 { + left: 69%; + transform: translateX(-50%); +} + +.calc-icon-col-2 { + left: 88%; + transform: translateX(-50%); +} + + diff --git a/common/DistributeCalc/entry/src/main/js/default/pages/index/index.hml b/common/DistributeCalc/entry/src/main/js/default/pages/index/index.hml new file mode 100644 index 0000000000000000000000000000000000000000..baffadc22af2b3bd9c273ce96f1f78c9365f9d69 --- /dev/null +++ b/common/DistributeCalc/entry/src/main/js/default/pages/index/index.hml @@ -0,0 +1,67 @@ + + +
+ + + + {{title}} + + + + + {{expression}} + + + {{result}} + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ 选择设备 + + + {{$item.deviceName}} + + + + 取消 +
+
+
diff --git a/common/DistributeCalc/entry/src/main/js/default/pages/index/index.js b/common/DistributeCalc/entry/src/main/js/default/pages/index/index.js new file mode 100644 index 0000000000000000000000000000000000000000..d866c1355c54ed0a16d0ee43608dca7862c8c87b --- /dev/null +++ b/common/DistributeCalc/entry/src/main/js/default/pages/index/index.js @@ -0,0 +1,288 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import {calc, isOperator} from '../../common/calculator.js'; +import app from '@system.app'; +import router from '@system.router'; +import distributedDataKit from '@ohos.data.distributeddata'; +import RemoteDeviceModel from '../../common/RemoteDeviceModel.js' +import featureAbility from '@ohos.ability.featureability'; + + +let pressedEqual = false; +let kvManager,kvStore; +const store_id = 'distributedCalc'; + +export default { + data: { + title:'计算器', + expression : '', + result : '', + selectedIndex: -1, + isPush:false, + isDistributed:false, + remoteDeviceModel: new RemoteDeviceModel(), + deviceList:[] + }, + onInit() { + this.$watch("expression", (value)=> { + if (value !== '') { + this.result = calc(value).toString(); + if(this.isDistributed && kvStore != null && !this.isPush) { + console.log('Calc[IndexPage] data changed'); + this.dataChange('expression',value); + } + } + }); + }, + onReady(){ + featureAbility.getWant((error, want) => { + console.info('Calc[IndexPage] featureAbility.getWant =' + JSON.stringify(want.parameters)); + if (want.parameters.isFA && want.parameters.isFA === 'FA') { + router.replace({ + uri:'pages/calc/calc' + }); + } + }); + }, + dataChange(key, value){ + kvStore.put(key,value).then((data)=>{ + console.log('Calc[IndexPage] put key value successed value:'+value); + }).catch((err)=>{ + console.log('Calc[IndexPage] put key value failed:'+err); + }); + }, + initKVManager(done){ + if(kvStore != null) { + this.isDistributed = true; + this.startDataListener(); + done(); + return; + } + console.log('Calc[IndexPage] get kv manager start'); + const config = + { + userInfo : { + userId : '0', + userType : 0 + }, + bundleName : 'com.example.distributedcalc' + }; + const promise = distributedDataKit.createKVManager(config); + promise.then((manager)=>{ + if(manager == null) { + console.log('Calc[IndexPage] get kv manager failed'); + return; + } + kvManager = manager; + console.log('Calc[IndexPage] get kv manager successed'); + const options = + { + createIfMissing : true, + encrypt : false, + backup : false, + autoSync : true, + kvStoreType : 1, + schema : '', + securityLevel : 3 + }; + kvManager.getKVStore(store_id,options).then((store)=>{ + if(store == null) { + console.log("Calc[IndexPage] get kv store failed"); + return; + } + kvStore = store; + console.log("Calc[IndexPage] get kv store successed"); + this.isDistributed = true; + this.startDataListener(); + done(); + }).catch((err)=>{ + console.log("Calc[IndexPage] get kv store failed:"+err); + }); + }).catch((err)=>{ + console.log('Calc[IndexPage] get kv manager failed:'+err); + }); + console.log('Calc[IndexPage] get kv manager end'); + }, + + startDataListener(){ + if(kvStore == null) { + console.info('Calc[IndexPage] startDataListener kvstore is null'); + return; + } + let that = this; + kvStore.on('dataChange', 1, function (data) { + console.info('Calc[IndexPage] dataChange, ' + JSON.stringify(data)); + console.info('Calc[IndexPage] dataChange, insert ' + data.insertEntries.length + " udpate " + data.updateEntries.length); + if(data) { + let arr = data.insertEntries.concat(data.updateEntries); + console.info('Calc[IndexPage] arr ' + JSON.stringify(arr)); + for(let i=0;i< arr.length;i++){ + let entry = arr[i]; + if(entry.key === 'expression') { + that.isPush = true; + if(entry.value.value == "clear") { + console.log("Calc[IndexPage] data expression:clear"); + that.expression = ''; + that.result = ''; + continue; + } + that.expression = entry.value.value; + console.log("Calc[IndexPage] data expression:" + that.expression); + console.log("Calc[IndexPage] data changed:" + entry.value.value); + } + } + } + }); + }, + + stopDataListener(){ + console.log("Calc[IndexPage] stopDataListener"); + }, + onDestroy(){ + this.remoteDeviceModel.unregisterDeviceListCallback(); + if(this.isDistributed && kvStore != null) { + this.stopDataListener(); + this.isDistributed = false; + } + }, + showDialog(){ + console.info('Calc[IndexPage] onContinueAbilityClick'); + let self = this; + this.remoteDeviceModel.registerDeviceListCallback(() => { + console.info('Calc[IndexPage] registerDeviceListCallback, callback entered'); + var list = new Array(); + list[0] = { + deviceId: '0', + deviceName: '本机', + checked: (self.selectedIndex == 0) + }; + console.info('Calc[IndexPage] on remote device updated, count=' + self.remoteDeviceModel.deviceList.length); + for (var i = 0; i < self.remoteDeviceModel.deviceList.length; i++) { + console.info('Calc[IndexPage] device ' + i + '/' + self.remoteDeviceModel.deviceList.length + + ' deviceId=' + self.remoteDeviceModel.deviceList[i].deviceId + ' deviceName=' + self.remoteDeviceModel.deviceList[i].deviceName + + ' deviceType=' + self.remoteDeviceModel.deviceList[i].deviceType); + list[i + 1] = { + deviceName: self.remoteDeviceModel.deviceList[i].deviceName, + deviceId: self.remoteDeviceModel.deviceList[i].deviceId, + checked: (self.selectedIndex == (i + 1)) + }; + } + self.deviceList = list; + }); + this.$element('showDialog').show(); + }, + cancelDialog(){ + this.$element('showDialog').close(); + this.remoteDeviceModel.unregisterDeviceListCallback(); + }, + selectDevice(index,e){ + if(!e.checked) { + return; + } + if(index === this.selectedIndex) { + return; + } + console.log("Calc[IndexPage] select index:"+index) + if (index == 0) { + console.log("Calc[IndexPage] stop ability") + this.dataChange('expression', 'exit'); + this.isDistributed = false; + this.stopDataListener(); + } else { + this.initKVManager(()=>{ + console.log("Calc[IndexPage] start ability deviceID:"+this.deviceList[index].deviceId); + featureAbility.startAbility({ + want:{ + bundleName: 'com.example.distributedcalc', + abilityName: 'com.example.distributedcalc.MainAbility', + deviceId:this.deviceList[index].deviceId, + parameters: { + isFA: 'FA' + } + } + }).then((data)=>{ + console.log("Calc[IndexPage] start ability finished:" + JSON.stringify(data)); + }); + console.log("Calc[IndexPage] start ability end") + }); + } + this.selectedIndex = index; + this.cancelDialog(); + }, + handleClear() { + this.expression = ''; + this.result = ''; + if(this.isDistributed && kvStore != null) { + console.log('Calc[IndexPage] data changed'); + this.dataChange('expression','clear'); + } + }, + handleInput(value) { + console.log("Calc[IndexPage] handle input value:"+value) + if (isOperator(value)) { + if (pressedEqual) { + pressedEqual = false; + } else { + const size = this.expression.length; + if (size) { + const last = this.expression.charAt(size - 1); + if (isOperator(last)) { + this.expression = this.expression.slice(0, -1); + } + } + } + if (!this.expression && (value == '*' || value == '/')) { + return; + } + this.expression += value; + } else { + if (pressedEqual) { + this.expression = value; + pressedEqual = false; + } else { + console.log("Calc[IndexPage] handle input:"+value) + this.expression += value; + } + } + this.isPush = false; + }, + handleBackspace() { + if(pressedEqual) { + this.expression = ''; + this.result = ''; + pressedEqual = false; + } else { + this.expression = this.expression.slice(0, -1); + if (!this.expression.length) { + this.result = ''; + } + } + }, + handleEqual() { + if (this.result !== '') { + this.expression = this.result; + this.result = ''; + pressedEqual = true; + } + }, + handleTerminate(e) { + if (e.direction === 'right') { + app.terminate(); + } + }, + handleExist() { + app.terminate(); + } +} \ No newline at end of file diff --git a/common/DistributeCalc/entry/src/main/resources/base/element/string.json b/common/DistributeCalc/entry/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..e259ca3b600f46254579b565b3e841bc0866fd9c --- /dev/null +++ b/common/DistributeCalc/entry/src/main/resources/base/element/string.json @@ -0,0 +1,12 @@ +{ + "string": [ + { + "name": "app_name", + "value": "DistributedCalc" + }, + { + "name": "mainability_description", + "value": "JS_Phone_Empty Feature Ability" + } + ] +} \ No newline at end of file diff --git a/common/DistributeCalc/entry/src/main/resources/base/media/icon.png b/common/DistributeCalc/entry/src/main/resources/base/media/icon.png new file mode 100644 index 0000000000000000000000000000000000000000..ce307a8827bd75456441ceb57d530e4c8d45d36c Binary files /dev/null and b/common/DistributeCalc/entry/src/main/resources/base/media/icon.png differ diff --git a/common/DistributeCalc/settings.gradle b/common/DistributeCalc/settings.gradle new file mode 100644 index 0000000000000000000000000000000000000000..28d595f2fba0d06b2025da200383d15f87c4e9f0 --- /dev/null +++ b/common/DistributeCalc/settings.gradle @@ -0,0 +1 @@ +include ':entry'