diff --git a/src/app/src/main/java/com/xfarrow/locatemydevice/MainActivity.java b/src/app/src/main/java/com/xfarrow/locatemydevice/MainActivity.java index a87f36f..bd567ce 100644 --- a/src/app/src/main/java/com/xfarrow/locatemydevice/MainActivity.java +++ b/src/app/src/main/java/com/xfarrow/locatemydevice/MainActivity.java @@ -9,6 +9,7 @@ import androidx.core.app.ActivityCompat; import android.Manifest; import android.content.Intent; +import android.content.pm.PackageManager; import android.os.Bundle; import android.util.Log; import android.view.Menu; @@ -17,33 +18,47 @@ import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { - private final int MY_PERMISSIONS_REQUEST_SMS_RECEIVE = 10; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); - requirePermissions(); + checkPermissions(); } - private void requirePermissions(){ - ActivityCompat.requestPermissions(this, - new String[]{Manifest.permission.RECEIVE_SMS}, - MY_PERMISSIONS_REQUEST_SMS_RECEIVE); // Understand what this is + private void checkPermissions(){ + if(ActivityCompat.checkSelfPermission(this, Manifest.permission.RECEIVE_SMS) != PackageManager.PERMISSION_GRANTED) { - ActivityCompat.requestPermissions(this, - new String[]{Manifest.permission.SEND_SMS}, - 200); // Understand what this is + ActivityCompat.requestPermissions(this, + new String[]{Manifest.permission.RECEIVE_SMS}, + Utils.PERMISSION_SMS_RECEIVE); + } - ActivityCompat.requestPermissions(this, - new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, - 1); + if(ActivityCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED) { + + ActivityCompat.requestPermissions(this, + new String[]{Manifest.permission.SEND_SMS}, + Utils.PERMISSION_SMS_SEND); + } + if(ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { + + ActivityCompat.requestPermissions(this, + new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, + Utils.PERMISSION_ACCESS_FINE_LOCATION); + } } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] 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){ + } } diff --git a/src/app/src/main/java/com/xfarrow/locatemydevice/SmsHandler.java b/src/app/src/main/java/com/xfarrow/locatemydevice/SmsHandler.java index 68c32ed..9bf3d16 100644 --- a/src/app/src/main/java/com/xfarrow/locatemydevice/SmsHandler.java +++ b/src/app/src/main/java/com/xfarrow/locatemydevice/SmsHandler.java @@ -57,57 +57,57 @@ public class SmsHandler { SmsManager smsManager = SmsManager.getDefault(); // locate - if (providedOption.equals(Settings.LOCATE_OPTION)) { + if (providedOption.equals(Utils.LOCATE_OPTION)) { LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); - // GPS is off - if (!locationManager.isLocationEnabled()) { - smsManager.sendTextMessage(sender, null, "GPS is off ", null, null); + if(!locationManager.isLocationEnabled()){ + // TODO: get last known location (requies google play services) + smsManager.sendTextMessage(sender, null, + "Location is not enabled. " + + "Unable to serve request.",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, - "GPS permission is 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) { + + smsManager.sendTextMessage(sender, null, + "Location permission is not granted. " + "Unable to serve request.",null, null); return; } - // API 30 and above - if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { - locationManager.getCurrentLocation(LocationManager.GPS_PROVIDER, null, context.getMainExecutor(), new Consumer() { + // 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() { @Override public void accept(Location location) { - double latitude = location.getLatitude(); - double longitude = location.getLongitude(); - smsManager.sendTextMessage(sender, null, - "GPS coordinates are: " + - "\nLatitude: " + latitude + - "\nLongitude: " + longitude + "\n" + - Utils.buildOSMLink(latitude, longitude), null, null); + sendGpsCoordinates(smsManager, sender, location.getLatitude(), location.getLongitude()); } }); } - // Legacy (API < 29) + // Legacy (API < 31) else{ Criteria locationCriteria = new Criteria(); locationCriteria.setAccuracy(Criteria.ACCURACY_FINE); locationManager.requestSingleUpdate(locationCriteria, new LocationListener() { @Override public void onLocationChanged(@NonNull Location location) { - double latitude = location.getLatitude(); - double longitude = location.getLongitude(); - smsManager.sendTextMessage(sender, null, - "GPS coordinates are:" + - "\nLatitude: " + latitude + - "\nLongitude: " + longitude + "\n" + - Utils.buildOSMLink(latitude, longitude), null, null); + sendGpsCoordinates(smsManager, sender, location.getLatitude(), location.getLongitude()); } }, 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); + } } 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 dd463b8..35e7535 100644 --- a/src/app/src/main/java/com/xfarrow/locatemydevice/Utils.java +++ b/src/app/src/main/java/com/xfarrow/locatemydevice/Utils.java @@ -1,8 +1,16 @@ package com.xfarrow.locatemydevice; 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 String buildOSMLink(double latitude, double longitude){ return "https://www.openstreetmap.org/?mlat=" + latitude + "&mlon=" + longitude; } + }