diff --git a/LocationKit/entry/src/main/ets/pages/CalFileMd5.ets b/LocationKit/entry/src/main/ets/pages/CalFileMd5.ets new file mode 100644 index 0000000000000000000000000000000000000000..af3db8093e4917adc8b860a96d6303287cfd569e --- /dev/null +++ b/LocationKit/entry/src/main/ets/pages/CalFileMd5.ets @@ -0,0 +1,45 @@ +/* +* Copyright (c) 2024 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. +*/ + +/* +* FAQ:如何校验文件一致性 + */ + +// [Start calFileMd5] +import { cryptoFramework } from '@kit.CryptoArchitectureKit'; +import { fileIo } from '@kit.CoreFileKit' +import { buffer } from '@kit.ArkTS'; + +async function calFileMd5(fileUrl: string) { + let md = cryptoFramework.createMd('MD5'); + let file = fileIo.openSync(fileUrl, fileIo.OpenMode.READ_ONLY); + let arrayBuffer = new ArrayBuffer(2048); + let len: number = 0; + let position: number = 0; + do { + len = fileIo.readSync(file.fd, arrayBuffer, { offset: position }); + if (len > 0) { + let uint8Array = new Uint8Array(arrayBuffer.slice(0, len)); + let updateMessageBlob: cryptoFramework.DataBlob = { data: uint8Array }; + await md.update(updateMessageBlob); + position += len; + } + } while (len > 0); + fileIo.closeSync(file); + let mdOutput = await md.digest(); + console.info('...calFileMd5: ' + buffer.from(mdOutput.data).toString('hex')); +} + +// [End calFileMd5] \ No newline at end of file diff --git a/LocationKit/entry/src/main/ets/pages/FaultLog.ets b/LocationKit/entry/src/main/ets/pages/FaultLog.ets new file mode 100644 index 0000000000000000000000000000000000000000..bd155b23c2b5a8977464678aa522b1c70940877f --- /dev/null +++ b/LocationKit/entry/src/main/ets/pages/FaultLog.ets @@ -0,0 +1,89 @@ +/* +* Copyright (c) 2024 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. +*/ + +/* +* FAQ:如何保存faultLogger + */ + +// [Start faultLogger] +import { fileIo } from '@kit.CoreFileKit'; +import { FaultLogger, hilog } from '@kit.PerformanceAnalysisKit'; +import { AbilityConstant, common } from '@kit.AbilityKit'; +import { preferences } from '@kit.ArkData'; +import { BusinessError } from '@kit.BasicServicesKit'; + +const TAG: string = 'testTag'; + +export class LogUtils { + static async queryAndUploadFaultLog(context: common.UIAbilityContext, launchParam: AbilityConstant.LaunchParam) { + if (launchParam.lastExitReason == AbilityConstant.LastExitReason.NORMAL) { + hilog.info(0x00000, TAG, '上次应用退出时未发生异常'); + return; + } + let value: Array = await FaultLogger.query(FaultLogger.FaultType.NO_SPECIFIC) + hilog.info(0x00000, TAG, '报错日志:' + value.toString()); + if (value) { + let len = value.length; + hilog.info(0x00000, TAG, '报错日志数量:' + len); + if (len === 0) { + return; + } + //取得实例 + let preference: preferences.Preferences = await preferences.getPreferences(context, 'STLiveness'); + // 查询上一次的处理的时间戳 + let lastFaultHandleTime = preference.getSync('faultHandleTime', 0); + hilog.info(0x0000, TAG, 'lastFaultHandleTime:' + lastFaultHandleTime); + for (let i = 0; i < len; i++) { + let timestamp = value[i].timestamp; + hilog.info(0x00000, TAG, '日志文件名称#' + timestamp); + if (lastFaultHandleTime >= timestamp) { + hilog.error(0x00000, TAG, 'Maple No New Logs.'); + return; + } + // 将日志保存到应用沙箱的目录 保存文件名为 时间戳.log + await LogUtils.save(value[i].fullLog, context.filesDir + '/crash', timestamp + '.log'); + await preference.put('faultHandleTime', timestamp); + await preference.flush(); + } + } + } + static async save(buffer: ArrayBuffer | string, destFilePath: string, name: string): Promise { + await LogUtils.mkdir(destFilePath); + hilog.info(0x00000, TAG, '写入日志的内容为:' + buffer); + hilog.info(0x00000, TAG, '被写入的日志文件路径为:' + destFilePath); + if (buffer) { + try { + let file = fileIo.openSync(destFilePath + '/' + name, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); + fileIo.writeSync(file.fd, buffer); + fileIo.closeSync(file); + } catch (error) { + let e: BusinessError = error as BusinessError; + hilog.info(0x0000, TAG, 'FileUtils:save:::' + e.message); + } + } + return destFilePath; + } + + static async mkdir(destFilePath: string) { + hilog.info(0x00000, TAG, '开始创建目录:' + destFilePath); + if (fileIo.accessSync(destFilePath)) { + hilog.info(0x00000, TAG, '目录已经存在,无需创建'); + return; + } + await fileIo.mkdir(destFilePath); + hilog.info(0x00000, TAG, '创建完成'); + } +} +// [End faultLogger] \ No newline at end of file diff --git a/LocationKit/entry/src/main/ets/pages/ParametersCheck.ets b/LocationKit/entry/src/main/ets/pages/ParametersCheck.ets new file mode 100644 index 0000000000000000000000000000000000000000..34c406269f99351982b30d75f86f47bc00842720 --- /dev/null +++ b/LocationKit/entry/src/main/ets/pages/ParametersCheck.ets @@ -0,0 +1,74 @@ +/* +* Copyright (c) 2024 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 { BusinessError,request } from "@kit.BasicServicesKit"; +import { fileUri } from "@kit.CoreFileKit"; + +/* +* FAQ:报错“the parameters check fails this is fail path”如何解决? + */ +let context: null = null; + +export class ParametersCheck { + // [Start ParametersCheck] + public static timelineReceivedResult(path: string): void { + if (!path) { + return; + } + // let file = fs.openSync(path, fs.OpenMode.READ_ONLY); + // let result = fs.readTextSync(path); + + let filename = path.substring(path.lastIndexOf("/") + 1); + let uri = fileUri.getUriFromPath(path); + let header = new Map(); + // header.set('key1', 'value1'); + // header.set('key2', 'value2'); + let files: Array = [ + { + filename: filename, + name: 'test', + uri: uri, + type: 'txt' + } + ] + let data: Array = []; // { name: 'name', value: 'value' } + let uploadConfig: request.UploadConfig = { + url: 'http://30.7.242.25:8800',// TODO + header: header, + method: 'POST', + files: files, + data: data + } + + // 将本地应用文件上传至网络服务器 + try { + request.uploadFile(context,uploadConfig) + .then((uploadTask: request.UploadTask) => { + uploadTask.on('complete',(taskStates: Array) => { + for (let i = 0; i < taskStates.length; i++) { + console.info(`upload complete taskState: ${JSON.stringify(taskStates[i])}`); + } + }); + }) + .catch((err: BusinessError) => { + console.error(`Invoke uploadFile failed, code is ${err.code}, message is ${err.message}`); + }) + } catch (error) { + let err: BusinessError = error as BusinessError; + console.error(`Invoke uploadFile failed, code is ${err.code}, message is ${err.message}`); + } + } + + // [End ParametersCheck] +} diff --git a/LocationKit/entry/src/main/ets/pages/ReadByte.ets b/LocationKit/entry/src/main/ets/pages/ReadByte.ets new file mode 100644 index 0000000000000000000000000000000000000000..c3418a8250e0bed9db01ee956a51c81f1b3bf91b --- /dev/null +++ b/LocationKit/entry/src/main/ets/pages/ReadByte.ets @@ -0,0 +1,51 @@ +/* +* Copyright (c) 2024 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. +*/ + +/* +* FAQ:如何从一个二进制文件中读取其字节数组?通过fs.createStreamSync只能获取到ArrayBuffer,如何转成number[] + */ + +// [Start ArrayBufferConversionArray] +@Component +export struct ArrayBufferConversionArray { + @State fileLength: number = 10; + tempData: number[] = []; + + aboutToAppear(): void { + let arrayBuffer: ArrayBuffer = new ArrayBuffer(this.fileLength); + let dataView: DataView = new DataView(arrayBuffer); + for (let index = 0; index < this.fileLength; index++) { + this.tempData[index] = dataView.getInt8(index); + } + console.info(this.tempData.toString()); + } + + build() { + RelativeContainer() { + Text(this.tempData.toString()) + .id('ArrayBufferHelloWorld') + .fontSize(50) + .fontWeight(FontWeight.Bold) + .alignRules({ + center: { anchor: '__container__', align: VerticalAlign.Center }, + middle: { anchor: '__container__', align: HorizontalAlign.Center } + }) + } + .height('100%') + .width('100%') + } +} + +// [End ArrayBufferConversionArray] \ No newline at end of file diff --git a/LocationKit/entry/src/main/ets/pages/SaveToUser.ets b/LocationKit/entry/src/main/ets/pages/SaveToUser.ets new file mode 100644 index 0000000000000000000000000000000000000000..cbd3c6ede402648d19e5f4f9eb8357df5dbae420 --- /dev/null +++ b/LocationKit/entry/src/main/ets/pages/SaveToUser.ets @@ -0,0 +1,47 @@ +/* +* Copyright (c) 2024 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. +*/ + +/* +* FAQ:如何存储文件才不会跟随app卸载而删除 + */ + +// [Start saveToUser] +import { BusinessError } from '@kit.BasicServicesKit'; +import { picker, fileIo } from '@kit.CoreFileKit'; + +let uri: string = ''; + +// 将content至公共目录 +export async function saveToUser(content: string) { + try { + let DocumentSaveOptions = new picker.DocumentSaveOptions(); + // 新建文件名 + DocumentSaveOptions.newFileNames = ['test.txt']; + let documentPicker = new picker.DocumentViewPicker(); + documentPicker.save(DocumentSaveOptions).then((DocumentSaveResult: Array) => { + console.info('DocumentViewPicker.save successfully, uri: ' + JSON.stringify(DocumentSaveResult)); + uri = DocumentSaveResult[0]; + // 将content写入新建的文件内 + let file = fileIo.openSync(uri, fileIo.OpenMode.READ_WRITE); + fileIo.writeSync(file.fd, content); + }).catch((err: BusinessError) => { + console.error('DocumentViewPicker.save failed with err: ' + JSON.stringify(err)); + }); + } catch (error) { + let err: BusinessError = error as BusinessError; + console.error('DocumentViewPicker failed with err: ' + err.message); + } +} +// [End saveToUser] \ No newline at end of file