From 60ea0421c88268b274c298e4953f050e0fdff3e5 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Mon, 2 May 2022 22:38:42 +0200 Subject: [PATCH] UploaderBatteryComplication -> kt --- .../UploaderBatteryComplication.java | 158 ------------------ .../UploaderBatteryComplication.kt | 108 ++++++++++++ 2 files changed, 108 insertions(+), 158 deletions(-) delete mode 100644 wear/src/main/java/info/nightscout/androidaps/complications/UploaderBatteryComplication.java create mode 100644 wear/src/main/java/info/nightscout/androidaps/complications/UploaderBatteryComplication.kt diff --git a/wear/src/main/java/info/nightscout/androidaps/complications/UploaderBatteryComplication.java b/wear/src/main/java/info/nightscout/androidaps/complications/UploaderBatteryComplication.java deleted file mode 100644 index 9e5e0b52c8..0000000000 --- a/wear/src/main/java/info/nightscout/androidaps/complications/UploaderBatteryComplication.java +++ /dev/null @@ -1,158 +0,0 @@ -package info.nightscout.androidaps.complications; - -import android.app.PendingIntent; -import android.graphics.drawable.Icon; -import android.support.wearable.complications.ComplicationData; -import android.support.wearable.complications.ComplicationText; - -import androidx.annotation.DrawableRes; - -import info.nightscout.androidaps.R; -import info.nightscout.androidaps.data.RawDisplayData; -import info.nightscout.shared.logging.LTag; - -/* - * Created by dlvoy on 2019-11-12 - */ -@SuppressWarnings("deprecation") -public class UploaderBatteryComplication extends BaseComplicationProviderService { - - public ComplicationData buildComplicationData(int dataType, RawDisplayData raw, PendingIntent complicationPendingIntent) { - - ComplicationData complicationData = null; - - @DrawableRes int batteryIcon = R.drawable.ic_battery_unknown; - @DrawableRes int burnInBatteryIcon = R.drawable.ic_battery_unknown_burnin; - int level = 0; - String levelStr = "???"; - - if (raw.getStatus().getBattery().matches("^[0-9]+$")) { - try { - level = Integer.parseInt(raw.getStatus().getBattery()); - level = Math.max(Math.min(level, 100), 0); - levelStr = level + "%"; - int iconNo = (int) Math.floor(level / 10.0); - if (level > 95) { - iconNo = 10; - } - switch (iconNo) { - case 10: - batteryIcon = R.drawable.ic_battery_charging_wireless; - break; - case 9: - batteryIcon = R.drawable.ic_battery_charging_wireless_90; - break; - case 8: - batteryIcon = R.drawable.ic_battery_charging_wireless_80; - break; - case 7: - batteryIcon = R.drawable.ic_battery_charging_wireless_70; - break; - case 6: - batteryIcon = R.drawable.ic_battery_charging_wireless_60; - break; - case 5: - batteryIcon = R.drawable.ic_battery_charging_wireless_50; - break; - case 4: - batteryIcon = R.drawable.ic_battery_charging_wireless_40; - break; - case 3: - batteryIcon = R.drawable.ic_battery_charging_wireless_30; - break; - case 2: - batteryIcon = R.drawable.ic_battery_charging_wireless_20; - break; - case 1: - batteryIcon = R.drawable.ic_battery_charging_wireless_10; - break; - case 0: - batteryIcon = R.drawable.ic_battery_alert_variant_outline; - break; - default: - batteryIcon = R.drawable.ic_battery_charging_wireless_outline; - } - - switch (iconNo) { - case 10: - burnInBatteryIcon = R.drawable.ic_battery_charging_wireless_burnin; - break; - case 9: - burnInBatteryIcon = R.drawable.ic_battery_charging_wireless_90_burnin; - break; - case 8: - burnInBatteryIcon = R.drawable.ic_battery_charging_wireless_80_burnin; - break; - case 7: - burnInBatteryIcon = R.drawable.ic_battery_charging_wireless_70_burnin; - break; - case 6: - burnInBatteryIcon = R.drawable.ic_battery_charging_wireless_60_burnin; - break; - case 5: - burnInBatteryIcon = R.drawable.ic_battery_charging_wireless_50_burnin; - break; - case 4: - burnInBatteryIcon = R.drawable.ic_battery_charging_wireless_40_burnin; - break; - case 3: - burnInBatteryIcon = R.drawable.ic_battery_charging_wireless_30_burnin; - break; - case 2: - burnInBatteryIcon = R.drawable.ic_battery_charging_wireless_20_burnin; - break; - case 1: - burnInBatteryIcon = R.drawable.ic_battery_charging_wireless_10_burnin; - break; - case 0: - burnInBatteryIcon = R.drawable.ic_battery_alert_variant_outline; - break; - default: - burnInBatteryIcon = R.drawable.ic_battery_charging_wireless_outline; - } - - - } catch (NumberFormatException ex) { - aapsLogger.error(LTag.WEAR, "Cannot parse battery level of: " + raw.getStatus().getBattery()); - } - } - - if (dataType == ComplicationData.TYPE_RANGED_VALUE) { - final ComplicationData.Builder builder = new ComplicationData.Builder(ComplicationData.TYPE_RANGED_VALUE) - .setMinValue(0) - .setMaxValue(100) - .setValue(level) - .setShortText(ComplicationText.plainText(levelStr)) - .setIcon(Icon.createWithResource(this, batteryIcon)) - .setBurnInProtectionIcon(Icon.createWithResource(this, burnInBatteryIcon)) - .setTapAction(complicationPendingIntent); - complicationData = builder.build(); - } else if (dataType == ComplicationData.TYPE_SHORT_TEXT) { - final ComplicationData.Builder builder = new ComplicationData.Builder(ComplicationData.TYPE_SHORT_TEXT) - .setShortText(ComplicationText.plainText(levelStr)) - .setIcon(Icon.createWithResource(this, batteryIcon)) - .setBurnInProtectionIcon(Icon.createWithResource(this, burnInBatteryIcon)) - .setTapAction(complicationPendingIntent); - complicationData = builder.build(); - } else if (dataType == ComplicationData.TYPE_ICON) { - final ComplicationData.Builder builder = new ComplicationData.Builder(ComplicationData.TYPE_ICON) - .setIcon(Icon.createWithResource(this, batteryIcon)) - .setBurnInProtectionIcon(Icon.createWithResource(this, burnInBatteryIcon)) - .setTapAction(complicationPendingIntent); - complicationData = builder.build(); - } else { - aapsLogger.warn(LTag.WEAR, "Unexpected complication type " + dataType); - } - return complicationData; - } - - @Override - public String getProviderCanonicalName() { - return UploaderBatteryComplication.class.getCanonicalName(); - } - - @Override - public ComplicationAction getComplicationAction() { - return ComplicationAction.STATUS; - } -} diff --git a/wear/src/main/java/info/nightscout/androidaps/complications/UploaderBatteryComplication.kt b/wear/src/main/java/info/nightscout/androidaps/complications/UploaderBatteryComplication.kt new file mode 100644 index 0000000000..6260b8d804 --- /dev/null +++ b/wear/src/main/java/info/nightscout/androidaps/complications/UploaderBatteryComplication.kt @@ -0,0 +1,108 @@ +@file:Suppress("DEPRECATION") + +package info.nightscout.androidaps.complications + +import android.app.PendingIntent +import android.graphics.drawable.Icon +import android.support.wearable.complications.ComplicationData +import android.support.wearable.complications.ComplicationText +import androidx.annotation.DrawableRes +import info.nightscout.androidaps.R +import info.nightscout.androidaps.data.RawDisplayData +import info.nightscout.shared.logging.LTag +import kotlin.math.floor +import kotlin.math.max +import kotlin.math.min + +/* + * Created by dlvoy on 2019-11-12 + */ +class UploaderBatteryComplication : BaseComplicationProviderService() { + + override fun buildComplicationData(dataType: Int, raw: RawDisplayData, complicationPendingIntent: PendingIntent): ComplicationData? { + var complicationData: ComplicationData? = null + @DrawableRes var batteryIcon = R.drawable.ic_battery_unknown + @DrawableRes var burnInBatteryIcon = R.drawable.ic_battery_unknown_burnin + var level = 0 + var levelStr = "???" + if (raw.status.battery.matches(Regex("^[0-9]+$"))) { + try { + level = raw.status.battery.toInt() + level = max(min(level, 100), 0) + levelStr = "$level%" + var iconNo = floor(level / 10.0).toInt() + if (level > 95) { + iconNo = 10 + } + batteryIcon = when (iconNo) { + 10 -> R.drawable.ic_battery_charging_wireless + 9 -> R.drawable.ic_battery_charging_wireless_90 + 8 -> R.drawable.ic_battery_charging_wireless_80 + 7 -> R.drawable.ic_battery_charging_wireless_70 + 6 -> R.drawable.ic_battery_charging_wireless_60 + 5 -> R.drawable.ic_battery_charging_wireless_50 + 4 -> R.drawable.ic_battery_charging_wireless_40 + 3 -> R.drawable.ic_battery_charging_wireless_30 + 2 -> R.drawable.ic_battery_charging_wireless_20 + 1 -> R.drawable.ic_battery_charging_wireless_10 + 0 -> R.drawable.ic_battery_alert_variant_outline + else -> R.drawable.ic_battery_charging_wireless_outline + } + burnInBatteryIcon = when (iconNo) { + 10 -> R.drawable.ic_battery_charging_wireless_burnin + 9 -> R.drawable.ic_battery_charging_wireless_90_burnin + 8 -> R.drawable.ic_battery_charging_wireless_80_burnin + 7 -> R.drawable.ic_battery_charging_wireless_70_burnin + 6 -> R.drawable.ic_battery_charging_wireless_60_burnin + 5 -> R.drawable.ic_battery_charging_wireless_50_burnin + 4 -> R.drawable.ic_battery_charging_wireless_40_burnin + 3 -> R.drawable.ic_battery_charging_wireless_30_burnin + 2 -> R.drawable.ic_battery_charging_wireless_20_burnin + 1 -> R.drawable.ic_battery_charging_wireless_10_burnin + 0 -> R.drawable.ic_battery_alert_variant_outline + else -> R.drawable.ic_battery_charging_wireless_outline + } + } catch (ex: NumberFormatException) { + aapsLogger.error(LTag.WEAR, "Cannot parse battery level of: " + raw.status.battery) + } + } + when (dataType) { + ComplicationData.TYPE_RANGED_VALUE -> { + val builder = ComplicationData.Builder(ComplicationData.TYPE_RANGED_VALUE) + .setMinValue(0f) + .setMaxValue(100f) + .setValue(level.toFloat()) + .setShortText(ComplicationText.plainText(levelStr)) + .setIcon(Icon.createWithResource(this, batteryIcon)) + .setBurnInProtectionIcon(Icon.createWithResource(this, burnInBatteryIcon)) + .setTapAction(complicationPendingIntent) + complicationData = builder.build() + } + + ComplicationData.TYPE_SHORT_TEXT -> { + val builder = ComplicationData.Builder(ComplicationData.TYPE_SHORT_TEXT) + .setShortText(ComplicationText.plainText(levelStr)) + .setIcon(Icon.createWithResource(this, batteryIcon)) + .setBurnInProtectionIcon(Icon.createWithResource(this, burnInBatteryIcon)) + .setTapAction(complicationPendingIntent) + complicationData = builder.build() + } + + ComplicationData.TYPE_ICON -> { + val builder = ComplicationData.Builder(ComplicationData.TYPE_ICON) + .setIcon(Icon.createWithResource(this, batteryIcon)) + .setBurnInProtectionIcon(Icon.createWithResource(this, burnInBatteryIcon)) + .setTapAction(complicationPendingIntent) + complicationData = builder.build() + } + + else -> { + aapsLogger.warn(LTag.WEAR, "Unexpected complication type $dataType") + } + } + return complicationData + } + + override fun getProviderCanonicalName(): String = UploaderBatteryComplication::class.java.canonicalName!! + override fun getComplicationAction(): ComplicationAction = ComplicationAction.STATUS +} \ No newline at end of file