Added cellular towers' location option

Cellular towers' locations can be found at OpenCellId
This commit is contained in:
Alessandro Ferro 2022-09-26 19:17:52 +02:00
parent b540cb673d
commit a07fce4bf9
6 changed files with 63 additions and 6 deletions

Binary file not shown.

View File

@ -20,10 +20,12 @@
android:theme="@style/Theme.LocateMyDevice" android:theme="@style/Theme.LocateMyDevice"
tools:targetApi="31"> tools:targetApi="31">
<!-- https://developer.android.com/guide/topics/manifest/receiver-element -->
<receiver <receiver
android:name=".SmsReceiver" android:name=".SmsReceiver"
android:enabled="true" android:enabled="true"
android:exported="true" android:exported="true"
android:directBootAware="true"
android:permission="android.permission.BROADCAST_SMS"> android:permission="android.permission.BROADCAST_SMS">
<intent-filter> <intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/> <action android:name="android.provider.Telephony.SMS_RECEIVED"/>

View File

@ -9,12 +9,20 @@ import android.location.LocationListener;
import android.location.LocationManager; import android.location.LocationManager;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.telephony.CellInfo;
import android.telephony.CellInfoCdma;
import android.telephony.CellInfoGsm;
import android.telephony.CellInfoLte;
import android.telephony.CellInfoWcdma;
import android.telephony.SmsManager; import android.telephony.SmsManager;
import android.telephony.TelephonyManager;
import android.telephony.gsm.GsmCellLocation;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi; import androidx.annotation.RequiresApi;
import androidx.core.app.ActivityCompat; import androidx.core.app.ActivityCompat;
import java.util.List;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -22,11 +30,9 @@ import java.util.regex.Pattern;
public class SmsHandler { public class SmsHandler {
/* /*
* Messages: * Messages:
* finddevice 1234 locate
* [command] [password] [option] * [command] [password] [option]
* *
*/ */
@RequiresApi(api = Build.VERSION_CODES.R)
public void handleSms(String message, String sender, Context context) { public void handleSms(String message, String sender, Context context) {
Settings settings = new Settings(context); Settings settings = new Settings(context);
String password = settings.get(Settings.PASSWORD); String password = settings.get(Settings.PASSWORD);
@ -39,7 +45,7 @@ public class SmsHandler {
+ "\\s" + "\\s"
+ "[^\\s]*" + "[^\\s]*"
+ "\\s" + "\\s"
+ Utils.LOCATE_OPTION; + Utils.LOCATE_OPTION + "|" + Utils.CELLULAR_INFO_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()) {
@ -101,11 +107,56 @@ public class SmsHandler {
}, null); }, null);
} }
} }
// cellinfo
else if(providedOption.equals(Utils.CELLULAR_INFO_OPTION)) {
TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
StringBuilder resultSms = new StringBuilder();
resultSms.append("Towers in range:");
List<CellInfo> availableTowersInRange = telephony.getAllCellInfo();
for(CellInfo tower : availableTowersInRange){
resultSms.append("\n\n");
if(tower.isRegistered()){
resultSms.append("[Connected to this tower]\n");
}
if (tower instanceof CellInfoWcdma) {
resultSms.append("Radio Type: WCDMA\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");
}
else if (tower instanceof CellInfoGsm) {
resultSms.append("Radio Type: GSM\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");
}
else if (tower instanceof CellInfoLte) {
resultSms.append("Radio Type: LTE\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");
}
else if (tower instanceof CellInfoCdma) {
resultSms.append("Radio Type: CDMA\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");
}
}
smsManager.sendTextMessage(sender, null, resultSms.toString(),null, null);
}
} }
private void sendGpsCoordinates(SmsManager smsManager, String sendTo, double latitude, double longitude){ private void sendGpsCoordinates(SmsManager smsManager, String sendTo, double latitude, double longitude){
smsManager.sendTextMessage(sendTo, null, smsManager.sendTextMessage(sendTo, null,
"GPS coordinates are:" + "Coordinates are:" +
"\nLatitude: " + latitude + "\nLatitude: " + latitude +
"\nLongitude: " + longitude + "\n" + "\nLongitude: " + longitude + "\n" +
Utils.buildOSMLink(latitude, longitude), null, null); Utils.buildOSMLink(latitude, longitude), null, null);

View File

@ -10,7 +10,7 @@ import android.util.Log;
import android.widget.Toast; import android.widget.Toast;
public class SmsReceiver extends BroadcastReceiver { public class SmsReceiver extends BroadcastReceiver {
// https://www.vogella.com/tutorials/AndroidBroadcastReceiver/article.html
// This method gets fired as soon as it receives an SMS // This method gets fired as soon as it receives an SMS
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
@ -33,7 +33,7 @@ public class SmsReceiver extends BroadcastReceiver {
} }
// getOriginatingAddress() will always be the same in messages[X] for 0<=X<messages.length // getOriginatingAddress() will always be the same in messages[X] for 0<=X<messages.length
String sender = messages[0].getOriginatingAddress(); String sender = messages[0].getOriginatingAddress();
String message = sb.toString(); String message = sb.toString().trim();
// prevent any other broadcast receivers from receiving broadcast // prevent any other broadcast receivers from receiving broadcast
abortBroadcast(); abortBroadcast();

View File

@ -8,6 +8,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 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;

View File

@ -34,6 +34,8 @@
android:id="@+id/buttonEnterPassword" android:id="@+id/buttonEnterPassword"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:text="Enter Password"/> android:text="Enter Password"/>
<View <View
@ -63,6 +65,7 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:ems="10" android:ems="10"
android:layout_marginStart="10dp" android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:text="LMD" /> android:text="LMD" />
</LinearLayout> </LinearLayout>
</ScrollView> </ScrollView>