Added battery option

This commit is contained in:
Alessandro Ferro 2022-09-27 16:21:45 +02:00
parent 422645cbe2
commit 3d73a613ae
2 changed files with 42 additions and 1 deletions

View File

@ -2,11 +2,14 @@ package com.xfarrow.locatemydevice;
import android.Manifest; import android.Manifest;
import android.content.Context; import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.location.Criteria; import android.location.Criteria;
import android.location.Location; import android.location.Location;
import android.location.LocationListener; import android.location.LocationListener;
import android.location.LocationManager; import android.location.LocationManager;
import android.os.BatteryManager;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.telephony.CellInfo; import android.telephony.CellInfo;
@ -47,7 +50,7 @@ public class SmsHandler {
+ "\\s" + "\\s"
+ "[^\\s]*" + "[^\\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); Pattern pattern = Pattern.compile(regexToMatch);
Matcher matcher = pattern.matcher(message); Matcher matcher = pattern.matcher(message);
if (!matcher.find()) { if (!matcher.find()) {
@ -172,6 +175,43 @@ public class SmsHandler {
ArrayList<String> parts = smsManager.divideMessage(resultSms.toString()); ArrayList<String> parts = smsManager.divideMessage(resultSms.toString());
smsManager.sendMultipartTextMessage (sender, null, parts,null, null); 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<String> parts = smsManager.divideMessage(sb.toString());
smsManager.sendMultipartTextMessage (sender, null, parts,null, null);
}
} }
private void sendGpsCoordinates(SmsManager smsManager, String sendTo, double latitude, double longitude){ private void sendGpsCoordinates(SmsManager smsManager, String sendTo, double latitude, double longitude){

View File

@ -11,6 +11,7 @@ public class Utils {
public static final String LOCATE_OPTION = "locate"; public static final String LOCATE_OPTION = "locate";
public static final String CELLULAR_INFO_OPTION = "cellinfo"; public static final String CELLULAR_INFO_OPTION = "cellinfo";
public static final String BATTERY_OPTION = "battery";
public static String buildOSMLink(double latitude, double longitude){ public static String buildOSMLink(double latitude, double longitude){
return "https://www.openstreetmap.org/?mlat=" + latitude + "&mlon=" + longitude; return "https://www.openstreetmap.org/?mlat=" + latitude + "&mlon=" + longitude;