From 3d73a613aecf82cf0a9bd9c60db05987fa4d5543 Mon Sep 17 00:00:00 2001 From: Alessandro Ferro <49845537+xfarrow@users.noreply.github.com> Date: Tue, 27 Sep 2022 16:21:45 +0200 Subject: [PATCH] Added battery option --- .../xfarrow/locatemydevice/SmsHandler.java | 42 ++++++++++++++++++- .../com/xfarrow/locatemydevice/Utils.java | 1 + 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/src/app/src/main/java/com/xfarrow/locatemydevice/SmsHandler.java b/src/app/src/main/java/com/xfarrow/locatemydevice/SmsHandler.java index 4a13d88..4d6d470 100644 --- a/src/app/src/main/java/com/xfarrow/locatemydevice/SmsHandler.java +++ b/src/app/src/main/java/com/xfarrow/locatemydevice/SmsHandler.java @@ -2,11 +2,14 @@ package com.xfarrow.locatemydevice; import android.Manifest; import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; import android.content.pm.PackageManager; import android.location.Criteria; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; +import android.os.BatteryManager; import android.os.Build; import android.os.Bundle; import android.telephony.CellInfo; @@ -47,7 +50,7 @@ public class SmsHandler { + "\\s" + "[^\\s]*" + "\\s" - + Utils.LOCATE_OPTION + "|" + Utils.CELLULAR_INFO_OPTION; + + Utils.LOCATE_OPTION + "|" + Utils.CELLULAR_INFO_OPTION + "|" + Utils.BATTERY_OPTION; Pattern pattern = Pattern.compile(regexToMatch); Matcher matcher = pattern.matcher(message); if (!matcher.find()) { @@ -172,6 +175,43 @@ public class SmsHandler { ArrayList parts = smsManager.divideMessage(resultSms.toString()); smsManager.sendMultipartTextMessage (sender, null, parts,null, null); } + + else if(providedOption.equals(Utils.BATTERY_OPTION)){ + IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); + Intent batteryStatus = context.registerReceiver(null, ifilter); + + StringBuilder sb = new StringBuilder(); + + // Battery level + int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); + int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1); + float batteryPct = level * 100 / (float)scale; + sb.append("Battery level: ").append(Math.round(batteryPct)).append("%\n"); + + // Are we charging / charged? + int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1); + boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING + || status == BatteryManager.BATTERY_STATUS_FULL; + sb.append("Charging: "); + if(isCharging) sb.append("Yes\n"); + else sb.append("No"); + + // How are we charging? + if(isCharging) { + int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1); + sb.append("Charging through: "); + if(chargePlug == BatteryManager.BATTERY_PLUGGED_USB) + sb.append("USB"); + else if(chargePlug == BatteryManager.BATTERY_PLUGGED_AC) + sb.append("AC (wall)"); + else if( chargePlug == BatteryManager.BATTERY_PLUGGED_WIRELESS) + sb.append("Wireless"); + else + sb.append("Unknown"); + } + ArrayList parts = smsManager.divideMessage(sb.toString()); + smsManager.sendMultipartTextMessage (sender, null, parts,null, null); + } } private void sendGpsCoordinates(SmsManager smsManager, String sendTo, double latitude, double longitude){ diff --git a/src/app/src/main/java/com/xfarrow/locatemydevice/Utils.java b/src/app/src/main/java/com/xfarrow/locatemydevice/Utils.java index 6d3ff5d..dea8f6f 100644 --- a/src/app/src/main/java/com/xfarrow/locatemydevice/Utils.java +++ b/src/app/src/main/java/com/xfarrow/locatemydevice/Utils.java @@ -11,6 +11,7 @@ public class Utils { public static final String LOCATE_OPTION = "locate"; public static final String CELLULAR_INFO_OPTION = "cellinfo"; + public static final String BATTERY_OPTION = "battery"; public static String buildOSMLink(double latitude, double longitude){ return "https://www.openstreetmap.org/?mlat=" + latitude + "&mlon=" + longitude;