improvements
This commit is contained in:
parent
7a615e279a
commit
b540cb673d
|
@ -9,6 +9,7 @@ import androidx.core.app.ActivityCompat;
|
||||||
|
|
||||||
import android.Manifest;
|
import android.Manifest;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.content.pm.PackageManager;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.view.Menu;
|
import android.view.Menu;
|
||||||
|
@ -17,33 +18,47 @@ import android.view.View;
|
||||||
import android.widget.Button;
|
import android.widget.Button;
|
||||||
|
|
||||||
public class MainActivity extends AppCompatActivity {
|
public class MainActivity extends AppCompatActivity {
|
||||||
private final int MY_PERMISSIONS_REQUEST_SMS_RECEIVE = 10;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
setContentView(R.layout.activity_main);
|
setContentView(R.layout.activity_main);
|
||||||
requirePermissions();
|
checkPermissions();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void requirePermissions(){
|
private void checkPermissions(){
|
||||||
|
if(ActivityCompat.checkSelfPermission(this, Manifest.permission.RECEIVE_SMS) != PackageManager.PERMISSION_GRANTED) {
|
||||||
|
|
||||||
ActivityCompat.requestPermissions(this,
|
ActivityCompat.requestPermissions(this,
|
||||||
new String[]{Manifest.permission.RECEIVE_SMS},
|
new String[]{Manifest.permission.RECEIVE_SMS},
|
||||||
MY_PERMISSIONS_REQUEST_SMS_RECEIVE); // Understand what this is
|
Utils.PERMISSION_SMS_RECEIVE);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(ActivityCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED) {
|
||||||
|
|
||||||
ActivityCompat.requestPermissions(this,
|
ActivityCompat.requestPermissions(this,
|
||||||
new String[]{Manifest.permission.SEND_SMS},
|
new String[]{Manifest.permission.SEND_SMS},
|
||||||
200); // Understand what this is
|
Utils.PERMISSION_SMS_SEND);
|
||||||
|
}
|
||||||
|
if(ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
|
||||||
|
|
||||||
ActivityCompat.requestPermissions(this,
|
ActivityCompat.requestPermissions(this,
|
||||||
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
|
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
|
||||||
1);
|
Utils.PERMISSION_ACCESS_FINE_LOCATION);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
||||||
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
||||||
if (requestCode == MY_PERMISSIONS_REQUEST_SMS_RECEIVE) {
|
if (requestCode == Utils.PERMISSION_SMS_RECEIVE) {
|
||||||
|
|
||||||
|
}
|
||||||
|
else if(requestCode == Utils.PERMISSION_SMS_SEND){
|
||||||
|
|
||||||
|
}
|
||||||
|
else if(requestCode == Utils.PERMISSION_ACCESS_FINE_LOCATION){
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -57,57 +57,57 @@ public class SmsHandler {
|
||||||
SmsManager smsManager = SmsManager.getDefault();
|
SmsManager smsManager = SmsManager.getDefault();
|
||||||
|
|
||||||
// locate
|
// locate
|
||||||
if (providedOption.equals(Settings.LOCATE_OPTION)) {
|
if (providedOption.equals(Utils.LOCATE_OPTION)) {
|
||||||
|
|
||||||
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
|
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
|
||||||
|
|
||||||
// GPS is off
|
if(!locationManager.isLocationEnabled()){
|
||||||
if (!locationManager.isLocationEnabled()) {
|
// TODO: get last known location (requies google play services)
|
||||||
smsManager.sendTextMessage(sender, null, "GPS is off ", null, null);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// GPS 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) {
|
|
||||||
smsManager.sendTextMessage(sender, null,
|
smsManager.sendTextMessage(sender, null,
|
||||||
"GPS permission is not granted. " +
|
"Location is not enabled. " +
|
||||||
"Unable to serve request.",null, null);
|
"Unable to serve request.",null, null);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// API 30 and above
|
// Location permission not granted
|
||||||
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
|
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
|
||||||
locationManager.getCurrentLocation(LocationManager.GPS_PROVIDER, null, context.getMainExecutor(), new Consumer<Location>() {
|
ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
|
||||||
|
|
||||||
|
smsManager.sendTextMessage(sender, null,
|
||||||
|
"Location permission is not granted. " +
|
||||||
|
"Unable to serve request.",null, null);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// API 31 and above
|
||||||
|
if(android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.S){
|
||||||
|
locationManager.getCurrentLocation(LocationManager.FUSED_PROVIDER, null, context.getMainExecutor(), new Consumer<Location>() {
|
||||||
@Override
|
@Override
|
||||||
public void accept(Location location) {
|
public void accept(Location location) {
|
||||||
double latitude = location.getLatitude();
|
sendGpsCoordinates(smsManager, sender, location.getLatitude(), location.getLongitude());
|
||||||
double longitude = location.getLongitude();
|
|
||||||
smsManager.sendTextMessage(sender, null,
|
|
||||||
"GPS coordinates are: " +
|
|
||||||
"\nLatitude: " + latitude +
|
|
||||||
"\nLongitude: " + longitude + "\n" +
|
|
||||||
Utils.buildOSMLink(latitude, longitude), null, null);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Legacy (API < 29)
|
// Legacy (API < 31)
|
||||||
else{
|
else{
|
||||||
Criteria locationCriteria = new Criteria();
|
Criteria locationCriteria = new Criteria();
|
||||||
locationCriteria.setAccuracy(Criteria.ACCURACY_FINE);
|
locationCriteria.setAccuracy(Criteria.ACCURACY_FINE);
|
||||||
locationManager.requestSingleUpdate(locationCriteria, new LocationListener() {
|
locationManager.requestSingleUpdate(locationCriteria, new LocationListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onLocationChanged(@NonNull Location location) {
|
public void onLocationChanged(@NonNull Location location) {
|
||||||
double latitude = location.getLatitude();
|
sendGpsCoordinates(smsManager, sender, location.getLatitude(), location.getLongitude());
|
||||||
double longitude = location.getLongitude();
|
|
||||||
smsManager.sendTextMessage(sender, null,
|
|
||||||
"GPS coordinates are:" +
|
|
||||||
"\nLatitude: " + latitude +
|
|
||||||
"\nLongitude: " + longitude + "\n" +
|
|
||||||
Utils.buildOSMLink(latitude, longitude), null, null);
|
|
||||||
}
|
}
|
||||||
}, null);
|
}, null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void sendGpsCoordinates(SmsManager smsManager, String sendTo, double latitude, double longitude){
|
||||||
|
smsManager.sendTextMessage(sendTo, null,
|
||||||
|
"GPS coordinates are:" +
|
||||||
|
"\nLatitude: " + latitude +
|
||||||
|
"\nLongitude: " + longitude + "\n" +
|
||||||
|
Utils.buildOSMLink(latitude, longitude), null, null);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,16 @@
|
||||||
package com.xfarrow.locatemydevice;
|
package com.xfarrow.locatemydevice;
|
||||||
|
|
||||||
public class Utils {
|
public class Utils {
|
||||||
|
|
||||||
|
public static final int PERMISSION_SMS_RECEIVE = 100;
|
||||||
|
public static final int PERMISSION_SMS_SEND = 101;
|
||||||
|
public static final int PERMISSION_ACCESS_FINE_LOCATION = 102;
|
||||||
|
|
||||||
|
|
||||||
public static final String LOCATE_OPTION = "locate";
|
public static final String LOCATE_OPTION = "locate";
|
||||||
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue