diff --git a/device/Compass/README.md b/device/Compass/README.md new file mode 100644 index 0000000000000000000000000000000000000000..9df1062bf22b1348da648ea23e0a79828997d76b --- /dev/null +++ b/device/Compass/README.md @@ -0,0 +1,6 @@ +# Compass + +- Sensors in OpenHarmony are abstraction of underlying hardware-based sensors. Your application can access the underlying sensors via OpenHarmony sensors. Using the application programming interfaces \(APIs\) provided by OpenHarmony sensors, you can query sensors on your device, subscribe to sensor data, customize algorithms based on sensor data, and develop various applications. + + This sample uses implementing a simple compass to demonstrate how to use sensor APIs, such as obtaining a sensor and subscribing to or unsubscribing from sensor data. + diff --git a/device/Compass/README_zh.md b/device/Compass/README_zh.md new file mode 100644 index 0000000000000000000000000000000000000000..0916f7f64e7a53fe59b4d785fe56a640219ef6c7 --- /dev/null +++ b/device/Compass/README_zh.md @@ -0,0 +1,7 @@ +# 指南针 + +- 传感器是应用访问底层硬件传感器的一种设备抽象概念。开发者根据传感器提供的 Sensor API,可以查询设备上的传感器,订阅传感器的数据,并根据传感器数据定制相应的算法,开发各类应用。 + + 本示例通过实现一个简单的指南针,来演示传感器的常规使用方法(获取传感器并进行监听、取消监听等)。 + + diff --git a/device/Compass/build.gradle b/device/Compass/build.gradle new file mode 100644 index 0000000000000000000000000000000000000000..e76a14f743baa5377849b43abf29dd0fe29b6478 --- /dev/null +++ b/device/Compass/build.gradle @@ -0,0 +1,34 @@ +// Top-level build file where you can add configuration options common to all sub-projects/modules. +apply plugin: 'com.huawei.ohos.app' + +ohos { + compileSdkVersion 5 + defaultConfig { + compatibleSdkVersion 4 + } +} +buildscript { + repositories { + maven { + url 'https://repo.huaweicloud.com/repository/maven/' + } + maven { + url 'https://developer.huawei.com/repo/' + } + jcenter() + } + dependencies { + classpath 'com.huawei.ohos:hap:2.4.4.2' + } +} +allprojects { + repositories { + maven { + url 'https://repo.huaweicloud.com/repository/maven/' + } + maven { + url 'https://developer.huawei.com/repo/' + } + jcenter() + } +} \ No newline at end of file diff --git a/device/Compass/entry/build.gradle b/device/Compass/entry/build.gradle new file mode 100644 index 0000000000000000000000000000000000000000..594f9016e51cd0afef06b965647a56a947055f21 --- /dev/null +++ b/device/Compass/entry/build.gradle @@ -0,0 +1,19 @@ +apply plugin: 'com.huawei.ohos.hap' +ohos { + compileSdkVersion 5 + defaultConfig { + compatibleSdkVersion 4 + } + buildTypes { + release { + proguardOpt { + proguardEnabled false + rulesFiles 'proguard-rules.pro' + } + } + } +} + +dependencies { + implementation fileTree(dir: 'libs', include: ['*.jar']) +} diff --git a/device/Compass/entry/src/main/config.json b/device/Compass/entry/src/main/config.json new file mode 100644 index 0000000000000000000000000000000000000000..9a471a598a78392c6eb8d169d7f199eba733058e --- /dev/null +++ b/device/Compass/entry/src/main/config.json @@ -0,0 +1,48 @@ +{ + "app": { + "bundleName": "ohos.samples.compass", + "version": { + "code": 1000000, + "name": "1.0" + } + }, + "deviceConfig": {}, + "module": { + "package": "ohos.samples.compass", + "name": ".PageAbility", + "reqCapabilities": [ + "video_support" + ], + "deviceType": [ + "default" + ], + "distro": { + "deliveryWithInstall": true, + "moduleName": "entry", + "moduleType": "entry", + "installationFree":false + }, + "abilities": [ + { + "skills": [ + { + "entities": [ + "entity.system.home" + ], + "actions": [ + "action.system.home" + ] + } + ], + "orientation": "portrait", + "formsEnabled": false, + "name": ".PageAbility", + "icon": "$media:aa_icon", + "description": "$string:ability_description", + "label": "$string:app_name", + "type": "page", + "launchType": "standard" + } + ] + } +} \ No newline at end of file diff --git a/device/Compass/entry/src/main/java/ohos/samples/compass/PageAbility.java b/device/Compass/entry/src/main/java/ohos/samples/compass/PageAbility.java new file mode 100644 index 0000000000000000000000000000000000000000..cd663514391edcebef37048c819399674283d48f --- /dev/null +++ b/device/Compass/entry/src/main/java/ohos/samples/compass/PageAbility.java @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ohos.samples.compass; + +import ohos.samples.compass.slice.PageAbilitySlice; + +import ohos.aafwk.ability.Ability; +import ohos.aafwk.content.Intent; + +/** + * The main frame of the sample program + * DemoCompass program startup processing + */ +public class PageAbility extends Ability { + @Override + public void onStart(Intent intent) { + super.onStart(intent); + super.setMainRoute(PageAbilitySlice.class.getName()); + } +} diff --git a/device/Compass/entry/src/main/java/ohos/samples/compass/slice/PageAbilitySlice.java b/device/Compass/entry/src/main/java/ohos/samples/compass/slice/PageAbilitySlice.java new file mode 100644 index 0000000000000000000000000000000000000000..118326c024b7dc60c7a90bc56ba2ec2ef470c8e5 --- /dev/null +++ b/device/Compass/entry/src/main/java/ohos/samples/compass/slice/PageAbilitySlice.java @@ -0,0 +1,133 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ohos.samples.compass.slice; + +import ohos.aafwk.ability.AbilitySlice; +import ohos.aafwk.content.Intent; +import ohos.agp.components.Image; +import ohos.agp.components.Text; +import ohos.eventhandler.EventHandler; +import ohos.eventhandler.EventRunner; +import ohos.eventhandler.InnerEvent; +import ohos.hiviewdfx.HiLog; +import ohos.hiviewdfx.HiLogLabel; +import ohos.samples.compass.ResourceTable; +import ohos.sensor.agent.CategoryOrientationAgent; +import ohos.sensor.bean.CategoryOrientation; +import ohos.sensor.data.CategoryOrientationData; +import ohos.sensor.listener.ICategoryOrientationDataCallback; + +import java.util.Locale; + +/** + * the Main page + */ +public class PageAbilitySlice extends AbilitySlice { + private static final String TAG = PageAbilitySlice.class.getSimpleName(); + + private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD000F00, TAG); + + private static final long SAMPLING_INTERVAL_NANOSECONDS = 500000000L; + + private static final String FORMAT_DEGREE = "%.2f"; + + private static final float DEFLECTION_FLAG = -1.0f; + + private CategoryOrientationAgent categoryOrientationAgent; + + private Image compassImg; + + private Text compassAngleText; + + private float degree; + + private ICategoryOrientationDataCallback categoryOrientationDataCallback; + + private EventHandler handler = new EventHandler(EventRunner.current()) { + @Override + protected void processEvent(InnerEvent event) { + compassAngleText.setText(getRotation(degree)); + compassImg.setRotation(DEFLECTION_FLAG * degree); + } + }; + + @Override + public void onStart(Intent intent) { + super.onStart(intent); + super.setUIContent(ResourceTable.Layout_page_ability_slice); + compassImg = (Image) findComponentById(ResourceTable.Id_compass_icon_img); + compassAngleText = (Text) findComponentById(ResourceTable.Id_compass_angle_text); + categoryOrientationAgent = new CategoryOrientationAgent(); + CategoryOrientation categoryOrientation = categoryOrientationAgent.getSingleSensor( + CategoryOrientation.SENSOR_TYPE_ORIENTATION); + categoryOrientationDataCallback = new ICategoryOrientationDataCallback() { + @Override + public void onSensorDataModified(CategoryOrientationData categoryOrientationData) { + degree = categoryOrientationData.getValues()[0]; + handler.sendEvent(0); + } + + @Override + public void onAccuracyDataModified(CategoryOrientation categoryOrientation, int i) { + // Called when sensor accuracy data changes. + // This method is called by the application layer to process the accuracy data of the sensors. + HiLog.info(LABEL_LOG, "%{public}s", "onAccuracyDataModified"); + } + + @Override + public void onCommandCompleted(CategoryOrientation categoryOrientation) { + // Called when the sensor completes command execution. + // This method is called by the application layer to process the command execution result of the sensor. + HiLog.info(LABEL_LOG, "%{public}s", "onCommandCompleted"); + } + }; + categoryOrientationAgent.setSensorDataCallback(categoryOrientationDataCallback, categoryOrientation, + SAMPLING_INTERVAL_NANOSECONDS); + } + + private String getRotation(float degree) { + if (degree >= 0 && degree <= 22.5) { + return String.format(Locale.ENGLISH, FORMAT_DEGREE, degree) + " N"; + } else if (degree > 22.5 && degree <= 67.5) { + return String.format(Locale.ENGLISH, FORMAT_DEGREE, degree) + " NE"; + } else if (degree > 67.5 && degree <= 112.5) { + return String.format(Locale.ENGLISH, FORMAT_DEGREE, degree) + " E"; + } else if (degree > 112.5 && degree <= 157.5) { + return String.format(Locale.ENGLISH, FORMAT_DEGREE, degree) + " SE"; + } else if (degree > 157.5 && degree <= 202.5) { + return String.format(Locale.ENGLISH, FORMAT_DEGREE, degree) + " S"; + } else if (degree > 202.5 && degree <= 247.5) { + return String.format(Locale.ENGLISH, FORMAT_DEGREE, degree) + " SW"; + } else if (degree > 247.5 && degree <= 282.5) { + return String.format(Locale.ENGLISH, FORMAT_DEGREE, degree) + " W"; + } else if (degree > 282.5 && degree <= 337.5) { + return String.format(Locale.ENGLISH, FORMAT_DEGREE, degree) + " NW"; + } else if (degree > 337.5 && degree <= 360.0) { + return String.format(Locale.ENGLISH, FORMAT_DEGREE, degree) + " N"; + } else { + return "/"; + } + } + + @Override + protected void onStop() { + HiLog.info(LABEL_LOG, "%{public}s", "onStop start"); + super.onStop(); + categoryOrientationAgent.releaseSensorDataCallback(categoryOrientationDataCallback); + handler.removeAllEvent(); + HiLog.info(LABEL_LOG, "%{public}s", "onStop end"); + } +} diff --git a/device/Compass/entry/src/main/resources/base/element/string.json b/device/Compass/entry/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..3e08d82259dc7dcebc46f16316e1d1ecebb6f00a --- /dev/null +++ b/device/Compass/entry/src/main/resources/base/element/string.json @@ -0,0 +1,12 @@ +{ + "string": [ + { + "name": "app_name", + "value": "Compass" + }, + { + "name": "ability_description", + "value": "hap sample empty page" + } + ] +} \ No newline at end of file diff --git a/device/Compass/entry/src/main/resources/base/layout/page_ability_slice.xml b/device/Compass/entry/src/main/resources/base/layout/page_ability_slice.xml new file mode 100644 index 0000000000000000000000000000000000000000..a08d7dcdeaec4b79804d57db3c70da00c93f0f4a --- /dev/null +++ b/device/Compass/entry/src/main/resources/base/layout/page_ability_slice.xml @@ -0,0 +1,32 @@ + + + + + + \ No newline at end of file diff --git a/device/Compass/entry/src/main/resources/base/media/aa_icon.png b/device/Compass/entry/src/main/resources/base/media/aa_icon.png new file mode 100644 index 0000000000000000000000000000000000000000..ce307a8827bd75456441ceb57d530e4c8d45d36c Binary files /dev/null and b/device/Compass/entry/src/main/resources/base/media/aa_icon.png differ diff --git a/device/Compass/entry/src/main/resources/base/media/compass.png b/device/Compass/entry/src/main/resources/base/media/compass.png new file mode 100644 index 0000000000000000000000000000000000000000..3c5de9be290be6f5991816e5827f9b222566a9f1 Binary files /dev/null and b/device/Compass/entry/src/main/resources/base/media/compass.png differ diff --git a/device/Compass/screenshots/phone/main.png b/device/Compass/screenshots/phone/main.png new file mode 100644 index 0000000000000000000000000000000000000000..ebced79a8fcbf0c4a28c82cb8f7cd916811cd827 Binary files /dev/null and b/device/Compass/screenshots/phone/main.png differ diff --git a/device/Compass/settings.gradle b/device/Compass/settings.gradle new file mode 100644 index 0000000000000000000000000000000000000000..4773db73233a570c2d0c01a22e75321acfbf7a07 --- /dev/null +++ b/device/Compass/settings.gradle @@ -0,0 +1 @@ +include ':entry'