1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-27 04:16:49 +01:00

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
This commit is contained in:
Daniele Gobbetti 2015-08-19 17:36:53 +02:00
parent 57a85e63b0
commit 0d8adeb7f9
10 changed files with 41 additions and 15 deletions

View File

@ -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

View File

@ -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;
}
}

View File

@ -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();

View File

@ -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);
}

View File

@ -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);

Binary file not shown.

After

Width:  |  Height:  |  Size: 174 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 151 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 165 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 217 B

View File

@ -167,7 +167,7 @@
<string name="pbw_install_handler_hw_revision_mismatch">Unable to install the given firmware: it doesn\'t match your Pebble\'s hardware revision.</string>
<string name="installer_activity_wait_while_determining_status">Please wait while determining the installation status...</string>
<string name="notif_battery_low_title">Gadget battery Low!</string>
<string name="notif_battery_low_percent">Battery left: %s%%</string>
<string name="notif_battery_low_percent">%1$s battery left: %2$s%%</string>
<string name="notif_battery_low_bigtext_last_charge_time">Last charge: %s \n</string>
<string name="notif_battery_low_bigtext_number_of_charges">Number of charges: %s</string>
</resources>