aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPo Lu2023-03-10 09:40:41 +0800
committerPo Lu2023-03-10 09:40:41 +0800
commit488a75f2e2b73038ff341f3484a8cf8584633eff (patch)
tree4871fe49e7009816a004f96d59fcb3b945c294d8
parent4392423cb6df5a8af9a0520da04378e189fd387e (diff)
downloademacs-488a75f2e2b73038ff341f3484a8cf8584633eff.tar.gz
emacs-488a75f2e2b73038ff341f3484a8cf8584633eff.zip
Port Android battery status to Android 4.4 and earlier
* java/org/gnu/emacs/EmacsService.java (EmacsService) (queryBattery19): New function. (queryBattery): Call it on old systems. Also, return AC line status and temperature. * lisp/battery.el (battery-android): Implement more format directives. * src/android.c (android_query_battery): Handle new status fields. * src/android.h (struct android_battery_state): Add `plugged' and `temperature'. * src/androidfns.c (Fandroid_query_battery): Return new fields.
-rw-r--r--java/org/gnu/emacs/EmacsService.java65
-rw-r--r--lisp/battery.el11
-rw-r--r--src/android.c4
-rw-r--r--src/android.h12
-rw-r--r--src/androidfns.c16
5 files changed, 94 insertions, 14 deletions
diff --git a/java/org/gnu/emacs/EmacsService.java b/java/org/gnu/emacs/EmacsService.java
index 848ad4de789..9c48c56ca26 100644
--- a/java/org/gnu/emacs/EmacsService.java
+++ b/java/org/gnu/emacs/EmacsService.java
@@ -40,6 +40,7 @@ import android.content.ClipboardManager;
40import android.content.Context; 40import android.content.Context;
41import android.content.ContentResolver; 41import android.content.ContentResolver;
42import android.content.Intent; 42import android.content.Intent;
43import android.content.IntentFilter;
43import android.content.pm.ApplicationInfo; 44import android.content.pm.ApplicationInfo;
44import android.content.pm.PackageManager.ApplicationInfoFlags; 45import android.content.pm.PackageManager.ApplicationInfoFlags;
45import android.content.pm.PackageManager; 46import android.content.pm.PackageManager;
@@ -738,6 +739,36 @@ public final class EmacsService extends Service
738 } 739 }
739 } 740 }
740 741
742 private long[]
743 queryBattery19 ()
744 {
745 IntentFilter filter;
746 Intent battery;
747 long capacity, chargeCounter, currentAvg, currentNow;
748 long status, remaining, plugged, temp;
749
750 filter = new IntentFilter (Intent.ACTION_BATTERY_CHANGED);
751 battery = registerReceiver (null, filter);
752
753 if (battery == null)
754 return null;
755
756 capacity = battery.getIntExtra (BatteryManager.EXTRA_LEVEL, 0);
757 chargeCounter
758 = (battery.getIntExtra (BatteryManager.EXTRA_SCALE, 0)
759 / battery.getIntExtra (BatteryManager.EXTRA_LEVEL, 100) * 100);
760 currentAvg = 0;
761 currentNow = 0;
762 status = battery.getIntExtra (BatteryManager.EXTRA_STATUS, 0);
763 remaining = -1;
764 plugged = battery.getIntExtra (BatteryManager.EXTRA_PLUGGED, 0);
765 temp = battery.getIntExtra (BatteryManager.EXTRA_TEMPERATURE, 0);
766
767 return new long[] { capacity, chargeCounter, currentAvg,
768 currentNow, remaining, status, plugged,
769 temp, };
770 }
771
741 /* Return the status of the battery. See struct 772 /* Return the status of the battery. See struct
742 android_battery_status for the order of the elements 773 android_battery_status for the order of the elements
743 returned. 774 returned.
@@ -750,14 +781,16 @@ public final class EmacsService extends Service
750 Object tem; 781 Object tem;
751 BatteryManager manager; 782 BatteryManager manager;
752 long capacity, chargeCounter, currentAvg, currentNow; 783 long capacity, chargeCounter, currentAvg, currentNow;
753 long status, remaining; 784 long status, remaining, plugged, temp;
754 int prop; 785 int prop;
786 IntentFilter filter;
787 Intent battery;
755 788
756 /* Android 4.4 or earlier require applications to listen to 789 /* Android 4.4 or earlier require applications to use a different
757 changes to the battery instead of querying for its status. */ 790 API to query the battery status. */
758 791
759 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) 792 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
760 return null; 793 return queryBattery19 ();
761 794
762 tem = getSystemService (Context.BATTERY_SERVICE); 795 tem = getSystemService (Context.BATTERY_SERVICE);
763 manager = (BatteryManager) tem; 796 manager = (BatteryManager) tem;
@@ -776,7 +809,8 @@ public final class EmacsService extends Service
776 only return ``charging'' or ``discharging''. */ 809 only return ``charging'' or ``discharging''. */
777 810
778 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) 811 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
779 status = manager.getIntProperty (BatteryManager.BATTERY_PROPERTY_STATUS); 812 status
813 = manager.getIntProperty (BatteryManager.BATTERY_PROPERTY_STATUS);
780 else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) 814 else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
781 status = (manager.isCharging () 815 status = (manager.isCharging ()
782 ? BatteryManager.BATTERY_STATUS_CHARGING 816 ? BatteryManager.BATTERY_STATUS_CHARGING
@@ -789,8 +823,27 @@ public final class EmacsService extends Service
789 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) 823 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
790 remaining = manager.computeChargeTimeRemaining (); 824 remaining = manager.computeChargeTimeRemaining ();
791 825
826 plugged = -1;
827 temp = -1;
828
829 /* Now obtain additional information from the battery manager. */
830
831 filter = new IntentFilter (Intent.ACTION_BATTERY_CHANGED);
832 battery = registerReceiver (null, filter);
833
834 if (battery != null)
835 {
836 plugged = battery.getIntExtra (BatteryManager.EXTRA_PLUGGED, 0);
837 temp = battery.getIntExtra (BatteryManager.EXTRA_TEMPERATURE, 0);
838
839 /* Make status more reliable. */
840 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
841 status = battery.getIntExtra (BatteryManager.EXTRA_STATUS, 0);
842 }
843
792 return new long[] { capacity, chargeCounter, currentAvg, 844 return new long[] { capacity, chargeCounter, currentAvg,
793 currentNow, remaining, status, }; 845 currentNow, remaining, status, plugged,
846 temp, };
794 } 847 }
795 848
796 /* Display the specified STRING in a small dialog box on the main 849 /* Display the specified STRING in a small dialog box on the main
diff --git a/lisp/battery.el b/lisp/battery.el
index a2bbd463c12..a51bc5267b3 100644
--- a/lisp/battery.el
+++ b/lisp/battery.el
@@ -1089,9 +1089,11 @@ The following %-sequences are provided:
1089The following %-sequences are provided: 1089The following %-sequences are provided:
1090%c Current capacity (mAh) 1090%c Current capacity (mAh)
1091%r Current rate of charge or discharge (mA) 1091%r Current rate of charge or discharge (mA)
1092%L AC line status (verbose).
1092%B Battery status (verbose) 1093%B Battery status (verbose)
1093%b Battery status, empty means high, `-' means low, 1094%b Battery status, empty means high, `-' means low,
1094 `+' means charging and `?' means unknown. 1095 `+' means charging and `?' means unknown.
1096%d Temperature (in degrees Celsius)
1095%p Battery load percentage. 1097%p Battery load percentage.
1096%m Remaining time (to charge) in minutes. 1098%m Remaining time (to charge) in minutes.
1097%h Remaining time (to charge) in hours. 1099%h Remaining time (to charge) in hours.
@@ -1139,7 +1141,14 @@ The following %-sequences are provided:
1139 (cons ?m (or minutes "N/A")) 1141 (cons ?m (or minutes "N/A"))
1140 (cons ?h (or hours "N/A")) 1142 (cons ?h (or hours "N/A"))
1141 (cons ?t (or remaining "N/A")) 1143 (cons ?t (or remaining "N/A"))
1142 (cons ?L "N/A"))))) 1144 (cons ?L (cl-case (nth 6 status)
1145 (0 "off-line")
1146 (1 "on-line")
1147 (2 "on-line (dock)")
1148 (3 "on-line (USB)")
1149 (4 "on-line (wireless)")
1150 (t "unknown")))
1151 (cons ?t (/ (or (nth 7 status) 0) 10.0))))))
1143 1152
1144 1153
1145;;; Private functions. 1154;;; Private functions.
diff --git a/src/android.c b/src/android.c
index 69c87e731bd..763e17e9430 100644
--- a/src/android.c
+++ b/src/android.c
@@ -5754,7 +5754,7 @@ android_get_current_api_level (void)
5754} 5754}
5755 5755
5756/* Query the status of the battery, and place it in *STATUS. 5756/* Query the status of the battery, and place it in *STATUS.
5757 Value is 1 if the system is too old, else 0. */ 5757 Value is 1 upon failure, else 0. */
5758 5758
5759int 5759int
5760android_query_battery (struct android_battery_state *status) 5760android_query_battery (struct android_battery_state *status)
@@ -5783,6 +5783,8 @@ android_query_battery (struct android_battery_state *status)
5783 status->current_now = longs[3]; 5783 status->current_now = longs[3];
5784 status->remaining = longs[4]; 5784 status->remaining = longs[4];
5785 status->status = longs[5]; 5785 status->status = longs[5];
5786 status->plugged = longs[6];
5787 status->temperature = longs[7];
5786 5788
5787 (*android_java_env)->ReleaseLongArrayElements (android_java_env, 5789 (*android_java_env)->ReleaseLongArrayElements (android_java_env,
5788 array, longs, 5790 array, longs,
diff --git a/src/android.h b/src/android.h
index ed0089ad94e..450f3859df9 100644
--- a/src/android.h
+++ b/src/android.h
@@ -160,6 +160,18 @@ struct android_battery_state
160 but is not charging either. 160 but is not charging either.
161 1, if the battery state is unknown. */ 161 1, if the battery state is unknown. */
162 int status; 162 int status;
163
164 /* The power source of the battery. Value is:
165
166 0, if on battery power.
167 1, for line power.
168 8, for dock power.
169 2, for USB power.
170 4, for wireless power. */
171 int plugged;
172
173 /* The temperature of the battery in 10 * degrees centigrade. */
174 int temperature;
163}; 175};
164 176
165extern Lisp_Object android_browse_url (Lisp_Object); 177extern Lisp_Object android_browse_url (Lisp_Object);
diff --git a/src/androidfns.c b/src/androidfns.c
index 5a23e8bd196..2724b9595c1 100644
--- a/src/androidfns.c
+++ b/src/androidfns.c
@@ -2797,11 +2797,13 @@ frame_parm_handler android_frame_parm_handlers[] =
2797DEFUN ("android-query-battery", Fandroid_query_battery, 2797DEFUN ("android-query-battery", Fandroid_query_battery,
2798 Sandroid_query_battery, 0, 0, 0, 2798 Sandroid_query_battery, 0, 0, 0,
2799 doc: /* Perform a query for battery information. 2799 doc: /* Perform a query for battery information.
2800This function will not work before Android 5.0.
2801Value is nil upon failure, or a list of the form: 2800Value is nil upon failure, or a list of the form:
2802 2801
2803 (CAPACITY CHARGE-COUNTER CURRENT-AVERAGE CURRENT-NOW STATUS 2802 (CAPACITY CHARGE-COUNTER CURRENT-AVERAGE CURRENT-NOW STATUS
2804 REMAINING) 2803 REMAINING PLUGGED TEMP)
2804
2805where REMAINING, CURRENT-AVERAGE, and CURRENT-NOW are undefined prior
2806to Android 5.0.
2805 2807
2806See the documentation at 2808See the documentation at
2807 2809
@@ -2822,12 +2824,14 @@ for more details about these values. */)
2822 if (android_query_battery (&state)) 2824 if (android_query_battery (&state))
2823 return Qnil; 2825 return Qnil;
2824 2826
2825 return listn (6, make_int (state.capacity), 2827 return listn (8, make_int (state.capacity),
2826 make_int (state.charge_counter), 2828 make_fixnum (state.charge_counter),
2827 make_int (state.current_average), 2829 make_int (state.current_average),
2828 make_int (state.current_now), 2830 make_int (state.current_now),
2829 make_int (state.status), 2831 make_fixnum (state.status),
2830 make_int (state.remaining)); 2832 make_int (state.remaining),
2833 make_fixnum (state.plugged),
2834 make_fixnum (state.temperature));
2831} 2835}
2832 2836
2833#endif 2837#endif