From 83954b95706f1a49b7a6d32f811222d828900ee4 Mon Sep 17 00:00:00 2001 From: yanyilin Date: Fri, 5 Jul 2024 16:02:32 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=9F=E4=B8=80=E4=BB=A3=E7=A0=81=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=EF=BC=8C=E5=90=8C=E6=AD=A5web=E5=B9=B3=E5=8F=B0?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../utssdk/app-android/index.uts | 128 ++++++++---------- .../utssdk/app-ios/index.uts | 48 +++---- .../uni-getbatteryinfo/utssdk/web/index.js | 23 ---- .../uni-getbatteryinfo/utssdk/web/index.uts | 20 +++ 4 files changed, 105 insertions(+), 114 deletions(-) delete mode 100644 uni_modules/uni-getbatteryinfo/utssdk/web/index.js create mode 100644 uni_modules/uni-getbatteryinfo/utssdk/web/index.uts diff --git a/uni_modules/uni-getbatteryinfo/utssdk/app-android/index.uts b/uni_modules/uni-getbatteryinfo/utssdk/app-android/index.uts index 5ae056d..ea5f4e8 100644 --- a/uni_modules/uni-getbatteryinfo/utssdk/app-android/index.uts +++ b/uni_modules/uni-getbatteryinfo/utssdk/app-android/index.uts @@ -1,84 +1,76 @@ import Context from "android.content.Context"; import BatteryManager from "android.os.BatteryManager"; -import { GetBatteryInfo, GetBatteryInfoOptions, GetBatteryInfoSuccess, GetBatteryInfoResult,GetBatteryInfoSync } from '../interface.uts' -import IntentFilter from 'android.content.IntentFilter'; -import Intent from 'android.content.Intent'; - -import { GetBatteryInfoFailImpl } from '../unierror'; +import { GetBatteryInfo, GetBatteryInfoOptions, GetBatteryInfoSuccess, GetBatteryInfoResult, GetBatteryInfoSync } from '../interface.uts' +import IntentFilter from 'android.content.IntentFilter'; +import Intent from 'android.content.Intent'; + +import { GetBatteryInfoFailImpl } from '../unierror'; /** * 异步获取电量 */ -export const getBatteryInfo : GetBatteryInfo = function (options : GetBatteryInfoOptions) { - +export const getBatteryInfo : GetBatteryInfo = function (options : GetBatteryInfoOptions) { + const context = UTSAndroid.getAppContext(); + if (context != null) { + const manager = context.getSystemService( + Context.BATTERY_SERVICE + ) as BatteryManager; + const level = manager.getIntProperty( + BatteryManager.BATTERY_PROPERTY_CAPACITY + ); + + let ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); + let batteryStatus = context.registerReceiver(null, ifilter); + let status = batteryStatus?.getIntExtra(BatteryManager.EXTRA_STATUS, -1); + let isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL; - const context = UTSAndroid.getAppContext(); - if (context != null) { - const manager = context.getSystemService( - Context.BATTERY_SERVICE - ) as BatteryManager; - const level = manager.getIntProperty( - BatteryManager.BATTERY_PROPERTY_CAPACITY - ); - - let ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); - let batteryStatus = context.registerReceiver(null, ifilter); - let status = batteryStatus?.getIntExtra(BatteryManager.EXTRA_STATUS, -1); - let isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL; - - const res : GetBatteryInfoSuccess = { - errMsg: 'getBatteryInfo:ok', - level, - isCharging: isCharging - } - options.success?.(res) - options.complete?.(res) - } else { - let res = new GetBatteryInfoFailImpl(1001); - options.fail?.(res) - options.complete?.(res) + const res : GetBatteryInfoSuccess = { + errMsg: 'getBatteryInfo:ok', + level, + isCharging: isCharging } + options.success?.(res) + options.complete?.(res) + } else { + let res = new GetBatteryInfoFailImpl(1001); + options.fail?.(res) + options.complete?.(res) + } } /** - * 同步获取电量示例 + * 同步获取电量 */ -export const getBatteryInfoSync : GetBatteryInfoSync = function (): GetBatteryInfoResult { - - const context = UTSAndroid.getAppContext(); - if (context != null) { - - - const manager = context.getSystemService( - Context.BATTERY_SERVICE - ) as BatteryManager; - const level = manager.getIntProperty( - BatteryManager.BATTERY_PROPERTY_CAPACITY - ); - - let ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); - let batteryStatus = context.registerReceiver(null, ifilter); - let status = batteryStatus?.getIntExtra(BatteryManager.EXTRA_STATUS, -1); - let isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL; - - const res : GetBatteryInfoResult = { - level: level, - isCharging: isCharging - }; - - return res; - } - else { - /** - * 无有效上下文 - */ - const res : GetBatteryInfoResult = { - level: -1, - isCharging: false - }; - return res; - } +export const getBatteryInfoSync : GetBatteryInfoSync = function () : GetBatteryInfoResult { + const context = UTSAndroid.getAppContext(); + if (context != null) { + const manager = context.getSystemService( + Context.BATTERY_SERVICE + ) as BatteryManager; + const level = manager.getIntProperty( + BatteryManager.BATTERY_PROPERTY_CAPACITY + ); + let ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); + let batteryStatus = context.registerReceiver(null, ifilter); + let status = batteryStatus?.getIntExtra(BatteryManager.EXTRA_STATUS, -1); + let isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL; + const res : GetBatteryInfoResult = { + level: level, + isCharging: isCharging + }; + return res; + } + else { + /** + * 无有效上下文 + */ + const res : GetBatteryInfoResult = { + level: -1, + isCharging: false + }; + return res; + } } diff --git a/uni_modules/uni-getbatteryinfo/utssdk/app-ios/index.uts b/uni_modules/uni-getbatteryinfo/utssdk/app-ios/index.uts index de45bdc..05cc721 100644 --- a/uni_modules/uni-getbatteryinfo/utssdk/app-ios/index.uts +++ b/uni_modules/uni-getbatteryinfo/utssdk/app-ios/index.uts @@ -2,33 +2,35 @@ import { UIDevice } from "UIKit"; import { GetBatteryInfo, GetBatteryInfoSuccess, GetBatteryInfoResult, GetBatteryInfoSync } from '../interface.uts'; + /** - * 导出 获取电量方法 + * 异步获取电量 */ export const getBatteryInfo : GetBatteryInfo = function (options) { + // 开启电量检测 + UIDevice.current.isBatteryMonitoringEnabled = true - // 开启电量检测 - UIDevice.current.isBatteryMonitoringEnabled = true - - // 返回数据 - const res : GetBatteryInfoSuccess = { - errMsg: "getBatteryInfo:ok", - level: Number(UIDevice.current.batteryLevel * 100), - isCharging: UIDevice.current.batteryState == UIDevice.BatteryState.charging, - }; - options.success?.(res); - options.complete?.(res); + // 返回数据 + const res : GetBatteryInfoSuccess = { + errMsg: "getBatteryInfo:ok", + level: Math.abs(Number(UIDevice.current.batteryLevel * 100)), + isCharging: UIDevice.current.batteryState == UIDevice.BatteryState.charging, + }; + options.success?.(res); + options.complete?.(res); } -export const getBatteryInfoSync : GetBatteryInfoSync = function (): GetBatteryInfoResult { - - // 开启电量检测 - UIDevice.current.isBatteryMonitoringEnabled = true +/** + * 同步获取电量 + */ +export const getBatteryInfoSync : GetBatteryInfoSync = function () : GetBatteryInfoResult { + // 开启电量检测 + UIDevice.current.isBatteryMonitoringEnabled = true - // 返回数据 - const res : GetBatteryInfoResult = { - level: Number(UIDevice.current.batteryLevel * 100), - isCharging: UIDevice.current.batteryState == UIDevice.BatteryState.charging, - }; - return res; -} \ No newline at end of file + // 返回数据 + const res : GetBatteryInfoResult = { + level: Math.abs(Number(UIDevice.current.batteryLevel * 100)), + isCharging: UIDevice.current.batteryState == UIDevice.BatteryState.charging, + }; + return res; +} diff --git a/uni_modules/uni-getbatteryinfo/utssdk/web/index.js b/uni_modules/uni-getbatteryinfo/utssdk/web/index.js deleted file mode 100644 index 4049f50..0000000 --- a/uni_modules/uni-getbatteryinfo/utssdk/web/index.js +++ /dev/null @@ -1,23 +0,0 @@ -export function getBatteryInfo(options) { - if (navigator.getBattery) { - navigator.getBattery().then(battery => { - const res = { - errCode: 0, - errSubject: "uni-getBatteryInfo", - errMsg: 'getBatteryInfo:ok', - level: battery.level * 100, - isCharging: battery.charging - } - options.success && options.success(res) - options.complete && options.complete(res) - }) - } else { - const res = { - errCode: 1002, - errSubject: "uni-getBatteryInfo", - errMsg: 'getBatteryInfo:fail navigator.getBattery is unsupported' - } - options.fail && options.fail(res) - options.complete && options.complete(res) - } -} diff --git a/uni_modules/uni-getbatteryinfo/utssdk/web/index.uts b/uni_modules/uni-getbatteryinfo/utssdk/web/index.uts new file mode 100644 index 0000000..bf7da47 --- /dev/null +++ b/uni_modules/uni-getbatteryinfo/utssdk/web/index.uts @@ -0,0 +1,20 @@ +import { GetBatteryInfo, GetBatteryInfoOptions, GetBatteryInfoSuccess } from '../interface.uts' +export const getBatteryInfo : GetBatteryInfo = function (options : GetBatteryInfoOptions) { + if (navigator.getBattery) { + navigator.getBattery().then(battery => { + const res = { + errCode: 0, + errSubject: "uni-getBatteryInfo", + errMsg: 'getBatteryInfo:ok', + level: battery.level * 100, + isCharging: battery.charging + } as GetBatteryInfoSuccess + options.success && options.success(res) + options.complete && options.complete(res) + }) + } else { + const res = new UniError("uni-getBatteryInfo", 1002, "getBatteryInfo:fail navigator.getBattery is unsupported") + options.fail && options.fail(res) + options.complete && options.complete(res) + } +} -- GitLab