diff options
author | Po Lu <luangruo@yahoo.com> | 2023-02-25 19:11:07 +0800 |
---|---|---|
committer | Po Lu <luangruo@yahoo.com> | 2023-02-25 19:11:07 +0800 |
commit | 8e4c5db193dc7baee5846520fe8b63d8bea99148 (patch) | |
tree | 595eb65601d38b7895ae1d00ec9a71a947cd1af7 /lisp/battery.el | |
parent | d5cccfdc564f1486431f9674503c794df33d7fbc (diff) |
Update Android port
* doc/emacs/android.texi (Android Startup, Android File System)
(Android Environment, Android Windowing, Android
Troubleshooting): Improve documentation; fix typos.
* doc/lispref/commands.texi (Misc Events): Likewise.
* java/org/gnu/emacs/EmacsService.java (queryBattery): New
function.
* lisp/battery.el (battery-status-function): Set appropriately
for Android.
(battery-android): New function.
* src/android.c (struct android_emacs_service): New method
`query_battery'.
(android_check_content_access): Improve exception checking.
(android_init_emacs_service): Look up new method.
(android_destroy_handle, android_create_window)
(android_init_android_rect_class, android_init_emacs_gc_class)
(android_set_clip_rectangles)
(android_create_pixmap_from_bitmap_data, android_fill_polygon)
(android_get_image, android_put_image, android_bell)
(android_set_input_focus, android_raise_window)
(android_lower_window, android_query_tree, android_get_geometry)
(android_translate_coordinates, android_wc_lookup_string)
(android_damage_window, android_build_string)
(android_build_jstring, android_exception_check_1)
(android_exception_check_2): New functions.
(android_browse_url): Improve exception handling. Always use
android_exception_check and don't leak local refs.
(android_query_battery): New function.
* src/android.h (struct android_battery_state): New struct.
* src/androidfns.c (Fandroid_query_battery, syms_of_androidfns):
New function.
* src/androidfont.c (androidfont_from_lisp, DO_SYMBOL_FIELD)
(DO_CARDINAL_FIELD, androidfont_list, androidfont_match)
(androidfont_draw, androidfont_open_font)
(androidfont_close_font):
* src/androidselect.c (Fandroid_set_clipboard)
(Fandroid_get_clipboard):
* src/sfnt.c (sfnt_map_glyf_table):
* src/sfntfont.c (sfntfont_free_outline_cache)
(sfntfont_free_raster_cache, sfntfont_close): Allow font close
functions to be called twice.
Diffstat (limited to 'lisp/battery.el')
-rw-r--r-- | lisp/battery.el | 76 |
1 files changed, 73 insertions, 3 deletions
diff --git a/lisp/battery.el b/lisp/battery.el index 4306d5b2058..a2bbd463c12 100644 --- a/lisp/battery.el +++ b/lisp/battery.el @@ -29,9 +29,11 @@ ;; - The `/sys/class/power_supply/' files of Linux >= 2.6.39. ;; - The `/proc/acpi/' directory structure of Linux 2.4.20 and 2.6. ;; - The `/proc/apm' file format of Linux version 1.3.58 or newer. +;; - The Haiku ACPI battery driver. ;; - 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. ;;; Code: @@ -95,17 +97,22 @@ Value does not include \".\" or \"..\"." (defcustom battery-status-function (cond ((member battery-upower-service (dbus-list-activatable-names)) #'battery-upower) - ((and (eq system-type 'gnu/linux) + ;; Try to find the relevant devices in /sys and /proc on + ;; Android as well, in case the system makes them available. + ((and (memq system-type '(gnu/linux android)) (file-readable-p "/sys/") (battery--find-linux-sysfs-batteries)) #'battery-linux-sysfs) - ((and (eq system-type 'gnu/linux) + ((and (memq system-type '(gnu/linux android)) (file-directory-p "/proc/acpi/battery")) #'battery-linux-proc-acpi) - ((and (eq system-type 'gnu/linux) + ((and (memq system-type '(gnu/linux android)) (file-readable-p "/proc/") (file-readable-p "/proc/apm")) #'battery-linux-proc-apm) + ;; Now try the Android battery status function. + ((eq system-type 'android) + #'battery-android) ((and (eq system-type 'berkeley-unix) (file-executable-p "/usr/sbin/apm")) #'battery-bsd-apm) @@ -1072,6 +1079,69 @@ The following %-sequences are provided: (cons ?t (or remaining-time "N/A"))))) +;;; `BatteryManager' interface for Android. + +(declare-function android-query-battery "androidfns.c") + +(defun battery-android () + "Get battery status information using Android. + +The following %-sequences are provided: +%c Current capacity (mAh) +%r Current rate of charge or discharge (mA) +%B Battery status (verbose) +%b Battery status, empty means high, `-' means low, + `+' means charging and `?' means unknown. +%p Battery load percentage. +%m Remaining time (to charge) in minutes. +%h Remaining time (to charge) in hours. +%t Remaining time (to charge) in the form `h:min'." + (when-let* ((status (android-query-battery))) + (let* ((percentage nil) + (capacity nil) + (sym-status nil) + (symbol nil) + (rate nil) + (remaining nil) + (hours nil) + (minutes nil)) + ;; Figure out the percentage. + (setq percentage (number-to-string (car status))) + ;; Figure out the capacity + (setq capacity (number-to-string (/ (cadr status) 1000))) + ;; Figure out the battery status. + (let ((percentage (car status))) + (cl-ecase (nth 4 status) + (2 (setq sym-status "charging" symbol "+")) + (3 (setq sym-status "discharging" + symbol (if (< percentage 15) "-" " "))) + (5 (setq sym-status "full" symbol " ")) + (4 (setq sym-status "not charging" + symbol (if (< percentage 15) "-" " "))) + (1 (setq sym-status "unknown" symbol "?")))) + ;; Figure out the rate of charge. + (setq rate (/ (nth 3 status) 1000)) + ;; Figure out the remaining time. + (let* ((time (nth 5 status)) + (mins (/ time (* 1000 60))) + (hours-left (/ mins 60)) + (mins (mod mins 60))) + (unless (eq time -1) + (setq remaining (format "%d:%d" hours-left mins) + hours (number-to-string hours-left) + minutes (number-to-string mins)))) + ;; Return results. + (list (cons ?c capacity) + (cons ?p percentage) + (cons ?r rate) + (cons ?B sym-status) + (cons ?b symbol) + (cons ?m (or minutes "N/A")) + (cons ?h (or hours "N/A")) + (cons ?t (or remaining "N/A")) + (cons ?L "N/A"))))) + + ;;; Private functions. (defun battery-format (format alist) |