add sensorDemo sample
Signed-off-by: li-yaoyao777 <liyaoyao7@huawei.com>
9
OAT.xml
@ -272,6 +272,15 @@ Note:If the text contains special characters, please escape them according to th
|
||||
<filteritem type="filepath" name="code/SystemFeature/Security/DLP/screenshots/haps/DlpManager.hap" desc="Provided by DlpManager"/>
|
||||
<filteritem type="filepath" name="code/SystemFeature/Security/DLP/screenshots/haps/FileEdit.hap" desc="Provided by FileEdit"/>
|
||||
<filteritem type="filepath" name="code/BasicFeature/DeviceManagement/Sensor/Capi/libs/library.har" desc="Provided by code/BasicFeature/DeviceManagement/Sensor"/>
|
||||
<filteritem type="filepath" name="code/DocsSample/Sensor/entry/src/main/resources/base/media/background.png" desc="Provided by code/DocsSample/Sensor"/>
|
||||
<filteritem type="filepath" name="code/DocsSample/Sensor/entry/src/main/resources/base/media/foreground.png" desc="Provided by code/DocsSample/Sensor"/>
|
||||
<filteritem type="filepath" name="code/DocsSample/Sensor/entry/src/main/resources/base/media/layered_image.png" desc="Provided by code/DocsSample/Sensor"/>
|
||||
<filteritem type="filepath" name="code/DocsSample/Sensor/entry/src/main/resources/base/media/startIcon.png" desc="Provided by code/DocsSample/Sensor"/>
|
||||
<filteritem type="filepath" name="code/DocsSample/Sensor/AppScope/resources/base/media/app_icon.png" desc="Provided by code/DocsSample/Sensor"/>
|
||||
<filteritem type="filepath" name="code/DocsSample/Sensor/screenshots/devices/homePage.jpeg" desc="Provided by code/DocsSample/Sensor"/>
|
||||
<filteritem type="filepath" name="code/DocsSample/Sensor/screenshots/devices/sampleRate.jpeg" desc="Provided by code/DocsSample/Sensor"/>
|
||||
<filteritem type="filepath" name="code/DocsSample/Sensor/screenshots/devices/sensorType.jpeg" desc="Provided by code/DocsSample/Sensor"/>
|
||||
<filteritem type="filepath" name="code/DocsSample/Sensor/screenshots/devices/subscribe.jpeg" desc="Provided by code/DocsSample/Sensor"/>
|
||||
<filteritem type="filepath" name="code/Solutions/Media/MultiMedia/lib/VideoPlayer.hap" desc="Provided by code/Solutions/Media/MultiMedia"/>
|
||||
<filteritem type="filepath" name="code/Solutions/Media/MultiMedia/lib/CameraPage-1.0.0.hap" desc="Provided by code/Solutions/Media/MultiMedia"/>
|
||||
<filteritem type="filepath" name="code/BasicFeature/Media/AVRecorder/lib/VideoRecorder-1.0.0.hap" desc="Provided by code/BasicFeature/Media/AVRecorder"/>
|
||||
|
12
code/DocsSample/Sensor/.gitignore
vendored
Normal file
@ -0,0 +1,12 @@
|
||||
/node_modules
|
||||
/oh_modules
|
||||
/local.properties
|
||||
/.idea
|
||||
**/build
|
||||
/.hvigor
|
||||
.cxx
|
||||
/.clangd
|
||||
/.clang-format
|
||||
/.clang-tidy
|
||||
**/.test
|
||||
/.appanalyzer
|
25
code/DocsSample/Sensor/AppScope/app.json5
Normal file
@ -0,0 +1,25 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
{
|
||||
"app": {
|
||||
"bundleName": "com.samples.sensor",
|
||||
"vendor": "example",
|
||||
"versionCode": 1000000,
|
||||
"versionName": "1.0.0",
|
||||
"icon": "$media:app_icon",
|
||||
"label": "$string:app_name"
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
{
|
||||
"string": [
|
||||
{
|
||||
"name": "app_name",
|
||||
"value": "Sensor"
|
||||
}
|
||||
]
|
||||
}
|
After Width: | Height: | Size: 2.7 KiB |
90
code/DocsSample/Sensor/README.md
Normal file
@ -0,0 +1,90 @@
|
||||
# 订阅/取消订阅传感器
|
||||
|
||||
### 介绍
|
||||
|
||||
本示例主要展示了传感器的基础功能,包括传感器订阅和取消传感器订阅。
|
||||
|
||||
### 效果预览
|
||||
|
||||
| 首页 | 选择传感器类型 | 数据上报 | 输入数据采样频率 |
|
||||
| ------------------------------------------------------------ | ------------------------------------------------------------ | ------------------------------------------------------------ | ------------------------------------------------------------ |
|
||||
| <img src="screenshots/devices/homePage.jpeg" alt="home" style="zoom:40%;" /> | <img src="screenshots/devices/sampleRate.jpeg" alt="play1" style="zoom:40%;" /> | <img src="screenshots/devices/sensorType.jpeg" alt="play1" style="zoom:40%;" /> | <img src="screenshots/devices/subscribe.jpeg" alt="play1" style="zoom:40%;" /> |
|
||||
|
||||
使用说明:
|
||||
|
||||
1.点击“input data sampleRate”按钮,输入100000000;该属性有最小值和最大值的限制,由硬件支持的上报频率决定,当设置频率大于最大值时以最大值上报数据,小于最小值时以最小值上报数据,置空则以默认频率上报;
|
||||
|
||||
2.在主界面点击”Please select sensor type“按钮,进入选择传感器类型页面;
|
||||
|
||||
3.在选择传感器页面选择需要订阅的传感器类型后,点击“订阅”按钮,持续订阅传感器数据会出现在下方;
|
||||
|
||||
4.如果需要取消订阅,点击“取消订阅”按钮,即可取消订阅传感器数据;
|
||||
|
||||
5.点击“订阅一次”按钮,将会上报订阅传感器一次的数据;
|
||||
|
||||
6.需要订阅其他传感器之前,需要先取消订阅本次传感器数据。
|
||||
|
||||
### 工程目录
|
||||
|
||||
```
|
||||
entry/src/main/
|
||||
|---ets
|
||||
|---|---common
|
||||
|---|---|---contants
|
||||
|---|---|---|---CommonConstants.ets // 用于存放应用程序共享的库文件和资源
|
||||
|---|---|---utils
|
||||
|---|---|---|---CommonUtils.ets // 共享资源库
|
||||
|---|---|---|---Logger.ets // 日志打印工具
|
||||
|---|---|---|---Utils.ets // 日志工具
|
||||
|---|---entryability
|
||||
|---|---|---EntryAbility.ets // 应用入口
|
||||
|---|---EntryBackupAbility
|
||||
|---|---|---EntryBackupAbility.ets // 备份恢复能力
|
||||
|---|---pages
|
||||
|---|---|---Index.ets // 订阅传感器,取消订阅传感器
|
||||
|---resources // 静态资源文件(图形、多媒体等)
|
||||
|
||||
```
|
||||
|
||||
### 具体实现
|
||||
|
||||
- 图片功能在Index中实现,具体参考[Index.ets](entry/src/main/ets/pages/Index.ets):
|
||||
- 点击“Please select sensor type”按钮,通过[sensor.getSensorListSync()](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-sensor-service-kit/js-apis-sensor.md#sensorgetsensorlistsync12)获取设备支持的传感器类型;
|
||||
- 点击“订阅”按钮,通过[sensor.on()](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-sensor-service-kit/js-apis-sensor.md#sensoron)订阅传感器,获取传感器数据;
|
||||
- 点击"取消订阅"按钮,通过[sensor.off()](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-sensor-service-kit/js-apis-sensor.md#sensoroff),取消订阅传感器。
|
||||
- 点击"订阅一次"按钮,通过[sensor.once()](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-sensor-service-kit/js-apis-sensor.md#sensoronce),取消订阅传感器。
|
||||
|
||||
### 相关权限
|
||||
|
||||
[ohos.permission.ACCELEROMETER](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-sensor-service-kit/js-apis-sensor.md#accelerometer9)
|
||||
|
||||
[ohos.permission.GYROSCOPE](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-sensor-service-kit/js-apis-sensor.md#gyroscope9)
|
||||
|
||||
[ohos.permission.ACTIVITY_MOTION](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-sensor-service-kit/js-apis-sensor.md#pedometer9)
|
||||
|
||||
[ohos.permission.READ_HEALTH_DATA](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-sensor-service-kit/js-apis-sensor.md#heart_rate9)
|
||||
|
||||
### 依赖
|
||||
|
||||
不涉及。
|
||||
|
||||
### 约束与限制
|
||||
|
||||
1. 本示例仅支持标准系统上运行,支持设备:RK3568、Phone、Tablet;
|
||||
2. 本示例为Stage模型,支持API14版本SDK,版本号:5.0.2.43;
|
||||
3. 本示例需要使用DevEco Studio Release(5.0.3.900)及以上版本才可编译运行;
|
||||
4. 传感器数据订阅和取消订阅接口成对调用,当不再需要订阅传感器数据时,开发者需要调用取消订阅接口停止数据上报;
|
||||
5. 本示例涉及系统接口,需要配置系统应用签名,可以参考[特殊权限配置方法](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/security/hapsigntool-overview.md),把配置文件中的“app-feature”字段信息改为“hos_system_app”。
|
||||
|
||||
### 下载
|
||||
|
||||
如需单独下载本工程,执行如下命令:
|
||||
|
||||
```
|
||||
git init
|
||||
git config core.sparsecheckout true
|
||||
echo code/DocsSample/Sensor > .git/info/sparse-checkout
|
||||
git remote add origin https://gitee.com/openharmony/applications_app_samples.git
|
||||
git pull origin master
|
||||
```
|
||||
|
51
code/DocsSample/Sensor/build-profile.json5
Normal file
@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright (c) 2024 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
{
|
||||
"app": {
|
||||
"signingConfigs": [],
|
||||
"products": [
|
||||
{
|
||||
"name": "default",
|
||||
"signingConfig": "default",
|
||||
"compileSdkVersion": 14,
|
||||
"compatibleSdkVersion": 14,
|
||||
"runtimeOS": "OpenHarmony"
|
||||
}
|
||||
],
|
||||
"buildModeSet": [
|
||||
{
|
||||
"name": "debug",
|
||||
},
|
||||
{
|
||||
"name": "release"
|
||||
}
|
||||
]
|
||||
},
|
||||
"modules": [
|
||||
{
|
||||
"name": "entry",
|
||||
"srcPath": "./entry",
|
||||
"targets": [
|
||||
{
|
||||
"name": "default",
|
||||
"applyToProducts": [
|
||||
"default"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
35
code/DocsSample/Sensor/code-linter.json5
Normal file
@ -0,0 +1,35 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
{
|
||||
"files": [
|
||||
"**/*.ets"
|
||||
],
|
||||
"ignore": [
|
||||
"**/src/ohosTest/**/*",
|
||||
"**/src/test/**/*",
|
||||
"**/src/mock/**/*",
|
||||
"**/node_modules/**/*",
|
||||
"**/oh_modules/**/*",
|
||||
"**/build/**/*",
|
||||
"**/.preview/**/*"
|
||||
],
|
||||
"ruleSet": [
|
||||
"plugin:@performance/recommended",
|
||||
"plugin:@typescript-eslint/recommended"
|
||||
],
|
||||
"rules": {
|
||||
}
|
||||
}
|
6
code/DocsSample/Sensor/entry/.gitignore
vendored
Normal file
@ -0,0 +1,6 @@
|
||||
/node_modules
|
||||
/oh_modules
|
||||
/.preview
|
||||
/build
|
||||
/.cxx
|
||||
/.test
|
42
code/DocsSample/Sensor/entry/build-profile.json5
Normal file
@ -0,0 +1,42 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
{
|
||||
"apiType": "stageMode",
|
||||
"buildOption": {
|
||||
},
|
||||
"buildOptionSet": [
|
||||
{
|
||||
"name": "release",
|
||||
"arkOptions": {
|
||||
"obfuscation" : {
|
||||
"ruleOptions":{
|
||||
"enable": false,
|
||||
"files": ["./obfuscation-rules.txt"]
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
],
|
||||
"targets": [
|
||||
{
|
||||
"name": "default"
|
||||
}
|
||||
,
|
||||
{
|
||||
"name": "ohosTest",
|
||||
}
|
||||
]
|
||||
}
|
6
code/DocsSample/Sensor/entry/hvigorfile.ts
Normal file
@ -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. */
|
||||
}
|
23
code/DocsSample/Sensor/entry/obfuscation-rules.txt
Normal file
@ -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
|
9
code/DocsSample/Sensor/entry/oh-package.json5
Normal file
@ -0,0 +1,9 @@
|
||||
{
|
||||
"name": "entry",
|
||||
"version": "1.0.0",
|
||||
"description": "Please describe the basic information.",
|
||||
"main": "",
|
||||
"author": "",
|
||||
"license": "",
|
||||
"dependencies": {}
|
||||
}
|
@ -0,0 +1,135 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Common constants for all features.
|
||||
*/
|
||||
|
||||
export default class CommonConstants {
|
||||
/**
|
||||
* The entry ability tag.
|
||||
*/
|
||||
static readonly TAG_ABILITY: string = 'EntryAbility';
|
||||
|
||||
/**
|
||||
* The home page tag.
|
||||
*/
|
||||
static readonly TAG_HOME: string = 'HomePage';
|
||||
|
||||
/**
|
||||
* The Common utils tag.
|
||||
*/
|
||||
static readonly MY_TAG: string = 'wzh';
|
||||
|
||||
/**
|
||||
* The Common utils tag.
|
||||
*/
|
||||
static readonly TAG_COMMON_UTILS: string = 'CommonUtils';
|
||||
|
||||
/**
|
||||
* Custom dialog widget tag.
|
||||
*/
|
||||
static readonly TAG_CUSTOM: string = 'CustomDialogWidget';
|
||||
|
||||
/**
|
||||
* Width the percentage of the 100.
|
||||
*/
|
||||
static readonly FULL_WIDTH: string = '100%';
|
||||
|
||||
/**
|
||||
* Height the percentage of the 100.
|
||||
*/
|
||||
static readonly FULL_HEIGHT: string = '100%';
|
||||
|
||||
/**
|
||||
* Date year
|
||||
*/
|
||||
static readonly DATE_YEAR: string = '年';
|
||||
|
||||
/**
|
||||
* Date month
|
||||
*/
|
||||
static readonly DATE_MONTH: string = '月';
|
||||
|
||||
/**
|
||||
* Date day
|
||||
*/
|
||||
static readonly DATE_DAY: string = '日';
|
||||
|
||||
/**
|
||||
* Date dialog start time.
|
||||
*/
|
||||
static readonly START_TIME: string = '1900-1-1';
|
||||
|
||||
/**
|
||||
* Date dialog select time.
|
||||
*/
|
||||
static readonly SELECT_TIME: string = '1990-1-1';
|
||||
|
||||
/**
|
||||
* Image back margin left of the 7.2.
|
||||
*/
|
||||
static readonly BACK_MARGIN_LEFT: string = '7.2%';
|
||||
|
||||
/**
|
||||
* Image avatar margin top of the 5.5.
|
||||
*/
|
||||
static readonly AVATAR_MARGIN_TOP: string = '5.5%';
|
||||
|
||||
/**
|
||||
* Text personal margin top of the 2.1.
|
||||
*/
|
||||
static readonly PERSONAL_MARGIN_TOP: string = '2.1%';
|
||||
|
||||
/**
|
||||
* Common dialog width of the 93.3.
|
||||
*/
|
||||
static readonly COMMON_DIALOG_WIDTH: string = '93.3%';
|
||||
|
||||
/**
|
||||
* Join comma
|
||||
*/
|
||||
static readonly COMMA: string = ',';
|
||||
|
||||
/**
|
||||
* Image back margin top of the 19.
|
||||
*/
|
||||
static readonly BACK_MARGIN_TOP: number = 19;
|
||||
|
||||
/**
|
||||
* Font weight of the 500.
|
||||
*/
|
||||
static readonly BIGGER: number = 500;
|
||||
|
||||
/**
|
||||
* Dialog y-axis offset distance of the 20.
|
||||
*/
|
||||
static readonly DY_OFFSET: number = -20;
|
||||
|
||||
/**
|
||||
* Current month plus one.
|
||||
*/
|
||||
static readonly PLUS_ONE: number = 1;
|
||||
|
||||
/**
|
||||
* Layout weight size of the 1.
|
||||
*/
|
||||
static readonly WEIGHT_ONE: number = 1;
|
||||
|
||||
/**
|
||||
* Text common max lines of the 1.
|
||||
*/
|
||||
static readonly ONE_LINES: number = 1;
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
/*
|
||||
* 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 Logger from './Logger';
|
||||
import CommonConstants from '../constants/CommonConstants';
|
||||
|
||||
/**
|
||||
* This is a pop-up window tool class, which is used to encapsulate dialog code.
|
||||
* Developers can directly invoke the methods in.
|
||||
*/
|
||||
export class CommonUtils {
|
||||
textPickerDialog(sensorItems: string[], sensorNameCallback: (sensorName: string) => void) {
|
||||
TextPickerDialog.show({
|
||||
range: sensorItems,
|
||||
selected: 0,
|
||||
onAccept: (result: TextPickerResult) => {
|
||||
sensorNameCallback(result.value.toString());
|
||||
Logger.info(CommonConstants.MY_TAG, result.value.toString());
|
||||
},
|
||||
onCancel: () => {
|
||||
Logger.info(CommonConstants.MY_TAG, 'TextPickerDialog onCancel');
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export default new CommonUtils();
|
@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright (c) 2024 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import hilog from '@ohos.hilog';
|
||||
|
||||
class Logger {
|
||||
private domain: number;
|
||||
private prefix: string;
|
||||
private format: string = '%{public}s, %{public}s';
|
||||
|
||||
/**
|
||||
* constructor.
|
||||
*
|
||||
* @param Prefix Identifies the log tag.
|
||||
* @param domain Domain Indicates the service domain, which is a hexadecimal integer ranging from 0x0 to 0xFFFFF.
|
||||
*/
|
||||
constructor(prefix: string = 'MyApp', domain: number = 0xFF00) {
|
||||
this.prefix = prefix;
|
||||
this.domain = domain;
|
||||
}
|
||||
|
||||
debug(...args: string[]): void {
|
||||
hilog.debug(this.domain, this.prefix, this.format, args);
|
||||
}
|
||||
|
||||
info(...args: string[]): void {
|
||||
hilog.info(this.domain, this.prefix, this.format, args);
|
||||
}
|
||||
|
||||
warn(...args: string[]): void {
|
||||
hilog.warn(this.domain, this.prefix, this.format, args);
|
||||
}
|
||||
|
||||
error(...args: string[]): void {
|
||||
hilog.error(this.domain, this.prefix, this.format, args);
|
||||
}
|
||||
}
|
||||
|
||||
export default new Logger('MySensorDemo', 0xFF00);
|
@ -0,0 +1,32 @@
|
||||
/*
|
||||
* 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 prompt from '@system.prompt';
|
||||
import abilityAccessCtrl from '@ohos.abilityAccessCtrl';
|
||||
import Logger from '../utils/Logger'
|
||||
|
||||
let TAG = 'wzh: ';
|
||||
|
||||
class Utils {
|
||||
public static TAG:string = 'wzh: ';
|
||||
public static atManager = abilityAccessCtrl.createAtManager();
|
||||
toast(msg:string) {
|
||||
Logger.info('toast in');
|
||||
prompt.showToast({
|
||||
message: msg
|
||||
});
|
||||
};
|
||||
}
|
||||
export default new Utils();
|
@ -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 Logger from '../common/utils/Logger';
|
||||
import { window } from '@kit.ArkUI';
|
||||
|
||||
export default class EntryAbility extends UIAbility {
|
||||
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
|
||||
Logger.info('testTag', '%{public}s', 'Ability onCreate');
|
||||
}
|
||||
|
||||
onDestroy(): void {
|
||||
Logger.info('testTag', '%{public}s', 'Ability onDestroy');
|
||||
}
|
||||
|
||||
onWindowStageCreate(windowStage: window.WindowStage): void {
|
||||
// Main window is created, set main page for this ability
|
||||
Logger.info('testTag', '%{public}s', 'Ability onWindowStageCreate');
|
||||
|
||||
windowStage.loadContent('pages/Index', (err) => {
|
||||
if (err.code) {
|
||||
Logger.error('testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
|
||||
return;
|
||||
}
|
||||
Logger.info('testTag', 'Succeeded in loading the content.');
|
||||
});
|
||||
}
|
||||
|
||||
onWindowStageDestroy(): void {
|
||||
// Main window is destroyed, release UI related resources
|
||||
Logger.info('testTag', '%{public}s', 'Ability onWindowStageDestroy');
|
||||
}
|
||||
|
||||
onForeground(): void {
|
||||
// Ability has brought to foreground
|
||||
Logger.info('testTag', '%{public}s', 'Ability onForeground');
|
||||
}
|
||||
|
||||
onBackground(): void {
|
||||
// Ability has back to background
|
||||
Logger.info('testTag', '%{public}s', 'Ability onBackground');
|
||||
}
|
||||
}
|
@ -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 Logger from '../common/utils/Logger';
|
||||
import { BackupExtensionAbility, BundleVersion } from '@kit.CoreFileKit';
|
||||
|
||||
export default class EntryBackupAbility extends BackupExtensionAbility {
|
||||
async onBackup () {
|
||||
Logger.info('testTag', 'onBackup ok');
|
||||
}
|
||||
|
||||
async onRestore (bundleVersion : BundleVersion) {
|
||||
Logger.info('testTag', 'onRestore ok %{public}s', JSON.stringify(bundleVersion));
|
||||
}
|
||||
}
|
223
code/DocsSample/Sensor/entry/src/main/ets/pages/Index.ets
Normal file
@ -0,0 +1,223 @@
|
||||
import abilityAccessCtrl from '@ohos.abilityAccessCtrl';
|
||||
import common from '@ohos.app.ability.common';
|
||||
import CommonUtils from '../common/utils/CommonUtils';
|
||||
import sensor from '@ohos.sensor';
|
||||
import Utils from '../common/utils/Utils';
|
||||
import Logger from '../common/utils/Logger';
|
||||
|
||||
const TAG = '[Sample_Sensor]';
|
||||
const DOMAIN = 0xF811;
|
||||
const BUNDLE = 'Sensor_';
|
||||
|
||||
@Entry
|
||||
@Component
|
||||
struct SensorList {
|
||||
@State
|
||||
private sensorItems: string[] = [];
|
||||
private sampleRate: number = 200000000;
|
||||
@State
|
||||
private realSensorName: string = '';
|
||||
private sensors = new Map<string, number>();
|
||||
@State
|
||||
private callbackContent: string = "";
|
||||
private supportSensorIdList: (number | undefined)[] = [1, 2, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
|
||||
256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268, 269, 277, 278, 279, 280, 281, 282];
|
||||
private sensorType = new Map<string, number>([
|
||||
['sensor_test', 0],
|
||||
['ACCELEROMETER', 1],
|
||||
['GYROSCOPE', 2],
|
||||
['AMBIENT_LIGHT', 5],
|
||||
['MAGNETIC_FIELD', 6],
|
||||
['CAPACITIVE', 7],
|
||||
['BAROMETER', 8],
|
||||
['TEMPERATURE', 9],
|
||||
['HALL', 10],
|
||||
['GESTURE', 11],
|
||||
['PROXIMITY', 12],
|
||||
['HUMIDITY', 13],
|
||||
['COLOR', 14],
|
||||
['SAR', 15],
|
||||
['AMBIENT_LIGHT1', 16],
|
||||
['SENSOR_TYPE_ID_HALL_EXT', 17],
|
||||
['PROXIMITY1', 18],
|
||||
['SENSOR_TYPE_ID_PHYSICAL_MAX', 0xFF],
|
||||
['ORIENTATION', 256],
|
||||
['GRAVITY', 257],
|
||||
['LINEAR_ACCELEROMETER', 258],
|
||||
['ROTATION_VECTOR', 259],
|
||||
['AMBIENT_TEMPERATURE', 260],
|
||||
['MAGNETIC_FIELD_UNCALIBRATED', 261],
|
||||
['GAME_ROTATION_VECTOR', 262],
|
||||
['GYROSCOPE_UNCALIBRATED', 263],
|
||||
['SIGNIFICANT_MOTION', 264],
|
||||
['PEDOMETER_DETECTION', 265],
|
||||
['PEDOMETER', 266],
|
||||
['POSTURE', 267],
|
||||
['HEAD_POSTURE', 268],
|
||||
['DROP_DETECTION', 269],
|
||||
['GEOMAGNETIC_ROTATION_VECTOR', 277],
|
||||
['HEART_RATE', 278],
|
||||
['DEVICE_ORIENTATION', 279],
|
||||
['WEAR_DETECTION', 280],
|
||||
['ACCELEROMETER_UNCALIBRATED', 281],
|
||||
['SENSOR_TYPE_ID_RPC', 282],
|
||||
['SENSOR_TYPE_ID_MAX', 30],
|
||||
]);
|
||||
|
||||
aboutToAppear() {
|
||||
let data = sensor.getSensorListSync();
|
||||
for (let i = 0; i < data.length; i++) {
|
||||
Logger.info('getSensorListSync successful: ' + JSON.stringify(data[i]));
|
||||
this.sensorType.forEach((val, key) => {
|
||||
if (val === data[i].sensorId) {
|
||||
this.sensorItems[i] = key;
|
||||
this.sensors.set(key, data[i].sensorId);
|
||||
return;
|
||||
}
|
||||
})
|
||||
this.sensorItems[i] = data[i].sensorName;
|
||||
this.sensors.set(data[i].sensorName, data[i].sensorId);
|
||||
}
|
||||
}
|
||||
|
||||
build() {
|
||||
Flex({ direction: FlexDirection.Column, justifyContent: FlexAlign.Start, alignItems: ItemAlign.Center }) {
|
||||
TextInput({ placeholder: 'Please select sensor type', text: this.realSensorName })
|
||||
.placeholderColor("red")
|
||||
.id('sensorType')
|
||||
.placeholderFont({
|
||||
size: 30,
|
||||
weight: 105,
|
||||
family: 'cursive',
|
||||
style: FontStyle.Italic
|
||||
})
|
||||
.caretColor(Color.Blue)
|
||||
.height(50)
|
||||
.type(InputType.Normal)
|
||||
.fontSize(30)
|
||||
.focusable(false)
|
||||
.fontWeight(FontWeight.Normal)
|
||||
.fontFamily("sans-serif")
|
||||
.fontStyle(FontStyle.Normal)
|
||||
.fontColor(Color.Red)
|
||||
.textAlign(TextAlign.Center)
|
||||
.borderStyle(BorderStyle.Solid)
|
||||
.borderWidth(1)
|
||||
.borderColor("black")
|
||||
.borderRadius(10)
|
||||
.onClick(() => {
|
||||
CommonUtils.textPickerDialog(this.sensorItems, (sensorName: string) => {
|
||||
this.realSensorName = sensorName;
|
||||
});
|
||||
})
|
||||
.width('100%')
|
||||
.margin({ top: 10 })
|
||||
|
||||
TextInput({ placeholder: 'Input data sampleRate' })
|
||||
.placeholderColor("blue")
|
||||
.id('sampleRate')
|
||||
.placeholderFont({
|
||||
size: 30,
|
||||
weight: 105,
|
||||
family: 'cursive',
|
||||
style: FontStyle.Italic
|
||||
})
|
||||
.caretColor(Color.Blue)
|
||||
.height(50)
|
||||
.type(InputType.Number)
|
||||
.fontSize(30)
|
||||
.fontWeight(FontWeight.Bold)
|
||||
.fontFamily("sans-serif")
|
||||
.fontStyle(FontStyle.Normal)
|
||||
.fontColor(Color.Blue)
|
||||
.textAlign(TextAlign.Center)
|
||||
.onChange((value: string) => {
|
||||
this.sampleRate = parseInt(value)
|
||||
})
|
||||
.width('100%')
|
||||
.margin({ top: 10 })
|
||||
|
||||
Flex({ direction: FlexDirection.Row, justifyContent: FlexAlign.SpaceAround, alignItems: ItemAlign.Center }) {
|
||||
Button($r('app.string.on')).onClick(() => {
|
||||
try {
|
||||
Logger.info('subscribe begin: ' + this.realSensorName);
|
||||
if (!this.supportSensorIdList.includes(this.sensors.get(this.realSensorName))) {
|
||||
Utils.toast("This is not support, please change other sensorId!");
|
||||
return;
|
||||
}
|
||||
if (this.sensors.get(this.realSensorName) == 265 || this.sensors.get(this.realSensorName) == 266) {
|
||||
Logger.info('subscribe begin' + this.sensors.get(this.realSensorName));
|
||||
let atManager = abilityAccessCtrl.createAtManager();
|
||||
let context = getContext(this) as common.UIAbilityContext;
|
||||
atManager.requestPermissionsFromUser(context, ["ohos.permission.ACTIVITY_MOTION"], (err, data) => {
|
||||
if (err) {
|
||||
Logger.error('Grant ACTIVITY_MOTION failed: ' + JSON.stringify(data));
|
||||
return;
|
||||
}
|
||||
Logger.info('data: ' + JSON.stringify(data));
|
||||
Logger.info('data permissions: ' + data.permissions);
|
||||
Logger.info('data authResults: ' + data.authResults);
|
||||
sensor.getSingleSensor(this.sensors.get(this.realSensorName), (err, data) => {
|
||||
if (err) {
|
||||
Logger.error(JSON.stringify(err));
|
||||
return;
|
||||
}
|
||||
if (data) {
|
||||
sensor.on(this.sensors.get(this.realSensorName), (data: sensor.Response) => {
|
||||
Logger.info('PEDOMETER: ' + JSON.stringify(data));
|
||||
this.callbackContent = JSON.stringify(data);
|
||||
})
|
||||
} else {
|
||||
Logger.error('Program run failed: ');
|
||||
}
|
||||
});
|
||||
});
|
||||
Logger.info('subscribe left: ' + this.sensors.get(this.realSensorName));
|
||||
} else { // normal sensor
|
||||
sensor.on(this.sensors.get(this.realSensorName), ((data: sensor.Response) => {
|
||||
Logger.info('callback: ' + JSON.stringify(data));
|
||||
this.callbackContent = JSON.stringify(data);
|
||||
}), { 'interval': this.sampleRate });
|
||||
}
|
||||
} catch (e) {
|
||||
Logger.error('Subscribe exception in,msg: ' + JSON.stringify(e));
|
||||
Logger.error('Unsubscribe exception in,Code: ' + e.code + ' ,msg: ' + e.message);
|
||||
}
|
||||
})
|
||||
|
||||
Button($r('app.string.off')).onClick(() => {
|
||||
Logger.info('Unsubscribe begin: ');
|
||||
try {
|
||||
sensor.off(this.sensors.get(this.realSensorName));
|
||||
sensor.on(1, (data) => {
|
||||
Logger.info('callback: ' + JSON.stringify(data));
|
||||
})
|
||||
} catch (e) {
|
||||
Logger.info('Unsubscribe exception in,msg: ' + JSON.stringify(e));
|
||||
}
|
||||
this.callbackContent = "";
|
||||
})
|
||||
|
||||
Button($r('app.string.once')).onClick(() => {
|
||||
Logger.info('Subscribe once begin: ');
|
||||
try {
|
||||
sensor.once(this.sensors.get(this.realSensorName), (data: sensor.Response) => {
|
||||
Logger.info('PEDOMETER: ' + JSON.stringify(data));
|
||||
this.callbackContent = JSON.stringify(data);
|
||||
})
|
||||
} catch {
|
||||
Logger.error('Program run failed: ');
|
||||
}
|
||||
this.callbackContent = "";
|
||||
})
|
||||
|
||||
}.margin({ top: 20 })
|
||||
|
||||
Text(`${this.callbackContent}`)
|
||||
.fontSize(20)
|
||||
.margin({ top: 20 })
|
||||
}
|
||||
.height('100%')
|
||||
.width('100%')
|
||||
}
|
||||
}
|
106
code/DocsSample/Sensor/entry/src/main/module.json5
Normal file
@ -0,0 +1,106 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
{
|
||||
"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:startIcon",
|
||||
"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.ACCELEROMETER",
|
||||
"usedScene": {
|
||||
"abilities": [
|
||||
"FormAbility"
|
||||
],
|
||||
"when":"inuse"
|
||||
}
|
||||
},
|
||||
{
|
||||
"name" : "ohos.permission.GYROSCOPE",
|
||||
"usedScene": {
|
||||
"abilities": [
|
||||
"FormAbility"
|
||||
],
|
||||
"when":"inuse"
|
||||
}
|
||||
},
|
||||
{
|
||||
"name" : "ohos.permission.ACTIVITY_MOTION",
|
||||
"reason" : "$string:app_name",
|
||||
"usedScene": {
|
||||
"abilities": [
|
||||
"FormAbility"
|
||||
],
|
||||
"when":"inuse"
|
||||
}
|
||||
},
|
||||
{
|
||||
"name" : "ohos.permission.READ_HEALTH_DATA",
|
||||
"reason" : "$string:app_name",
|
||||
"usedScene": {
|
||||
"abilities": [
|
||||
"FormAbility"
|
||||
],
|
||||
"when":"inuse"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
{
|
||||
"color": [
|
||||
{
|
||||
"name": "start_window_background",
|
||||
"value": "#FFFFFF"
|
||||
}
|
||||
]
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
{
|
||||
"string": [
|
||||
{
|
||||
"name": "module_desc",
|
||||
"value": "module description"
|
||||
}
|
||||
,{
|
||||
"name": "EntryAbility_desc",
|
||||
"value": "description"
|
||||
},{
|
||||
"name": "EntryAbility_label",
|
||||
"value": "label"
|
||||
},{
|
||||
"name": "on",
|
||||
"value": "on"
|
||||
},{
|
||||
"name": "off",
|
||||
"value": "off"
|
||||
},{
|
||||
"name": "once",
|
||||
"value": "once"
|
||||
}
|
||||
]
|
||||
}
|
After Width: | Height: | Size: 56 KiB |
After Width: | Height: | Size: 12 KiB |
@ -0,0 +1,7 @@
|
||||
{
|
||||
"layered-image":
|
||||
{
|
||||
"background" : "$media:background",
|
||||
"foreground" : "$media:foreground"
|
||||
}
|
||||
}
|
After Width: | Height: | Size: 20 KiB |
@ -0,0 +1,3 @@
|
||||
{
|
||||
"allowToBackupRestore": true
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
{
|
||||
"src": [
|
||||
"pages/Index"
|
||||
]
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
{
|
||||
"string": [
|
||||
{
|
||||
"name": "module_desc",
|
||||
"value": "module description"
|
||||
}
|
||||
,{
|
||||
"name": "EntryAbility_desc",
|
||||
"value": "description"
|
||||
},{
|
||||
"name": "EntryAbility_label",
|
||||
"value": "label"
|
||||
},{
|
||||
"name": "on",
|
||||
"value": "on"
|
||||
},{
|
||||
"name": "off",
|
||||
"value": "off"
|
||||
},{
|
||||
"name": "once",
|
||||
"value": "once"
|
||||
}
|
||||
]
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
{
|
||||
"string": [
|
||||
{
|
||||
"name": "module_desc",
|
||||
"value": "模块描述"
|
||||
}
|
||||
,{
|
||||
"name": "EntryAbility_desc",
|
||||
"value": "description"
|
||||
},{
|
||||
"name": "on",
|
||||
"value": "订阅"
|
||||
},{
|
||||
"name": "off",
|
||||
"value": "取消订阅"
|
||||
},{
|
||||
"name": "once",
|
||||
"value": "订阅一次"
|
||||
}
|
||||
]
|
||||
}
|
@ -0,0 +1,142 @@
|
||||
/*
|
||||
* 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 '@ohos.hilog';
|
||||
import { describe, it, expect } from '@ohos/hypium';
|
||||
import AbilityDelegatorRegistry from '@ohos.application.abilityDelegatorRegistry';
|
||||
import { Driver, ON } from '@ohos.UiTest';
|
||||
import Want from '@ohos.app.ability.Want';
|
||||
import { MatchPattern } from '@kit.TestKit';
|
||||
|
||||
const TAG = '[Sample_Haptic]';
|
||||
const DOMAIN = 0xF811;
|
||||
const BUNDLE = 'Haptic_';
|
||||
|
||||
let abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
|
||||
async function getResourceString(resource: Resource): Promise<string> {
|
||||
let manage = abilityDelegator.getAppContext().resourceManager;
|
||||
let text = await manage.getStringValue(resource);
|
||||
return text;
|
||||
}
|
||||
|
||||
export default function abilityTest() {
|
||||
describe('AppTest', () => {
|
||||
/**
|
||||
* 启动应用
|
||||
*/
|
||||
it(BUNDLE + 'VerifySensorFunction_001', 0, async (done: Function) => {
|
||||
hilog.info(DOMAIN, TAG, BUNDLE + 'VerifySensorFunction_001 begin');
|
||||
let want: Want = {
|
||||
bundleName : "com.samples.sensor",
|
||||
abilityName: "EntryAbility"
|
||||
};
|
||||
let abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
|
||||
abilityDelegator.startAbility(want, (err) => {
|
||||
hilog.info(DOMAIN, TAG, BUNDLE + 'VerifySensorFunction_001,err.code:' + err.code);
|
||||
expect(0).assertEqual(err.code);
|
||||
});
|
||||
let driver = Driver.create();
|
||||
await driver.delayMs(1000);
|
||||
hilog.info(DOMAIN, TAG, BUNDLE + 'VerifySensorFunction_001 end');
|
||||
done();
|
||||
});
|
||||
|
||||
/**
|
||||
* 输入数据采样频率
|
||||
*/
|
||||
it(BUNDLE + 'VerifySensorFunction_002', 0, async (done: Function) => {
|
||||
hilog.info(DOMAIN, TAG, BUNDLE + 'VerifySensorFunction_002 begin');
|
||||
let driver = Driver.create();
|
||||
//点击切换按钮
|
||||
await driver.assertComponentExist(ON.id('sampleRate'));
|
||||
let btnChang = await driver.findComponent(ON.id('sampleRate'));
|
||||
await btnChang.click();
|
||||
await driver.delayMs(1000);
|
||||
//输入数据采样频率
|
||||
await driver.assertComponentExist(ON.id('sampleRate'));
|
||||
let modifySampleRateInput = await driver.findComponent(ON.id('sampleRate'));
|
||||
await modifySampleRateInput.inputText('100000000');
|
||||
await driver.delayMs(2000);
|
||||
hilog.info(DOMAIN, TAG, BUNDLE + 'VerifySensorFunction_002 end');
|
||||
done();
|
||||
});
|
||||
|
||||
/**
|
||||
* 进入切换按钮选择accelerometer类型传感器
|
||||
*/
|
||||
it(BUNDLE + 'VerifySensorFunction_003', 0, async (done: Function) => {
|
||||
hilog.info(DOMAIN, TAG, BUNDLE + 'VerifySensorFunction_003 begin');
|
||||
let driver = Driver.create();
|
||||
await driver.delayMs(1000);
|
||||
//点击切换按钮
|
||||
await driver.assertComponentExist(ON.id('sensorType'));
|
||||
let btnChang = await driver.findComponent(ON.id('sensorType'));
|
||||
await btnChang.click();
|
||||
await driver.delayMs(1000);
|
||||
//确认
|
||||
await driver.assertComponentExist(ON.text('确定'));
|
||||
let btnYes = await driver.findComponent(ON.text('确定'));
|
||||
await btnYes.click();
|
||||
await driver.delayMs(1000);
|
||||
hilog.info(DOMAIN, TAG, BUNDLE + 'VerifySensorFunction_003 end');
|
||||
done();
|
||||
});
|
||||
|
||||
/**
|
||||
* 订阅传感器
|
||||
*/
|
||||
it(BUNDLE + 'VerifySensorFunction_004', 0, async (done: Function) => {
|
||||
hilog.info(DOMAIN, TAG, BUNDLE + 'VerifySensorFunction_004 begin');
|
||||
let driver = Driver.create();
|
||||
await driver.assertComponentExist(ON.text(await getResourceString($r('app.string.on')), MatchPattern.CONTAINS));
|
||||
let indexDiscover = await driver.findComponent(ON.text(await getResourceString($r('app.string.on')),
|
||||
MatchPattern.CONTAINS));
|
||||
await indexDiscover.click();
|
||||
await driver.delayMs(3000);
|
||||
hilog.info(DOMAIN, TAG, BUNDLE + 'VerifySensorFunction_004 end');
|
||||
done();
|
||||
});
|
||||
|
||||
/**
|
||||
* 取消订阅传感器
|
||||
*/
|
||||
it(BUNDLE + 'VerifySensorFunction_005', 0, async (done: Function) => {
|
||||
hilog.info(DOMAIN, TAG, BUNDLE + 'VerifySensorFunction_005 begin');
|
||||
let driver = Driver.create();
|
||||
await driver.assertComponentExist(ON.text(await getResourceString($r('app.string.off')), MatchPattern.CONTAINS));
|
||||
let indexDiscover = await driver.findComponent(ON.text(await getResourceString($r('app.string.off')),
|
||||
MatchPattern.CONTAINS));
|
||||
await indexDiscover.click();
|
||||
await driver.delayMs(2000);
|
||||
hilog.info(DOMAIN, TAG, BUNDLE + 'VerifySensorFunction_005 end');
|
||||
done();
|
||||
});
|
||||
|
||||
/**
|
||||
* 订阅一次传感器
|
||||
*/
|
||||
it(BUNDLE + 'VerifySensorFunction_006', 0, async (done: Function) => {
|
||||
hilog.info(DOMAIN, TAG, BUNDLE + 'VerifySensorFunction_006 begin');
|
||||
let driver = Driver.create();
|
||||
await driver.assertComponentExist(ON.text(await getResourceString($r('app.string.once')), MatchPattern.CONTAINS));
|
||||
let indexDiscover = await driver.findComponent(ON.text(await getResourceString($r('app.string.once')),
|
||||
MatchPattern.CONTAINS));
|
||||
await indexDiscover.click();
|
||||
await driver.delayMs(2000);
|
||||
hilog.info(DOMAIN, TAG, BUNDLE + 'VerifySensorFunction_006 end');
|
||||
done();
|
||||
});
|
||||
})
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
/*
|
||||
* 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 abilityTest from './Ability.test';
|
||||
|
||||
export default function testsuite() {
|
||||
abilityTest();
|
||||
}
|
27
code/DocsSample/Sensor/entry/src/ohosTest/module.json5
Normal file
@ -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.
|
||||
*/
|
||||
|
||||
{
|
||||
"module": {
|
||||
"name": "entry_test",
|
||||
"type": "feature",
|
||||
"deviceTypes": [
|
||||
"default",
|
||||
"tablet"
|
||||
],
|
||||
"deliveryWithInstall": true,
|
||||
"installationFree": false
|
||||
}
|
||||
}
|
20
code/DocsSample/Sensor/entry/src/test/List.test.ets
Normal file
@ -0,0 +1,20 @@
|
||||
/*
|
||||
* 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 localUnitTest from './LocalUnit.test';
|
||||
|
||||
export default function testsuite() {
|
||||
localUnitTest();
|
||||
}
|
48
code/DocsSample/Sensor/entry/src/test/LocalUnit.test.ets
Normal file
@ -0,0 +1,48 @@
|
||||
/*
|
||||
* 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 { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium';
|
||||
|
||||
export default function localUnitTest() {
|
||||
describe('localUnitTest', () => {
|
||||
// Defines a test suite. Two parameters are supported: test suite name and test suite function.
|
||||
beforeAll(() => {
|
||||
// Presets an action, which is performed only once before all test cases of the test suite start.
|
||||
// This API supports only one parameter: preset action function.
|
||||
});
|
||||
beforeEach(() => {
|
||||
// Presets an action, which is performed before each unit test case starts.
|
||||
// The number of execution times is the same as the number of test cases defined by **it**.
|
||||
// This API supports only one parameter: preset action function.
|
||||
});
|
||||
afterEach(() => {
|
||||
// Presets a clear action, which is performed after each unit test case ends.
|
||||
// The number of execution times is the same as the number of test cases defined by **it**.
|
||||
// This API supports only one parameter: clear action function.
|
||||
});
|
||||
afterAll(() => {
|
||||
// Presets a clear action, which is performed after all test cases of the test suite end.
|
||||
// This API supports only one parameter: clear action function.
|
||||
});
|
||||
it('assertContain', 0, () => {
|
||||
// Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function.
|
||||
let a = 'abc';
|
||||
let b = 'b';
|
||||
// Defines a variety of assertion methods, which are used to declare expected boolean conditions.
|
||||
expect(a).assertContain(b);
|
||||
expect(a).assertEqual(a);
|
||||
});
|
||||
});
|
||||
}
|
37
code/DocsSample/Sensor/hvigor/hvigor-config.json5
Normal file
@ -0,0 +1,37 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
{
|
||||
"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*/
|
||||
}
|
||||
}
|
21
code/DocsSample/Sensor/hvigorfile.ts
Normal file
@ -0,0 +1,21 @@
|
||||
/*
|
||||
* 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 { 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. */
|
||||
}
|
26
code/DocsSample/Sensor/oh-package.json5
Normal file
@ -0,0 +1,26 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
{
|
||||
"modelVersion": "5.0.0",
|
||||
"description": "Please describe the basic information.",
|
||||
"dependencies": {
|
||||
},
|
||||
"devDependencies": {
|
||||
"@ohos/hypium": "1.0.19"
|
||||
,
|
||||
"@ohos/hamock": "1.0.0"
|
||||
}
|
||||
}
|
12
code/DocsSample/Sensor/ohosTest.md
Normal file
@ -0,0 +1,12 @@
|
||||
# 数据防泄漏测试用例归档
|
||||
|
||||
## 用例表
|
||||
|
||||
| 测试功能 | 预置条件 | 预期输出 | 是否自动 |测试结果|
|
||||
|---------|---------------------|-------------------------------|--------------------------------|--------------------------------|
|
||||
| 拉起应用 | 设备正常运行 | 成功拉起应用 | 是 |Pass|
|
||||
| 输入数据采样 | 自动输入100000000 | 拉起输入页面,输入100000000 | 是 |Pass|
|
||||
| 选择传感器类型 | 选择accelerometer传感器 | 弹出“选择传感器”下拉栏,点击确定订阅accelerometer | 是 |Pass|
|
||||
| 订阅传感器类型 | 持续订阅传感器,数据正常上报 | 成功订阅accelerometer传感器,持续上报数据 | 是 | Pass|
|
||||
| 取消订阅传感器 | 取消订阅传感器,页面无数据上报 | 取消订阅传感器成功,无数据 | 是 | Pass|
|
||||
| 订阅一次传感器 | 上报订阅传感器一次数据 | 订阅一次传感器成功,上报一次数据 | 是 | Pass|
|
BIN
code/DocsSample/Sensor/screenshots/devices/homePage.jpeg
Normal file
After Width: | Height: | Size: 39 KiB |
BIN
code/DocsSample/Sensor/screenshots/devices/sampleRate.jpeg
Normal file
After Width: | Height: | Size: 72 KiB |
BIN
code/DocsSample/Sensor/screenshots/devices/sensorType.jpeg
Normal file
After Width: | Height: | Size: 51 KiB |
BIN
code/DocsSample/Sensor/screenshots/devices/subscribe.jpeg
Normal file
After Width: | Height: | Size: 54 KiB |