Bugfixes and improvements

This commit is contained in:
Alessandro Ferro 2022-09-27 15:11:54 +02:00
parent ccc09e35e7
commit 529fe80bba
4 changed files with 55 additions and 62 deletions

View File

@ -11,7 +11,7 @@
</map>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="true" project-jdk-name="Android Studio default JDK" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" />
</component>
<component name="ProjectType">

View File

@ -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) {
// 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);
}
else if(requestCode == Utils.PERMISSION_SMS_SEND){
}
else if(requestCode == Utils.PERMISSION_ACCESS_FINE_LOCATION){
}
}

View File

@ -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<String> 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<String> 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<String> 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:" +
String response = "Coordinates are:" +
"\nLatitude: " + latitude +
"\nLongitude: " + longitude + "\n" +
Utils.buildOSMLink(latitude, longitude), null, null);
Utils.buildOSMLink(latitude, longitude);
ArrayList<String> parts = smsManager.divideMessage(response);
smsManager.sendMultipartTextMessage (sendTo, null, parts,null, null);
}
}

View File

@ -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";