diff --git a/src/.idea/misc.xml b/src/.idea/misc.xml index b286d63..64abe92 100644 --- a/src/.idea/misc.xml +++ b/src/.idea/misc.xml @@ -11,7 +11,7 @@ - + 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 033a5c3..2ad61a9 100644 --- a/src/app/src/main/java/com/xfarrow/locatemydevice/MainActivity.java +++ b/src/app/src/main/java/com/xfarrow/locatemydevice/MainActivity.java @@ -28,55 +28,47 @@ public class MainActivity extends AppCompatActivity { } private void checkPermissions(){ - if(ActivityCompat.checkSelfPermission(this, Manifest.permission.RECEIVE_SMS) != PackageManager.PERMISSION_GRANTED) { + if (ActivityCompat.checkSelfPermission( + this, + Manifest.permission.RECEIVE_SMS + ) != PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission( + this, + Manifest.permission.SEND_SMS + ) != PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission( + this, + Manifest.permission.ACCESS_COARSE_LOCATION + ) != PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission( + this, + Manifest.permission.ACCESS_FINE_LOCATION + ) != PackageManager.PERMISSION_GRANTED + ) { - ActivityCompat.requestPermissions(this, - new String[]{Manifest.permission.RECEIVE_SMS}, - Utils.PERMISSION_SMS_RECEIVE); + ActivityCompat.requestPermissions( + this, + new String[]{ + Manifest.permission.RECEIVE_SMS, + Manifest.permission.SEND_SMS, + Manifest.permission.ACCESS_COARSE_LOCATION, + Manifest.permission.ACCESS_FINE_LOCATION + }, + Utils.PERMISSION_MULTIPLE + ); } - - 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_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { - - ActivityCompat.requestPermissions(this, - new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, - Utils.PERMISSION_ACCESS_COARSE_LOCATION); - } - - 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); - } - - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { - if(ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_BACKGROUND_LOCATION) != PackageManager.PERMISSION_GRANTED) { - ActivityCompat.requestPermissions(this, - new String[]{Manifest.permission.ACCESS_BACKGROUND_LOCATION}, - Utils.PERMISSION_ACCESS_BACKGROUND_LOCATION); - } - } - } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); - if (requestCode == Utils.PERMISSION_SMS_RECEIVE) { - - } - else if(requestCode == Utils.PERMISSION_SMS_SEND){ - - } - else if(requestCode == Utils.PERMISSION_ACCESS_FINE_LOCATION){ + // Grant Background location if and only if FINE_LOCATION is already granted + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED + && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_BACKGROUND_LOCATION) != PackageManager.PERMISSION_GRANTED) + { + ActivityCompat.requestPermissions(this, + new String[]{Manifest.permission.ACCESS_BACKGROUND_LOCATION}, + Utils.PERMISSION_ACCESS_BACKGROUND_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 f4134b4..4a13d88 100644 --- a/src/app/src/main/java/com/xfarrow/locatemydevice/SmsHandler.java +++ b/src/app/src/main/java/com/xfarrow/locatemydevice/SmsHandler.java @@ -23,6 +23,7 @@ import androidx.annotation.RequiresApi; import androidx.core.app.ActivityCompat; import java.security.cert.CertPathValidatorException; +import java.util.ArrayList; import java.util.List; import java.util.function.Consumer; import java.util.regex.Matcher; @@ -68,21 +69,20 @@ public class SmsHandler { LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); + // location not enabled 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); + String response ="Location is not enabled. Unable to serve request."; + ArrayList parts = smsManager.divideMessage(response); + smsManager.sendMultipartTextMessage (sender, null, parts,null, null); 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) { - - smsManager.sendTextMessage(sender, null, - "Location permission is not granted. " + - "Unable to serve request.",null, null); + 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); return; } @@ -169,15 +169,19 @@ public class SmsHandler { resultSms.append("System ID: ").append(((CellInfoCdma) tower).getCellIdentity().getSystemId()).append("\n"); } } - smsManager.sendTextMessage(sender, null, resultSms.toString(),null, null); + ArrayList parts = smsManager.divideMessage(resultSms.toString()); + smsManager.sendMultipartTextMessage (sender, null, parts,null, null); } } private void sendGpsCoordinates(SmsManager smsManager, String sendTo, double latitude, double longitude){ - smsManager.sendTextMessage(sendTo, null, - "Coordinates are:" + - "\nLatitude: " + latitude + - "\nLongitude: " + longitude + "\n" + - Utils.buildOSMLink(latitude, longitude), null, null); + + 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 6967ac3..6d3ff5d 100644 --- a/src/app/src/main/java/com/xfarrow/locatemydevice/Utils.java +++ b/src/app/src/main/java/com/xfarrow/locatemydevice/Utils.java @@ -5,11 +5,8 @@ import java.util.Locale; 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 int PERMISSION_ACCESS_COARSE_LOCATION = 103; - public static final int PERMISSION_ACCESS_BACKGROUND_LOCATION = 104; + public static final int PERMISSION_MULTIPLE = 100; + public static final int PERMISSION_ACCESS_BACKGROUND_LOCATION = 101; public static final String LOCATE_OPTION = "locate";