From 78eecc34df8206a8c3636ffa1e7f65dbfa04d988 Mon Sep 17 00:00:00 2001
From: Alessandro Ferro <49845537+xfarrow@users.noreply.github.com>
Date: Thu, 29 Sep 2022 16:54:11 +0200
Subject: [PATCH] code quality + created 'callme' option
---
src/app/build.gradle | 2 +-
src/app/src/main/AndroidManifest.xml | 2 +
.../xfarrow/locatemydevice/MainActivity.java | 63 ++++---
.../xfarrow/locatemydevice/SmsHandler.java | 157 +++++++++---------
.../com/xfarrow/locatemydevice/Utils.java | 23 ++-
5 files changed, 147 insertions(+), 100 deletions(-)
diff --git a/src/app/build.gradle b/src/app/build.gradle
index 9fc1f12..34b0d2a 100644
--- a/src/app/build.gradle
+++ b/src/app/build.gradle
@@ -10,7 +10,7 @@ android {
minSdk 28
targetSdk 32
versionCode 1
- versionName "1.0"
+ versionName "0.1"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
diff --git a/src/app/src/main/AndroidManifest.xml b/src/app/src/main/AndroidManifest.xml
index 45bc7a3..8880307 100644
--- a/src/app/src/main/AndroidManifest.xml
+++ b/src/app/src/main/AndroidManifest.xml
@@ -8,6 +8,8 @@
+
+
parts = smsManager.divideMessage(response);
- smsManager.sendMultipartTextMessage (sender, null, parts,null, null);
+ Utils.sendSms(smsManager, response, sender);
return;
}
// Location permission not granted
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
String response ="Location permission is not granted. Unable to serve request.";
- ArrayList parts = smsManager.divideMessage(response);
- smsManager.sendMultipartTextMessage (sender, null, parts,null, null);
+ Utils.sendSms(smsManager, response, sender);
return;
}
@@ -94,7 +94,8 @@ public class SmsHandler {
locationManager.getCurrentLocation(LocationManager.FUSED_PROVIDER, null, context.getMainExecutor(), new Consumer() {
@Override
public void accept(Location location) {
- sendGpsCoordinates(smsManager, sender, location.getLatitude(), location.getLongitude());
+ String response = Utils.buildCoordinatesResponse(location.getLatitude(), location.getLongitude());
+ Utils.sendSms(smsManager, response , sender);
}
});
}
@@ -106,7 +107,8 @@ public class SmsHandler {
locationManager.requestSingleUpdate(locationCriteria, new LocationListener() {
@Override
public void onLocationChanged(@NonNull Location location) {
- sendGpsCoordinates(smsManager, sender, location.getLatitude(), location.getLongitude());
+ String response = Utils.buildCoordinatesResponse(location.getLatitude(), location.getLongitude());
+ Utils.sendSms(smsManager, response, sender);
}
}, null);
}
@@ -117,111 +119,118 @@ public class SmsHandler {
TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
- StringBuilder resultSms = new StringBuilder();
+ StringBuilder responseSms = new StringBuilder();
- resultSms.append("Network country: ");
+ responseSms.append("Network country: ");
String country = telephony.getNetworkCountryIso();
- resultSms.append(Utils.getCountryNameByIso(country)).append("\n\n");
+ responseSms.append(Utils.getCountryNameByIso(country)).append("\n\n");
List availableTowersInRange = telephony.getAllCellInfo();
- resultSms.append("Towers in range: ");
+ responseSms.append("Towers in range: ");
if(availableTowersInRange.size() == 0) {
- resultSms.append("none or location is off.");
+ responseSms.append("none or location is off.");
}
for(CellInfo tower : availableTowersInRange){
- resultSms.append("\n\n");
+ responseSms.append("\n\n");
if(tower.isRegistered()){
- resultSms.append("[Connected to this tower]\n");
+ responseSms.append("[Connected to this tower]\n");
}
if (tower instanceof CellInfoWcdma) {
- resultSms.append("Radio Type: WCDMA\n");
- resultSms.append("Strength: ");
- resultSms.append(((CellInfoWcdma)tower).getCellSignalStrength().getLevel()).append("/4\n");
- resultSms.append("CID: ").append(((CellInfoWcdma) tower).getCellIdentity().getCid()).append("\n");
- resultSms.append("LAC: ").append(((CellInfoWcdma) tower).getCellIdentity().getLac()).append("\n");
- resultSms.append("MCC: ").append(((CellInfoWcdma) tower).getCellIdentity().getMccString()).append("\n");
- resultSms.append("MNC: ").append(((CellInfoWcdma) tower).getCellIdentity().getMncString()).append("\n");
+ responseSms.append("Radio Type: WCDMA\n");
+ responseSms.append("Strength: ");
+ responseSms.append(((CellInfoWcdma)tower).getCellSignalStrength().getLevel()).append("/4\n");
+ responseSms.append("CID: ").append(((CellInfoWcdma) tower).getCellIdentity().getCid()).append("\n");
+ responseSms.append("LAC: ").append(((CellInfoWcdma) tower).getCellIdentity().getLac()).append("\n");
+ responseSms.append("MCC: ").append(((CellInfoWcdma) tower).getCellIdentity().getMccString()).append("\n");
+ responseSms.append("MNC: ").append(((CellInfoWcdma) tower).getCellIdentity().getMncString()).append("\n");
}
else if (tower instanceof CellInfoGsm) {
- resultSms.append("Radio Type: GSM\n");
- resultSms.append("Strength: ");
- resultSms.append(((CellInfoGsm)tower).getCellSignalStrength().getLevel()).append("/4\n");
- resultSms.append("CID: ").append(((CellInfoGsm) tower).getCellIdentity().getCid()).append("\n");
- resultSms.append("LAC: ").append(((CellInfoGsm) tower).getCellIdentity().getLac()).append("\n");
- resultSms.append("MCC: ").append(((CellInfoGsm) tower).getCellIdentity().getMccString()).append("\n");
- resultSms.append("MNC: ").append(((CellInfoGsm) tower).getCellIdentity().getMncString()).append("\n");
+ responseSms.append("Radio Type: GSM\n");
+ responseSms.append("Strength: ");
+ responseSms.append(((CellInfoGsm)tower).getCellSignalStrength().getLevel()).append("/4\n");
+ responseSms.append("CID: ").append(((CellInfoGsm) tower).getCellIdentity().getCid()).append("\n");
+ responseSms.append("LAC: ").append(((CellInfoGsm) tower).getCellIdentity().getLac()).append("\n");
+ responseSms.append("MCC: ").append(((CellInfoGsm) tower).getCellIdentity().getMccString()).append("\n");
+ responseSms.append("MNC: ").append(((CellInfoGsm) tower).getCellIdentity().getMncString()).append("\n");
}
else if (tower instanceof CellInfoLte) {
- resultSms.append("Radio Type: LTE\n");
- resultSms.append("Strength: ");
- resultSms.append(((CellInfoLte)tower).getCellSignalStrength().getLevel()).append("/4\n");
- resultSms.append("CI: ").append(((CellInfoLte) tower).getCellIdentity().getCi()).append("\n");
- resultSms.append("TAC: ").append(((CellInfoLte) tower).getCellIdentity().getTac()).append("\n");
- resultSms.append("MCC: ").append(((CellInfoLte) tower).getCellIdentity().getMccString()).append("\n");
- resultSms.append("MNC: ").append(((CellInfoLte) tower).getCellIdentity().getMncString()).append("\n");
+ responseSms.append("Radio Type: LTE\n");
+ responseSms.append("Strength: ");
+ responseSms.append(((CellInfoLte)tower).getCellSignalStrength().getLevel()).append("/4\n");
+ responseSms.append("CI: ").append(((CellInfoLte) tower).getCellIdentity().getCi()).append("\n");
+ responseSms.append("TAC: ").append(((CellInfoLte) tower).getCellIdentity().getTac()).append("\n");
+ responseSms.append("MCC: ").append(((CellInfoLte) tower).getCellIdentity().getMccString()).append("\n");
+ responseSms.append("MNC: ").append(((CellInfoLte) tower).getCellIdentity().getMncString()).append("\n");
}
else if (tower instanceof CellInfoCdma) {
- resultSms.append("Radio Type: CDMA\n");
- resultSms.append("Strength: ");
- resultSms.append(((CellInfoCdma)tower).getCellSignalStrength().getLevel()).append("/4\n");
- resultSms.append("Latitude: ").append(((CellInfoCdma) tower).getCellIdentity().getLatitude()).append("\n");
- resultSms.append("Longitude: ").append(((CellInfoCdma) tower).getCellIdentity().getLongitude()).append("\n");
- resultSms.append("Network ID: ").append(((CellInfoCdma) tower).getCellIdentity().getNetworkId()).append("\n");
- resultSms.append("System ID: ").append(((CellInfoCdma) tower).getCellIdentity().getSystemId()).append("\n");
+ responseSms.append("Radio Type: CDMA\n");
+ responseSms.append("Strength: ");
+ responseSms.append(((CellInfoCdma)tower).getCellSignalStrength().getLevel()).append("/4\n");
+ responseSms.append("Latitude: ").append(((CellInfoCdma) tower).getCellIdentity().getLatitude()).append("\n");
+ responseSms.append("Longitude: ").append(((CellInfoCdma) tower).getCellIdentity().getLongitude()).append("\n");
+ responseSms.append("Network ID: ").append(((CellInfoCdma) tower).getCellIdentity().getNetworkId()).append("\n");
+ responseSms.append("System ID: ").append(((CellInfoCdma) tower).getCellIdentity().getSystemId()).append("\n");
}
}
- ArrayList parts = smsManager.divideMessage(resultSms.toString());
- smsManager.sendMultipartTextMessage (sender, null, parts,null, null);
+ Utils.sendSms(smsManager, responseSms.toString(), sender);
}
+ // battery
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();
+ StringBuilder responseSms = 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");
+ responseSms.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");
+ responseSms.append("Charging: ");
+ if(isCharging) responseSms.append("Yes\n");
+ else responseSms.append("No");
// How are we charging?
if(isCharging) {
int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
- sb.append("Charging through: ");
+ responseSms.append("Charging through: ");
if(chargePlug == BatteryManager.BATTERY_PLUGGED_USB)
- sb.append("USB");
+ responseSms.append("USB");
else if(chargePlug == BatteryManager.BATTERY_PLUGGED_AC)
- sb.append("AC (wall)");
+ responseSms.append("AC (wall)");
else if( chargePlug == BatteryManager.BATTERY_PLUGGED_WIRELESS)
- sb.append("Wireless");
+ responseSms.append("Wireless");
else
- sb.append("Unknown");
+ responseSms.append("Unknown");
}
- ArrayList parts = smsManager.divideMessage(sb.toString());
- smsManager.sendMultipartTextMessage (sender, null, parts,null, null);
+ Utils.sendSms(smsManager, responseSms.toString(), sender);
+ }
+
+ //callme
+ // Permission restrictions: https://developer.android.com/guide/components/activities/background-starts
+ // So we ask to grant the SYSTEM_ALERT_WINDOW permission
+ else if(providedOption.equals(Utils.CALL_ME_OPTION)){
+ // if canDrawOverlays() returns false, not necessarily it will be impossible to
+ // launch a call.
+ if(!android.provider.Settings.canDrawOverlays(context)){
+ Utils.sendSms(smsManager, "\"Display over other app\" permission was not " +
+ "granted, thus it might be impossible to initiate a call.", sender);
+ }
+ Intent intent = new Intent(Intent.ACTION_CALL);
+ intent.setData(Uri.parse("tel:" + sender));
+ intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+ context.startActivity(intent);
+
+ DevicePolicyManager manager = ((DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE));
+ manager.lockNow();
}
}
-
- private void sendGpsCoordinates(SmsManager smsManager, String sendTo, double latitude, double longitude){
-
- String response = "Coordinates are:" +
- "\nLatitude: " + latitude +
- "\nLongitude: " + longitude + "\n" +
- Utils.buildOSMLink(latitude, longitude);
-
- ArrayList parts = smsManager.divideMessage(response);
- smsManager.sendMultipartTextMessage (sendTo, null, parts,null, null);
- }
}
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 dea8f6f..e3e5987 100644
--- a/src/app/src/main/java/com/xfarrow/locatemydevice/Utils.java
+++ b/src/app/src/main/java/com/xfarrow/locatemydevice/Utils.java
@@ -1,5 +1,8 @@
package com.xfarrow.locatemydevice;
+import android.telephony.SmsManager;
+
+import java.util.ArrayList;
import java.util.Arrays;
import java.util.Locale;
@@ -12,14 +15,26 @@ 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;
- }
+ public static final String CALL_ME_OPTION = "callme";
public static String getCountryNameByIso(String iso){
Locale locale = new Locale("", iso);
return locale.getDisplayCountry();
}
+ public static void sendSms(SmsManager smsManager, String text, String sendTo){
+ ArrayList parts = smsManager.divideMessage(text);
+ smsManager.sendMultipartTextMessage (sendTo, null, parts,null, null);
+ }
+
+ public static String buildCoordinatesResponse(double latitude, double longitude){
+ return new StringBuilder().append("Coordinates are:")
+ .append("\nLatitude: ")
+ .append(latitude)
+ .append("\nLongitude: ")
+ .append(longitude)
+ .append("\n")
+ .append("https://www.openstreetmap.org/?mlat=").append(latitude).append("&mlon=").append(longitude).toString();
+ }
+
}