diff --git a/device/BatteryInfo/README.md b/device/BatteryInfo/README.md new file mode 100644 index 0000000000000000000000000000000000000000..99dd85bbcf58ff3fd2ddb5e8ee01f4aa97c2e3eb --- /dev/null +++ b/device/BatteryInfo/README.md @@ -0,0 +1,4 @@ +# Battery Information + +- This sample demonstrates how to obtain the battery information of a device, including the battery level, health status, charger type, and charging status. +- Licensing diff --git a/device/BatteryInfo/README_zh.md b/device/BatteryInfo/README_zh.md new file mode 100644 index 0000000000000000000000000000000000000000..1c91766d4d63f536eb87fccff98f206d9f52113f --- /dev/null +++ b/device/BatteryInfo/README_zh.md @@ -0,0 +1,4 @@ +# 电池信息 + +- 本示例演示了获取设备的电池信息,提供获取电池电量、健康状态、充电器类型、充电状态等能力。 +- Licensing diff --git a/device/BatteryInfo/build.gradle b/device/BatteryInfo/build.gradle new file mode 100644 index 0000000000000000000000000000000000000000..dede97233a0367a47b29085b9cf491932a1674e0 --- /dev/null +++ b/device/BatteryInfo/build.gradle @@ -0,0 +1,48 @@ +/* + * 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. + */ +// 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/BatteryInfo/entry/build.gradle b/device/BatteryInfo/entry/build.gradle new file mode 100644 index 0000000000000000000000000000000000000000..1e7f626967c0dbf1f2661de4769d0751dcafc80f --- /dev/null +++ b/device/BatteryInfo/entry/build.gradle @@ -0,0 +1,25 @@ +/* + * 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. + */ +apply plugin: 'com.huawei.ohos.hap' +ohos { + compileSdkVersion 5 + defaultConfig { + compatibleSdkVersion 4 + } +} + +dependencies { + implementation fileTree(dir: 'libs', include: ['*.jar']) +} diff --git a/device/BatteryInfo/entry/src/main/config.json b/device/BatteryInfo/entry/src/main/config.json new file mode 100644 index 0000000000000000000000000000000000000000..bf62e282ec9ff9a1714a9ba1d75f230dcdc4ab6a --- /dev/null +++ b/device/BatteryInfo/entry/src/main/config.json @@ -0,0 +1,49 @@ +{ + "app": { + "bundleName": "ohos.samples.batteryinfo", + "version": { + "code": 2000000, + "name": "2.0" + } + }, + "deviceConfig": {}, + "module": { + "package": "ohos.samples.batteryinfo", + "name": ".MainAbility", + "reqCapabilities": [ + "video_support" + ], + "deviceType": [ + "default" + ], + "distro": { + "deliveryWithInstall": true, + "moduleName": "entry", + "moduleType": "entry", + "installationFree": false + }, + "abilities": [ + { + "name": ".MainAbility", + "label": "$string:app_name", + "icon": "$media:icon", + "description": "$string:mainability_description", + "orientation": "portrait", + "launchType": "standard", + "type": "page", + "visible": true, + "formsEnabled": false, + "skills": [ + { + "entities": [ + "entity.system.home" + ], + "actions": [ + "action.system.home" + ] + } + ] + } + ] + } +} \ No newline at end of file diff --git a/device/BatteryInfo/entry/src/main/java/ohos/samples/batteryinfo/MainAbility.java b/device/BatteryInfo/entry/src/main/java/ohos/samples/batteryinfo/MainAbility.java new file mode 100644 index 0000000000000000000000000000000000000000..8800a237e976f9cc02723c28b5a079d6ca8ae42b --- /dev/null +++ b/device/BatteryInfo/entry/src/main/java/ohos/samples/batteryinfo/MainAbility.java @@ -0,0 +1,32 @@ +/* + * 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.batteryinfo; + +import ohos.samples.batteryinfo.slice.MainAbilitySlice; + +import ohos.aafwk.ability.Ability; +import ohos.aafwk.content.Intent; + +/** + * Main ability + */ +public class MainAbility extends Ability { + @Override + public void onStart(Intent intent) { + super.onStart(intent); + super.setMainRoute(MainAbilitySlice.class.getName()); + } +} diff --git a/device/BatteryInfo/entry/src/main/java/ohos/samples/batteryinfo/slice/MainAbilitySlice.java b/device/BatteryInfo/entry/src/main/java/ohos/samples/batteryinfo/slice/MainAbilitySlice.java new file mode 100644 index 0000000000000000000000000000000000000000..d3ee81782f185842be95ccb49e24811202601548 --- /dev/null +++ b/device/BatteryInfo/entry/src/main/java/ohos/samples/batteryinfo/slice/MainAbilitySlice.java @@ -0,0 +1,120 @@ +/* + * 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.batteryinfo.slice; + +import ohos.aafwk.ability.AbilitySlice; +import ohos.aafwk.content.Intent; +import ohos.agp.components.Button; +import ohos.agp.components.Text; +import ohos.batterymanager.BatteryInfo; +import ohos.event.commonevent.CommonEventData; +import ohos.event.commonevent.CommonEventManager; +import ohos.event.commonevent.CommonEventSubscribeInfo; +import ohos.event.commonevent.CommonEventSubscriber; +import ohos.event.commonevent.CommonEventSupport; +import ohos.event.commonevent.MatchingSkills; +import ohos.hiviewdfx.HiLog; +import ohos.hiviewdfx.HiLogLabel; +import ohos.rpc.RemoteException; +import ohos.samples.batteryinfo.ResourceTable; + +import java.util.Locale; + +/** + * Main AbilitySlice + */ +public class MainAbilitySlice extends AbilitySlice { + private static final String TAG = MainAbilitySlice.class.getSimpleName(); + + private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD000F00, TAG); + + private static final String TEMPLATE = "batteryCapacity: %d%%" + System.lineSeparator() + "isCharging: %b" + + System.lineSeparator() + "healthState: %s" + System.lineSeparator() + "pluggedType: %s"; + + private Text batteryInfoText; + + private CommonEventSubscriber commonEventSubscriber; + + private boolean isSubscribedBatteryChange; + + @Override + public void onStart(Intent intent) { + super.onStart(intent); + super.setUIContent(ResourceTable.Layout_main_ability_slice); + + initComponents(); + } + + private void initComponents() { + batteryInfoText = (Text) findComponentById(ResourceTable.Id_battery_level_result); + findComponentById(ResourceTable.Id_get_battery_level).setClickedListener(listener -> { + batteryInfoText.setText(getBatteryInfo()); + }); + Button subscribeBatteryChange = (Button) findComponentById(ResourceTable.Id_subscribe_battery_level_change); + subscribeBatteryChange.setClickedListener(listener -> { + if (!isSubscribedBatteryChange) { + subscribeBatteryChange(); + isSubscribedBatteryChange = true; + } + }); + } + + @Override + public void onStop() { + super.onStop(); + unsubscribeBatteryChange(); + } + + private void subscribeBatteryChange() { + MatchingSkills matchingSkills = new MatchingSkills(); + matchingSkills.addEvent(CommonEventSupport.COMMON_EVENT_BATTERY_CHANGED); + CommonEventSubscribeInfo subscribeInfo = new CommonEventSubscribeInfo(matchingSkills); + + commonEventSubscriber = new CommonEventSubscriber(subscribeInfo) { + @Override + public void onReceiveEvent(CommonEventData commonEventData) { + batteryInfoText.setText(getBatteryInfo()); + } + }; + try { + CommonEventManager.subscribeCommonEvent(commonEventSubscriber); + } catch (RemoteException exception) { + HiLog.error(LABEL_LOG, "%{public}s", "Subscribe error!"); + } + } + + private void unsubscribeBatteryChange() { + if (commonEventSubscriber == null) { + return; + } + try { + CommonEventManager.unsubscribeCommonEvent(commonEventSubscriber); + } catch (RemoteException exception) { + HiLog.error(LABEL_LOG, "%{public}s", "unsubscribe error!"); + } + } + + private String getBatteryInfo() { + BatteryInfo batteryInfo = new BatteryInfo(); + int batteryCapacity = batteryInfo.getCapacity(); + BatteryInfo.BatteryHealthState healthState = batteryInfo.getHealthStatus(); + BatteryInfo.BatteryPluggedType pluggedType = batteryInfo.getPluggedType(); + BatteryInfo.BatteryChargeState chargeState = batteryInfo.getChargingStatus(); + boolean isCharging = chargeState == BatteryInfo.BatteryChargeState.ENABLE + || chargeState == BatteryInfo.BatteryChargeState.FULL; + return String.format(Locale.ENGLISH, TEMPLATE, batteryCapacity, isCharging, healthState, pluggedType); + } +} \ No newline at end of file diff --git a/device/BatteryInfo/entry/src/main/resources/base/element/string.json b/device/BatteryInfo/entry/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..cfcd62df9dd7b3ad060f08a599e4fd411d53602e --- /dev/null +++ b/device/BatteryInfo/entry/src/main/resources/base/element/string.json @@ -0,0 +1,12 @@ +{ + "string": [ + { + "name": "app_name", + "value": "BatteryInfo" + }, + { + "name": "mainability_description", + "value": "hap sample empty page" + } + ] +} \ No newline at end of file diff --git a/device/BatteryInfo/entry/src/main/resources/base/graphic/bg.xml b/device/BatteryInfo/entry/src/main/resources/base/graphic/bg.xml new file mode 100644 index 0000000000000000000000000000000000000000..1a847ac190b495b7b9183e7cd49d6bdace55200d --- /dev/null +++ b/device/BatteryInfo/entry/src/main/resources/base/graphic/bg.xml @@ -0,0 +1,23 @@ + + + + + + \ No newline at end of file diff --git a/device/BatteryInfo/entry/src/main/resources/base/layout/main_ability_slice.xml b/device/BatteryInfo/entry/src/main/resources/base/layout/main_ability_slice.xml new file mode 100644 index 0000000000000000000000000000000000000000..0c26cce0698f4087eed09adbdd1c260b9463a27c --- /dev/null +++ b/device/BatteryInfo/entry/src/main/resources/base/layout/main_ability_slice.xml @@ -0,0 +1,54 @@ + + + + +