From a39f6480ea5b6d3f81db90808123cdeb54790787 Mon Sep 17 00:00:00 2001 From: Po Lu Date: Sun, 16 Jun 2024 15:27:43 +0800 Subject: [PATCH] Minor adjustments to battery.el on Android * src/android.c (android_query_battery): * java/org/gnu/emacs/EmacsService.java (queryBattery19) (queryBattery): Reorder items in value for consistency with Fandroid_query_battery. * lisp/battery.el (battery-status-function): Select battery-android only when android-query-battery is present. (battery-android): Return temperature and correct values of power source constants. --- java/org/gnu/emacs/EmacsService.java | 4 ++-- lisp/battery.el | 22 +++++++++++++++------- src/android.c | 4 ++-- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/java/org/gnu/emacs/EmacsService.java b/java/org/gnu/emacs/EmacsService.java index cfe9e42de4d..77124a7d80f 100644 --- a/java/org/gnu/emacs/EmacsService.java +++ b/java/org/gnu/emacs/EmacsService.java @@ -1212,7 +1212,7 @@ If a display name can be requested from URI (using the resolver temp = battery.getIntExtra (BatteryManager.EXTRA_TEMPERATURE, 0); return new long[] { capacity, chargeCounter, currentAvg, - currentNow, remaining, status, plugged, + currentNow, status, remaining, plugged, temp, }; } @@ -1289,7 +1289,7 @@ else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) } return new long[] { capacity, chargeCounter, currentAvg, - currentNow, remaining, status, plugged, + currentNow, status, remaining, plugged, temp, }; } diff --git a/lisp/battery.el b/lisp/battery.el index 4aae3e0ef54..4959e48317f 100644 --- a/lisp/battery.el +++ b/lisp/battery.el @@ -33,7 +33,7 @@ ;; - BSD by using the `apm' program. ;; - Darwin (macOS) by using the `pmset' program. ;; - Windows via the GetSystemPowerStatus API call. -;; - Android 5 or later via the BatteryManager APIs. +;; - Android via the BatteryManager APIs. ;;; Code: @@ -112,7 +112,7 @@ Value does not include \".\" or \"..\"." ;; Note that even though the Linux kernel APIs are sometimes ;; available on Android, they are badly implemented by Android ;; kernels, so avoid using those. - ((eq system-type 'android) + ((fboundp 'android-query-battery) #'battery-android) ((and (eq system-type 'berkeley-unix) (file-executable-p "/usr/sbin/apm")) @@ -1107,7 +1107,8 @@ The following %-sequences are provided: (rate nil) (remaining nil) (hours nil) - (minutes nil)) + (minutes nil) + (temperature nil)) ;; Figure out the percentage. (setq percentage (number-to-string (car status))) ;; Figure out the capacity @@ -1133,6 +1134,12 @@ The following %-sequences are provided: (setq remaining (format "%d:%d" hours-left mins) hours (number-to-string hours-left) minutes (number-to-string mins)))) + ;; Return the temperature, so long as its value is not downright + ;; absurd (as when the sensor is faulty or the battery controller + ;; driver does not provide temperature readouts). + (unless (or (< (nth 7 status) -1000) + (> (nth 7 status) 1000)) + (setq temperature (/ (nth 7 status) 10.0))) ;; Return results. (list (cons ?c capacity) (cons ?p percentage) @@ -1145,11 +1152,12 @@ The following %-sequences are provided: (cons ?L (cl-case (nth 6 status) (0 "off-line") (1 "on-line") - (2 "on-line (dock)") - (3 "on-line (USB)") - (4 "on-line (wireless)") + (2 "on-line (USB)") + (4 "on-line (dock)") + (8 "on-line (wireless)") (t "unknown"))) - (cons ?t (/ (or (nth 7 status) 0) 10.0)))))) + (cons ?t (/ (or (nth 7 status) 0) 10.0)) + (cons ?d temperature))))) ;;; Private functions. diff --git a/src/android.c b/src/android.c index 17b5d6d4115..3c0e3ee1558 100644 --- a/src/android.c +++ b/src/android.c @@ -6574,8 +6574,8 @@ android_query_battery (struct android_battery_state *status) status->charge_counter = longs[1]; status->current_average = longs[2]; status->current_now = longs[3]; - status->remaining = longs[4]; - status->status = longs[5]; + status->status = longs[4]; + status->remaining = longs[5]; status->plugged = longs[6]; status->temperature = longs[7];