[gnome-settings-daemon/wip/hadess/power-add-coarse-level-support] power: Add support for BatteryLevel UPower property



commit 44686d9cd7a3835d7c03e96f5b75ed5982145ad8
Author: Bastien Nocera <hadess hadess net>
Date:   Wed Nov 27 12:30:34 2019 +0100

    power: Add support for BatteryLevel UPower property
    
    Don't show a battery percentage when the device reports coarse battery
    levels.

 plugins/power/gsd-power-manager.c | 121 +++++++++++++++++++++++++++++---------
 plugins/power/test.py             |  53 +++++++++++++++++
 2 files changed, 146 insertions(+), 28 deletions(-)
---
diff --git a/plugins/power/gsd-power-manager.c b/plugins/power/gsd-power-manager.c
index c500fa38..c6d681df 100644
--- a/plugins/power/gsd-power-manager.c
+++ b/plugins/power/gsd-power-manager.c
@@ -452,6 +452,7 @@ engine_charge_low (GsdPowerManager *manager, UpDevice *device)
         gchar *tmp;
         gchar *remaining_text;
         gdouble percentage;
+        guint battery_level;
         char *icon_name;
         gint64 time_to_empty;
         UpDeviceKind kind;
@@ -461,9 +462,13 @@ engine_charge_low (GsdPowerManager *manager, UpDevice *device)
                       "kind", &kind,
                       "percentage", &percentage,
                       "time-to-empty", &time_to_empty,
+                      "battery-level", &battery_level,
                       "icon-name", &icon_name,
                       NULL);
 
+        if (battery_level == UP_DEVICE_LEVEL_UNKNOWN)
+                battery_level = UP_DEVICE_LEVEL_NONE;
+
         if (kind == UP_DEVICE_KIND_BATTERY) {
 
                 /* if the user has no other batteries, drop the "Laptop" wording */
@@ -499,49 +504,70 @@ engine_charge_low (GsdPowerManager *manager, UpDevice *device)
                 title = _("Mouse battery low");
 
                 /* TRANSLATORS: tell user more details */
-                message = g_strdup_printf (_("Wireless mouse is low in power (%.0f%%)"), percentage);
+                if (battery_level == UP_DEVICE_LEVEL_NONE)
+                        message = g_strdup_printf (_("Wireless mouse is low in power (%.0f%%)"), percentage);
+                else
+                        message = g_strdup_printf (_("Wireless mouse is low in power"));
 
         } else if (kind == UP_DEVICE_KIND_KEYBOARD) {
                 /* TRANSLATORS: keyboard is getting a little low */
                 title = _("Keyboard battery low");
 
                 /* TRANSLATORS: tell user more details */
-                message = g_strdup_printf (_("Wireless keyboard is low in power (%.0f%%)"), percentage);
+                if (battery_level == UP_DEVICE_LEVEL_NONE)
+                        message = g_strdup_printf (_("Wireless keyboard is low in power (%.0f%%)"), 
percentage);
+                else
+                        message = g_strdup_printf (_("Wireless keyboard is low in power"));
 
         } else if (kind == UP_DEVICE_KIND_PDA) {
                 /* TRANSLATORS: PDA is getting a little low */
                 title = _("PDA battery low");
 
                 /* TRANSLATORS: tell user more details */
-                message = g_strdup_printf (_("PDA is low in power (%.0f%%)"), percentage);
+                if (battery_level == UP_DEVICE_LEVEL_NONE)
+                        message = g_strdup_printf (_("PDA is low in power (%.0f%%)"), percentage);
+                else
+                        message = g_strdup_printf (_("PDA is low in power"));
 
         } else if (kind == UP_DEVICE_KIND_PHONE) {
                 /* TRANSLATORS: cell phone (mobile) is getting a little low */
                 title = _("Cell phone battery low");
 
                 /* TRANSLATORS: tell user more details */
-                message = g_strdup_printf (_("Cell phone is low in power (%.0f%%)"), percentage);
+                if (battery_level == UP_DEVICE_LEVEL_NONE)
+                        message = g_strdup_printf (_("Cell phone is low in power (%.0f%%)"), percentage);
+                else
+                        message = g_strdup_printf (_("Cell phone is low in power"));
 
         } else if (kind == UP_DEVICE_KIND_MEDIA_PLAYER) {
                 /* TRANSLATORS: media player, e.g. mp3 is getting a little low */
                 title = _("Media player battery low");
 
                 /* TRANSLATORS: tell user more details */
-                message = g_strdup_printf (_("Media player is low in power (%.0f%%)"), percentage);
+                if (battery_level == UP_DEVICE_LEVEL_NONE)
+                        message = g_strdup_printf (_("Media player is low in power (%.0f%%)"), percentage);
+                else
+                        message = g_strdup_printf (_("Media player is low in power"));
 
         } else if (kind == UP_DEVICE_KIND_TABLET) {
                 /* TRANSLATORS: graphics tablet, e.g. wacom is getting a little low */
                 title = _("Tablet battery low");
 
                 /* TRANSLATORS: tell user more details */
-                message = g_strdup_printf (_("Tablet is low in power (%.0f%%)"), percentage);
+                if (battery_level == UP_DEVICE_LEVEL_NONE)
+                        message = g_strdup_printf (_("Tablet is low in power (%.0f%%)"), percentage);
+                else
+                        message = g_strdup_printf (_("Tablet is low in power"));
 
         } else if (kind == UP_DEVICE_KIND_COMPUTER) {
                 /* TRANSLATORS: computer, e.g. ipad is getting a little low */
                 title = _("Attached computer battery low");
 
                 /* TRANSLATORS: tell user more details */
-                message = g_strdup_printf (_("Attached computer is low in power (%.0f%%)"), percentage);
+                if (battery_level == UP_DEVICE_LEVEL_NONE)
+                        message = g_strdup_printf (_("Attached computer is low in power (%.0f%%)"), 
percentage);
+                else
+                        message = g_strdup_printf (_("Attached computer is low in power"));
         }
 
         /* close any existing notification of this class */
@@ -575,6 +601,7 @@ engine_charge_critical (GsdPowerManager *manager, UpDevice *device)
         gboolean ret;
         gchar *message = NULL;
         gdouble percentage;
+        guint battery_level;
         char *icon_name;
         gint64 time_to_empty;
         GsdPowerActionType policy;
@@ -584,10 +611,14 @@ engine_charge_critical (GsdPowerManager *manager, UpDevice *device)
         g_object_get (device,
                       "kind", &kind,
                       "percentage", &percentage,
+                      "battery-level", &battery_level,
                       "time-to-empty", &time_to_empty,
                       "icon-name", &icon_name,
                       NULL);
 
+        if (battery_level == UP_DEVICE_LEVEL_UNKNOWN)
+                battery_level = UP_DEVICE_LEVEL_NONE;
+
         if (kind == UP_DEVICE_KIND_BATTERY) {
 
                 /* if the user has no other batteries, drop the "Laptop" wording */
@@ -633,26 +664,40 @@ engine_charge_critical (GsdPowerManager *manager, UpDevice *device)
                 title = _("Mouse battery low");
 
                 /* TRANSLATORS: the device is just going to stop working */
-                message = g_strdup_printf (_("Wireless mouse is very low in power (%.0f%%). "
-                                             "This device will soon stop functioning if not charged."),
-                                           percentage);
+                if (battery_level == UP_DEVICE_LEVEL_NONE)
+                        message = g_strdup_printf (_("Wireless mouse is very low in power (%.0f%%). "
+                                                     "This device will soon stop functioning if not 
charged."),
+                                                   percentage);
+                else
+                        message = g_strdup_printf (_("Wireless mouse is very low in power. "
+                                                     "This device will soon stop functioning if not 
charged."));
+
         } else if (kind == UP_DEVICE_KIND_KEYBOARD) {
                 /* TRANSLATORS: the keyboard battery is very low */
                 title = _("Keyboard battery low");
 
                 /* TRANSLATORS: the device is just going to stop working */
-                message = g_strdup_printf (_("Wireless keyboard is very low in power (%.0f%%). "
-                                             "This device will soon stop functioning if not charged."),
-                                           percentage);
+                if (battery_level == UP_DEVICE_LEVEL_NONE)
+                        message = g_strdup_printf (_("Wireless keyboard is very low in power (%.0f%%). "
+                                                     "This device will soon stop functioning if not 
charged."),
+                                                   percentage);
+                else
+                        message = g_strdup_printf (_("Wireless keyboard is very low in power. "
+                                                     "This device will soon stop functioning if not 
charged."));
+
         } else if (kind == UP_DEVICE_KIND_PDA) {
 
                 /* TRANSLATORS: the PDA battery is very low */
                 title = _("PDA battery low");
 
                 /* TRANSLATORS: the device is just going to stop working */
-                message = g_strdup_printf (_("PDA is very low in power (%.0f%%). "
-                                             "This device will soon stop functioning if not charged."),
-                                           percentage);
+                if (battery_level == UP_DEVICE_LEVEL_NONE)
+                        message = g_strdup_printf (_("PDA is very low in power (%.0f%%). "
+                                                     "This device will soon stop functioning if not 
charged."),
+                                                   percentage);
+                else
+                        message = g_strdup_printf (_("PDA is very low in power. "
+                                                     "This device will soon stop functioning if not 
charged."));
 
         } else if (kind == UP_DEVICE_KIND_PHONE) {
 
@@ -660,36 +705,56 @@ engine_charge_critical (GsdPowerManager *manager, UpDevice *device)
                 title = _("Cell phone battery low");
 
                 /* TRANSLATORS: the device is just going to stop working */
-                message = g_strdup_printf (_("Cell phone is very low in power (%.0f%%). "
-                                             "This device will soon stop functioning if not charged."),
-                                           percentage);
+                if (battery_level == UP_DEVICE_LEVEL_NONE)
+                        message = g_strdup_printf (_("Cell phone is very low in power (%.0f%%). "
+                                                     "This device will soon stop functioning if not 
charged."),
+                                                   percentage);
+                else
+                        message = g_strdup_printf (_("Cell phone is very low in power. "
+                                                     "This device will soon stop functioning if not 
charged."));
+
         } else if (kind == UP_DEVICE_KIND_MEDIA_PLAYER) {
 
                 /* TRANSLATORS: the cell battery is very low */
                 title = _("Cell phone battery low");
 
                 /* TRANSLATORS: the device is just going to stop working */
-                message = g_strdup_printf (_("Media player is very low in power (%.0f%%). "
-                                             "This device will soon stop functioning if not charged."),
-                                           percentage);
+                if (battery_level == UP_DEVICE_LEVEL_NONE)
+                        message = g_strdup_printf (_("Media player is very low in power (%.0f%%). "
+                                                     "This device will soon stop functioning if not 
charged."),
+                                                   percentage);
+                else
+                        message = g_strdup_printf (_("Media player is very low in power. "
+                                                     "This device will soon stop functioning if not 
charged."));
+
         } else if (kind == UP_DEVICE_KIND_TABLET) {
 
                 /* TRANSLATORS: the cell battery is very low */
                 title = _("Tablet battery low");
 
                 /* TRANSLATORS: the device is just going to stop working */
-                message = g_strdup_printf (_("Tablet is very low in power (%.0f%%). "
-                                             "This device will soon stop functioning if not charged."),
-                                           percentage);
+                if (battery_level == UP_DEVICE_LEVEL_NONE)
+                        message = g_strdup_printf (_("Tablet is very low in power (%.0f%%). "
+                                                     "This device will soon stop functioning if not 
charged."),
+                                                   percentage);
+                else
+                        message = g_strdup_printf (_("Tablet is very low in power. "
+                                                     "This device will soon stop functioning if not 
charged."));
+
         } else if (kind == UP_DEVICE_KIND_COMPUTER) {
 
                 /* TRANSLATORS: the cell battery is very low */
                 title = _("Attached computer battery low");
 
                 /* TRANSLATORS: the device is just going to stop working */
-                message = g_strdup_printf (_("Attached computer is very low in power (%.0f%%). "
-                                             "The device will soon shutdown if not charged."),
-                                           percentage);
+                if (battery_level == UP_DEVICE_LEVEL_NONE)
+                        message = g_strdup_printf (_("Attached computer is very low in power (%.0f%%). "
+                                                     "The device will soon shutdown if not charged."),
+                                                   percentage);
+                else
+                        message = g_strdup_printf (_("Attached computer is very low in power. "
+                                                     "The device will soon shutdown if not charged."));
+
         }
 
         /* close any existing notification of this class */
diff --git a/plugins/power/test.py b/plugins/power/test.py
index 5e922f17..3a2c1be3 100755
--- a/plugins/power/test.py
+++ b/plugins/power/test.py
@@ -939,8 +939,61 @@ class PowerPluginTest6(PowerPluginBase):
         # we should have gotten a notification by now
         notify_log = self.p_notify.stdout.read()
 
+        # verify notification
+        self.assertRegex(notify_log, b'[0-9.]+ Notify "Power" .* ".*" ".*Wireless mouse .*low.* 
power.*\([0-9.]+%\).*"')
+
+    def test_notify_device_battery_coarse_level(self):
+        '''critical power level notification for device batteries with coarse level'''
+
+        # Set internal battery to discharging
+        self.set_composite_battery_discharging()
+
+        # Add a device battery
+        bat2_path = '/org/freedesktop/UPower/devices/' + 'mock_MOUSE_BAT1'
+        self.obj_upower.AddObject(bat2_path,
+                                  'org.freedesktop.UPower.Device',
+                                  {
+                                      'PowerSupply': dbus.Boolean(False, variant_level=1),
+                                      'IsPresent': dbus.Boolean(True, variant_level=1),
+                                      'Model': dbus.String('Bat1', variant_level=1),
+                                      'Percentage': dbus.Double(40.0, variant_level=1),
+                                      'BatteryLevel': dbus.UInt32(UPowerGlib.DeviceLevel.LOW, 
variant_level=1),
+                                      'TimeToEmpty': dbus.Int64(1600, variant_level=1),
+                                      'EnergyFull': dbus.Double(100.0, variant_level=1),
+                                      'Energy': dbus.Double(40.0, variant_level=1),
+                                      'State': dbus.UInt32(UPowerGlib.DeviceState.DISCHARGING, 
variant_level=1),
+                                      'Type': dbus.UInt32(UPowerGlib.DeviceKind.MOUSE, variant_level=1),
+                                      'WarningLevel': dbus.UInt32(UPowerGlib.DeviceLevel.NONE, 
variant_level=1),
+                                   }, dbus.Array([], signature='(ssss)'))
+
+        obj_bat2 = self.system_bus_con.get_object('org.freedesktop.UPower', bat2_path)
+        self.obj_upower.EmitSignal('', 'DeviceAdded', 'o', [bat2_path],
+                                   dbus_interface='org.freedesktop.DBus.Mock')
+        time.sleep(1)
+
+        # now change the mouse battery to critical charge
+        obj_bat2.Set('org.freedesktop.UPower.Device', 'TimeToEmpty',
+                     dbus.Int64(30, variant_level=1),
+                     dbus_interface=dbus.PROPERTIES_IFACE)
+        obj_bat2.Set('org.freedesktop.UPower.Device', 'Energy',
+                     dbus.Double(0.5, variant_level=1),
+                     dbus_interface=dbus.PROPERTIES_IFACE)
+        obj_bat2.Set('org.freedesktop.UPower.Device', 'WarningLevel',
+                     dbus.UInt32(UPowerGlib.DeviceLevel.CRITICAL, variant_level=1),
+                     dbus_interface=dbus.PROPERTIES_IFACE)
+        obj_bat2.EmitSignal('', 'Changed', '', [], dbus_interface='org.freedesktop.DBus.Mock')
+        self.obj_upower.EmitSignal('', 'DeviceChanged', 'o', [bat2_path],
+                                   dbus_interface='org.freedesktop.DBus.Mock')
+
+        self.check_plugin_log('EMIT: charge-critical', 2)
+        time.sleep(0.5)
+
+        # we should have gotten a notification by now
+        notify_log = self.p_notify.stdout.read()
+
         # verify notification
         self.assertRegex(notify_log, b'[0-9.]+ Notify "Power" .* ".*" ".*Wireless mouse .*low.* power.*"')
+        self.assertNotRegex(notify_log, b'[0-9.]+ Notify "Power" .* ".*" ".*\([0-9.]+%\).*"')
 
     def test_forced_logout(self):
         '''Test forced logout'''


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]