From 0d8adeb7f964650837aaa4566de8f53adc65d171 Mon Sep 17 00:00:00 2001 From: Daniele Gobbetti Date: Wed, 19 Aug 2015 17:36:53 +0200 Subject: [PATCH] Some refinements: - only show the bigtext notification if the device has set extended battery info - custom icon for the low battery notification (with license information) - show device name in the notification - set the notification to high priority - the battery threshold is now set in GBDevice --- LICENSE.artwork | 5 ++++- .../GBDeviceEventBatteryInfo.java | 9 ++++++++- .../gadgetbridge/impl/GBDevice.java | 11 +++++++++++ .../service/AbstractDeviceSupport.java | 11 ++++++----- .../freeyourgadget/gadgetbridge/util/GB.java | 18 +++++++++++------- .../ic_notification_low_battery.png | Bin 0 -> 174 bytes .../ic_notification_low_battery.png | Bin 0 -> 151 bytes .../ic_notification_low_battery.png | Bin 0 -> 165 bytes .../ic_notification_low_battery.png | Bin 0 -> 217 bytes app/src/main/res/values/strings.xml | 2 +- 10 files changed, 41 insertions(+), 15 deletions(-) create mode 100644 app/src/main/res/drawable-hdpi/ic_notification_low_battery.png create mode 100644 app/src/main/res/drawable-mdpi/ic_notification_low_battery.png create mode 100644 app/src/main/res/drawable-xhdpi/ic_notification_low_battery.png create mode 100644 app/src/main/res/drawable-xxhdpi/ic_notification_low_battery.png diff --git a/LICENSE.artwork b/LICENSE.artwork index 3d8766bea..5fa78b05c 100644 --- a/LICENSE.artwork +++ b/LICENSE.artwork @@ -1,10 +1,13 @@ The following artwork is licensed under the following licenses Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0): - ic_device_pebble.png (by xphnx) + ic_device_pebble.png (by 9) ic_device_miband.png (by xphnx) ic_activitytracker.png (by xphnx) ic_watchface.png (by xphnx) Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0): "GET IT ON F-Droid" button by Laura Kalbag. Source: https://ind.ie/about/blog/f-droid-button/ + +Creative Commons Attribution 3.0 Unported license (CC BY-3.0): + ic_notification_battery_low.png by Picol.org. Source: https://commons.wikimedia.org/wiki/File:Battery_1_Picol_icon.svg diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/deviceevents/GBDeviceEventBatteryInfo.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/deviceevents/GBDeviceEventBatteryInfo.java index 2bc311afb..78c315916 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/deviceevents/GBDeviceEventBatteryInfo.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/deviceevents/GBDeviceEventBatteryInfo.java @@ -4,7 +4,7 @@ package nodomain.freeyourgadget.gadgetbridge.deviceevents; import java.util.GregorianCalendar; public class GBDeviceEventBatteryInfo extends GBDeviceEvent { - public GregorianCalendar lastChargeTime; + public GregorianCalendar lastChargeTime= null; public BatteryState state = BatteryState.UNKNOWN; //TODO: I think the string should be deprecated in favor of the Enum above public String status; @@ -22,4 +22,11 @@ public class GBDeviceEventBatteryInfo extends GBDeviceEvent { CHARGE_LOW, CHARGING, } + + public boolean extendedInfoAvailable() { + if (numCharges != -1 && lastChargeTime != null) { + return true; + } + return false; + } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/impl/GBDevice.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/impl/GBDevice.java index bf2e213e8..5c7a346e1 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/impl/GBDevice.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/impl/GBDevice.java @@ -30,6 +30,7 @@ public class GBDevice implements Parcelable { private static final Logger LOG = LoggerFactory.getLogger(GBDevice.class); public static final short RSSI_UNKNOWN = 0; public static final short BATTERY_UNKNOWN = -1; + private static final short BATTERY_THRESHOLD_PERCENT = 10; public static final String EXTRA_DEVICE = "device"; private final String mName; private final String mAddress; @@ -38,6 +39,7 @@ public class GBDevice implements Parcelable { private String mHardwareVersion = null; private State mState = State.NOT_CONNECTED; private short mBatteryLevel = BATTERY_UNKNOWN; + private short mBatteryThresholdPercent = BATTERY_THRESHOLD_PERCENT; //TODO: get rid of String mBatteryStatus in favor of Enum mBatteryState private String mBatteryStatus; private short mRssi = RSSI_UNKNOWN; @@ -293,6 +295,15 @@ public class GBDevice implements Parcelable { mBatteryStatus = batteryStatus; } + + public short getBatteryThresholdPercent() { + return mBatteryThresholdPercent; + } + + public void setBatteryThresholdPercent(short batteryThresholdPercent) { + this.mBatteryThresholdPercent = batteryThresholdPercent; + } + @Override public String toString() { return "Device " + getName() + ", " + getAddress() + ", " + getStateString(); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/AbstractDeviceSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/AbstractDeviceSupport.java index 5783aec31..037a12f2d 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/AbstractDeviceSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/AbstractDeviceSupport.java @@ -230,12 +230,13 @@ public abstract class AbstractDeviceSupport implements DeviceSupport { gbDevice.setBatteryLevel(deviceEvent.level); gbDevice.setBatteryStatus(deviceEvent.status); - - //TODO: maybe switch to a device-dependent threshold - if (deviceEvent.level < 10) { - GB.updateBatteryNotification(deviceEvent.level, - context.getString(R.string.notif_battery_low_bigtext_last_charge_time, DateFormat.getDateTimeInstance().format(deviceEvent.lastChargeTime.getTime()).toString()) + + if (deviceEvent.level <= gbDevice.getBatteryThresholdPercent()) { + GB.updateBatteryNotification(context.getString(R.string.notif_battery_low_percent, gbDevice.getName(), deviceEvent.level), + deviceEvent.extendedInfoAvailable() ? + context.getString(R.string.notif_battery_low_percent, gbDevice.getName(), deviceEvent.level) + "\n" + + context.getString(R.string.notif_battery_low_bigtext_last_charge_time, DateFormat.getDateTimeInstance().format(deviceEvent.lastChargeTime.getTime()).toString()) + context.getString(R.string.notif_battery_low_bigtext_number_of_charges, deviceEvent.numCharges) + : "" , context); } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/GB.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/GB.java index 4d624121b..fb1f4bca9 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/GB.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/GB.java @@ -300,7 +300,7 @@ public class GB { nm.notify(NOTIFICATION_ID_INSTALL, notification); } - private static Notification createBatteryNotification(int level, String text, Context context) { + private static Notification createBatteryNotification(String text, String bigText, Context context) { Intent notificationIntent = new Intent(context, ControlCenter.class); notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); @@ -308,18 +308,22 @@ public class GB { notificationIntent, 0); NotificationCompat.Builder nb = new NotificationCompat.Builder(context) - .setContentTitle(context.getString(R.string.notif_battery_low_title)) - .setContentText(context.getString(R.string.notif_battery_low_percent, level)) + .setContentTitle( context.getString(R.string.notif_battery_low_title)) + .setContentText(text) .setContentIntent(pendingIntent) - .setSmallIcon(R.drawable.ic_notification) - .setStyle(new NotificationCompat.BigTextStyle().bigText(text)) + .setSmallIcon(R.drawable.ic_notification_low_battery) + .setPriority(NotificationCompat.PRIORITY_HIGH) .setOngoing(false); + if (bigText != null) { + nb.setStyle(new NotificationCompat.BigTextStyle().bigText(bigText)); + } + return nb.build(); } - public static void updateBatteryNotification(int level, String text, Context context) { - Notification notification = createBatteryNotification(level, text, context); + public static void updateBatteryNotification(String text, String bigText, Context context) { + Notification notification = createBatteryNotification(text, bigText, context); NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); nm.notify(NOTIFICATION_ID_LOW_BATTERY, notification); diff --git a/app/src/main/res/drawable-hdpi/ic_notification_low_battery.png b/app/src/main/res/drawable-hdpi/ic_notification_low_battery.png new file mode 100644 index 0000000000000000000000000000000000000000..dad13ba6e3b0f4c85a82556e27ec854fcda88b96 GIT binary patch literal 174 zcmeAS@N?(olHy`uVBq!ia0vp^Dj>|k1|%Oc%$NbBN}1CrGd^P7vv73N&yz z`2YX^>C7MNs*;@ecKx4!t|5Y5l&O$`x6ouIpV5NCO9BU8DJaz2N&WNjWoJBl(ZP8C z>`T_|Nnn3 zhdU0sJ!hHP{|kJO=y)I85Z}ymFujqfRdHfN661uu@?GaACo^7gm?6n_L&t%ANz{dg xCD#+LFm2LVkgIapX67a1#TU1&02!ylz)-Tu`g+cKLs_6r44$rjF6*2UngGMeHtYZZ literal 0 HcmV?d00001 diff --git a/app/src/main/res/drawable-xhdpi/ic_notification_low_battery.png b/app/src/main/res/drawable-xhdpi/ic_notification_low_battery.png new file mode 100644 index 0000000000000000000000000000000000000000..f1aff36a375d7fe90eae6dc45cca179c94b05eec GIT binary patch literal 165 zcmeAS@N?(olHy`uVBq!ia0vp^1|ZDA1|-9oezpUt98VX=kcwMxub<{@Fc5G!IAz+U zOOak1M7%rXY&sVy9H~i5c>i0W-Is%bp=afkNnVSW@kD$(X@CBgs)E^vC7*XN9^dr* zq$zV=Sw}weyek|#Cb4h`C^$4UXfpk>Q;k_H%Tn@x;Yam#atsU(x6P)fSgm1S6LK49 OFN3G6pUXO@geCyV{yXph literal 0 HcmV?d00001 diff --git a/app/src/main/res/drawable-xxhdpi/ic_notification_low_battery.png b/app/src/main/res/drawable-xxhdpi/ic_notification_low_battery.png new file mode 100644 index 0000000000000000000000000000000000000000..60dacdf781a11b1574063cc18de04b9abf3d4689 GIT binary patch literal 217 zcmeAS@N?(olHy`uVBq!ia0vp^9w5xY1|&n@ZgvM!3p`yMLn>~)y|r7=!9m0!aH)nP zr&nN?pU47^My4rS%mi+KjGtrtS*dex=gjS0Oh6qB4eBidvn<|Tf9|nB@lS&T(^KJ$ zp38e{erYqC%K!KUnable to install the given firmware: it doesn\'t match your Pebble\'s hardware revision. Please wait while determining the installation status... Gadget battery Low! - Battery left: %s%% + %1$s battery left: %2$s%% Last charge: %s \n Number of charges: %s