diff --git a/CoreFile/UserFile/FileUriDevelopment_C/.gitignore b/CoreFile/UserFile/FileUriDevelopment_C/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..d2ff20141ceed86d87c0ea5d99481973005bab2b --- /dev/null +++ b/CoreFile/UserFile/FileUriDevelopment_C/.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/CoreFile/UserFile/FileUriDevelopment_C/AppScope/app.json5 b/CoreFile/UserFile/FileUriDevelopment_C/AppScope/app.json5 new file mode 100644 index 0000000000000000000000000000000000000000..faa606f8a1db76b33e2b33740c17295753b56941 --- /dev/null +++ b/CoreFile/UserFile/FileUriDevelopment_C/AppScope/app.json5 @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 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. + */ + +{ + "app": { + "bundleName": "com.samples.fileuridevelopment_c", + "vendor": "samples", + "versionCode": 1000000, + "versionName": "1.0.0", + "icon": "$media:app_icon", + "label": "$string:app_name" + } +} diff --git a/CoreFile/UserFile/FileUriDevelopment_C/AppScope/resources/base/element/string.json b/CoreFile/UserFile/FileUriDevelopment_C/AppScope/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..f1da2e079ed5b583dc877fbeb3909bb94e8fa4b4 --- /dev/null +++ b/CoreFile/UserFile/FileUriDevelopment_C/AppScope/resources/base/element/string.json @@ -0,0 +1,8 @@ +{ + "string": [ + { + "name": "app_name", + "value": "FileUriDevelopment_C" + } + ] +} diff --git a/CoreFile/UserFile/FileUriDevelopment_C/AppScope/resources/base/media/app_icon.png b/CoreFile/UserFile/FileUriDevelopment_C/AppScope/resources/base/media/app_icon.png new file mode 100644 index 0000000000000000000000000000000000000000..a39445dc87828b76fed6d2ec470dd455c45319e3 Binary files /dev/null and b/CoreFile/UserFile/FileUriDevelopment_C/AppScope/resources/base/media/app_icon.png differ diff --git a/CoreFile/UserFile/FileUriDevelopment_C/PageImg/FileUriDevelopment_c.jpeg b/CoreFile/UserFile/FileUriDevelopment_C/PageImg/FileUriDevelopment_c.jpeg new file mode 100644 index 0000000000000000000000000000000000000000..e7a5dfae8c5213963db7ba118185abcfbf4ef0dd Binary files /dev/null and b/CoreFile/UserFile/FileUriDevelopment_C/PageImg/FileUriDevelopment_c.jpeg differ diff --git a/CoreFile/UserFile/FileUriDevelopment_C/README_zh.md b/CoreFile/UserFile/FileUriDevelopment_C/README_zh.md new file mode 100644 index 0000000000000000000000000000000000000000..40c96c8e6d53ee4d5764e4332d796035f701f4f6 --- /dev/null +++ b/CoreFile/UserFile/FileUriDevelopment_C/README_zh.md @@ -0,0 +1,93 @@ +# FileUri开发指导(C/C++) + +### 介绍 + +该工程提供了关于文件URI的基本操作,对外提供了URI与沙箱路径之间互相转换、远端URI判定、获取URI所在目录路径的URI等接口,方便用户将文件URI与沙箱路径相互转换。该工程中展示的代码详细描述可查如下链接。 + +- [FileUri开发指导(C/C++)](https://gitee.com/openharmony/docs/blob/OpenHarmony-5.0.1-Release/zh-cn/application-dev/file-management/native-fileuri-guidelines.md) + +### 效果预览 + +|主页| +|--------------------------------| +|| + +使用说明: + +1. 在主界面,点击GetUriFromPath接口按钮获取代码里指定文件路径对应的uri,并输出到文本框里。 +2. 在主界面,点击GetPathFromUri接口按钮获取代码里指定uri对应的文件路径,并输出到文本框里。 +3. 在主界面,点击GetFullDirectoryUri接口按钮获取代码里指定文件uri所在路径的uri,并输出到文本框里。 +4. 在主界面,点击IsValidUri接口按钮判断代码里指定文件uri是否正确,并判断结果输出到文本框里。 +5. 在主界面,点击GetFileName接口按钮获取uri中的文件名称,并输出到文本框里。 +6. 切换到Deveco日志页面,搜索”HiAppEvent eventInfo“,出现以下日志。 + +``` +HiAppEvent eventInfo.WatcherType=OnTrigger: /data/storage/el2/base/haps/entry/files/test.txt +HiAppEvent eventInfo.WatcherType=OnTrigger: file://com.samples.fileuridevelopment_c/data/storage/el2/base/haps/entry/files/test.txt +HiAppEvent eventInfo.WatcherType=OnTrigger: file://com.samples.fileuridevelopment_c/data/storage/el2/base/haps/entry/files/test.txt +HiAppEvent eventInfo.WatcherType=OnTrigger: /data/storage/el2/base/haps/entry/files/test.txt +HiAppEvent eventInfo.WatcherType=OnTrigger: file://com.samples.fileuridevelopment_c/data/storage/el2/base/haps/entry/files/test.txt +HiAppEvent eventInfo.WatcherType=OnTrigger: file://com.samples.fileuridevelopment_c/data/storage/el2/base/haps/entry/files +HiAppEvent eventInfo.WatcherType=OnTrigger: file://com.samples.fileuridevelopment_c/data/storage/el2/base/haps/entry/files/test.txt +HiAppEvent eventInfo.WatcherType=OnTrigger: file://com.samples.fileuridevelopment_c/data/storage/el2/base/haps/entry/files/test.txt +HiAppEvent eventInfo.WatcherType=OnTrigger: test.txt +``` + + + +### 工程目录 + +``` +├──entry/src/main +| ├──cpp +| | └──CMakeLists.txt // 动态库链接 +| | └──napi_init.cpp // c++函数实现文件 +| | ├──types // c++函数配置目录 +| ├──ets +| | ├──entryability +| | | └──EntryAbility.ets // 程序入口类 +| | ├──entrybackupability +| | | └──EntryBackupAbility.ets +| | └──pages // 页面文件 +| | └──Index.ets // 主界面 +| ├──resources // 资源文件目录 +``` + +### 具体实现 + +* 在CMake脚本中链接动态库,添加头文件。 + +* 调用OH_FileUri_GetUriFromPath接口,在接口中malloc的内存需要在使用完后释放,因此需要free对应的内存。 +* 调用OH_FileUri_GetPathFromUri通过URi转成对应的PATH,在接口中malloc的内存需要在使用完后释放,因此需要free对应的内存。 +* 调用OH_FileUri_GetFullDirectoryUri获取URI所在路径的URI,在接口中malloc的内存需要在使用完后释放,因此需要free对应的内存。 +* 调用OH_FileUri_IsValidUri接口进行URI格式验证。 +* 调用OH_FileUri_GetFileName获取URI中的文件名称,在接口中malloc的内存需要在使用完后释放,因此需要free对应的内存。 + +### 相关权限 + +不涉及 + +### 依赖 + +不涉及 + +### 约束与限制 + +1. 本示例仅支持标准系统上运行,支持设备:华为手机。 +2. HarmonyOS系统:HarmonyOS 5.1.1 Release及以上。 +3. DevEco Studio版本:DevEco Studio 5.1.1 Release及以上。 +4. HarmonyOS SDK版本:HarmonyOS 5.1.1 Release及以上。 +5. 转换或者判断URI类型之前必须保证传入的参数正确有效。 +6. 为保证数据的准确性,在转换或者判断过程中只允许处理一个对象。 + +### 下载 + +如需单独下载本工程,执行如下命令: + +``` +git init +git config core.sparsecheckout true +echo CoreFile/UserFile/FileUriDevelopment_C > .git/info/sparse-checkout +git remote add origin https://gitee.com/harmonyos_samples/guide-snippets.git +git pull origin master +``` \ No newline at end of file diff --git a/CoreFile/UserFile/FileUriDevelopment_C/build-profile.json5 b/CoreFile/UserFile/FileUriDevelopment_C/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..56be7b8e9354c5a9d03e9953feae0a2bfb7a0aed --- /dev/null +++ b/CoreFile/UserFile/FileUriDevelopment_C/build-profile.json5 @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2025 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. + */ + +{ + "app": { + "products": [ + { + "name": "default", + "signingConfig": "default", + "compatibleSdkVersion": "5.1.1(19)", + "targetSdkVersion": "5.1.1(19)", + "runtimeOS": "HarmonyOS", + "buildOption": { + "externalNativeOptions": { + "abiFilters": [ + "arm64-v8a", + "x86_64" + ] + } + } + } + ], + "buildModeSet": [ + { + "name": "debug", + }, + { + "name": "release" + } + ] + }, + "modules": [ + { + "name": "entry", + "srcPath": "./entry", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + } + ] +} \ No newline at end of file diff --git a/CoreFile/UserFile/FileUriDevelopment_C/entry/.gitignore b/CoreFile/UserFile/FileUriDevelopment_C/entry/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/CoreFile/UserFile/FileUriDevelopment_C/entry/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/CoreFile/UserFile/FileUriDevelopment_C/entry/build-profile.json5 b/CoreFile/UserFile/FileUriDevelopment_C/entry/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/CoreFile/UserFile/FileUriDevelopment_C/entry/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 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. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/CoreFile/UserFile/FileUriDevelopment_C/entry/hvigorfile.ts b/CoreFile/UserFile/FileUriDevelopment_C/entry/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/CoreFile/UserFile/FileUriDevelopment_C/entry/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 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 { 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/CoreFile/UserFile/FileUriDevelopment_C/entry/obfuscation-rules.txt b/CoreFile/UserFile/FileUriDevelopment_C/entry/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/CoreFile/UserFile/FileUriDevelopment_C/entry/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# 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 + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/CoreFile/UserFile/FileUriDevelopment_C/entry/oh-package.json5 b/CoreFile/UserFile/FileUriDevelopment_C/entry/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..118bdd4fe7699368a010e04c24f5bfc887cf1298 --- /dev/null +++ b/CoreFile/UserFile/FileUriDevelopment_C/entry/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 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. + */ + +{ + "name": "entry", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libentry.so": "file:./src/main/cpp/types/libentry" + } +} \ No newline at end of file diff --git a/CoreFile/UserFile/FileUriDevelopment_C/entry/src/main/cpp/CMakeLists.txt b/CoreFile/UserFile/FileUriDevelopment_C/entry/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..0492dce8c055fce65a30e726a96fe5723b4fe846 --- /dev/null +++ b/CoreFile/UserFile/FileUriDevelopment_C/entry/src/main/cpp/CMakeLists.txt @@ -0,0 +1,15 @@ +# the minimum version of CMake. +cmake_minimum_required(VERSION 3.5.0) +project(FileUriDevelopment_C) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) + +if(DEFINED PACKAGE_FIND_FILE) + include(${PACKAGE_FIND_FILE}) +endif() + +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(entry SHARED napi_init.cpp) +target_link_libraries(entry PUBLIC libace_napi.z.so libohfileuri.so libhilog_ndk.z.so) \ No newline at end of file diff --git a/CoreFile/UserFile/FileUriDevelopment_C/entry/src/main/cpp/napi_init.cpp b/CoreFile/UserFile/FileUriDevelopment_C/entry/src/main/cpp/napi_init.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ff8a2d2363a6fa89803b1a2ea02c811eb7eb10c1 --- /dev/null +++ b/CoreFile/UserFile/FileUriDevelopment_C/entry/src/main/cpp/napi_init.cpp @@ -0,0 +1,278 @@ +/* + * Copyright (c) 2025 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. + */ + +#include "napi/native_api.h" +#include +#include +#include + +#undef LOG_TAG +#define LOG_TAG "Sample_fileUri" + +// [Start get_uri_from_path_example] +static napi_value NAPI_Global_OH_FileUri_GetUriFromPathExample(napi_env env, napi_callback_info info) +{ + // [StartExclude get_uri_from_path_example] + // 参数个数为1 + size_t argc = 1; + napi_value args[1] = { nullptr }; + + napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); + // 获取字符串的长度,初始化字符串长度为0 + size_t strLength = 0; + napi_get_value_string_utf8(env, args[0], nullptr, 0, &strLength); + // [EndExclude get_uri_from_path_example] + // 为 char* uri 分配内存 + char *path = new char[strLength + 1]; // +1 for null terminator + // 将 JavaScript 字符串复制到 uri + // [StartExclude get_uri_from_path_example] + napi_get_value_string_utf8(env, args[0], path, strLength + 1, &strLength); + // [EndExclude get_uri_from_path_example] + unsigned int length = strlen(path); + // 输出传入路径字符串 + // [StartExclude get_uri_from_path_example] + OH_LOG_INFO(LogType::LOG_APP, "HiAppEvent eventInfo.WatcherType=OnTrigger: %{public}s", path); + // [EndExclude get_uri_from_path_example] + char *uriResult = nullptr; + FileManagement_ErrCode ret = OH_FileUri_GetUriFromPath(path, length, &uriResult); + // 输出结果uri字符串 + // [StartExclude get_uri_from_path_example] + OH_LOG_INFO(LogType::LOG_APP, "HiAppEvent eventInfo.WatcherType=OnTrigger: %{public}s", uriResult); + napi_value result; + // [EndExclude get_uri_from_path_example] + if (ret == 0 && uriResult != nullptr) { + // 将C字符串转换为napi_value + napi_status status = napi_create_string_utf8(env, uriResult, NAPI_AUTO_LENGTH, &result); + if (status != napi_ok) { + free(uriResult); + return nullptr; + } + free(uriResult); // 释放临时字符串 + } else { + // 将C字符串转换为napi_value + napi_status status = napi_create_string_utf8(env, "Hello World", NAPI_AUTO_LENGTH, &result); + if (status != napi_ok) { + return nullptr; + } + } + return result; +} +// [End get_uri_from_path_example] + +// [Start get_path_from_uri_example] +static napi_value NAPI_Global_OH_FileUri_GetPathFromUriExample(napi_env env, napi_callback_info info) +{ + // [StartExclude get_path_from_uri_example] + // 参数个数为1 + size_t argc = 1; + napi_value args[1] = { nullptr }; + + napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); + + // 获取字符串的长度,初始化字符串长度为0 + size_t strLength = 0; + napi_get_value_string_utf8(env, args[0], nullptr, 0, &strLength); + // 为 char* uri 分配内存 + // [EndExclude get_path_from_uri_example] + char *uri = new char[strLength + 1]; // +1 for null terminator + // 将 JavaScript 字符串复制到 uri + napi_get_value_string_utf8(env, args[0], uri, strLength + 1, &strLength); + + unsigned int length = strlen(uri); + // 输出传入uri符串 + OH_LOG_INFO(LogType::LOG_APP, "HiAppEvent eventInfo.WatcherType=OnTrigger: %{public}s", uri); + char *pathResult = nullptr; + FileManagement_ErrCode ret = OH_FileUri_GetPathFromUri(uri, length, &pathResult); + // 输出获取路径结果符串 + // [StartExclude get_path_from_uri_example] + OH_LOG_INFO(LogType::LOG_APP, "HiAppEvent eventInfo.WatcherType=OnTrigger: %{public}s", pathResult); + napi_value result; + // [EndExclude get_path_from_uri_example] + if (ret == 0 && pathResult != nullptr) { + // 将C字符串转换为napi_value + napi_status status = napi_create_string_utf8(env, pathResult, NAPI_AUTO_LENGTH, &result); + if (status != napi_ok) { + free(pathResult); + return nullptr; + } + free(pathResult); // 释放临时字符串 + } else { + // 将空字符串转换为napi_value + napi_status status = napi_create_string_utf8(env, "", NAPI_AUTO_LENGTH, &result); + if (status != napi_ok) { + return nullptr; + } + } + return result; +} +// [End get_path_from_uri_example] + +// [Start get_full_directory_uri] +static napi_value NAPI_Global_OH_FileUri_GetFullDirectoryUriExample(napi_env env, napi_callback_info info) +{ + // [StartExclude get_full_directory_uri] + // 参数个数为1 + size_t argc = 1; + napi_value args[1] = { nullptr }; + + napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); + + // 获取字符串的长度,初始化字符串长度为0 + size_t strLength = 0; + napi_get_value_string_utf8(env, args[0], nullptr, 0, &strLength); + // 为 char* uri 分配内存 + // [EndExclude get_full_directory_uri] + char *uri = new char[strLength + 1]; // +1 for null terminator + // 将 JavaScript 字符串复制到 uri + napi_get_value_string_utf8(env, args[0], uri, strLength + 1, &strLength); + + unsigned int length = strlen(uri); + // 输出传入uri字符串 + OH_LOG_INFO(LogType::LOG_APP, "HiAppEvent eventInfo.WatcherType=OnTrigger: %{public}s", uri); + char *uriResult = nullptr; + FileManagement_ErrCode ret = OH_FileUri_GetFullDirectoryUri(uri, length, &uriResult); + // 输出所在路径uri字符串 + // [StartExclude get_full_directory_uri] + OH_LOG_INFO(LogType::LOG_APP, "HiAppEvent eventInfo.WatcherType=OnTrigger: %{public}s", uriResult); + napi_value result = nullptr; + // [EndExclude get_full_directory_uri] + if (ret == 0 && uriResult != nullptr) { + // 使用napi接口创建一个字符串类型的napi_value来返回正确结果 + napi_create_string_utf8(env, uriResult, NAPI_AUTO_LENGTH, &result); + } else { + // 使用napi接口创建一个表示null值的napi_value来返回错误或空值情况 + napi_get_null(env, &result); + } + if (uriResult != nullptr) { + free(uriResult); + } + return result; +} +// [Start get_full_directory_uri] + +// [Start is_valid_uri_example] +static napi_value NAPI_Global_OH_FileUri_IsValidUriExample(napi_env env, napi_callback_info info) +{ + // [StartExclude is_valid_uri_example] + // 参数个数为1 + size_t argc = 1; + napi_value args[1] = { nullptr }; + + napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); + + // 获取字符串的长度,初始化字符串长度为0 + size_t strLength = 0; + napi_get_value_string_utf8(env, args[0], nullptr, 0, &strLength); + // 为 char* uri 分配内存 + // [EndExclude is_valid_uri_example] + char *uri = new char[strLength + 1]; // +1 for null terminator + // 将 JavaScript 字符串复制到 uri + napi_get_value_string_utf8(env, args[0], uri, strLength + 1, &strLength); + unsigned int length = strlen(uri); + // 输出传入uri字符串 + OH_LOG_INFO(LogType::LOG_APP, "HiAppEvent eventInfo.WatcherType=OnTrigger: %{public}s", uri); + bool flags = OH_FileUri_IsValidUri(uri, length); + // [StartExclude is_valid_uri_example] + napi_value result; + // 将bool值转换为napi_value + napi_status status = napi_get_boolean(env, flags, &result); + if (status != napi_ok) { + // 处理错误 + napi_throw_error(env, nullptr, "Failed to convert boolean to napi_value"); + return nullptr; + } + return result; + // [EndExclude is_valid_uri_example] +} +// [End is_valid_uri_example] + +// [Start get_file_name_example] +static napi_value NAPI_Global_OH_FileUri_GetFileNameExample(napi_env env, napi_callback_info info) +{ + // [StartExclude get_file_name_example] + // 参数个数为1 + size_t argc = 1; + napi_value args[1] = { nullptr }; + + napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); + + // 获取字符串的长度,初始化字符串长度为0 + size_t strLength = 0; + napi_get_value_string_utf8(env, args[0], nullptr, 0, &strLength); + // 为 char* uri 分配内存 + // [EndExclude get_file_name_example] + char *uri = new char[strLength + 1]; // +1 for null terminator + // 将 JavaScript 字符串复制到 uri + napi_get_value_string_utf8(env, args[0], uri, strLength + 1, &strLength); + + unsigned int length = strlen(uri); + // 输出传入uri字符串 + OH_LOG_INFO(LogType::LOG_APP, "HiAppEvent eventInfo.WatcherType=OnTrigger: %{public}s", uri); + char *uriResult = nullptr; + FileManagement_ErrCode ret = OH_FileUri_GetFileName(uri, length, &uriResult); + // 输出获取到的文件名称 + // [StartExclude get_file_name_example] + OH_LOG_INFO(LogType::LOG_APP, "HiAppEvent eventInfo.WatcherType=OnTrigger: %{public}s", uriResult); + napi_value result; + // [EndExclude get_file_name_example] + if (ret == 0 && uriResult != nullptr) { + // 将C字符串转换为napi_value + napi_status status = napi_create_string_utf8(env, uriResult, NAPI_AUTO_LENGTH, &result); + if (status != napi_ok) { + free(uriResult); + return NULL; + } + free(uriResult); // 释放临时字符串 + } else { + // 将空字符串转换为napi_value + napi_status status = napi_create_string_utf8(env, "", NAPI_AUTO_LENGTH, &result); + if (status != napi_ok) { + return nullptr; + } + } + return result; +} +// [End get_file_name_example] + +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = { + {"OH_FileUri_GetUriFromPathExample", nullptr, NAPI_Global_OH_FileUri_GetUriFromPathExample, nullptr, nullptr, + nullptr, napi_default, nullptr}, + {"OH_FileUri_GetPathFromUriExample", nullptr, NAPI_Global_OH_FileUri_GetPathFromUriExample, nullptr, nullptr, + nullptr, napi_default, nullptr}, + {"OH_FileUri_GetFullDirectoryUriExample", nullptr, NAPI_Global_OH_FileUri_GetFullDirectoryUriExample, nullptr, + nullptr, nullptr, napi_default, nullptr}, + {"OH_FileUri_IsValidUriExample", nullptr, NAPI_Global_OH_FileUri_IsValidUriExample, nullptr, nullptr, nullptr, + napi_default, nullptr}, + {"OH_FileUri_GetFileNameExample", nullptr, NAPI_Global_OH_FileUri_GetFileNameExample, nullptr, nullptr, nullptr, + napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, // 模块版本号,设置为1遵循当前的 N-API 版本规范 + .nm_flags = 0, // 模块标志,设置为0,表示没有特殊标志 + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "entry", + .nm_priv = ((void *)0), + .reserved = {0}, // 保留字段,通常初始化为0 +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/CoreFile/UserFile/FileUriDevelopment_C/entry/src/main/cpp/types/libentry/Index.d.ts b/CoreFile/UserFile/FileUriDevelopment_C/entry/src/main/cpp/types/libentry/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..2385e74adf0d95611ec79daf797336daeeaa25a0 --- /dev/null +++ b/CoreFile/UserFile/FileUriDevelopment_C/entry/src/main/cpp/types/libentry/Index.d.ts @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2025 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 const OH_FileUri_GetUriFromPathExample: (path: string) => string; + +export const OH_FileUri_GetPathFromUriExample: (uri: string) => string; + +export const OH_FileUri_GetFullDirectoryUriExample: (uri: string) => string; + +export const OH_FileUri_IsValidUriExample: (uri: string) => boolean; + +export const OH_FileUri_GetFileNameExample: (uri: string) => string; \ No newline at end of file diff --git a/CoreFile/UserFile/FileUriDevelopment_C/entry/src/main/cpp/types/libentry/oh-package.json5 b/CoreFile/UserFile/FileUriDevelopment_C/entry/src/main/cpp/types/libentry/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..6abf3b7c20f22c62aaac6a995a25cae672f73f35 --- /dev/null +++ b/CoreFile/UserFile/FileUriDevelopment_C/entry/src/main/cpp/types/libentry/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 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. + */ + +{ + "name": "libentry.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/CoreFile/UserFile/FileUriDevelopment_C/entry/src/main/ets/common/Logger.ts b/CoreFile/UserFile/FileUriDevelopment_C/entry/src/main/ets/common/Logger.ts new file mode 100644 index 0000000000000000000000000000000000000000..148b9e9cacba97fab13dcc6526e2f0ed4143ff0f --- /dev/null +++ b/CoreFile/UserFile/FileUriDevelopment_C/entry/src/main/ets/common/Logger.ts @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2025 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'; + +const DOMAIN: number = 0x0000; // 此处使用自行申请的应用代码号 +const PREFIX: string = '[Sample_FileUriDevelopment_C]'; // 应用log标签 + +export class Logger { + private domain: number; + private prefix: string; + private format: string = '%{public}s'; + + constructor(prefix: string) { + this.prefix = prefix; + this.domain = DOMAIN; + } + + getDomain() { + return this.domain; + } + + getPrefix() { + return this.prefix; + } + + getFormat() { + return this.format; + } + + debug(...args: string[]) { + hilog.debug(this.domain, this.prefix, this.format, args); + } + + info(...args: string[]) { + hilog.info(this.domain, this.prefix, this.format, args); + } + + warn(...args: string[]) { + hilog.warn(this.domain, this.prefix, this.format, args); + } + + error(...args: string[]) { + hilog.error(this.domain, this.prefix, this.format, args); + } + + fatal(...args: string[]) { + hilog.fatal(this.domain, this.prefix, this.format, args); + } + + isLoggable(level: number) { + hilog.isLoggable(this.domain, this.prefix, level); + } +} + +export default new Logger(PREFIX); diff --git a/CoreFile/UserFile/FileUriDevelopment_C/entry/src/main/ets/entryability/EntryAbility.ets b/CoreFile/UserFile/FileUriDevelopment_C/entry/src/main/ets/entryability/EntryAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..c5539bddd7a772710f55da0ddd7b5676305469de --- /dev/null +++ b/CoreFile/UserFile/FileUriDevelopment_C/entry/src/main/ets/entryability/EntryAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 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/Index', (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/CoreFile/UserFile/FileUriDevelopment_C/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets b/CoreFile/UserFile/FileUriDevelopment_C/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..b74fe507979e31c9d239b7f29ea9102f6144b7cf --- /dev/null +++ b/CoreFile/UserFile/FileUriDevelopment_C/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 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 { BackupExtensionAbility, BundleVersion } from '@kit.CoreFileKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; + +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/CoreFile/UserFile/FileUriDevelopment_C/entry/src/main/ets/pages/Index.ets b/CoreFile/UserFile/FileUriDevelopment_C/entry/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..529cb201a958a3f55b0c1aff286349382b67cbe2 --- /dev/null +++ b/CoreFile/UserFile/FileUriDevelopment_C/entry/src/main/ets/pages/Index.ets @@ -0,0 +1,108 @@ +/* + * Copyright (c) 2025 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 { buffer } from '@kit.ArkTS'; +import { fileUri, fileIo as fs, ReadOptions } from '@kit.CoreFileKit'; +import common from '@ohos.app.ability.common'; +import Logger from '../common/Logger'; +import testNapi from 'libentry.so'; + +// 获取应用文件沙箱路径 +let context = getContext(this) as common.UIAbilityContext; +let filesDir = context.filesDir; +let filePath = filesDir + '/test.txt'; // 文件路径 +let uri = fileUri.getUriFromPath(filePath); // 从文件路径获取 URI + +export function createFile(): string { + // 新建并打开文件 + let file = fs.openSync(filesDir + '/test.txt', fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); + // 写入一段内容至文件 + let writeLen = fs.writeSync(file.fd, 'Try to write str : hello world'); + Logger.info('The length of str is: ' + writeLen); // 写入内容的长度 + // 从文件读取一段内容 + let arrayBuffer = new ArrayBuffer(1024); // 分配一个 1024 字节的缓冲区 + let readOptions: ReadOptions = { + offset: 0, // 从文件的开头开始读取 + length: arrayBuffer.byteLength // 读取的长度为缓冲区的大小 + }; + let readLen = fs.readSync(file.fd, arrayBuffer, readOptions); // 实际读取的字节数 + // 这个 buf 对象是 arrayBuffer 中从位置 0 开始的前 readLen 字节 + let buf = buffer.from(arrayBuffer, 0, readLen); + Logger.info('the content of file: ' + buf.toString()); // 打印文件内容 + let str = 'the content of file: ' + buf.toString(); + // 关闭文件 + fs.closeSync(file); + return str; +} + +@Entry +@Component +struct Index { + @State message: string = ''; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize($r('app.float.size_50')) // 设置字体大小为 50 + .fontWeight(FontWeight.Bold) + Button($r('app.string.invokeInterfaceButton1')) + .width('80%') // 设置按钮宽度为 80% + .height('10%') // 设置按钮高度为 10% + .margin({ top: $r('app.float.size_20') }) // 设置按钮上边距为 20 + .onClick(() => { + createFile(); + this.message = testNapi.OH_FileUri_GetUriFromPathExample(filePath); + }) + + Button($r('app.string.invokeInterfaceButton2')) + .width('80%') // 设置按钮宽度为 80% + .height('10%') // 设置按钮高度为 10% + .margin({ top: $r('app.float.size_20') }) // 设置按钮上边距为 20 + .onClick(() => { + this.message = testNapi.OH_FileUri_GetPathFromUriExample(uri); + }) + + Button($r('app.string.invokeInterfaceButton3')) + .width('80%') // 设置按钮宽度为 80% + .height('10%') // 设置按钮高度为 10% + .margin({ top: $r('app.float.size_20') }) // 设置按钮上边距为 20 + .onClick(() => { + this.message = testNapi.OH_FileUri_GetFullDirectoryUriExample(uri); + }) + + Button($r('app.string.invokeInterfaceButton4')) + .width('80%') // 设置按钮宽度为 80% + .height('10%') // 设置按钮高度为 10% + .margin({ top: $r('app.float.size_20') }) // 设置按钮上边距为 20 + .onClick(() => { + if (testNapi.OH_FileUri_IsValidUriExample(uri)) { + this.message = 'UriIsTrue'; + } + }) + + Button($r('app.string.invokeInterfaceButton5')) + .width('80%') // 设置按钮宽度为 80% + .height('10%') // 设置按钮高度为 10% + .margin({ top: $r('app.float.size_20') }) // 设置按钮上边距为 20 + .onClick(() => { + this.message = testNapi.OH_FileUri_GetFileNameExample(uri); + }) + } + .width('100%') // 设置列的宽度为 100% + } + .height('100%') // 设置行的高度为 100% + } +} \ No newline at end of file diff --git a/CoreFile/UserFile/FileUriDevelopment_C/entry/src/main/module.json5 b/CoreFile/UserFile/FileUriDevelopment_C/entry/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..4144486d1af4c03b0d767cce1cda86fc0d697f91 --- /dev/null +++ b/CoreFile/UserFile/FileUriDevelopment_C/entry/src/main/module.json5 @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2025 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. + */ + +{ + "module": { + "name": "entry", + "type": "entry", + "description": "$string:module_desc", + "mainElement": "EntryAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "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" + } + ] + } + ] + } +} \ No newline at end of file diff --git a/CoreFile/UserFile/FileUriDevelopment_C/entry/src/main/resources/base/element/color.json b/CoreFile/UserFile/FileUriDevelopment_C/entry/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..b99ebd2b9c4b3a455465e850b4eeee9548ba886a --- /dev/null +++ b/CoreFile/UserFile/FileUriDevelopment_C/entry/src/main/resources/base/element/color.json @@ -0,0 +1,24 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + }, + { + "name": "whiteColor", + "value": "#FFFFFF" + }, + { + "name": "lightGrayColor", + "value": "#F0F0F0" + }, + { + "name": "grayColor", + "value": "#808080" + }, + { + "name": "blackColor", + "value": "#000000" + } + ] +} \ No newline at end of file diff --git a/CoreFile/UserFile/FileUriDevelopment_C/entry/src/main/resources/base/element/float.json b/CoreFile/UserFile/FileUriDevelopment_C/entry/src/main/resources/base/element/float.json new file mode 100644 index 0000000000000000000000000000000000000000..30e6a4c0edf724b1459705043d7488912328bd16 --- /dev/null +++ b/CoreFile/UserFile/FileUriDevelopment_C/entry/src/main/resources/base/element/float.json @@ -0,0 +1,36 @@ +{ + "float": [ + { + "name": "size_5", + "value": "10" + }, + { + "name": "size_10", + "value": "10" + }, + { + "name": "size_13", + "value": "13" + }, + { + "name": "size_15", + "value": "15" + }, + { + "name": "size_20", + "value": "20" + }, + { + "name": "size_25", + "value": "25" + }, + { + "name": "size_40", + "value": "40" + }, + { + "name": "size_50", + "value": "50" + } + ] +} \ No newline at end of file diff --git a/CoreFile/UserFile/FileUriDevelopment_C/entry/src/main/resources/base/element/string.json b/CoreFile/UserFile/FileUriDevelopment_C/entry/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..c1fa7ac34d315f0057cd5a2f0d33f890a3d768dd --- /dev/null +++ b/CoreFile/UserFile/FileUriDevelopment_C/entry/src/main/resources/base/element/string.json @@ -0,0 +1,36 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "EntryAbility_desc", + "value": "description" + }, + { + "name": "EntryAbility_label", + "value": "FileUriDevelopment_C" + }, + { + "name": "invokeInterfaceButton1", + "value": "GetUriFromPath" + }, + { + "name": "invokeInterfaceButton2", + "value": "GetPathFromUri" + }, + { + "name": "invokeInterfaceButton3", + "value": "GetFullDirectoryUri" + }, + { + "name": "invokeInterfaceButton4", + "value": "IsValidUri" + }, + { + "name": "invokeInterfaceButton5", + "value": "GetFileName" + } + ] +} \ No newline at end of file diff --git a/CoreFile/UserFile/FileUriDevelopment_C/entry/src/main/resources/base/media/background.png b/CoreFile/UserFile/FileUriDevelopment_C/entry/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/CoreFile/UserFile/FileUriDevelopment_C/entry/src/main/resources/base/media/background.png differ diff --git a/CoreFile/UserFile/FileUriDevelopment_C/entry/src/main/resources/base/media/foreground.png b/CoreFile/UserFile/FileUriDevelopment_C/entry/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/CoreFile/UserFile/FileUriDevelopment_C/entry/src/main/resources/base/media/foreground.png differ diff --git a/CoreFile/UserFile/FileUriDevelopment_C/entry/src/main/resources/base/media/layered_image.json b/CoreFile/UserFile/FileUriDevelopment_C/entry/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/CoreFile/UserFile/FileUriDevelopment_C/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/CoreFile/UserFile/FileUriDevelopment_C/entry/src/main/resources/base/media/startIcon.png b/CoreFile/UserFile/FileUriDevelopment_C/entry/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/CoreFile/UserFile/FileUriDevelopment_C/entry/src/main/resources/base/media/startIcon.png differ diff --git a/CoreFile/UserFile/FileUriDevelopment_C/entry/src/main/resources/base/profile/backup_config.json b/CoreFile/UserFile/FileUriDevelopment_C/entry/src/main/resources/base/profile/backup_config.json new file mode 100644 index 0000000000000000000000000000000000000000..78f40ae7c494d71e2482278f359ec790ca73471a --- /dev/null +++ b/CoreFile/UserFile/FileUriDevelopment_C/entry/src/main/resources/base/profile/backup_config.json @@ -0,0 +1,3 @@ +{ + "allowToBackupRestore": true +} \ No newline at end of file diff --git a/CoreFile/UserFile/FileUriDevelopment_C/entry/src/main/resources/base/profile/main_pages.json b/CoreFile/UserFile/FileUriDevelopment_C/entry/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/CoreFile/UserFile/FileUriDevelopment_C/entry/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/CoreFile/UserFile/FileUriDevelopment_C/entry/src/main/resources/en_US/element/string.json b/CoreFile/UserFile/FileUriDevelopment_C/entry/src/main/resources/en_US/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..db4dedd6b71384eef6ce60ec319fedbaf5b5f99f --- /dev/null +++ b/CoreFile/UserFile/FileUriDevelopment_C/entry/src/main/resources/en_US/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "EntryAbility_desc", + "value": "description" + }, + { + "name": "EntryAbility_label", + "value": "FileUriDevelopment_C" + } + ] +} \ No newline at end of file diff --git a/CoreFile/UserFile/FileUriDevelopment_C/entry/src/main/resources/zh_CN/element/string.json b/CoreFile/UserFile/FileUriDevelopment_C/entry/src/main/resources/zh_CN/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..5cb723ed88048c84158b1ac0b1d423cdfc5567f6 --- /dev/null +++ b/CoreFile/UserFile/FileUriDevelopment_C/entry/src/main/resources/zh_CN/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "模块描述" + }, + { + "name": "EntryAbility_desc", + "value": "description" + }, + { + "name": "EntryAbility_label", + "value": "FileUriDevelopment_C" + } + ] +} \ No newline at end of file diff --git a/CoreFile/UserFile/FileUriDevelopment_C/entry/src/ohosTest/ets/test/Ability.test.ets b/CoreFile/UserFile/FileUriDevelopment_C/entry/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..d1adb0ac477672784a3591e55bcd03a7b9ade3f3 --- /dev/null +++ b/CoreFile/UserFile/FileUriDevelopment_C/entry/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,182 @@ +/* + * Copyright (c) 2025 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. + */ + +// 导入测试依赖kit +import { abilityDelegatorRegistry, Driver, ON } from '@kit.TestKit'; +import { describe, it, expect } from '@ohos/hypium'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { UIAbility, Want } from '@kit.AbilityKit'; + +const TAG = '[Sample_FileUriDevelopment_c]'; +const DOMAIN = 0xF811; +const BUNDLE = 'FileUriDevelopment_c_'; +const DELAY_TIME_1S = 1000; + +const DELEGATOR: abilityDelegatorRegistry.AbilityDelegator = abilityDelegatorRegistry.getAbilityDelegator(); +const BUNDLENAME = abilityDelegatorRegistry.getArguments().bundleName; + +export default function abilityTest() { + describe('FileUriDevelopment_cTest', () => { + /** + * @tc.number CoreFileKit_StartAbility_001 + * @tc.name CoreFileKit_StartAbility_001 + * @tc.desc 启动Ability + */ + it(BUNDLE + 'StartAbility_001', 0, async (done: Function) => { + hilog.info(DOMAIN, TAG, BUNDLE + 'StartAbility_001,begin'); + // start tested ability + const want: Want = { + bundleName: BUNDLENAME, + abilityName: 'EntryAbility' + }; + await DELEGATOR.startAbility(want); + let driver: Driver = Driver.create(); + await driver.delayMs(DELAY_TIME_1S); + // check top display ability + const ability: UIAbility = await DELEGATOR.getCurrentTopAbility(); + hilog.info(DOMAIN, TAG, BUNDLE + 'StartAbility_001,get top ability'); + expect(ability.context.abilityInfo.name).assertEqual('EntryAbility'); + done(); + hilog.info(DOMAIN, TAG, BUNDLE + 'StartAbility_001,end'); + }) + /** + * @tc.number CoreFileKit_InterfaceTest_001 + * @tc.name CoreFileKit_InterfaceTest_001 + * @tc.desc 测试接口OH_FileUri_GetUriFromPath + */ + it(BUNDLE + 'InterfaceTest_001', 0, async (done: Function) => { + hilog.info(DOMAIN, TAG, BUNDLE + 'InterfaceTest_001, begin'); + // 初始化 Driver,用于 UI 操作 + const driver = Driver.create(); + // 等待 1000 毫秒,确保 UI 渲染完成 + await driver.delayMs(DELAY_TIME_1S); + + // 查找按钮组件 + const getUriFromPathButton = await driver.findComponent(ON.text('GetUriFromPath')); + // 点击按钮 + await getUriFromPathButton.click(); + // 等待 1000 毫秒,确保点击后的操作完成 + await driver.delayMs(DELAY_TIME_1S); + // 检查文本组件是否改变 + await driver.assertComponentExist( + ON.text('file://' + BUNDLENAME + '/data/storage/el2/base/haps/entry/files/test.txt') + ); + + // 标记测试用例完成 + hilog.info(DOMAIN, TAG, BUNDLE + 'InterfaceTest_001, end'); + done(); + }) + /** + * @tc.number CoreFileKit_InterfaceTest_002 + * @tc.name CoreFileKit_InterfaceTest_002 + * @tc.desc 测试接口OH_FileUri_GetPathFromUri + */ + it(BUNDLE + 'InterfaceTest_002', 0, async (done: Function) => { + hilog.info(DOMAIN, TAG, BUNDLE + 'InterfaceTest_002, begin'); + // 初始化 Driver,用于 UI 操作 + const driver = Driver.create(); + // 等待 1000 毫秒,确保 UI 渲染完成 + await driver.delayMs(DELAY_TIME_1S); + + // 查找按钮组件 + const getPathFromUriButton = await driver.findComponent(ON.text('GetPathFromUri')); + // 点击按钮 + await getPathFromUriButton.click(); + // 等待 1000 毫秒,确保点击后的操作完成 + await driver.delayMs(DELAY_TIME_1S); + // 检查文本组件是否改变 + await driver.assertComponentExist(ON.text('/data/storage/el2/base/haps/entry/files/test.txt')); + + // 标记测试用例完成 + hilog.info(DOMAIN, TAG, BUNDLE + 'InterfaceTest_002, end'); + done(); + }) + /** + * @tc.number CoreFileKit_InterfaceTest_003 + * @tc.name CoreFileKit_InterfaceTest_003 + * @tc.desc 测试接口OH_FileUri_GetFullDirectoryUri + */ + it(BUNDLE + 'InterfaceTest_003', 0, async (done: Function) => { + hilog.info(DOMAIN, TAG, BUNDLE + 'InterfaceTest_003, begin'); + // 初始化 Driver,用于 UI 操作 + const driver = Driver.create(); + // 等待 1000 毫秒,确保 UI 渲染完成 + await driver.delayMs(DELAY_TIME_1S); + + // 查找按钮组件 + const getFullDirectoryUriButton = await driver.findComponent(ON.text('GetFullDirectoryUri')); + // 点击按钮 + await getFullDirectoryUriButton.click(); + // 等待 1000 毫秒,确保点击后的操作完成 + await driver.delayMs(DELAY_TIME_1S); + // 检查文本组件是否改变 + await driver.assertComponentExist(ON.text('file://' + BUNDLENAME + '/data/storage/el2/base/haps/entry/files')); + + // 标记测试用例完成 + hilog.info(DOMAIN, TAG, BUNDLE + 'InterfaceTest_003, end'); + done(); + }) + /** + * @tc.number CoreFileKit_InterfaceTest_004 + * @tc.name CoreFileKit_InterfaceTest_004 + * @tc.desc 测试接口OH_FileUri_IsValidUri + */ + it(BUNDLE + 'InterfaceTest_004', 0, async (done: Function) => { + hilog.info(DOMAIN, TAG, BUNDLE + 'InterfaceTest_004, begin'); + // 初始化 Driver,用于 UI 操作 + const driver = Driver.create(); + // 等待 1000 毫秒,确保 UI 渲染完成 + await driver.delayMs(DELAY_TIME_1S); + + // 查找按钮组件 + const isValidUriButton = await driver.findComponent(ON.text('IsValidUri')); + // 点击按钮 + await isValidUriButton.click(); + // 等待 1000 毫秒,确保点击后的操作完成 + await driver.delayMs(DELAY_TIME_1S); + // 检查文本组件是否改变 + await driver.assertComponentExist(ON.text('UriIsTrue')); + + // 标记测试用例完成 + hilog.info(DOMAIN, TAG, BUNDLE + 'InterfaceTest_004, end'); + done(); + }) + /** + * @tc.number CoreFileKit_InterfaceTest_005 + * @tc.name CoreFileKit_InterfaceTest_005 + * @tc.desc 测试接口OH_FileUri_GetFileName + */ + it(BUNDLE + 'InterfaceTest_005', 0, async (done: Function) => { + hilog.info(DOMAIN, TAG, BUNDLE + 'InterfaceTest_005, begin'); + // 初始化 Driver,用于 UI 操作 + const driver = Driver.create(); + // 等待 1000 毫秒,确保 UI 渲染完成 + await driver.delayMs(DELAY_TIME_1S); + + // 查找按钮组件 + const getFileNameButton = await driver.findComponent(ON.text('GetFileName')); + // 点击按钮 + await getFileNameButton.click(); + // 等待 1000 毫秒,确保点击后的操作完成 + await driver.delayMs(DELAY_TIME_1S); + // 检查文本组件是否改变 + await driver.assertComponentExist(ON.text('test.txt')); + + // 标记测试用例完成 + hilog.info(DOMAIN, TAG, BUNDLE + 'InterfaceTest_005, end'); + done(); + }) + }) +} \ No newline at end of file diff --git a/CoreFile/UserFile/FileUriDevelopment_C/entry/src/ohosTest/ets/test/List.test.ets b/CoreFile/UserFile/FileUriDevelopment_C/entry/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/CoreFile/UserFile/FileUriDevelopment_C/entry/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 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 abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/CoreFile/UserFile/FileUriDevelopment_C/entry/src/ohosTest/module.json5 b/CoreFile/UserFile/FileUriDevelopment_C/entry/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..c3fd9dda3040d888d9d8b0b62bcb5d3b6fbeb614 --- /dev/null +++ b/CoreFile/UserFile/FileUriDevelopment_C/entry/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 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. + */ + +{ + "module": { + "name": "entry_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/CoreFile/UserFile/FileUriDevelopment_C/hvigor/hvigor-config.json5 b/CoreFile/UserFile/FileUriDevelopment_C/hvigor/hvigor-config.json5 new file mode 100644 index 0000000000000000000000000000000000000000..43beb743cbd25c3507b1cf8a744bf8197b3bf2fb --- /dev/null +++ b/CoreFile/UserFile/FileUriDevelopment_C/hvigor/hvigor-config.json5 @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2025 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. + */ + +{ + "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/CoreFile/UserFile/FileUriDevelopment_C/hvigorfile.ts b/CoreFile/UserFile/FileUriDevelopment_C/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..2a5e543f190732c159beb574dfc9fa37bc94e156 --- /dev/null +++ b/CoreFile/UserFile/FileUriDevelopment_C/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 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 { 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/CoreFile/UserFile/FileUriDevelopment_C/oh-package.json5 b/CoreFile/UserFile/FileUriDevelopment_C/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..93f097993a458e967d6d5239ea0580e79b5d6998 --- /dev/null +++ b/CoreFile/UserFile/FileUriDevelopment_C/oh-package.json5 @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 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. + */ + +{ + "modelVersion": "5.0.0", + "description": "Please describe the basic information.", + "dependencies": { + }, + "devDependencies": { + "@ohos/hypium": "1.0.19", + "@ohos/hamock": "1.0.0" + } +} diff --git a/CoreFile/UserFile/FileUriDevelopment_C/ohosTest.md b/CoreFile/UserFile/FileUriDevelopment_C/ohosTest.md new file mode 100644 index 0000000000000000000000000000000000000000..7e0bd48c959710787f1d4a3416f8489c873cbf2d --- /dev/null +++ b/CoreFile/UserFile/FileUriDevelopment_C/ohosTest.md @@ -0,0 +1,13 @@ +# FileUriDevelopment_C 测试用例归档 + +## 用例表 + +| 测试功能 | 预置条件 | 输入 | 预期输出 | 是否自动 | 测试结果 | +| ---------------------------------- | ------------------ | ---- | ---------------------------------------- | -------- | -------- | +| 拉起应用 | 设备正常运行 | | 成功拉起应用 | 是 | Pass | +| 调用OH_FileUri_GetUriFromPath | 安装应用,进入首页 | | 获取到的URI输出到应用页面文本框内 | 是 | Pass | +| 调用OH_FileUri_GetPathFromUri | 安装应用,进入首页 | | 获取到的PATH输出到应用页面文本框内 | 是 | Pass | +| 调用OH_FileUri_GetFullDirectoryUri | 安装应用,进入首页 | | 获取URI所在路径的URI输出到应用页面文本框 | 是 | Pass | +| 调用OH_FileUri_IsValidUri | 安装应用,进入首页 | | 将URI判断结果输出到应用页面文本框 | 是 | Pass | +| 调用OH_FileUri_GetFileName | 安装应用,进入首页 | | 获取URI中的文件名称输出到应用页面文本框 | 是 | Pass | +