Skip to content

Commit

Permalink
Fix ANR issues with locate command
Browse files Browse the repository at this point in the history
  • Loading branch information
ClementGre committed Aug 29, 2023
1 parent 796ed88 commit 73b61de
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 38 deletions.
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_LOCATION" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

<!-- other commands -->
<uses-permission
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class CommandExecutor {

private final String[] args;
private final Context context;
private final String fromNumber;
public final String fromNumber;
public CommandExecutor(String[] args, Context context, String fromNumber){
this.args = args;
this.context = context;
Expand Down Expand Up @@ -55,16 +55,16 @@ public String executeAuto() {
break;
}
}
if(terminatedMessage == null) return "No terminated message, a fail probably occurred";
if(terminatedMessage == null) return "No terminated message";
else return terminatedMessage;
}


public void replyAndTerminate(String message){
reply(message);
reply(fromNumber, message);
terminate("From replyAndTerminate() \"" + message + "\"");
}
public void reply(String message){
public static void reply(String fromNumber, String message){
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendMultipartTextMessage(fromNumber, null, smsManager.divideMessage(message), null, null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,10 @@
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;

import androidx.core.content.ContextCompat;

import java.util.Date;
import java.util.concurrent.BlockingQueue;

import fr.themsou.monitorinternetless.R;

public class LocateCommandExecutor{
Expand All @@ -32,26 +28,10 @@ public void execute(String[] args){
return;
}

commandExecutor.reply(context.getString(R.string.info_localizing));

Intent serviceIntent = new Intent(context, LocationService.class);
serviceIntent.putExtra("number", commandExecutor.fromNumber);
ContextCompat.startForegroundService(context, serviceIntent);

BlockingQueue<Location> locationQueue = LocationService.getLocationQueue();

try{
Location location = locationQueue.take();
commandExecutor.replyAndTerminate(
"Maps : https://www.google.com/maps/place/" + location.getLatitude() + "%20" + location.getLongitude() + "\n" +
context.getString(R.string.info_latitude) + " : " + location.getLatitude() + \n" +
context.getString(R.string.info_longitude) + " : " + location.getLongitude() + \n" +
context.getString(R.string.info_accuracy) + " : " + location.getAccuracy() + " m" + "\n" +
context.getString(R.string.info_bearing) + " : " + location.getBearing() + \n" +
context.getString(R.string.info_speed) + " : " + location.getSpeed() + " m/s \n" +
"Date : " + new Date(location.getTime()));
}catch(InterruptedException e){
e.printStackTrace();
commandExecutor.replyAndTerminate(context.getString(R.string.info_location_unknown));
}
commandExecutor.replyAndTerminate(context.getString(R.string.info_localizing));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,32 @@
import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.Date;

import fr.themsou.monitorinternetless.MainActivity;
import fr.themsou.monitorinternetless.R;

public class LocationService extends Service implements LocationListener {

private LocationManager locationManager;

private static final BlockingQueue<Location> locationQueue = new LinkedBlockingQueue<>();
private String number;

private static final int NOTIFICATION_ID = 2;
private static final String CHANNEL_ID = "LocationService";



@Override
public void onCreate() {
super.onCreate();
locationQueue.clear();
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
}

@SuppressLint("MissingPermission")
@RequiresApi(api = Build.VERSION_CODES.S)
@Override
public int onStartCommand(Intent intent, int flags, int startId){

number = intent.getStringExtra("number");

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q){
startForeground(NOTIFICATION_ID, createNotification(), ServiceInfo.FOREGROUND_SERVICE_TYPE_LOCATION);
Expand All @@ -60,7 +59,15 @@ public int onStartCommand(Intent intent, int flags, int startId){
@Override
public void onLocationChanged(@NonNull Location location) {
Log.d(CHANNEL_ID, "Location listener called. lat,long:" + location.getLatitude() + "," + location.getLongitude());
locationQueue.add(location);

CommandExecutor.reply(number,
"Maps : https://www.google.com/maps/place/" + location.getLatitude() + "%20" + location.getLongitude() + "\n" +
getString(R.string.info_latitude) + " : " + location.getLatitude() + \n" +
getString(R.string.info_longitude) + " : " + location.getLongitude() + \n" +
getString(R.string.info_accuracy) + " : " + location.getAccuracy() + " m" + "\n" +
getString(R.string.info_bearing) + " : " + location.getBearing() + \n" +
getString(R.string.info_speed) + " : " + location.getSpeed() + " m/s \n" +
"Date : " + new Date(location.getTime()));

stopForeground(true);
stopSelf();
Expand All @@ -87,8 +94,4 @@ private Notification createNotification() {
public IBinder onBind(Intent intent) {
return null;
}

public static BlockingQueue<Location> getLocationQueue() {
return locationQueue;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
listView = root.findViewById(R.id.command_listview);
listView.addHeaderView(layoutInflater.inflate(R.layout.header_commands, null));

adapter = new CommandsListAdapter(getContext(), ((MainActivity) getActivity()), getCommands((MainActivity) getActivity()));
adapter = new CommandsListAdapter(getContext(), ((MainActivity) getActivity()), getCommands(getActivity()));
listView.setAdapter(adapter);


Expand Down

0 comments on commit 73b61de

Please sign in to comment.