diff --git a/PreHttpRequestUseFiles/.gitignore b/PreHttpRequestUseFiles/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..d2ff20141ceed86d87c0ea5d99481973005bab2b --- /dev/null +++ b/PreHttpRequestUseFiles/.gitignore @@ -0,0 +1,12 @@ +/node_modules +/oh_modules +/local.properties +/.idea +**/build +/.hvigor +.cxx +/.clangd +/.clang-format +/.clang-tidy +**/.test +/.appanalyzer \ No newline at end of file diff --git a/PreHttpRequestUseFiles/AppScope/app.json5 b/PreHttpRequestUseFiles/AppScope/app.json5 new file mode 100644 index 0000000000000000000000000000000000000000..076d01dd66673c840554b285a2bf39f08b59a8e0 --- /dev/null +++ b/PreHttpRequestUseFiles/AppScope/app.json5 @@ -0,0 +1,10 @@ +{ + "app": { + "bundleName": "com.example.useimagehttprequest", + "vendor": "example", + "versionCode": 1000000, + "versionName": "1.0.0", + "icon": "$media:app_icon", + "label": "$string:app_name" + } +} diff --git a/PreHttpRequestUseFiles/AppScope/resources/base/element/string.json b/PreHttpRequestUseFiles/AppScope/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..98a14fff89a8e3b7134c05c5383e4f642b9639eb --- /dev/null +++ b/PreHttpRequestUseFiles/AppScope/resources/base/element/string.json @@ -0,0 +1,8 @@ +{ + "string": [ + { + "name": "app_name", + "value": "UseImageHttpRequest" + } + ] +} diff --git a/PreHttpRequestUseFiles/AppScope/resources/base/media/app_icon.png b/PreHttpRequestUseFiles/AppScope/resources/base/media/app_icon.png new file mode 100644 index 0000000000000000000000000000000000000000..a39445dc87828b76fed6d2ec470dd455c45319e3 Binary files /dev/null and b/PreHttpRequestUseFiles/AppScope/resources/base/media/app_icon.png differ diff --git a/PreHttpRequestUseFiles/LICENSE b/PreHttpRequestUseFiles/LICENSE new file mode 100644 index 0000000000000000000000000000000000000000..338e5b0bc22082e0ffcc7121c2ed3897a3ddccb0 --- /dev/null +++ b/PreHttpRequestUseFiles/LICENSE @@ -0,0 +1,78 @@ + Copyright (c) 2024 Huawei Device Co., Ltd. All rights reserved. + + 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. + +Apache License, Version 2.0 +TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + +1. Definitions. + +"License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. + +"Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. + +"Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. + +"You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. + +"Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. + +"Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. + +"Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). + +"Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. + +"Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." + +"Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. + +2. Grant of Copyright License. + +Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. + +3. Grant of Patent License. + +Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. + +4. Redistribution. + +You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: +1.You must give any other recipients of the Work or Derivative Works a copy of this License; and +2.You must cause any modified files to carry prominent notices stating that You changed the files; and +3.You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and +4.If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. + +You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. + +5. Submission of Contributions. + +Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. + +6. Trademarks. + +This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. + +7. Disclaimer of Warranty. + +Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. + +8. Limitation of Liability. + +In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. + +9. Accepting Warranty or Additional Liability. + +While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. + +END OF TERMS AND CONDITIONS \ No newline at end of file diff --git a/PreHttpRequestUseFiles/README.md b/PreHttpRequestUseFiles/README.md new file mode 100644 index 0000000000000000000000000000000000000000..64740e425516a787e7db2a7508fa0b78eae3ed53 --- /dev/null +++ b/PreHttpRequestUseFiles/README.md @@ -0,0 +1,52 @@ +# Image白块解决指导 + +### 简介 + +本示例主要解决使用原生Image组件加载大的网络图片时,出现长时间Image白块的问题。 + +| 效果预览 | +|----------------------------------------| +| | + + +### 使用说明 + +1、父页面点击"push Path to pageOne"按钮,跳转子页面PageOne。 + +2、子页面展示网络图片,无明显的长时间白块现象。 + +### 工程目录 +``` +├──entry/src/main/ets +│ ├──entryability +│ │ └──EntryAbility.ets // 程序入口类 +│ ├──pages +│ │ ├──MainPage.ets // 视图层-父页面 +│ │ └──PageOne.ets // 视图层-子页面 +│ └──utils +│ ├──Logger.ets // 日志工具类 +│ └──NetRequest.ets // 网络请求类 +└──entry/src/main/resources // 应用静态资源目录 +``` + +### 具体实现 + +1. 在父组件aboutToAppear()中提前发起网络请求,当父页面点击按钮跳转子页面PageOne,此时触发pixMap请求读取应用沙箱中已缓存解码的网络图片并存储在localStorage中,非首次点击时,不再重复调用getPixMap(),避免每次点击都从沙箱里读取文件。 +2. 在NetRequest.ets中定义网络请求httpRequest(),通过fs.access()检查文件是否存在,当文件存在时不再重复请求,并写入沙箱中。 +3. 在子组件里:通过在子组件Image中传入被@StorageLink修饰的变量ImageData进行数据刷新,图片送显。 +4. 从而通过提前预下载的方式,解决使用Image原生组件加载大的网络图片时,出现长时间Image白块的问题。 + +### 相关权限 + +网络权限:ohos.permission.INTERNET + +### 依赖 + +不涉及。 + +### 约束与限制 + +1. 本示例仅支持标准系统上运行,支持设备:华为手机。 +2. HarmonyOS系统:HarmonyOS NEXT Beta1及以上。 +3. DevEco Studio版本:DevEco Studio NEXT Beta1及以上。 +4. HarmonyOS SDK版本:HarmonyOS NEXT Beta1 SDK及以上。 diff --git a/PreHttpRequestUseFiles/build-profile.json5 b/PreHttpRequestUseFiles/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..c033ce4278fb0641d84107c3c134047ac487368e --- /dev/null +++ b/PreHttpRequestUseFiles/build-profile.json5 @@ -0,0 +1,35 @@ +{ + "app": { + "signingConfigs": [], + "products": [ + { + "name": "default", + "signingConfig": "default", + "compatibleSdkVersion": "5.0.0(12)", + "runtimeOS": "HarmonyOS", + } + ], + "buildModeSet": [ + { + "name": "debug", + }, + { + "name": "release" + } + ] + }, + "modules": [ + { + "name": "entry", + "srcPath": "./entry", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + } + ] +} \ No newline at end of file diff --git a/PreHttpRequestUseFiles/entry/.gitignore b/PreHttpRequestUseFiles/entry/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/PreHttpRequestUseFiles/entry/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/PreHttpRequestUseFiles/entry/build-profile.json5 b/PreHttpRequestUseFiles/entry/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..b695582d3680556f4cce2ec518f65720a9413ca3 --- /dev/null +++ b/PreHttpRequestUseFiles/entry/build-profile.json5 @@ -0,0 +1,28 @@ +{ + "apiType": "stageMode", + "buildOption": { + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": true, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/PreHttpRequestUseFiles/entry/hvigorfile.ts b/PreHttpRequestUseFiles/entry/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..c6edcd90486dd5a853cf7d34c8647f08414ca7a3 --- /dev/null +++ b/PreHttpRequestUseFiles/entry/hvigorfile.ts @@ -0,0 +1,6 @@ +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/PreHttpRequestUseFiles/entry/obfuscation-rules.txt b/PreHttpRequestUseFiles/entry/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..69c4d6a8a5531548e4886fa766090c5c157a87d9 --- /dev/null +++ b/PreHttpRequestUseFiles/entry/obfuscation-rules.txt @@ -0,0 +1,18 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope \ No newline at end of file diff --git a/PreHttpRequestUseFiles/entry/oh-package.json5 b/PreHttpRequestUseFiles/entry/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..248c3b7541a589682a250f86a6d3ecf7414d2d6a --- /dev/null +++ b/PreHttpRequestUseFiles/entry/oh-package.json5 @@ -0,0 +1,10 @@ +{ + "name": "entry", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": {} +} + diff --git a/PreHttpRequestUseFiles/entry/src/main/ets/entryability/EntryAbility.ets b/PreHttpRequestUseFiles/entry/src/main/ets/entryability/EntryAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..e8c1e5db6d8cfa8003414ebb28f6a5321f3403ed --- /dev/null +++ b/PreHttpRequestUseFiles/entry/src/main/ets/entryability/EntryAbility.ets @@ -0,0 +1,56 @@ +/* + * 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 { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class EntryAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/MainPage', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +} diff --git a/PreHttpRequestUseFiles/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets b/PreHttpRequestUseFiles/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..6b744d7eaa66e51e79fc4e0896e251292ee767c5 --- /dev/null +++ b/PreHttpRequestUseFiles/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets @@ -0,0 +1,27 @@ +/* + * 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 { hilog } from '@kit.PerformanceAnalysisKit'; +import { BackupExtensionAbility, BundleVersion } from '@kit.CoreFileKit'; + +export default class EntryBackupAbility extends BackupExtensionAbility { + async onBackup() { + hilog.info(0x0000, 'testTag', 'onBackup ok'); + } + + async onRestore(bundleVersion: BundleVersion) { + hilog.info(0x0000, 'testTag', 'onRestore ok %{public}s', JSON.stringify(bundleVersion)); + } +} \ No newline at end of file diff --git a/PreHttpRequestUseFiles/entry/src/main/ets/pages/MainPage.ets b/PreHttpRequestUseFiles/entry/src/main/ets/pages/MainPage.ets new file mode 100644 index 0000000000000000000000000000000000000000..0ef525435e1c81a6ddf61b80e956662f677269ca --- /dev/null +++ b/PreHttpRequestUseFiles/entry/src/main/ets/pages/MainPage.ets @@ -0,0 +1,82 @@ +/* + * 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 { fileIo as fs } from '@kit.CoreFileKit'; +import { image } from '@kit.ImageKit'; +import { common } from '@kit.AbilityKit'; +import { httpRequest } from '../utils/NetRequest'; +import Logger from '../utils/Logger'; + +// Obtain the path of the application file +let context = getContext(this) as common.UIAbilityContext; +let filesDir = context.filesDir; +let fileUrl = filesDir + '/xxx.png'; +let para: Record = { 'imageData': undefined }; +let localStorage: LocalStorage = new LocalStorage(para); +const TAG = '[GetPixMapFunc]'; + +@Entry(localStorage) +@Component +struct MainPage { + @State childNavStack: NavPathStack = new NavPathStack(); + @LocalStorageLink('imageData') imageData: PixelMap | undefined = undefined; + + getPixMap() { // Read files from the application sandbox + try { + let file = fs.openSync(fileUrl, fs.OpenMode.READ_WRITE); // Open the file in a synchronous manner + const imageSource: image.ImageSource = image.createImageSource(file.fd); + const options: image.InitializationOptions = { + 'alphaType': 0, // transparency + 'editable': false, // Editable or not + 'pixelFormat': 3, // Pixel format + 'scaleMode': 1, // Abbreviated value + 'size': { height: 100, width: 100 } + }; + fs.close(file) + imageSource.createPixelMap(options).then((pixelMap: PixelMap) => { + this.imageData = pixelMap; + }); + } catch (e) { + Logger.error(TAG, 'Resource loading error, file or does not exist!'); + } + } + + aboutToAppear(): void { + httpRequest(); // Initiate a network request ahead of the parent component + } + + build() { + Navigation(this.childNavStack) { + Column() { + Button('push Path to pageOne', { stateEffect: true, type: ButtonType.Capsule }) + .width('80%') + .height(40) + .margin({ bottom: '36vp' }) + .onClick(() => { + // Do not call getPixMap() repeatedly except for the first click to avoid reading files from the sandbox with each click. + if (!localStorage.get('imageData')) { + this.getPixMap(); + } + this.childNavStack.pushPath({ name: 'pageOne' }); + }) + } + .width('100%') + .height('100%') + .justifyContent(FlexAlign.End) + } + .backgroundColor(Color.Transparent) + .title('ParentNavigation') + } +} \ No newline at end of file diff --git a/PreHttpRequestUseFiles/entry/src/main/ets/pages/PageOne.ets b/PreHttpRequestUseFiles/entry/src/main/ets/pages/PageOne.ets new file mode 100644 index 0000000000000000000000000000000000000000..146da226d103d4b8fd863bca0cb2ea9fae85af2a --- /dev/null +++ b/PreHttpRequestUseFiles/entry/src/main/ets/pages/PageOne.ets @@ -0,0 +1,43 @@ +/* + * 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. + */ + +@Builder +export function PageOneBuilder(name: string, param: Object) { + PageOne() +} + +@Component +export struct PageOne { + pageInfo: NavPathStack = new NavPathStack(); + @State name: string = 'pageOne'; + @LocalStorageLink('imageData') imageData: PixelMap | undefined = undefined; + + build() { + NavDestination() { + Row() { + // Positive example: At this time, the Image has obtained the network image that has been loaded in advance, + // reducing the time for white blocks to appear. + Image(this.imageData) + .objectFit(ImageFit.Auto) + .width('100%') + .height('100%') + } + .width('100%') + .height('100%') + .justifyContent(FlexAlign.Center) + } + .title(this.name) + } +} \ No newline at end of file diff --git a/PreHttpRequestUseFiles/entry/src/main/ets/utils/Logger.ets b/PreHttpRequestUseFiles/entry/src/main/ets/utils/Logger.ets new file mode 100644 index 0000000000000000000000000000000000000000..af1f96dbad73cc319a5188a6dba827942b924363 --- /dev/null +++ b/PreHttpRequestUseFiles/entry/src/main/ets/utils/Logger.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. + */ + +import { hilog } from '@kit.PerformanceAnalysisKit'; + +class Logger { + private domain: number; + private prefix: string; + private format: string = "%{public}s, %{public}s"; + + constructor(prefix: string) { + this.prefix = prefix; + this.domain = 0xFF00; + } + + debug(...args: Object[]): void { + hilog.debug(this.domain, this.prefix, this.format, args); + } + + info(...args: Object[]): void { + hilog.info(this.domain, this.prefix, this.format, args); + } + + warn(...args: Object[]): void { + hilog.warn(this.domain, this.prefix, this.format, args); + } + + error(...args: Object[]): void { + hilog.error(this.domain, this.prefix, this.format, args); + } +} + +export default new Logger('[PreHttpRequest]'); \ No newline at end of file diff --git a/PreHttpRequestUseFiles/entry/src/main/ets/utils/NetRequest.ets b/PreHttpRequestUseFiles/entry/src/main/ets/utils/NetRequest.ets new file mode 100644 index 0000000000000000000000000000000000000000..338ace2c65b56c35ca1d80d67635befb2f218a5e --- /dev/null +++ b/PreHttpRequestUseFiles/entry/src/main/ets/utils/NetRequest.ets @@ -0,0 +1,55 @@ +/* + * 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 { http } from '@kit.NetworkKit'; +import { BusinessError } from '@kit.BasicServicesKit'; +import { fileIo as fs } from '@kit.CoreFileKit'; +import { common } from '@kit.AbilityKit'; + +// Obtain the path of the application file +let context = getContext(this) as common.UIAbilityContext; +let filesDir = context.filesDir; +let fileUrl = filesDir + '/xxx.png'; + +export async function httpRequest() { + fs.access(fileUrl, fs.AccessModeType.READ).then((res) => { // Check whether files exist + if (!res) { // If the address does not exist in the sandbox, re-request the network image resource + http.createHttp() + // Please fill in a specific network image address here, example: https://img.picui.cn/free/2024/09/09/66deb127cf1c0.png + // If you fill in the real address, you need to replace the global fileUrl with the real address suffix. + .request('https://example.com/xxx.png', + (error: BusinessError, data: http.HttpResponse) => { + if (error) { + // If the download fails, no subsequent logic is executed + return; + } + // Processing data returned by network requests + if (http.ResponseCode.OK === data.responseCode) { + const imageData: ArrayBuffer = data.result as ArrayBuffer; + // Save the image to the app sandbox + readWriteFileWithStream(imageData) + } + } + ) + } + }) +} + +// Write to the sandbox +async function readWriteFileWithStream(imageData: ArrayBuffer): Promise { + let outputStream = fs.createStreamSync(fileUrl, 'w+'); + await outputStream.write(imageData); + outputStream.closeSync(); +} \ No newline at end of file diff --git a/PreHttpRequestUseFiles/entry/src/main/module.json5 b/PreHttpRequestUseFiles/entry/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..f0ca92afaf91eb456192d84a7d751ea9ec30cfd9 --- /dev/null +++ b/PreHttpRequestUseFiles/entry/src/main/module.json5 @@ -0,0 +1,63 @@ +{ + "module": { + "name": "entry", + "type": "entry", + "description": "$string:module_desc", + "mainElement": "EntryAbility", + "routerMap": "$profile:router_map", + "deviceTypes": [ + "phone" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "EntryAbility", + "srcEntry": "./ets/entryability/EntryAbility.ets", + "description": "$string:EntryAbility_desc", + "icon": "$media:layered_image", + "label": "$string:EntryAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true, + "skills": [ + { + "entities": [ + "entity.system.home" + ], + "actions": [ + "action.system.home" + ] + } + ] + } + ], + "extensionAbilities": [ + { + "name": "EntryBackupAbility", + "srcEntry": "./ets/entrybackupability/EntryBackupAbility.ets", + "type": "backup", + "exported": false, + "metadata": [ + { + "name": "ohos.extension.backup", + "resource": "$profile:backup_config" + } + ], + } + ], + "requestPermissions": [ + { + "name": "ohos.permission.INTERNET", + "reason": "$string:internet_reason", + "usedScene": { + "abilities": [ + "EntryAbility" + ], + "when": "inuse" + } + } + ] + } +} \ No newline at end of file diff --git a/PreHttpRequestUseFiles/entry/src/main/resources/base/element/color.json b/PreHttpRequestUseFiles/entry/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/PreHttpRequestUseFiles/entry/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/PreHttpRequestUseFiles/entry/src/main/resources/base/element/string.json b/PreHttpRequestUseFiles/entry/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..32ea56af0ac0e3390dadd8496a9aaee7390a4aaa --- /dev/null +++ b/PreHttpRequestUseFiles/entry/src/main/resources/base/element/string.json @@ -0,0 +1,20 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "EntryAbility_desc", + "value": "description" + }, + { + "name": "EntryAbility_label", + "value": "label" + }, + { + "name": "internet_reason", + "value": "This permission is required for images to load network resources" + } + ] +} \ No newline at end of file diff --git a/PreHttpRequestUseFiles/entry/src/main/resources/base/media/background.png b/PreHttpRequestUseFiles/entry/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/PreHttpRequestUseFiles/entry/src/main/resources/base/media/background.png differ diff --git a/PreHttpRequestUseFiles/entry/src/main/resources/base/media/foreground.png b/PreHttpRequestUseFiles/entry/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/PreHttpRequestUseFiles/entry/src/main/resources/base/media/foreground.png differ diff --git a/PreHttpRequestUseFiles/entry/src/main/resources/base/media/layered_image.json b/PreHttpRequestUseFiles/entry/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/PreHttpRequestUseFiles/entry/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/PreHttpRequestUseFiles/entry/src/main/resources/base/media/startIcon.png b/PreHttpRequestUseFiles/entry/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/PreHttpRequestUseFiles/entry/src/main/resources/base/media/startIcon.png differ diff --git a/PreHttpRequestUseFiles/entry/src/main/resources/base/profile/backup_config.json b/PreHttpRequestUseFiles/entry/src/main/resources/base/profile/backup_config.json new file mode 100644 index 0000000000000000000000000000000000000000..78f40ae7c494d71e2482278f359ec790ca73471a --- /dev/null +++ b/PreHttpRequestUseFiles/entry/src/main/resources/base/profile/backup_config.json @@ -0,0 +1,3 @@ +{ + "allowToBackupRestore": true +} \ No newline at end of file diff --git a/PreHttpRequestUseFiles/entry/src/main/resources/base/profile/main_pages.json b/PreHttpRequestUseFiles/entry/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..f4241ef2f72e78047b4c449d92bd1a92accc39fb --- /dev/null +++ b/PreHttpRequestUseFiles/entry/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/MainPage" + ] +} diff --git a/PreHttpRequestUseFiles/entry/src/main/resources/base/profile/router_map.json b/PreHttpRequestUseFiles/entry/src/main/resources/base/profile/router_map.json new file mode 100644 index 0000000000000000000000000000000000000000..d54fa89e19edf4e9bba311784818c084ed961105 --- /dev/null +++ b/PreHttpRequestUseFiles/entry/src/main/resources/base/profile/router_map.json @@ -0,0 +1,12 @@ +{ + "routerMap": [ + { + "name": "pageOne", + "pageSourceFile": "src/main/ets/pages/PageOne.ets", + "buildFunction": "PageOneBuilder", + "data": { + "description": "this is pageOne" + } + } + ] +} \ No newline at end of file diff --git a/PreHttpRequestUseFiles/entry/src/main/resources/en_US/element/string.json b/PreHttpRequestUseFiles/entry/src/main/resources/en_US/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..32ea56af0ac0e3390dadd8496a9aaee7390a4aaa --- /dev/null +++ b/PreHttpRequestUseFiles/entry/src/main/resources/en_US/element/string.json @@ -0,0 +1,20 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "EntryAbility_desc", + "value": "description" + }, + { + "name": "EntryAbility_label", + "value": "label" + }, + { + "name": "internet_reason", + "value": "This permission is required for images to load network resources" + } + ] +} \ No newline at end of file diff --git a/PreHttpRequestUseFiles/entry/src/main/resources/zh_CN/element/string.json b/PreHttpRequestUseFiles/entry/src/main/resources/zh_CN/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..2cfac88316a0993b7b743aadbf5a58811ef9bc69 --- /dev/null +++ b/PreHttpRequestUseFiles/entry/src/main/resources/zh_CN/element/string.json @@ -0,0 +1,20 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "模块描述" + }, + { + "name": "EntryAbility_desc", + "value": "description" + }, + { + "name": "EntryAbility_label", + "value": "label" + }, + { + "name": "internet_reason", + "value": "图片加载网络资源时需要此权限" + } + ] +} \ No newline at end of file diff --git a/PreHttpRequestUseFiles/hvigor/hvigor-config.json5 b/PreHttpRequestUseFiles/hvigor/hvigor-config.json5 new file mode 100644 index 0000000000000000000000000000000000000000..06b2783670a348f95533b352c1ceda909a842bbc --- /dev/null +++ b/PreHttpRequestUseFiles/hvigor/hvigor-config.json5 @@ -0,0 +1,22 @@ +{ + "modelVersion": "5.0.0", + "dependencies": { + }, + "execution": { + // "analyze": "normal", /* Define the build analyze mode. Value: [ "normal" | "advanced" | false ]. Default: "normal" */ + // "daemon": true, /* Enable daemon compilation. Value: [ true | false ]. Default: true */ + // "incremental": true, /* Enable incremental compilation. Value: [ true | false ]. Default: true */ + // "parallel": true, /* Enable parallel compilation. Value: [ true | false ]. Default: true */ + // "typeCheck": false, /* Enable typeCheck. Value: [ true | false ]. Default: false */ + }, + "logging": { + // "level": "info" /* Define the log level. Value: [ "debug" | "info" | "warn" | "error" ]. Default: "info" */ + }, + "debugging": { + // "stacktrace": false /* Disable stacktrace compilation. Value: [ true | false ]. Default: false */ + }, + "nodeOptions": { + // "maxOldSpaceSize": 8192 /* Enable nodeOptions maxOldSpaceSize compilation. Unit M. Used for the daemon process. Default: 8192*/ + // "exposeGC": true /* Enable to trigger garbage collection explicitly. Default: true*/ + } +} diff --git a/PreHttpRequestUseFiles/hvigorfile.ts b/PreHttpRequestUseFiles/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..f3cb9f1a87a81687554a76283af8df27d8bda775 --- /dev/null +++ b/PreHttpRequestUseFiles/hvigorfile.ts @@ -0,0 +1,6 @@ +import { appTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: appTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/PreHttpRequestUseFiles/oh-package.json5 b/PreHttpRequestUseFiles/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..0cec98259052cc175d04dbcf61780de4bc9785f4 --- /dev/null +++ b/PreHttpRequestUseFiles/oh-package.json5 @@ -0,0 +1,7 @@ +{ + "modelVersion": "5.0.0", + "description": "Please describe the basic information.", + "dependencies": { + }, + "devDependencies": {} +} diff --git a/PreHttpRequestUseFiles/screenshots/devices/show.gif b/PreHttpRequestUseFiles/screenshots/devices/show.gif new file mode 100644 index 0000000000000000000000000000000000000000..6794907ac4e64b092260e0d517e7b95ecb21d663 Binary files /dev/null and b/PreHttpRequestUseFiles/screenshots/devices/show.gif differ