code quality + created 'callme' option

This commit is contained in:
Alessandro Ferro 2022-09-29 16:54:11 +02:00
parent 54121b4b7d
commit 78eecc34df
5 changed files with 147 additions and 100 deletions

View File

@ -10,7 +10,7 @@ android {
minSdk 28 minSdk 28
targetSdk 32 targetSdk 32
versionCode 1 versionCode 1
versionName "1.0" versionName "0.1"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
} }

View File

@ -8,6 +8,8 @@
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<application <application
android:allowBackup="true" android:allowBackup="true"

View File

@ -1,22 +1,18 @@
package com.xfarrow.locatemydevice; package com.xfarrow.locatemydevice;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat; import androidx.core.app.ActivityCompat;
import android.Manifest; import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity { public class MainActivity extends AppCompatActivity {
@ -28,20 +24,19 @@ public class MainActivity extends AppCompatActivity {
} }
private void checkPermissions(){ private void checkPermissions(){
if (ActivityCompat.checkSelfPermission( if (ActivityCompat.checkSelfPermission(this, Manifest.permission.RECEIVE_SMS) != PackageManager.PERMISSION_GRANTED
this, ||
Manifest.permission.RECEIVE_SMS ActivityCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED
) != PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission( ||
this, ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED
Manifest.permission.SEND_SMS ||
) != PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission( ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
this, ||
Manifest.permission.ACCESS_COARSE_LOCATION ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED
) != PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission( ||
this, ActivityCompat.checkSelfPermission(this, Manifest.permission.SYSTEM_ALERT_WINDOW) != PackageManager.PERMISSION_GRANTED
Manifest.permission.ACCESS_FINE_LOCATION )
) != PackageManager.PERMISSION_GRANTED {
) {
ActivityCompat.requestPermissions( ActivityCompat.requestPermissions(
this, this,
@ -49,11 +44,15 @@ public class MainActivity extends AppCompatActivity {
Manifest.permission.RECEIVE_SMS, Manifest.permission.RECEIVE_SMS,
Manifest.permission.SEND_SMS, Manifest.permission.SEND_SMS,
Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.CALL_PHONE
}, },
Utils.PERMISSION_MULTIPLE Utils.PERMISSION_MULTIPLE
); );
} }
if(!android.provider.Settings.canDrawOverlays(this)) {
displayDrawOverlayPermissionDialog();
}
} }
@Override @Override
@ -88,4 +87,26 @@ public class MainActivity extends AppCompatActivity {
} }
return super.onOptionsItemSelected(item); return super.onOptionsItemSelected(item);
} }
public void displayDrawOverlayPermissionDialog(){
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Display over other apps");
alert.setMessage("Please, enable 'Display over other apps' permission. " +
"It is required for using the option 'callme'");
alert.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
MainActivity.this.startActivity(intent);
}
});
alert.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
});
alert.show();
}
} }

View File

@ -1,6 +1,7 @@
package com.xfarrow.locatemydevice; package com.xfarrow.locatemydevice;
import android.Manifest; import android.Manifest;
import android.app.admin.DevicePolicyManager;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
@ -9,9 +10,9 @@ 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.net.Uri;
import android.os.BatteryManager; import android.os.BatteryManager;
import android.os.Build; import android.os.Build;
import android.os.Bundle;
import android.telephony.CellInfo; import android.telephony.CellInfo;
import android.telephony.CellInfoCdma; import android.telephony.CellInfoCdma;
import android.telephony.CellInfoGsm; import android.telephony.CellInfoGsm;
@ -19,14 +20,8 @@ import android.telephony.CellInfoLte;
import android.telephony.CellInfoWcdma; import android.telephony.CellInfoWcdma;
import android.telephony.SmsManager; import android.telephony.SmsManager;
import android.telephony.TelephonyManager; import android.telephony.TelephonyManager;
import android.telephony.gsm.GsmCellLocation;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.core.app.ActivityCompat; import androidx.core.app.ActivityCompat;
import java.security.cert.CertPathValidatorException;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.regex.Matcher; import java.util.regex.Matcher;
@ -50,7 +45,14 @@ public class SmsHandler {
+ "\\s" + "\\s"
+ "[^\\s]*" + "[^\\s]*"
+ "\\s" + "\\s"
+ Utils.LOCATE_OPTION + "|" + Utils.CELLULAR_INFO_OPTION + "|" + Utils.BATTERY_OPTION; + Utils.LOCATE_OPTION
+ "|"
+ Utils.CELLULAR_INFO_OPTION
+ "|"
+ Utils.BATTERY_OPTION
+ "|"
+ Utils.CALL_ME_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()) {
@ -76,16 +78,14 @@ public class SmsHandler {
if(!locationManager.isLocationEnabled()){ if(!locationManager.isLocationEnabled()){
// TODO: get last known location (requies google play services) // TODO: get last known location (requies google play services)
String response ="Location is not enabled. Unable to serve request."; String response ="Location is not enabled. Unable to serve request.";
ArrayList<String> parts = smsManager.divideMessage(response); Utils.sendSms(smsManager, response, sender);
smsManager.sendMultipartTextMessage (sender, null, parts,null, null);
return; return;
} }
// Location permission not granted // 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) { 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."; String response ="Location permission is not granted. Unable to serve request.";
ArrayList<String> parts = smsManager.divideMessage(response); Utils.sendSms(smsManager, response, sender);
smsManager.sendMultipartTextMessage (sender, null, parts,null, null);
return; return;
} }
@ -94,7 +94,8 @@ public class SmsHandler {
locationManager.getCurrentLocation(LocationManager.FUSED_PROVIDER, null, context.getMainExecutor(), new Consumer<Location>() { locationManager.getCurrentLocation(LocationManager.FUSED_PROVIDER, null, context.getMainExecutor(), new Consumer<Location>() {
@Override @Override
public void accept(Location location) { 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() { locationManager.requestSingleUpdate(locationCriteria, new LocationListener() {
@Override @Override
public void onLocationChanged(@NonNull Location location) { 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); }, null);
} }
@ -117,111 +119,118 @@ public class SmsHandler {
TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); 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(); String country = telephony.getNetworkCountryIso();
resultSms.append(Utils.getCountryNameByIso(country)).append("\n\n"); responseSms.append(Utils.getCountryNameByIso(country)).append("\n\n");
List<CellInfo> availableTowersInRange = telephony.getAllCellInfo(); List<CellInfo> availableTowersInRange = telephony.getAllCellInfo();
resultSms.append("Towers in range: "); responseSms.append("Towers in range: ");
if(availableTowersInRange.size() == 0) { if(availableTowersInRange.size() == 0) {
resultSms.append("none or location is off."); responseSms.append("none or location is off.");
} }
for(CellInfo tower : availableTowersInRange){ for(CellInfo tower : availableTowersInRange){
resultSms.append("\n\n"); responseSms.append("\n\n");
if(tower.isRegistered()){ if(tower.isRegistered()){
resultSms.append("[Connected to this tower]\n"); responseSms.append("[Connected to this tower]\n");
} }
if (tower instanceof CellInfoWcdma) { if (tower instanceof CellInfoWcdma) {
resultSms.append("Radio Type: WCDMA\n"); responseSms.append("Radio Type: WCDMA\n");
resultSms.append("Strength: "); responseSms.append("Strength: ");
resultSms.append(((CellInfoWcdma)tower).getCellSignalStrength().getLevel()).append("/4\n"); responseSms.append(((CellInfoWcdma)tower).getCellSignalStrength().getLevel()).append("/4\n");
resultSms.append("CID: ").append(((CellInfoWcdma) tower).getCellIdentity().getCid()).append("\n"); responseSms.append("CID: ").append(((CellInfoWcdma) tower).getCellIdentity().getCid()).append("\n");
resultSms.append("LAC: ").append(((CellInfoWcdma) tower).getCellIdentity().getLac()).append("\n"); responseSms.append("LAC: ").append(((CellInfoWcdma) tower).getCellIdentity().getLac()).append("\n");
resultSms.append("MCC: ").append(((CellInfoWcdma) tower).getCellIdentity().getMccString()).append("\n"); responseSms.append("MCC: ").append(((CellInfoWcdma) tower).getCellIdentity().getMccString()).append("\n");
resultSms.append("MNC: ").append(((CellInfoWcdma) tower).getCellIdentity().getMncString()).append("\n"); responseSms.append("MNC: ").append(((CellInfoWcdma) tower).getCellIdentity().getMncString()).append("\n");
} }
else if (tower instanceof CellInfoGsm) { else if (tower instanceof CellInfoGsm) {
resultSms.append("Radio Type: GSM\n"); responseSms.append("Radio Type: GSM\n");
resultSms.append("Strength: "); responseSms.append("Strength: ");
resultSms.append(((CellInfoGsm)tower).getCellSignalStrength().getLevel()).append("/4\n"); responseSms.append(((CellInfoGsm)tower).getCellSignalStrength().getLevel()).append("/4\n");
resultSms.append("CID: ").append(((CellInfoGsm) tower).getCellIdentity().getCid()).append("\n"); responseSms.append("CID: ").append(((CellInfoGsm) tower).getCellIdentity().getCid()).append("\n");
resultSms.append("LAC: ").append(((CellInfoGsm) tower).getCellIdentity().getLac()).append("\n"); responseSms.append("LAC: ").append(((CellInfoGsm) tower).getCellIdentity().getLac()).append("\n");
resultSms.append("MCC: ").append(((CellInfoGsm) tower).getCellIdentity().getMccString()).append("\n"); responseSms.append("MCC: ").append(((CellInfoGsm) tower).getCellIdentity().getMccString()).append("\n");
resultSms.append("MNC: ").append(((CellInfoGsm) tower).getCellIdentity().getMncString()).append("\n"); responseSms.append("MNC: ").append(((CellInfoGsm) tower).getCellIdentity().getMncString()).append("\n");
} }
else if (tower instanceof CellInfoLte) { else if (tower instanceof CellInfoLte) {
resultSms.append("Radio Type: LTE\n"); responseSms.append("Radio Type: LTE\n");
resultSms.append("Strength: "); responseSms.append("Strength: ");
resultSms.append(((CellInfoLte)tower).getCellSignalStrength().getLevel()).append("/4\n"); responseSms.append(((CellInfoLte)tower).getCellSignalStrength().getLevel()).append("/4\n");
resultSms.append("CI: ").append(((CellInfoLte) tower).getCellIdentity().getCi()).append("\n"); responseSms.append("CI: ").append(((CellInfoLte) tower).getCellIdentity().getCi()).append("\n");
resultSms.append("TAC: ").append(((CellInfoLte) tower).getCellIdentity().getTac()).append("\n"); responseSms.append("TAC: ").append(((CellInfoLte) tower).getCellIdentity().getTac()).append("\n");
resultSms.append("MCC: ").append(((CellInfoLte) tower).getCellIdentity().getMccString()).append("\n"); responseSms.append("MCC: ").append(((CellInfoLte) tower).getCellIdentity().getMccString()).append("\n");
resultSms.append("MNC: ").append(((CellInfoLte) tower).getCellIdentity().getMncString()).append("\n"); responseSms.append("MNC: ").append(((CellInfoLte) tower).getCellIdentity().getMncString()).append("\n");
} }
else if (tower instanceof CellInfoCdma) { else if (tower instanceof CellInfoCdma) {
resultSms.append("Radio Type: CDMA\n"); responseSms.append("Radio Type: CDMA\n");
resultSms.append("Strength: "); responseSms.append("Strength: ");
resultSms.append(((CellInfoCdma)tower).getCellSignalStrength().getLevel()).append("/4\n"); responseSms.append(((CellInfoCdma)tower).getCellSignalStrength().getLevel()).append("/4\n");
resultSms.append("Latitude: ").append(((CellInfoCdma) tower).getCellIdentity().getLatitude()).append("\n"); responseSms.append("Latitude: ").append(((CellInfoCdma) tower).getCellIdentity().getLatitude()).append("\n");
resultSms.append("Longitude: ").append(((CellInfoCdma) tower).getCellIdentity().getLongitude()).append("\n"); responseSms.append("Longitude: ").append(((CellInfoCdma) tower).getCellIdentity().getLongitude()).append("\n");
resultSms.append("Network ID: ").append(((CellInfoCdma) tower).getCellIdentity().getNetworkId()).append("\n"); responseSms.append("Network ID: ").append(((CellInfoCdma) tower).getCellIdentity().getNetworkId()).append("\n");
resultSms.append("System ID: ").append(((CellInfoCdma) tower).getCellIdentity().getSystemId()).append("\n"); responseSms.append("System ID: ").append(((CellInfoCdma) tower).getCellIdentity().getSystemId()).append("\n");
} }
} }
ArrayList<String> parts = smsManager.divideMessage(resultSms.toString()); Utils.sendSms(smsManager, responseSms.toString(), sender);
smsManager.sendMultipartTextMessage (sender, null, parts,null, null);
} }
// battery
else if(providedOption.equals(Utils.BATTERY_OPTION)){ else if(providedOption.equals(Utils.BATTERY_OPTION)){
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = context.registerReceiver(null, ifilter); Intent batteryStatus = context.registerReceiver(null, ifilter);
StringBuilder sb = new StringBuilder(); StringBuilder responseSms = new StringBuilder();
// Battery level // Battery level
int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1); int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
float batteryPct = level * 100 / (float)scale; 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? // Are we charging / charged?
int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1); int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING
|| status == BatteryManager.BATTERY_STATUS_FULL; || status == BatteryManager.BATTERY_STATUS_FULL;
sb.append("Charging: "); responseSms.append("Charging: ");
if(isCharging) sb.append("Yes\n"); if(isCharging) responseSms.append("Yes\n");
else sb.append("No"); else responseSms.append("No");
// How are we charging? // How are we charging?
if(isCharging) { if(isCharging) {
int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1); int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
sb.append("Charging through: "); responseSms.append("Charging through: ");
if(chargePlug == BatteryManager.BATTERY_PLUGGED_USB) if(chargePlug == BatteryManager.BATTERY_PLUGGED_USB)
sb.append("USB"); responseSms.append("USB");
else if(chargePlug == BatteryManager.BATTERY_PLUGGED_AC) else if(chargePlug == BatteryManager.BATTERY_PLUGGED_AC)
sb.append("AC (wall)"); responseSms.append("AC (wall)");
else if( chargePlug == BatteryManager.BATTERY_PLUGGED_WIRELESS) else if( chargePlug == BatteryManager.BATTERY_PLUGGED_WIRELESS)
sb.append("Wireless"); responseSms.append("Wireless");
else else
sb.append("Unknown"); responseSms.append("Unknown");
} }
ArrayList<String> parts = smsManager.divideMessage(sb.toString()); Utils.sendSms(smsManager, responseSms.toString(), sender);
smsManager.sendMultipartTextMessage (sender, null, parts,null, null); }
//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<String> parts = smsManager.divideMessage(response);
smsManager.sendMultipartTextMessage (sendTo, null, parts,null, null);
}
} }

View File

@ -1,5 +1,8 @@
package com.xfarrow.locatemydevice; package com.xfarrow.locatemydevice;
import android.telephony.SmsManager;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Locale; import java.util.Locale;
@ -12,14 +15,26 @@ 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 final String BATTERY_OPTION = "battery";
public static final String CALL_ME_OPTION = "callme";
public static String buildOSMLink(double latitude, double longitude){
return "https://www.openstreetmap.org/?mlat=" + latitude + "&mlon=" + longitude;
}
public static String getCountryNameByIso(String iso){ public static String getCountryNameByIso(String iso){
Locale locale = new Locale("", iso); Locale locale = new Locale("", iso);
return locale.getDisplayCountry(); return locale.getDisplayCountry();
} }
public static void sendSms(SmsManager smsManager, String text, String sendTo){
ArrayList<String> 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();
}
} }