aboutsummaryrefslogtreecommitdiffstats
path: root/java
diff options
context:
space:
mode:
authorPo Lu2023-03-10 09:40:41 +0800
committerPo Lu2023-03-10 09:40:41 +0800
commit488a75f2e2b73038ff341f3484a8cf8584633eff (patch)
tree4871fe49e7009816a004f96d59fcb3b945c294d8 /java
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.
Diffstat (limited to 'java')
-rw-r--r--java/org/gnu/emacs/EmacsService.java65
1 files changed, 59 insertions, 6 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